1 /*
2 * Copyright (c) 2022 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 #include "locator_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26
27 #include "cached_locations_callback_host.h"
28 #include "common_utils.h"
29 #include "constant_definition.h"
30 #include "country_code_callback_host.h"
31 #include "geo_address.h"
32 #include "geo_coding_mock_info.h"
33 #include "gnss_status_callback_host.h"
34 #include "i_locator_callback.h"
35 #include "location.h"
36 #include "locator.h"
37 #include "location_switch_callback_host.h"
38 #include "locator.h"
39 #include "locator_ability.h"
40 #include "locator_callback_host.h"
41 #include "location_log.h"
42 #include "nmea_message_callback_host.h"
43 #include "request_config.h"
44 #include "satellite_status.h"
45
46 namespace OHOS {
47 using namespace OHOS::Location;
48 auto locatorCallbackHostForTest_ =
49 sptr<LocatorCallbackHost>(new (std::nothrow) LocatorCallbackHost());
50 auto g_locatorImpl = Locator::GetInstance();
51 bool g_isGrant = false;
52 const int32_t MAX_CODE_LEN = 512;
53 const int32_t MAX_CODE_NUM = 40;
54 const int32_t MIN_SIZE_NUM = 10;
55 const int32_t SWITCH_STATE_ON = 1;
56 const int32_t WAIT_TIME_SEC = 1000;
57 const int32_t COUNT = 10;
58
TestStartLocating(const uint8_t * data,size_t size)59 bool TestStartLocating(const uint8_t* data, size_t size)
60 {
61 /* init locator and LocatorCallbackHost */
62 std::unique_ptr<Locator> locator = Locator::GetInstance();
63 int index = 0;
64 locatorCallbackHostForTest_->SetFixNumber(data[index++]);
65 /* init requestConfig */
66 std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
67 requestConfig->SetScenario(data[index++]);
68 requestConfig->SetPriority(data[index++]);
69 requestConfig->SetTimeInterval(data[index++]);
70 requestConfig->SetDistanceInterval(data[index++]);
71 requestConfig->SetMaxAccuracy(data[index++]);
72 requestConfig->SetFixNumber(data[index++]);
73 requestConfig->SetTimeOut(data[index++]);
74 /* test StartLocating */
75 sptr<ILocatorCallback> locatorCallback = sptr<ILocatorCallback>(locatorCallbackHostForTest_);
76 locator->StartLocating(requestConfig, locatorCallback);
77 /* test StopLocating */
78 locator->StopLocating(locatorCallback);
79 /* test GetCurrentLocation */
80 requestConfig->SetFixNumber(1);
81 locator->StartLocating(requestConfig, locatorCallback);
82 locator->StopLocating(locatorCallback);
83 return true;
84 }
85
LocatorProxySendRequestTest(const uint8_t * data,size_t size)86 bool LocatorProxySendRequestTest(const uint8_t* data, size_t size)
87 {
88 if ((data == nullptr) || (size > MAX_CODE_LEN)) {
89 LBSLOGE(LOCATOR, "param error");
90 return false;
91 }
92 sptr<ISystemAbilityManager> systemAbilityManager =
93 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
94 sptr<IRemoteObject> object = systemAbilityManager->GetSystemAbility(LOCATION_LOCATOR_SA_ID);
95 auto client = std::make_unique<LocatorProxyTestFuzzer>(object);
96 if (client == nullptr) {
97 LBSLOGE(LOCATOR, "client is nullptr");
98 return false;
99 }
100 MessageParcel request;
101 if (!request.WriteInterfaceToken(client->GetDescriptor())) {
102 LBSLOGE(LOCATOR, "cannot write interface token");
103 return false;
104 }
105 MessageParcel reply;
106 MessageOption option;
107 sptr<IRemoteObject> remote = client->GetRemote();
108 if (remote == nullptr) {
109 LBSLOGE(LOCATOR, "cannot get remote object");
110 return false;
111 }
112 int index = 0;
113 int32_t result = remote->SendRequest(data[index++] % MAX_CODE_NUM, request, reply, option);
114 return result == SUCCESS;
115 }
116
AddPermission()117 void AddPermission()
118 {
119 if (!g_isGrant) {
120 const char *perms[] = {
121 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
122 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
123 };
124 NativeTokenInfoParams infoInstance = {
125 .dcapsNum = 0,
126 .permsNum = 4,
127 .aclsNum = 0,
128 .dcaps = nullptr,
129 .perms = perms,
130 .acls = nullptr,
131 .processName = "LocatorFuzzer",
132 .aplStr = "system_basic",
133 };
134 uint64_t tokenId = GetAccessTokenId(&infoInstance);
135 SetSelfTokenID(tokenId);
136 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
137 g_isGrant = true;
138 }
139 }
140
LocatorImplFuzzerTest(const uint8_t * data,size_t size)141 bool LocatorImplFuzzerTest(const uint8_t* data, size_t size)
142 {
143 int index = 0;
144 g_locatorImpl->IsLocationEnabled();
145 g_locatorImpl->ShowNotification();
146 g_locatorImpl->RequestPermission();
147 g_locatorImpl->RequestEnableLocation();
148
149 g_locatorImpl->EnableAbility(false);
150 g_locatorImpl->EnableAbility(true);
151
152 g_locatorImpl->GetCachedLocation();
153 g_locatorImpl->IsGeoServiceAvailable();
154 MessageParcel parcel;
155 std::list<std::shared_ptr<GeoAddress>> geoAddressList;
156 g_locatorImpl->GetAddressByCoordinate(parcel, geoAddressList);
157 g_locatorImpl->GetAddressByLocationName(parcel, geoAddressList);
158
159 g_locatorImpl->IsLocationPrivacyConfirmed(data[index++]);
160 g_locatorImpl->SetLocationPrivacyConfirmStatus(data[index++], true);
161 g_locatorImpl->SetLocationPrivacyConfirmStatus(data[index++], false);
162
163 g_locatorImpl->GetCachedGnssLocationsSize();
164 g_locatorImpl->FlushCachedGnssLocations();
165
166 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
167 g_locatorImpl->SendCommand(command);
168
169 std::unique_ptr<GeofenceRequest> fence = std::make_unique<GeofenceRequest>();
170 g_locatorImpl->AddFence(fence);
171 g_locatorImpl->RemoveFence(fence);
172 g_locatorImpl->GetIsoCountryCode();
173 g_locatorImpl->ProxyUidForFreeze(data[index++], true);
174 g_locatorImpl->ProxyUidForFreeze(data[index++], false);
175 g_locatorImpl->ResetAllProxy();
176 return true;
177 }
178
LocatorImplV9FuzzerTest(const uint8_t * data,size_t size)179 bool LocatorImplV9FuzzerTest(const uint8_t* data, size_t size)
180 {
181 int index = 0;
182 bool isEnabled = false;
183 g_locatorImpl->IsLocationEnabledV9(isEnabled);
184 g_locatorImpl->EnableAbilityV9(false);
185 g_locatorImpl->EnableAbilityV9(true);
186 std::unique_ptr<OHOS::Location::Location> loc =
187 std::make_unique<OHOS::Location::Location>();
188 g_locatorImpl->GetCachedLocationV9(loc);
189 bool isAvailable = false;
190 g_locatorImpl->IsGeoServiceAvailableV9(isAvailable);
191 MessageParcel parcel;
192 std::list<std::shared_ptr<GeoAddress>> geoAddressList;
193 g_locatorImpl->GetAddressByCoordinateV9(parcel, geoAddressList);
194 g_locatorImpl->GetAddressByLocationNameV9(parcel, geoAddressList);
195
196 bool isConfirmed = false;
197 g_locatorImpl->IsLocationPrivacyConfirmedV9(data[index++], isConfirmed);
198 g_locatorImpl->SetLocationPrivacyConfirmStatusV9(data[index++], true);
199 g_locatorImpl->SetLocationPrivacyConfirmStatusV9(data[index++], false);
200
201 int locSize = -1;
202 g_locatorImpl->GetCachedGnssLocationsSizeV9(locSize);
203 g_locatorImpl->FlushCachedGnssLocationsV9();
204
205 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
206 g_locatorImpl->SendCommandV9(command);
207
208 std::unique_ptr<GeofenceRequest> fence = std::make_unique<GeofenceRequest>();
209 g_locatorImpl->AddFenceV9(fence);
210 g_locatorImpl->RemoveFenceV9(fence);
211 std::shared_ptr<CountryCode> countryCode = std::make_shared<CountryCode>();
212 g_locatorImpl->GetIsoCountryCodeV9(countryCode);
213 g_locatorImpl->ProxyUidForFreezeV9(data[index++], true);
214 g_locatorImpl->ProxyUidForFreezeV9(data[index++], false);
215 g_locatorImpl->ResetAllProxyV9();
216 return true;
217 }
218
TestCallbackRegister(const uint8_t * data,size_t size)219 bool TestCallbackRegister(const uint8_t* data, size_t size)
220 {
221 int index = 0;
222 auto switchCallbackHost =
223 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
224 g_locatorImpl->RegisterSwitchCallback(switchCallbackHost->AsObject(), data[index++]);
225 g_locatorImpl->UnregisterSwitchCallback(switchCallbackHost->AsObject());
226
227 auto gnssCallbackHost =
228 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
229 g_locatorImpl->RegisterGnssStatusCallback(gnssCallbackHost->AsObject(), data[index++]);
230 g_locatorImpl->UnregisterGnssStatusCallback(gnssCallbackHost->AsObject());
231
232 auto nmeaCallbackHost =
233 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
234 g_locatorImpl->RegisterNmeaMessageCallback(nmeaCallbackHost->AsObject(), data[index++]);
235 g_locatorImpl->UnregisterNmeaMessageCallback(nmeaCallbackHost->AsObject());
236
237 auto countryCodeCallbackHost =
238 sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
239 g_locatorImpl->RegisterCountryCodeCallback(countryCodeCallbackHost->AsObject(),
240 data[index++]);
241 g_locatorImpl->UnregisterCountryCodeCallback(countryCodeCallbackHost->AsObject());
242
243 auto cachedLocationsCallbackHost =
244 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
245 auto cachedCallback = sptr<ICachedLocationsCallback>(cachedLocationsCallbackHost);
246 auto request = std::make_unique<CachedGnssLocationsRequest>();
247 g_locatorImpl->RegisterCachedLocationCallback(request, cachedCallback);
248 g_locatorImpl->UnregisterCachedLocationCallback(cachedCallback);
249 return true;
250 }
251
TestCallbackRegisterV9(const uint8_t * data,size_t size)252 bool TestCallbackRegisterV9(const uint8_t* data, size_t size)
253 {
254 auto switchCallbackHost =
255 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
256 g_locatorImpl->RegisterSwitchCallbackV9(switchCallbackHost->AsObject());
257 g_locatorImpl->UnregisterSwitchCallbackV9(switchCallbackHost->AsObject());
258
259 auto gnssCallbackHost =
260 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
261 g_locatorImpl->RegisterGnssStatusCallbackV9(gnssCallbackHost->AsObject());
262 g_locatorImpl->UnregisterGnssStatusCallbackV9(gnssCallbackHost->AsObject());
263
264 auto nmeaCallbackHost =
265 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
266 g_locatorImpl->RegisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject());
267 g_locatorImpl->UnregisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject());
268
269 auto countryCodeCallbackHost =
270 sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
271 g_locatorImpl->RegisterCountryCodeCallbackV9(countryCodeCallbackHost->AsObject());
272 g_locatorImpl->UnregisterCountryCodeCallbackV9(countryCodeCallbackHost->AsObject());
273
274 auto cachedLocationsCallbackHost =
275 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
276 auto cachedCallback = sptr<ICachedLocationsCallback>(cachedLocationsCallbackHost);
277 auto request = std::make_unique<CachedGnssLocationsRequest>();
278 g_locatorImpl->RegisterCachedLocationCallbackV9(request, cachedCallback);
279 g_locatorImpl->UnregisterCachedLocationCallbackV9(cachedCallback);
280 return true;
281 }
282
TestMockFunc(const uint8_t * data,size_t size)283 bool TestMockFunc(const uint8_t* data, size_t size)
284 {
285 int index = 0;
286 g_locatorImpl->EnableLocationMock();
287 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
288 g_locatorImpl->SetMockedLocations(data[index++], locations);
289 g_locatorImpl->DisableLocationMock();
290
291 g_locatorImpl->EnableReverseGeocodingMock();
292 std::vector<std::shared_ptr<GeocodingMockInfo>> geoMockInfo;
293 g_locatorImpl->SetReverseGeocodingMockInfo(geoMockInfo);
294 g_locatorImpl->DisableReverseGeocodingMock();
295 return true;
296 }
297
TestMockFuncV9(const uint8_t * data,size_t size)298 bool TestMockFuncV9(const uint8_t* data, size_t size)
299 {
300 int index = 0;
301 g_locatorImpl->EnableLocationMockV9();
302 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
303 g_locatorImpl->SetMockedLocationsV9(data[index++], locations);
304 g_locatorImpl->DisableLocationMockV9();
305
306 g_locatorImpl->EnableReverseGeocodingMockV9();
307 std::vector<std::shared_ptr<GeocodingMockInfo>> geoMockInfo;
308 g_locatorImpl->SetReverseGeocodingMockInfoV9(geoMockInfo);
309 g_locatorImpl->DisableReverseGeocodingMockV9();
310 return true;
311 }
312
CachedLocationsCallbackHostFuzzerTest(const uint8_t * data,size_t size)313 bool CachedLocationsCallbackHostFuzzerTest(const uint8_t* data, size_t size)
314 {
315 int index = 0;
316 auto cachedCallbackHost =
317 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
318 MessageParcel request;
319 MessageParcel reply;
320 MessageOption option;
321 cachedCallbackHost->OnRemoteRequest(data[index++], request, reply, option);
322 cachedCallbackHost->IsRemoteDied();
323
324 std::vector<std::shared_ptr<OHOS::Location::Location>> locationsForSend;
325 cachedCallbackHost->Send(locationsForSend);
326 std::vector<std::unique_ptr<OHOS::Location::Location>> locationsForReport;
327 cachedCallbackHost->OnCacheLocationsReport(locationsForReport);
328 cachedCallbackHost->DeleteHandler();
329 return true;
330 }
331
CountryCodeCallbackHostFuzzerTest(const uint8_t * data,size_t size)332 bool CountryCodeCallbackHostFuzzerTest(const uint8_t* data, size_t size)
333 {
334 int index = 0;
335 auto callbackHost =
336 sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
337 MessageParcel request;
338 MessageParcel reply;
339 MessageOption option;
340 callbackHost->OnRemoteRequest(data[index++], request, reply, option);
341
342 auto countryCodePtr = CountryCode::Unmarshalling(request);
343 callbackHost->Send(countryCodePtr);
344 callbackHost->SetEnv(nullptr);
345 callbackHost->SetCallback(nullptr);
346 callbackHost->OnCountryCodeChange(countryCodePtr);
347 callbackHost->DeleteHandler();
348 return true;
349 }
350
GnssStatusCallbackHostFuzzerTest(const uint8_t * data,size_t size)351 bool GnssStatusCallbackHostFuzzerTest(const uint8_t* data, size_t size)
352 {
353 int index = 0;
354 auto gnssCallbackHost =
355 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
356 MessageParcel request;
357 MessageParcel reply;
358 MessageOption option;
359 gnssCallbackHost->OnRemoteRequest(data[index++], request, reply, option);
360 gnssCallbackHost->IsRemoteDied();
361 std::unique_ptr<SatelliteStatus> statusInfo = nullptr;
362 gnssCallbackHost->Send(statusInfo);
363
364 gnssCallbackHost->OnStatusChange(statusInfo);
365 gnssCallbackHost->DeleteHandler();
366 return true;
367 }
368
LocationSwitchCallbackHostFuzzerTest(const uint8_t * data,size_t size)369 bool LocationSwitchCallbackHostFuzzerTest(const uint8_t* data, size_t size)
370 {
371 int index = 0;
372 auto switchCallbackHost =
373 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
374 MessageParcel request;
375 MessageParcel reply;
376 MessageOption option;
377 switchCallbackHost->OnRemoteRequest(data[index++], request, reply, option);
378 switchCallbackHost->IsRemoteDied();
379 switchCallbackHost->PackResult(true);
380 switchCallbackHost->Send(SWITCH_STATE_ON);
381
382 switchCallbackHost->OnSwitchChange(SWITCH_STATE_ON);
383 switchCallbackHost->DeleteHandler();
384 return true;
385 }
386
LocationCallbackHostFuzzerTest(const uint8_t * data,size_t size)387 bool LocationCallbackHostFuzzerTest(const uint8_t* data, size_t size)
388 {
389 int index = 0;
390 auto callbackHost =
391 sptr<LocatorCallbackHost>(new (std::nothrow) LocatorCallbackHost());
392 MessageParcel request;
393 MessageParcel reply;
394 MessageOption option;
395 callbackHost->OnRemoteRequest(data[index++], request, reply, option);
396 callbackHost->OnErrorReport(SUCCESS);
397 std::unique_ptr<OHOS::Location::Location> location =
398 std::make_unique<OHOS::Location::Location>();
399 callbackHost->OnLocationReport(location);
400 callbackHost->OnLocatingStatusChange(SWITCH_STATE_ON);
401
402 callbackHost->DeleteAllCallbacks();
403 callbackHost->IsSystemGeoLocationApi();
404 callbackHost->IsSingleLocationRequest();
405 callbackHost->CountDown();
406 callbackHost->Wait(data[index++] % WAIT_TIME_SEC);
407 callbackHost->SetCount(data[index++] % COUNT);
408 callbackHost->GetCount();
409 return true;
410 }
411
NmeaMessageCallbackHostFuzzerTest(const uint8_t * data,size_t size)412 bool NmeaMessageCallbackHostFuzzerTest(const uint8_t* data, size_t size)
413 {
414 int index = 0;
415 auto nmeaCallbackHost =
416 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
417 MessageParcel request;
418 MessageParcel reply;
419 MessageOption option;
420 nmeaCallbackHost->OnRemoteRequest(data[index++], request, reply, option);
421 nmeaCallbackHost->IsRemoteDied();
422 std::string msg((const char*) data, size);
423 nmeaCallbackHost->PackResult(msg);
424 nmeaCallbackHost->Send(msg);
425 int64_t timestamp = 0;
426 nmeaCallbackHost->OnMessageChange(timestamp, msg);
427 nmeaCallbackHost->DeleteHandler();
428 return true;
429 }
430
LocatorAbility001FuzzerTest(const uint8_t * data,size_t size)431 bool LocatorAbility001FuzzerTest(const uint8_t* data, size_t size)
432 {
433 int index = 0;
434 auto locatorAbility =
435 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
436 std::string deviceId((const char*) data, size);
437 locatorAbility->OnAddSystemAbility(data[index++], deviceId);
438 locatorAbility->OnRemoveSystemAbility(data[index++], deviceId);
439 locatorAbility->QueryServiceState();
440 locatorAbility->InitSaAbility();
441 locatorAbility->InitRequestManagerMap();
442 locatorAbility->UpdateSaAbility();
443 int state = 0;
444 locatorAbility->GetSwitchState(state);
445 locatorAbility->EnableAbility(true);
446 locatorAbility->EnableAbility(false);
447 auto switchCallbackHost =
448 sptr<LocationSwitchCallbackHost>(new (std::nothrow) LocationSwitchCallbackHost());
449 locatorAbility->RegisterSwitchCallback(switchCallbackHost, data[index++]);
450 locatorAbility->UnregisterSwitchCallback(switchCallbackHost);
451 auto gnssCallbackHost =
452 sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
453 locatorAbility->RegisterGnssStatusCallback(gnssCallbackHost, data[index++]);
454 locatorAbility->UnregisterGnssStatusCallback(gnssCallbackHost);
455
456 auto nmeaCallbackHost =
457 sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
458 locatorAbility->RegisterNmeaMessageCallback(nmeaCallbackHost, data[index++]);
459 locatorAbility->UnregisterNmeaMessageCallback(nmeaCallbackHost);
460
461 auto countryCodeCallbackHost =
462 sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
463 locatorAbility->RegisterCountryCodeCallback(countryCodeCallbackHost, data[index++]);
464 locatorAbility->UnregisterCountryCodeCallback(countryCodeCallbackHost);
465 sptr<ILocatorCallback> locatorCallback = sptr<ILocatorCallback>(locatorCallbackHostForTest_);
466 AppIdentity identity;
467 locatorAbility->StopLocating(locatorCallback);
468 std::unique_ptr<OHOS::Location::Location> loc =
469 std::make_unique<OHOS::Location::Location>();
470 locatorAbility->GetCacheLocation(loc, identity);
471 bool isAvailable = false;
472 locatorAbility->IsGeoConvertAvailable(isAvailable);
473
474 MessageParcel request;
475 MessageParcel reply;
476 locatorAbility->GetAddressByCoordinate(request, reply);
477 locatorAbility->GetAddressByLocationName(request, reply);
478 return true;
479 }
480
LocatorAbility002FuzzerTest(const uint8_t * data,size_t size)481 bool LocatorAbility002FuzzerTest(const uint8_t* data, size_t size)
482 {
483 auto locatorAbility =
484 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
485 int index = 0;
486 bool isConfirmed = false;
487 locatorAbility->IsLocationPrivacyConfirmed(data[index++], isConfirmed);
488 locatorAbility->SetLocationPrivacyConfirmStatus(data[index++], true);
489 locatorAbility->SetLocationPrivacyConfirmStatus(data[index++], false);
490 auto cachedLocationsCallbackHost =
491 sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
492 auto cachedCallback = sptr<ICachedLocationsCallback>(cachedLocationsCallbackHost);
493 auto cachedRequest = std::make_unique<CachedGnssLocationsRequest>();
494 locatorAbility->RegisterCachedLocationCallback(cachedRequest, cachedCallback, "fuzz.test");
495 locatorAbility->UnregisterCachedLocationCallback(cachedCallback);
496 int locSize;
497 locatorAbility->GetCachedGnssLocationsSize(locSize);
498 locatorAbility->FlushCachedGnssLocations();
499
500 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
501 locatorAbility->SendCommand(command);
502
503 std::unique_ptr<GeofenceRequest> fence = std::make_unique<GeofenceRequest>();
504 locatorAbility->AddFence(fence);
505 locatorAbility->RemoveFence(fence);
506 std::shared_ptr<CountryCode> country = std::make_shared<CountryCode>();
507 locatorAbility->GetIsoCountryCode(country);
508 return true;
509 }
510
LocatorAbility003FuzzerTest(const uint8_t * data,size_t size)511 bool LocatorAbility003FuzzerTest(const uint8_t* data, size_t size)
512 {
513 auto locatorAbility =
514 sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
515 int index = 0;
516 locatorAbility->EnableLocationMock();
517 locatorAbility->DisableLocationMock();
518 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
519 locatorAbility->SetMockedLocations(data[index++], locations);
520 locatorAbility->EnableReverseGeocodingMock();
521 locatorAbility->DisableReverseGeocodingMock();
522 std::vector<std::shared_ptr<GeocodingMockInfo>> geoMockInfo;
523 locatorAbility->SetReverseGeocodingMockInfo(geoMockInfo);
524
525 auto location = std::make_unique<OHOS::Location::Location>();
526 std::string abilityName((const char*) data, size);
527 int timeInterval = 2;
528 locatorAbility->ReportLocation(location, abilityName);
529 locatorAbility->ProcessLocationMockMsg(timeInterval, locations, data[index++]);
530 locatorAbility->SendLocationMockMsgToGnssSa(nullptr, timeInterval, locations, data[index++]);
531 locatorAbility->SendLocationMockMsgToNetworkSa(nullptr, timeInterval, locations, data[index++]);
532 locatorAbility->GetRequests();
533 locatorAbility->GetReceivers();
534 locatorAbility->GetProxyMap();
535 locatorAbility->UpdateSaAbilityHandler();
536 locatorAbility->RegisterAction();
537 locatorAbility->ProxyUidForFreeze(data[index++], true);
538 locatorAbility->ProxyUidForFreeze(data[index++], false);
539 locatorAbility->ResetAllProxy();
540 locatorAbility->IsProxyUid(data[index++]);
541 locatorAbility->GetActiveRequestNum();
542 std::vector<std::string> permissionNameList;
543 locatorAbility->RegisterPermissionCallback(data[index++], permissionNameList);
544 locatorAbility->UnregisterPermissionCallback(data[index++]);
545 return true;
546 }
547 }
548
549 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)550 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
551 {
552 if (size < OHOS::MIN_SIZE_NUM) {
553 return 0;
554 }
555 /* Run your code on data */
556 OHOS::AddPermission();
557 OHOS::TestStartLocating(data, size);
558 OHOS::LocatorProxySendRequestTest(data, size);
559 OHOS::LocatorImplFuzzerTest(data, size);
560 OHOS::TestMockFunc(data, size);
561 OHOS::TestCallbackRegister(data, size);
562
563 OHOS::LocatorImplV9FuzzerTest(data, size);
564 OHOS::TestMockFuncV9(data, size);
565 OHOS::TestCallbackRegisterV9(data, size);
566
567 OHOS::LocatorAbility001FuzzerTest(data, size);
568 OHOS::LocatorAbility002FuzzerTest(data, size);
569 OHOS::LocatorAbility003FuzzerTest(data, size);
570
571 OHOS::CachedLocationsCallbackHostFuzzerTest(data, size);
572 OHOS::CountryCodeCallbackHostFuzzerTest(data, size);
573 OHOS::GnssStatusCallbackHostFuzzerTest(data, size);
574 OHOS::LocationSwitchCallbackHostFuzzerTest(data, size);
575 OHOS::LocationCallbackHostFuzzerTest(data, size);
576 OHOS::NmeaMessageCallbackHostFuzzerTest(data, size);
577 return 0;
578 }
579
580