• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fuzz_data_generator.h"
22 #include "vtp_stream_socket.h"
23 #include "stream_common.h"
24 #include "stream_common_data.h"
25 
26 namespace OHOS {
27     Communication::SoftBus::VtpStreamSocket vtpStreamSocket;
28 
VtpCreateClientTest(const uint8_t * data,size_t size)29     void VtpCreateClientTest(const uint8_t* data, size_t size)
30     {
31         if (data == nullptr || size < sizeof(int32_t)) {
32             return;
33         }
34 
35         int32_t streamType = *(reinterpret_cast<const int32_t *>(data));
36         Communication::SoftBus::IpAndPort ipPort;
37         std::pair<uint8_t*, uint32_t> sessionKey = std::make_pair(nullptr, 0);
38 
39         vtpStreamSocket.CreateClient(ipPort, streamType, sessionKey);
40         vtpStreamSocket.CreateClient(ipPort, ipPort, streamType, sessionKey);
41     }
42 
VtpCreateServerTest(const uint8_t * data,size_t size)43     void VtpCreateServerTest(const uint8_t* data, size_t size)
44     {
45         if (data == nullptr || size < sizeof(int32_t)) {
46             return;
47         }
48 
49         int32_t streamType = *(reinterpret_cast<const int32_t *>(data));
50         Communication::SoftBus::IpAndPort ipPort;
51         std::pair<uint8_t*, uint32_t> sessionKey = std::make_pair(nullptr, 0);
52 
53         vtpStreamSocket.CreateServer(ipPort, streamType, sessionKey);
54     }
55 
VtpDestroyStreamSocketTest(const uint8_t * data,size_t size)56     void VtpDestroyStreamSocketTest(const uint8_t* data, size_t size)
57     {
58         (void)data;
59         (void)size;
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 < sizeof(int32_t)) {
67             return;
68         }
69 
70         Communication::SoftBus::IpAndPort ipPort;
71         ipPort.ip = {0};
72         ipPort.port = *(reinterpret_cast<const int32_t *>(data));
73         vtpStreamSocket.Connect(ipPort);
74     }
75 
VtpSetOptionTest(const uint8_t * data,size_t size)76     void VtpSetOptionTest(const uint8_t* data, size_t size)
77     {
78         if (data == nullptr || size < sizeof(int32_t)) {
79             return;
80         }
81 
82         int32_t type = *(reinterpret_cast<const int32_t *>(data));
83         Communication::SoftBus::StreamAttr tmp;
84 
85         vtpStreamSocket.SetOption(type, tmp);
86     }
87 
VtpGetOptionTest(const uint8_t * data,size_t size)88     void VtpGetOptionTest(const uint8_t* data, size_t size)
89     {
90         if (data == nullptr || size < sizeof(int32_t)) {
91             return;
92         }
93 
94         int32_t type = *(reinterpret_cast<const int32_t *>(data));
95 
96         vtpStreamSocket.GetOption(type);
97     }
98 
VtpSetStreamListenerTest(const uint8_t * data,size_t size)99     void VtpSetStreamListenerTest(const uint8_t* data, size_t size)
100     {
101         (void)data;
102         (void)size;
103 
104         std::shared_ptr<Communication::SoftBus::IStreamSocketListener> receiver = nullptr;
105 
106         vtpStreamSocket.SetStreamListener(receiver);
107     }
108 
VtpGetEncryptOverheadTest(const uint8_t * data,size_t size)109     void VtpGetEncryptOverheadTest(const uint8_t* data, size_t size)
110     {
111         (void)data;
112         (void)size;
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 < sizeof(size_t)) {
120             return;
121         }
122         DataGenerator::Write(data, size);
123         int64_t inlen = 0;
124         int64_t outlen = 0;
125         GenerateInt64(inlen);
126         GenerateInt64(outlen);
127         const void *in = reinterpret_cast<const void *>(data + 1);
128         void *out = const_cast<void *>(reinterpret_cast<const void *>(data));
129 
130         vtpStreamSocket.Encrypt(in, inlen, out, outlen);
131         DataGenerator::Clear();
132     }
133 
VtpDecrypt(const uint8_t * data,size_t size)134     void VtpDecrypt(const uint8_t* data, size_t size)
135     {
136         if (data == nullptr || size < sizeof(size_t)) {
137             return;
138         }
139         DataGenerator::Write(data, size);
140         int64_t inlen = 0;
141         int64_t outlen = 0;
142         GenerateInt64(inlen);
143         GenerateInt64(outlen);
144         const void *in = reinterpret_cast<const void *>(data + 1);
145         void *out = const_cast<void *>(reinterpret_cast<const void *>(data));
146 
147         vtpStreamSocket.Decrypt(in, inlen, out, outlen);
148         DataGenerator::Clear();
149     }
150 }
151 
152 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)153 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
154 {
155     /* Run your code on data */
156     OHOS::VtpCreateServerTest(data, size);
157     OHOS::VtpDestroyStreamSocketTest(data, size);
158     OHOS::VtpConnectTest(data, size);
159     OHOS::VtpSetOptionTest(data, size);
160     OHOS::VtpGetOptionTest(data, size);
161     OHOS::VtpSetStreamListenerTest(data, size);
162     OHOS::VtpGetEncryptOverheadTest(data, size);
163     OHOS::VtpEncrypt(data, size);
164     OHOS::VtpDecrypt(data, size);
165     return 0;
166 }
167