1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifdef FEATURE_GNSS_SUPPORT
17 #include "gnss_fuzzer.h"
18
19 #include <vector>
20
21 #include "ipc_skeleton.h"
22 #include "iremote_object.h"
23
24 #include "cached_locations_callback_host.h"
25 #include "common_utils.h"
26 #include "constant_definition.h"
27 #include "country_code_callback_host.h"
28 #include "gnss_ability.h"
29 #include "gnss_ability_proxy.h"
30 #include "gnss_status_callback_host.h"
31 #include "location.h"
32 #include "location_switch_callback_host.h"
33 #include "nmea_message_callback_host.h"
34 #include "subability_common.h"
35 #include "work_record.h"
36
37 namespace OHOS {
38 using namespace OHOS::Location;
39 const int32_t MIN_DATA_LEN = 4;
40
GnssProxyFuzzTest(const uint8_t * data,size_t size)41 bool GnssProxyFuzzTest(const uint8_t* data, size_t size)
42 {
43 if (size < MIN_DATA_LEN) {
44 return true;
45 }
46 int index = 0;
47 sptr<OHOS::Location::GnssAbility> ability = new (std::nothrow) GnssAbility();
48 sptr<OHOS::Location::GnssAbilityProxy> proxy =
49 new (std::nothrow) GnssAbilityProxy(ability);
50 proxy->SetEnable(true);
51 proxy->SetEnable(false);
52 proxy->RefrashRequirements();
53 auto gnssCallbackHost =
54 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
55 proxy->RegisterGnssStatusCallback(gnssCallbackHost, data[index++]);
56 proxy->UnregisterGnssStatusCallback(gnssCallbackHost);
57 auto nmeaCallbackHost =
58 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
59 proxy->RegisterNmeaMessageCallback(nmeaCallbackHost, data[index++]);
60 proxy->UnregisterNmeaMessageCallback(nmeaCallbackHost);
61 auto cachedRequest = std::make_unique<CachedGnssLocationsRequest>();
62 auto cachedLocationsCallbackHost =
63 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
64 proxy->RegisterCachedCallback(cachedRequest, cachedLocationsCallbackHost);
65 proxy->UnregisterCachedCallback(cachedLocationsCallbackHost);
66 int locSize;
67 proxy->GetCachedGnssLocationsSize(locSize);
68 proxy->FlushCachedGnssLocations();
69 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
70 proxy->SendCommand(command);
71 std::unique_ptr<GeofenceRequest> fence = std::make_unique<GeofenceRequest>();
72 proxy->AddFence(fence);
73 proxy->RemoveFence(fence);
74 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
75 proxy->EnableMock();
76 proxy->DisableMock();
77 proxy->SetMocked(data[index++], locations);
78 return true;
79 }
80 }
81
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 OHOS::GnssProxyFuzzTest(data, size);
87 return 0;
88 }
89 #endif // FEATURE_GNSS_SUPPORT
90