1 /*
2  * Copyright (c) 2022 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 "vtpstreamsocket_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "vtp_stream_socket.h"
22 #include "stream_common.h"
23 #include "stream_common_data.h"
24 
25 namespace OHOS {
26     Communication::SoftBus::VtpStreamSocket vtpStreamSocket;
27 
VtpCreateClientTest(const uint8_t * data,size_t size)28     void VtpCreateClientTest(const uint8_t* data, size_t size)
29     {
30         if ((data == nullptr) || (size < sizeof(int))) {
31             return;
32         }
33 
34         int streamType = *(reinterpret_cast<const int*>(data));
35         Communication::SoftBus::IpAndPort ipPort;
36         std::pair<uint8_t*, uint32_t> sessionKey = std::make_pair(nullptr, 0);
37 
38         vtpStreamSocket.CreateClient(ipPort, streamType, sessionKey);
39         vtpStreamSocket.CreateClient(ipPort, ipPort, streamType, sessionKey);
40     }
41 
VtpCreateServerTest(const uint8_t * data,size_t size)42     void VtpCreateServerTest(const uint8_t* data, size_t size)
43     {
44         if ((data == nullptr) || (size < sizeof(int))) {
45             return;
46         }
47 
48         int streamType = *(reinterpret_cast<const int*>(data));
49         Communication::SoftBus::IpAndPort ipPort;
50         std::pair<uint8_t*, uint32_t> sessionKey = std::make_pair(nullptr, 0);
51 
52         vtpStreamSocket.CreateServer(ipPort, streamType, sessionKey);
53     }
54 
VtpDestroyStreamSocketTest(const uint8_t * data,size_t size)55     void VtpDestroyStreamSocketTest(const uint8_t* data, size_t size)
56     {
57         if ((data == nullptr) || (size == 0)) {
58             return;
59         }
60 
61         vtpStreamSocket.DestroyStreamSocket();
62     }
63 
VtpConnectTest(const uint8_t * data,size_t size)64     void VtpConnectTest(const uint8_t* data, size_t size)
65     {
66         if ((data == nullptr) || (size == 0)) {
67             return;
68         }
69 
70         Communication::SoftBus::IpAndPort ipPort;
71         vtpStreamSocket.Connect(ipPort);
72     }
73 
VtpSetOptionTest(const uint8_t * data,size_t size)74     void VtpSetOptionTest(const uint8_t* data, size_t size)
75     {
76         if ((data == nullptr) || (size < sizeof(int))) {
77             return;
78         }
79 
80         int type = *(reinterpret_cast<const int*>(data));
81         Communication::SoftBus::StreamAttr tmp;
82 
83         vtpStreamSocket.SetOption(type, tmp);
84     }
85 
VtpGetOptionTest(const uint8_t * data,size_t size)86     void VtpGetOptionTest(const uint8_t* data, size_t size)
87     {
88         if ((data == nullptr) || (size < sizeof(int))) {
89             return;
90         }
91 
92         int type = *(reinterpret_cast<const int*>(data));
93 
94         vtpStreamSocket.GetOption(type);
95     }
96 
VtpSetStreamListenerTest(const uint8_t * data,size_t size)97     void VtpSetStreamListenerTest(const uint8_t* data, size_t size)
98     {
99         if ((data == nullptr) || (size == 0)) {
100             return;
101         }
102 
103         std::shared_ptr<Communication::SoftBus::IStreamSocketListener> receiver = nullptr;
104 
105         vtpStreamSocket.SetStreamListener(receiver);
106     }
107 
VtpGetEncryptOverheadTest(const uint8_t * data,size_t size)108     void VtpGetEncryptOverheadTest(const uint8_t* data, size_t size)
109     {
110         if ((data == nullptr) || (size == 0)) {
111             return;
112         }
113 
114         vtpStreamSocket.GetEncryptOverhead();
115     }
116 
VtpEncrypt(const uint8_t * data,size_t size)117     void VtpEncrypt(const uint8_t* data, size_t size)
118     {
119         if ((data == nullptr) || (size == 0)) {
120             return;
121         }
122 
123         vtpStreamSocket.Encrypt(nullptr, size, nullptr, size);
124     }
125 
VtpDecrypt(const uint8_t * data,size_t size)126     void VtpDecrypt(const uint8_t* data, size_t size)
127     {
128         if ((data == nullptr) || (size == 0)) {
129             return;
130         }
131 
132         vtpStreamSocket.Decrypt(nullptr, size, nullptr, size);
133     }
134 }
135 
136 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
138 {
139     /* Run your code on data */
140     OHOS::VtpCreateServerTest(data, size);
141     OHOS::VtpDestroyStreamSocketTest(data, size);
142     OHOS::VtpConnectTest(data, size);
143     OHOS::VtpSetOptionTest(data, size);
144     OHOS::VtpGetOptionTest(data, size);
145     OHOS::VtpSetStreamListenerTest(data, size);
146     OHOS::VtpGetEncryptOverheadTest(data, size);
147     OHOS::VtpEncrypt(data, size);
148     OHOS::VtpDecrypt(data, size);
149     return 0;
150 }
151