1 /*
2 * Copyright (c) 2025 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 <fuzzer/FuzzedDataProvider.h>
17 #include <chrono>
18 #include <securec.h>
19 #include <string>
20
21 #include "device_manager_impl.h"
22 #include "dm_constants.h"
23 #include "softbus_listener.h"
24 #include "dm_anonymous.h"
25 namespace OHOS {
26 namespace DistributedHardware {
27
DmAnonyousFuzzTest(const uint8_t * data,size_t size)28 void DmAnonyousFuzzTest(const uint8_t* data, size_t size)
29 {
30 if ((data == nullptr) || (size < sizeof(int32_t))) {
31 return;
32 }
33 FuzzedDataProvider fdp(data, size);
34 std::vector<std::string> strList;
35 int64_t testNumber = fdp.ConsumeIntegral<int64_t>();
36 std::string test1 = fdp.ConsumeRandomLengthString();
37 std::string test2 = fdp.ConsumeRandomLengthString();
38 strList.push_back(test1);
39 strList.push_back(test2);
40 GetAnonyStringList(strList);
41 std::vector<int32_t> intList;
42 intList.push_back(1);
43 GetAnonyInt32List(intList);
44 std::string key1 = fdp.ConsumeRandomLengthString();
45 std::string key2 = fdp.ConsumeRandomLengthString();
46 std::string key3 = fdp.ConsumeRandomLengthString();
47 std::string value1 = fdp.ConsumeRandomLengthString();
48 std::string value2 = fdp.ConsumeRandomLengthString();
49 JsonObject jsonObj;
50 jsonObj[key1] = testNumber;
51 jsonObj[key2] = value2;
52 jsonObj[key3] = true;
53 IsUint32(jsonObj, key1);
54 IsUint64(jsonObj, key1);
55 IsBool(jsonObj, key3);
56 std::map<std::string, std::string> paramMap;
57 paramMap[key1] = value1;
58 paramMap[key2] = value2;
59 std::map<std::string, std::string> paramMap2 = {};
60 std::string jsonStr = ConvertMapToJsonString(paramMap);
61 ParseMapFromJsonString(jsonStr, paramMap2);
62 IsJsonValIntegerString(jsonObj, key2);
63 }
64
DmAnonyousFuzzTestFirst(const uint8_t * data,size_t size)65 void DmAnonyousFuzzTestFirst(const uint8_t* data, size_t size)
66 {
67 if ((data == nullptr) || (size < sizeof(int32_t))) {
68 return;
69 }
70 FuzzedDataProvider fdp(data, size);
71 std::string str1 = fdp.ConsumeRandomLengthString();
72 std::string str2 = fdp.ConsumeRandomLengthString();
73 int32_t versionNum = 0;
74 int64_t decimal = 10;
75 int64_t testNumber = fdp.ConsumeIntegral<int64_t>();
76 StringToInt(str1, decimal);
77 StringToInt64(str1, decimal);
78 GetVersionNumber("1.0.0", versionNum);
79 GetCallerPkgName("com.example.app#test");
80 GetSubscribeId("123#12345");
81 std::multimap<std::string, int32_t> unorderedmap;
82 unorderedmap.insert(std::make_pair(str1, testNumber));
83 IsValueExist(unorderedmap, str1, testNumber);
84 GetSubStr("test#123", "#", 0);
85 }
86 }
87 }
88
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
91 {
92 /* Run your code on data */
93 OHOS::DistributedHardware::DmAnonyousFuzzTest(data, size);
94 OHOS::DistributedHardware::DmAnonyousFuzzTestFirst(data, size);
95 return 0;
96 }
97