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 "passiveabilityproxy_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 #include "locator_ability.h"
27 #include "locationhub_ipc_interface_code.h"
28 
29 #ifdef FEATURE_PASSIVE_SUPPORT
30 #include "passive_ability.h"
31 #include "passive_ability_proxy.h"
32 #endif
33 #include "permission_manager.h"
34 #include "work_record_statistic.h"
35 
36 namespace OHOS {
37 using namespace OHOS::Location;
38 const int32_t SLEEP_TIMES = 1000;
39 const int32_t LOCATION_PERM_NUM = 4;
40 
MockNativePermission()41 void MockNativePermission()
42 {
43     const char *perms[] = {
44         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
45         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
46     };
47     NativeTokenInfoParams infoInstance = {
48         .dcapsNum = 0,
49         .permsNum = LOCATION_PERM_NUM,
50         .aclsNum = 0,
51         .dcaps = nullptr,
52         .perms = perms,
53         .acls = nullptr,
54         .processName = "GnssAbility_FuzzTest",
55         .aplStr = "system_basic",
56     };
57     auto tokenId = GetAccessTokenId(&infoInstance);
58     SetSelfTokenID(tokenId);
59     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
60 }
61 
62 #ifdef FEATURE_PASSIVE_SUPPORT
PassiveAbility001FuzzTest(const uint8_t * data,size_t size)63 bool PassiveAbility001FuzzTest(const uint8_t* data, size_t size)
64 {
65     int index = 0;
66     sptr<OHOS::Location::PassiveAbility> ability = new (std::nothrow) PassiveAbility();
67     sptr<OHOS::Location::PassiveAbilityProxy> proxy =
68         new (std::nothrow) PassiveAbilityProxy(ability);
69     proxy->SetEnable(true);
70     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
71     proxy->SetEnable(false);
72     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
73     std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
74     proxy->EnableMock();
75     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
76     proxy->DisableMock();
77     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
78     proxy->SetMocked(data[index++], locations);
79     WorkRecordStatistic::DestroyInstance();
80     return true;
81 }
82 #endif
83 } // namespace OHOS
84 
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88     OHOS::MockNativePermission();
89 #ifdef FEATURE_PASSIVE_SUPPORT
90     OHOS::PassiveAbility001FuzzTest(data, size);
91 #endif
92     return 0;
93 }
94 
95