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 "evaluateqos_fuzzer.h"
17 
18 #include <memory>
19 #include <securec.h>
20 #include <string>
21 
22 #include "socket.h"
23 
24 namespace OHOS {
25 static std::string g_defaultSocketPeerNetworkid =
26     "a8ynvpdaihw1f6nknjd2hkfhxljxypkr6kvjsbhnhpp16974uo4fvsrpfa6t50fm";
EvaluateQosTestWithNetworkId(const uint8_t * data,size_t size)27 void EvaluateQosTestWithNetworkId(const uint8_t *data, size_t size)
28 {
29     if ((data == nullptr) || (size == 0)) {
30         return;
31     }
32 
33     const size_t bufSize = size + 1;
34     std::unique_ptr<char[]> peerNetworkId = std::make_unique<char[]>(bufSize);
35     if (memset_s(peerNetworkId.get(), bufSize, 0, bufSize) != EOK) {
36         return;
37     }
38 
39     if (memcpy_s(peerNetworkId.get(), bufSize, data, size) != EOK) {
40         return;
41     }
42 
43     QosTV qosInfo[] = {
44         { .qos = QOS_TYPE_MIN_BW,           .value = 160 * 1024 * 1024 },
45         { .qos = QOS_TYPE_MAX_WAIT_TIMEOUT, .value = 10                },
46         { .qos = QOS_TYPE_MIN_LATENCY,      .value = 5                 },
47     };
48 
49     (void)EvaluateQos(peerNetworkId.get(), DATA_TYPE_MESSAGE, qosInfo, sizeof(qosInfo) / sizeof(qosInfo[0]));
50 }
51 
EvaluateQosTestWithDataType(const uint8_t * data,size_t size)52 void EvaluateQosTestWithDataType(const uint8_t *data, size_t size)
53 {
54     if ((data == nullptr) || (size < sizeof(TransDataType))) {
55         return;
56     }
57 
58     TransDataType socketDataType = DATA_TYPE_BUTT;
59     if (memcpy_s(&socketDataType, sizeof(TransDataType), data, sizeof(TransDataType)) != EOK) {
60         return;
61     }
62 
63     QosTV qosInfo[] = {
64         { .qos = QOS_TYPE_MIN_BW,           .value = 160 * 1024 * 1024 },
65         { .qos = QOS_TYPE_MAX_WAIT_TIMEOUT, .value = 10                },
66         { .qos = QOS_TYPE_MIN_LATENCY,      .value = 5                 },
67     };
68 
69     (void)EvaluateQos(g_defaultSocketPeerNetworkid.c_str(), socketDataType, qosInfo,
70         sizeof(qosInfo) / sizeof(qosInfo[0]));
71 }
72 
EvaluateQosTestWithQosInfo(const uint8_t * data,size_t size)73 void EvaluateQosTestWithQosInfo(const uint8_t *data, size_t size)
74 {
75     if ((data == nullptr) || (size < sizeof(QosTV))) {
76         return;
77     }
78 
79     size_t count = size / sizeof(QosTV);
80     if (count == 0) {
81         return;
82     }
83 
84     std::unique_ptr<QosTV[]> qosInfo = std::make_unique<QosTV[]>(count);
85     if (memcpy_s(qosInfo.get(), sizeof(QosTV) * count, data, sizeof(QosTV) * count) != EOK) {
86         return;
87     }
88     (void)EvaluateQos(g_defaultSocketPeerNetworkid.c_str(), DATA_TYPE_MESSAGE, qosInfo.get(), count);
89 }
90 } // namespace OHOS
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
94 {
95     OHOS::EvaluateQosTestWithNetworkId(data, size);
96     return 0;
97 }
98