• 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 "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 
35 namespace OHOS {
36 using namespace OHOS::Location;
37 const int32_t SLEEP_TIMES = 1000;
38 const int32_t LOCATION_PERM_NUM = 4;
39 
MockNativePermission()40 void MockNativePermission()
41 {
42     const char *perms[] = {
43         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
44         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
45     };
46     NativeTokenInfoParams infoInstance = {
47         .dcapsNum = 0,
48         .permsNum = LOCATION_PERM_NUM,
49         .aclsNum = 0,
50         .dcaps = nullptr,
51         .perms = perms,
52         .acls = nullptr,
53         .processName = "GnssAbility_FuzzTest",
54         .aplStr = "system_basic",
55     };
56     auto tokenId = GetAccessTokenId(&infoInstance);
57     SetSelfTokenID(tokenId);
58     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
59     LocatorAbility::GetInstance()->EnableAbility(true);
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     return true;
80 }
81 #endif
82 } // namespace OHOS
83 
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87     OHOS::MockNativePermission();
88 #ifdef FEATURE_PASSIVE_SUPPORT
89     OHOS::PassiveAbility001FuzzTest(data, size);
90 #endif
91     return 0;
92 }
93 
94