• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "devattestnetworkcallback_fuzzer.h"
17 
18 #include <string>
19 #include <securec.h>
20 #include "devattest_network_callback.h"
21 
22 using namespace std;
23 using namespace OHOS::DevAttest;
24 using namespace OHOS::NetManagerStandard;
25 namespace OHOS {
26     const uint8_t *g_baseFuzzData = nullptr;
27     size_t g_baseFuzzSize = 0;
28     size_t g_baseFuzzPos;
29     std::vector<NetCap> netCaps = {NET_CAPABILITY_MMS, NET_CAPABILITY_NOT_METERED, NET_CAPABILITY_INTERNET,
30                                    NET_CAPABILITY_NOT_VPN, NET_CAPABILITY_PORTAL,
31                                    NET_CAPABILITY_INTERNAL_DEFAULT};
32 
33     template <class T>
GetData()34     T GetData()
35     {
36         T object {};
37         size_t objectSize = sizeof(object);
38         if (g_baseFuzzData == nullptr || objectSize > g_baseFuzzSize - g_baseFuzzPos) {
39             return object;
40         }
41         errno_t ret = memcpy_s(&object, objectSize, g_baseFuzzData + g_baseFuzzPos, objectSize);
42         if (ret != EOK) {
43             return {};
44         }
45         g_baseFuzzPos += objectSize;
46         return object;
47     }
48 
NetCapabilitiesChange(const uint8_t * data,size_t size)49     static void NetCapabilitiesChange(const uint8_t* data, size_t size)
50     {
51         g_baseFuzzData = data;
52         g_baseFuzzSize = size;
53         g_baseFuzzPos = 0;
54         uint32_t netId = GetData<uint32_t>();
55         sptr<NetHandle> handle = (std::make_unique<NetHandle>()).release();
56         handle->SetNetId(netId);
57         sptr<NetAllCapabilities> allCap = (std::make_unique<NetAllCapabilities>()).release();
58         for (NetCap netCap : netCaps) {
59             if (GetData<bool>()) {
60                 allCap->netCaps_.insert(netCap);
61             }
62         }
63         sptr<DevAttestNetworkCallback> devattestnetworkcallback =
64             (std::make_unique<DevAttestNetworkCallback>()).release();
65         (void)devattestnetworkcallback->NetCapabilitiesChange(handle, allCap);
66     }
67 
DevattestNetworkCallbackFuzzTest(const uint8_t * data,size_t size)68     void DevattestNetworkCallbackFuzzTest(const uint8_t* data, size_t size)
69     {
70         int32_t demandSize = sizeof(int32_t) + sizeof(char);
71         if (static_cast<int32_t>(size) >= demandSize) {
72             NetCapabilitiesChange(data, size);
73         }
74         return;
75     }
76 }
77 
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81     /* Run your code on data */
82     OHOS::DevattestNetworkCallbackFuzzTest(data, size);
83     return 0;
84 }
85