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 "reportmanager_fuzzer.h"
17 
18 #include "i_locator_callback.h"
19 #include "location.h"
20 #include "locator_callback_napi.h"
21 #include "report_manager.h"
22 #include "request.h"
23 
24 namespace OHOS {
25     using namespace OHOS::Location;
26     const int MIN_DATA_LEN = 6;
ReportManagerFuzzerTest(const uint8_t * data,size_t size)27     bool ReportManagerFuzzerTest(const uint8_t* data, size_t size)
28     {
29         if (size < MIN_DATA_LEN) {
30             return true;
31         }
32         auto reportManager =
33             ReportManager::GetInstance();
34         if (reportManager == nullptr) {
35             return false;
36         }
37         std::unique_ptr<OHOS::Location::Location> location =
38             std::make_unique<OHOS::Location::Location>();
39         auto locatorCallbackHostForTest =
40             sptr<LocatorCallbackNapi>(new (std::nothrow) LocatorCallbackNapi());
41         sptr<ILocatorCallback> locatorCallback =
42             sptr<ILocatorCallback>(locatorCallbackHostForTest);
43         int index = 0;
44         int type = data[index++];
45         int result = data[index++];
46         reportManager->ReportRemoteCallback(locatorCallback, type, result);
47         std::shared_ptr<Request> request = std::make_shared<Request>();
48         reportManager->ResultCheck(location, request);
49         reportManager->UpdateCacheLocation(location, "gps");
50         reportManager->GetLastLocation();
51         request->SetUid(data[index++]);
52         request->SetTokenId(data[index++]);
53         request->SetFirstTokenId(data[index++]);
54         request->SetTokenIdEx(data[index++]);
55         reportManager->GetPermittedLocation(request, location);
56         reportManager->UpdateRandom();
57         return true;
58     }
59 }
60 
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     /* Run your code on data */
65     OHOS::ReportManagerFuzzerTest(data, size);
66     return 0;
67 }