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#include "gnss_fuzzer.h" 17 18#include <vector> 19#include <thread> 20 21#include "ipc_skeleton.h" 22#include "iremote_object.h" 23 24#include "cached_locations_callback_napi.h" 25#include "common_utils.h" 26#include "constant_definition.h" 27#include "country_code_callback_napi.h" 28#include "gnss_ability.h" 29#include "gnss_ability_proxy.h" 30#include "gnss_status_callback_napi.h" 31#include "location.h" 32#include "location_switch_callback_napi.h" 33#include "nmea_message_callback_napi.h" 34#include "subability_common.h" 35#include "work_record.h" 36#include "geofence_request.h" 37#include "work_record_statistic.h" 38#include "if_system_ability_manager.h" 39#include "system_ability_definition.h" 40#include "iservice_registry.h" 41 42namespace OHOS { 43 using namespace OHOS::Location; 44#ifdef FEATURE_GNSS_SUPPORT 45 const int32_t MIN_DATA_LEN = 4; 46 const int32_t SLEEP_TIMES = 1000; 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 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 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#endif // FEATURE_GNSS_SUPPORT 164} 165 166/* Fuzzer entry point */ 167extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 168{ 169#ifdef FEATURE_GNSS_SUPPORT 170 /* Run your code on data */ 171 OHOS::GnssProxyFuzzTest001(data, size); 172 OHOS::GnssProxyFuzzTest002(data, size); 173 OHOS::GnssProxyFuzzTest003(data, size); 174#endif // FEATURE_GNSS_SUPPORT 175 return 0; 176} 177