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 #include <thread>
21
22 #include "ipc_skeleton.h"
23 #include "iremote_object.h"
24
25 #include "cached_locations_callback_napi.h"
26 #include "common_utils.h"
27 #include "constant_definition.h"
28 #include "country_code_callback_napi.h"
29 #include "gnss_ability.h"
30 #include "gnss_ability_proxy.h"
31 #include "gnss_status_callback_napi.h"
32 #include "location.h"
33 #include "location_switch_callback_napi.h"
34 #include "nmea_message_callback_napi.h"
35 #include "subability_common.h"
36 #include "work_record.h"
37 #include "geofence_request.h"
38 #include "work_record_statistic.h"
39
40 namespace OHOS {
41 using namespace OHOS::Location;
42 const int32_t MIN_DATA_LEN = 4;
43 const int32_t SLEEP_TIMES = 1000;
GnssProxyFuzzTest001(const uint8_t * data,size_t size)44 bool GnssProxyFuzzTest001(const uint8_t* data, size_t size)
45 {
46 if (size < MIN_DATA_LEN) {
47 return true;
48 }
49 int index = 0;
50 sptr<OHOS::Location::GnssAbility> ability = new (std::nothrow) GnssAbility();
51 sptr<OHOS::Location::GnssAbilityProxy> proxy =
52 new (std::nothrow) GnssAbilityProxy(ability);
53 proxy->SetEnable(true);
54 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
55 proxy->SetEnable(false);
56 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
57 proxy->RefrashRequirements();
58 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
59 auto gnssCallbackHost =
60 sptr<GnssStatusCallbackNapi>(new (std::nothrow) GnssStatusCallbackNapi());
61 proxy->RegisterGnssStatusCallback(gnssCallbackHost, data[index++]);
62 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
63 proxy->UnregisterGnssStatusCallback(gnssCallbackHost);
64 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
65 auto nmeaCallbackHost =
66 sptr<NmeaMessageCallbackNapi>(new (std::nothrow) NmeaMessageCallbackNapi());
67 proxy->RegisterNmeaMessageCallback(nmeaCallbackHost, data[index++]);
68 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
69 proxy->UnregisterNmeaMessageCallback(nmeaCallbackHost);
70 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
71 WorkRecordStatistic::DestroyInstance();
72 return true;
73 }
74
GnssProxyFuzzTest002(const uint8_t * data,size_t size)75 bool GnssProxyFuzzTest002(const uint8_t* data, size_t size)
76 {
77 if (size < MIN_DATA_LEN) {
78 return true;
79 }
80 int index = 0;
81 sptr<OHOS::Location::GnssAbility> ability = new (std::nothrow) GnssAbility();
82 sptr<OHOS::Location::GnssAbilityProxy> proxy =
83 new (std::nothrow) GnssAbilityProxy(ability);
84 auto cachedRequest = std::make_unique<CachedGnssLocationsRequest>();
85 auto cachedLocationsCallbackHost =
86 sptr<CachedLocationsCallbackNapi>(new (std::nothrow) CachedLocationsCallbackNapi());
87 proxy->RegisterCachedCallback(cachedRequest, cachedLocationsCallbackHost);
88 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
89 proxy->UnregisterCachedCallback(cachedLocationsCallbackHost);
90 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
91 int locSize;
92 proxy->GetCachedGnssLocationsSize(locSize);
93 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
94 proxy->FlushCachedGnssLocations();
95 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
96 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
97 proxy->SendCommand(command);
98 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
99 std::shared_ptr<GeofenceRequest> fence = std::make_shared<GeofenceRequest>();
100 proxy->AddFence(fence);
101 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
102 proxy->RemoveFence(fence);
103 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
104 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
105 proxy->EnableMock();
106 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
107 proxy->DisableMock();
108 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
109 proxy->SetMocked(data[index++], locations);
110 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
111 WorkRecordStatistic::DestroyInstance();
112 return true;
113 }
114
GnssProxyFuzzTest003(const uint8_t * data,size_t size)115 bool GnssProxyFuzzTest003(const uint8_t* data, size_t size)
116 {
117 if (size < MIN_DATA_LEN) {
118 return true;
119 }
120 sptr<OHOS::Location::GnssAbility> ability = new (std::nothrow) GnssAbility();
121 sptr<OHOS::Location::GnssAbilityProxy> proxy =
122 new (std::nothrow) GnssAbilityProxy(ability);
123 std::shared_ptr<GeofenceRequest> fence = std::make_shared<GeofenceRequest>();
124 proxy->AddGnssGeofence(fence);
125 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
126 proxy->RemoveGnssGeofence(fence);
127 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
128 std::vector<CoordinateSystemType> coordinateSystemTypes;
129 proxy->QuerySupportCoordinateSystemType(coordinateSystemTypes);
130 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
131 WorkRecordStatistic::DestroyInstance();
132 return true;
133 }
134 }
135
136 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
138 {
139 /* Run your code on data */
140 OHOS::GnssProxyFuzzTest001(data, size);
141 OHOS::GnssProxyFuzzTest002(data, size);
142 OHOS::GnssProxyFuzzTest003(data, size);
143 return 0;
144 }
145 #endif // FEATURE_GNSS_SUPPORT
146