• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gnss_fuzzer.h"
17 
18 #include <vector>
19 
20 #include "ipc_skeleton.h"
21 #include "iremote_object.h"
22 
23 #include "cached_locations_callback_host.h"
24 #include "common_utils.h"
25 #include "constant_definition.h"
26 #include "country_code_callback_host.h"
27 #include "gnss_ability.h"
28 #include "gnss_ability_proxy.h"
29 #include "gnss_status_callback_host.h"
30 #include "location.h"
31 #include "location_switch_callback_host.h"
32 #include "nmea_message_callback_host.h"
33 #include "subability_common.h"
34 #include "work_record.h"
35 
36 namespace OHOS {
37     using namespace OHOS::Location;
38     const int32_t MIN_DATA_LEN = 4;
39 
GnssProxyFuzzTest(const uint8_t * data,size_t size)40     bool GnssProxyFuzzTest(const uint8_t* data, size_t size)
41     {
42         if (size < MIN_DATA_LEN) {
43             return true;
44         }
45         int index = 0;
46         sptr<OHOS::Location::GnssAbility> ability = new (std::nothrow) GnssAbility();
47         sptr<OHOS::Location::GnssAbilityProxy> proxy =
48             new (std::nothrow) GnssAbilityProxy(ability);
49         proxy->SetEnable(true);
50         proxy->SetEnable(false);
51         proxy->RefrashRequirements();
52         auto gnssCallbackHost =
53             sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
54         proxy->RegisterGnssStatusCallback(gnssCallbackHost, data[index++]);
55         proxy->UnregisterGnssStatusCallback(gnssCallbackHost);
56         auto nmeaCallbackHost =
57             sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
58         proxy->RegisterNmeaMessageCallback(nmeaCallbackHost, data[index++]);
59         proxy->UnregisterNmeaMessageCallback(nmeaCallbackHost);
60         auto cachedRequest = std::make_unique<CachedGnssLocationsRequest>();
61         auto cachedLocationsCallbackHost =
62             sptr<CachedLocationsCallbackHost>(new (std::nothrow) CachedLocationsCallbackHost());
63         proxy->RegisterCachedCallback(cachedRequest, cachedLocationsCallbackHost);
64         proxy->UnregisterCachedCallback(cachedLocationsCallbackHost);
65         int locSize;
66         proxy->GetCachedGnssLocationsSize(locSize);
67         proxy->FlushCachedGnssLocations();
68         std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
69         proxy->SendCommand(command);
70         std::unique_ptr<GeofenceRequest> fence = std::make_unique<GeofenceRequest>();
71         proxy->AddFence(fence);
72         proxy->RemoveFence(fence);
73         std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
74         proxy->EnableMock();
75         proxy->DisableMock();
76         proxy->SetMocked(data[index++], locations);
77         return true;
78     }
79 }
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     OHOS::GnssProxyFuzzTest(data, size);
86     return 0;
87 }
88 
89