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 "locatorbackgroundproxy_fuzzer.h"
17 
18 #include "i_locator_callback.h"
19 #include "locator_background_proxy.h"
20 #include "locator_callback_proxy.h"
21 #include "request.h"
22 #include "request_config.h"
23 
24 namespace OHOS {
25     using namespace OHOS::Location;
26     const int MIN_DATA_LEN = 11;
LocatorBackgroundProxyFuzzerTest(const uint8_t * data,size_t size)27     bool LocatorBackgroundProxyFuzzerTest(const uint8_t* data, size_t size)
28     {
29         int index = 0;
30         auto backgroundProxy =
31             LocatorBackgroundProxy::GetInstance();
32         if (backgroundProxy == nullptr) {
33             return false;
34         }
35         std::shared_ptr<Request> request =
36             std::make_shared<Request>();
37         std::string bundleName((const char*) data, size);
38         request->SetUid(data[index++]);
39         request->SetPid(data[index++]);
40         request->SetPackageName(bundleName);
41         std::unique_ptr<RequestConfig> requestConfig =
42             std::make_unique<RequestConfig>();
43         requestConfig->SetScenario(data[index++]);
44         requestConfig->SetPriority(data[index++]);
45         requestConfig->SetTimeInterval(data[index++]);
46         requestConfig->SetDistanceInterval(data[index++]);
47         requestConfig->SetMaxAccuracy(data[index++]);
48         requestConfig->SetFixNumber(data[index++]);
49         requestConfig->SetTimeOut(data[index++]);
50         request->SetRequestConfig(*requestConfig);
51         request->SetRequesting(true);
52         request->SetTokenId(data[index++]);
53         request->SetFirstTokenId(data[index++]);
54         request->SetLocationPermState(false);
55         request->SetBackgroundPermState(false);
56         request->SetApproximatelyPermState(false);
57         backgroundProxy->UpdateListOnRequestChange(request);
58         backgroundProxy->OnSuspend(request, true);
59         backgroundProxy->OnSuspend(request, false);
60         backgroundProxy->OnSaStateChange(true);
61         backgroundProxy->OnSaStateChange(false);
62         backgroundProxy->OnDeleteRequestRecord(request);
63         sptr<ILocatorCallback> callbackStub =
64             new (std::nothrow) LocatorCallbackStub();
65         backgroundProxy->IsCallbackInProxy(callbackStub);
66         backgroundProxy->IsAppBackground(bundleName);
67         backgroundProxy->RegisterAppStateObserver();
68         backgroundProxy->UnregisterAppStateObserver();
69         return true;
70     }
71 }
72 
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76     if (size < OHOS::MIN_DATA_LEN) {
77         return 0;
78     }
79     /* Run your code on data */
80     OHOS::LocatorBackgroundProxyFuzzerTest(data, size);
81     return 0;
82 }