1 /*
2 * Copyright (c) 2024 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 "networkabilityproxy_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_NETWORK_SUPPORT
30 #include "network_ability.h"
31 #include "network_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;
MockNativePermission()39 void MockNativePermission()
40 {
41 const char *perms[] = {
42 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
43 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
44 };
45 NativeTokenInfoParams infoInstance = {
46 .dcapsNum = 0,
47 .permsNum = LOCATION_PERM_NUM,
48 .aclsNum = 0,
49 .dcaps = nullptr,
50 .perms = perms,
51 .acls = nullptr,
52 .processName = "NetworkAbilityProxy_FuzzTest",
53 .aplStr = "system_basic",
54 };
55 auto tokenId = GetAccessTokenId(&infoInstance);
56 SetSelfTokenID(tokenId);
57 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
58 LocatorAbility::GetInstance()->EnableAbility(true);
59 }
60
61 #ifdef FEATURE_NETWORK_SUPPORT
NetworkAbilityProxy001FuzzTest(const uint8_t * data,size_t size)62 bool NetworkAbilityProxy001FuzzTest(const uint8_t* data, size_t size)
63 {
64 int index = 0;
65 sptr<OHOS::Location::NetworkAbility> ability = new (std::nothrow) NetworkAbility();
66 sptr<OHOS::Location::NetworkAbilityProxy> proxy =
67 new (std::nothrow) NetworkAbilityProxy(ability);
68 proxy->SetEnable(true);
69 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
70 proxy->SetEnable(false);
71 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
72 std::vector<std::shared_ptr<OHOS::Location::Location>> locations;
73 proxy->EnableMock();
74 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
75 proxy->DisableMock();
76 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMES));
77 proxy->SetMocked(data[index++], locations);
78 ability = nullptr;
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_NETWORK_SUPPORT
89 OHOS::NetworkAbilityProxy001FuzzTest(data, size);
90 #endif
91 return 0;
92 }
93
94