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