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 #include "if_system_ability_manager.h"
40 #include "system_ability_definition.h"
41 #include "iservice_registry.h"
42
43 namespace OHOS {
44 using namespace OHOS::Location;
45 const int32_t MIN_DATA_LEN = 4;
46 const int32_t SLEEP_TIMES = 1000;
GnssProxyFuzzTest001(const uint8_t * data,size_t size)47 bool GnssProxyFuzzTest001(const uint8_t* data, size_t size)
48 {
49 if (size < MIN_DATA_LEN) {
50 return true;
51 }
52 int index = 0;
53 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54 if (sam == nullptr) {
55 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get samgr failed.", __func__);
56 return true;
57 }
58 sptr<IRemoteObject> obj = sam->CheckSystemAbility(LOCATION_GNSS_SA_ID);
59 if (obj == nullptr) {
60 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get remote service failed.", __func__);
61 return true;
62 }
63 auto proxy = sptr<GnssAbilityProxy>(new (std::nothrow) GnssAbilityProxy(obj));
64 proxy->SetEnable(true);
65 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
66 proxy->SetEnable(false);
67 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
68 proxy->RefrashRequirements();
69 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
70 auto gnssCallbackHost =
71 sptr<GnssStatusCallbackNapi>(new (std::nothrow) GnssStatusCallbackNapi());
72 AppIdentity identity;
73 identity.SetPid(data[index++]);
74 proxy->RegisterGnssStatusCallback(gnssCallbackHost, identity);
75 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
76 proxy->UnregisterGnssStatusCallback(gnssCallbackHost);
77 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
78 auto nmeaCallbackHost =
79 sptr<NmeaMessageCallbackNapi>(new (std::nothrow) NmeaMessageCallbackNapi());
80 proxy->RegisterNmeaMessageCallback(nmeaCallbackHost, identity);
81 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
82 proxy->UnregisterNmeaMessageCallback(nmeaCallbackHost);
83 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
84 WorkRecordStatistic::DestroyInstance();
85 return true;
86 }
87
GnssProxyFuzzTest002(const uint8_t * data,size_t size)88 bool GnssProxyFuzzTest002(const uint8_t* data, size_t size)
89 {
90 if (size < MIN_DATA_LEN) {
91 return true;
92 }
93 int index = 0;
94 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
95 if (sam == nullptr) {
96 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get samgr failed.", __func__);
97 return true;
98 }
99 sptr<IRemoteObject> obj = sam->CheckSystemAbility(LOCATION_GNSS_SA_ID);
100 if (obj == nullptr) {
101 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get remote service failed.", __func__);
102 return true;
103 }
104 auto proxy = sptr<GnssAbilityProxy>(new (std::nothrow) GnssAbilityProxy(obj));
105 auto cachedRequest = std::make_unique<CachedGnssLocationsRequest>();
106 auto cachedLocationsCallbackHost =
107 sptr<CachedLocationsCallbackNapi>(new (std::nothrow) CachedLocationsCallbackNapi());
108 proxy->RegisterCachedCallback(cachedRequest, cachedLocationsCallbackHost);
109 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
110 proxy->UnregisterCachedCallback(cachedLocationsCallbackHost);
111 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
112 int locSize;
113 proxy->GetCachedGnssLocationsSize(locSize);
114 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
115 proxy->FlushCachedGnssLocations();
116 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
117 std::unique_ptr<LocationCommand> command = std::make_unique<LocationCommand>();
118 proxy->SendCommand(command);
119 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
120 std::shared_ptr<GeofenceRequest> fence = std::make_shared<GeofenceRequest>();
121 proxy->AddFence(fence);
122 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
123 proxy->RemoveFence(fence);
124 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
125 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
126 proxy->EnableMock();
127 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
128 proxy->DisableMock();
129 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
130 proxy->SetMocked(data[index++], locations);
131 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
132 WorkRecordStatistic::DestroyInstance();
133 return true;
134 }
135
GnssProxyFuzzTest003(const uint8_t * data,size_t size)136 bool GnssProxyFuzzTest003(const uint8_t* data, size_t size)
137 {
138 if (size < MIN_DATA_LEN) {
139 return true;
140 }
141 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
142 if (sam == nullptr) {
143 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get samgr failed.", __func__);
144 return true;
145 }
146 sptr<IRemoteObject> obj = sam->CheckSystemAbility(LOCATION_GNSS_SA_ID);
147 if (obj == nullptr) {
148 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get remote service failed.", __func__);
149 return true;
150 }
151 auto proxy = sptr<GnssAbilityProxy>(new (std::nothrow) GnssAbilityProxy(obj));
152 std::shared_ptr<GeofenceRequest> fence = std::make_shared<GeofenceRequest>();
153 proxy->AddGnssGeofence(fence);
154 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
155 proxy->RemoveGnssGeofence(fence);
156 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
157 std::vector<CoordinateSystemType> coordinateSystemTypes;
158 proxy->QuerySupportCoordinateSystemType(coordinateSystemTypes);
159 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
160 WorkRecordStatistic::DestroyInstance();
161 return true;
162 }
163 }
164
165 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)166 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
167 {
168 /* Run your code on data */
169 OHOS::GnssProxyFuzzTest001(data, size);
170 OHOS::GnssProxyFuzzTest002(data, size);
171 OHOS::GnssProxyFuzzTest003(data, size);
172 return 0;
173 }
174 #endif // FEATURE_GNSS_SUPPORT
175