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_host.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 = 6;
RequestManagerFuzzerTest(const uint8_t * data,size_t size)27 bool RequestManagerFuzzerTest(const uint8_t* data, size_t size)
28 {
29 std::shared_ptr<RequestManager> requestManager =
30 DelayedSingleton<RequestManager>::GetInstance();
31 if (requestManager == nullptr) {
32 return false;
33 }
34 std::shared_ptr<Request> request = std::make_shared<Request>();
35 requestManager->InitSystemListeners();
36 auto locatorCallbackHostForTest =
37 sptr<LocatorCallbackHost>(new (std::nothrow) LocatorCallbackHost());
38 sptr<ILocatorCallback> locatorCallback =
39 sptr<ILocatorCallback>(locatorCallbackHostForTest);
40 requestManager->HandleStopLocating(locatorCallback);
41 int index = 0;
42 int32_t pid = data[index++];
43 int32_t uid = data[index++];
44 int32_t state = data[index++];
45 requestManager->HandlePowerSuspendChanged(pid, uid, state);
46 requestManager->UpdateRequestRecord(request, true);
47 requestManager->UpdateRequestRecord(request, false);
48 requestManager->HandleRequest();
49 requestManager->UpdateUsingPermission(request);
50 requestManager->HandlePermissionChanged(data[index++]);
51 return true;
52 }
53
RequestFuzzerTest(const uint8_t * data,size_t size)54 bool RequestFuzzerTest(const uint8_t* data, size_t size)
55 {
56 int index = 0;
57 std::shared_ptr<Request> request = std::make_shared<Request>();
58 request->SetUid(data[index++]);
59 request->SetPid(data[index++]);
60 std::string packageName((const char*) data, size);
61 request->SetPackageName(packageName);
62 auto requestConfig = std::make_unique<RequestConfig>();
63 request->SetRequestConfig(*requestConfig);
64 request->SetLocatorCallBack(nullptr);
65 request->SetRequesting(true);
66 request->SetTokenId(data[index++]);
67 request->SetFirstTokenId(data[index++]);
68 request->SetLocationPermState(true);
69 request->SetBackgroundPermState(data[index++]);
70 request->SetApproximatelyPermState(data[index++]);
71
72 request->GetLastLocation();
73 request->GetTokenId();
74 request->GetFirstTokenId();
75 request->GetLocationPermState();
76 request->GetBackgroundPermState();
77 request->GetApproximatelyPermState();
78 std::shared_ptr<std::list<std::string>> proxys =
79 std::make_shared<std::list<std::string>>();
80 request->GetProxyName(proxys);
81 request->GetUid();
82 request->GetPid();
83 request->GetPackageName();
84 request->GetRequestConfig();
85 request->GetLocatorCallBack();
86 request->GetIsRequesting();
87 request->ToString();
88 return true;
89 }
90 }
91
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95 /* Run your code on data */
96 if (size < OHOS::MIN_DATA_LEN) {
97 return 0;
98 }
99 OHOS::RequestFuzzerTest(data, size);
100 OHOS::RequestManagerFuzzerTest(data, size);
101 return 0;
102 }