• 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 "requestmanager_fuzzer.h"
17 
18 #include "i_locator_callback.h"
19 #include "locator_callback_napi.h"
20 #include "request.h"
21 #include "request_config.h"
22 #include "request_manager.h"
23 
24 namespace OHOS {
25     using namespace OHOS::Location;
26     const int MIN_DATA_LEN = 7;
RequestManagerFuzzerTest(const uint8_t * data,size_t size)27     bool RequestManagerFuzzerTest(const uint8_t* data, size_t size)
28     {
29         if (size < MIN_DATA_LEN) {
30             return true;
31         }
32         RequestManager* requestManager =
33             RequestManager::GetInstance();
34         if (requestManager == nullptr) {
35             return false;
36         }
37         auto requestConfig = std::make_unique<RequestConfig>();
38         requestManager->InitSystemListeners();
39         auto locatorCallbackHostForTest =
40             sptr<LocatorCallbackNapi>(new (std::nothrow) LocatorCallbackNapi());
41         sptr<ILocatorCallback> locatorCallback =
42             sptr<ILocatorCallback>(locatorCallbackHostForTest);
43         AppIdentity identity;
44         std::shared_ptr<Request> request = std::make_shared<Request>(requestConfig, locatorCallback, identity);
45         request->SetLocationErrorCallBack(locatorCallback);
46         request->GetLocationErrorCallBack();
47         requestManager->HandleStopLocating(locatorCallback);
48         int index = 0;
49         int32_t pid = data[index++];
50         int32_t uid = data[index++];
51         int32_t state = data[index++];
52         requestManager->HandlePowerSuspendChanged(pid, uid, state);
53         requestManager->UpdateRequestRecord(request, true);
54         requestManager->UpdateRequestRecord(request, false);
55         requestManager->HandleRequest();
56         requestManager->UpdateUsingPermission(request, true);
57         return true;
58     }
59 
RequestFuzzerTest(const uint8_t * data,size_t size)60     bool RequestFuzzerTest(const uint8_t* data, size_t size)
61     {
62         if (size < MIN_DATA_LEN) {
63             return true;
64         }
65         int index = 0;
66         std::shared_ptr<Request> request = std::make_shared<Request>();
67         std::unique_ptr<OHOS::Location::Location> location =
68             std::make_unique<OHOS::Location::Location>();
69         request->SetUid(data[index++]);
70         request->SetPid(data[index++]);
71         std::string packageName((const char*) data, size);
72         request->SetPackageName(packageName);
73         auto requestConfig = std::make_unique<RequestConfig>();
74         request->SetRequestConfig(*requestConfig);
75         request->SetLocatorCallBack(nullptr);
76         request->SetRequesting(true);
77         request->SetTokenId(data[index++]);
78         request->SetTokenIdEx(data[index++]);
79         request->SetFirstTokenId(data[index++]);
80         request->SetLocationPermState(true);
81         request->SetBackgroundPermState(data[index++]);
82         request->SetApproximatelyPermState(data[index++]);
83         request->SetBestLocation(location);
84         request->GetBestLocation();
85         request->SetLastLocation(location);
86         request->GetLastLocation();
87         request->GetTokenId();
88         request->GetTokenIdEx();
89         request->GetFirstTokenId();
90         request->GetLocationPermState();
91         request->GetBackgroundPermState();
92         request->GetApproximatelyPermState();
93         std::shared_ptr<std::list<std::string>> proxys =
94             std::make_shared<std::list<std::string>>();
95         request->GetProxyName(proxys);
96         request->GetUid();
97         request->GetPid();
98         request->GetPackageName();
99         request->GetRequestConfig();
100         request->GetLocatorCallBack();
101         request->GetIsRequesting();
102         request->ToString();
103         return true;
104     }
105 }
106 
107 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
109 {
110     /* Run your code on data */
111     if (size < OHOS::MIN_DATA_LEN) {
112         return 0;
113     }
114     OHOS::RequestFuzzerTest(data, size);
115     OHOS::RequestManagerFuzzerTest(data, size);
116     return 0;
117 }