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 "clienttransudpstreaminterface_fuzzer.h"
17
18 #include <securec.h>
19
20 #include "client_trans_udp_stream_interface.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_def.h"
23
24 namespace OHOS {
SendVtpStreamTest(const uint8_t * data,size_t size)25 void SendVtpStreamTest(const uint8_t* data, size_t size)
26 {
27 if (data == nullptr || size < sizeof(int64_t)) {
28 return;
29 }
30 uint8_t *ptr = static_cast<uint8_t *>(SoftBusCalloc(size + 1));
31 if (ptr == nullptr) {
32 return;
33 }
34 if (memcpy_s(ptr, size, data, size) != EOK) {
35 SoftBusFree(ptr);
36 return;
37 }
38 int32_t channelId = *(reinterpret_cast<const int32_t *>(ptr));
39 StreamData streamdata = {
40 .buf = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
41 .bufLen = size,
42 };
43 StreamData ext = {
44 .buf = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
45 .bufLen = size,
46 };
47 TV tv = {
48 .type = *(reinterpret_cast<const int32_t *>(ptr)),
49 .value = *(reinterpret_cast<const int64_t *>(ptr)),
50 };
51 StreamFrameInfo param = {
52 .frameType = *(reinterpret_cast<const int32_t *>(ptr)),
53 .timeStamp = *(reinterpret_cast<const int32_t *>(ptr)),
54 .seqNum = *(reinterpret_cast<const int32_t *>(ptr)),
55 .seqSubNum = *(reinterpret_cast<const int32_t *>(ptr)),
56 .level = *(reinterpret_cast<const int32_t *>(ptr)),
57 .bitMap = *(reinterpret_cast<const int32_t *>(ptr)),
58 .tvCount = 1,
59 .tvList = &tv,
60 };
61
62 SendVtpStream(channelId, &streamdata, &ext, ¶m);
63 SoftBusFree(ptr);
64 }
65
StartVtpStreamChannelServerTest(const uint8_t * data,size_t size)66 void StartVtpStreamChannelServerTest(const uint8_t* data, size_t size)
67 {
68 if (data == nullptr || size < sizeof(int64_t)) {
69 return;
70 }
71 uint8_t *ptr = static_cast<uint8_t *>(SoftBusCalloc(size + 1));
72 if (ptr == nullptr) {
73 return;
74 }
75 if (memcpy_s(ptr, size, data, size) != EOK) {
76 SoftBusFree(ptr);
77 return;
78 }
79 int32_t channelId = *(reinterpret_cast<const int32_t *>(ptr));
80 VtpStreamOpenParam param = {
81 .pkgName = reinterpret_cast<const char *>(ptr),
82 .myIp = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
83 .peerIp = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
84 .peerPort = *(reinterpret_cast<const int32_t *>(ptr)),
85 .type = *(reinterpret_cast<const StreamType *>(ptr)),
86 .sessionKey = const_cast<uint8_t *>(ptr),
87 .keyLen = *(reinterpret_cast<const uint32_t *>(ptr)),
88 .isRawStreamEncrypt = size % 2,
89 };
90 IStreamListener *callback = nullptr;
91
92 StartVtpStreamChannelServer(channelId, ¶m, callback);
93 SoftBusFree(ptr);
94 }
95
StartVtpStreamChannelClientTest(const uint8_t * data,size_t size)96 void StartVtpStreamChannelClientTest(const uint8_t* data, size_t size)
97 {
98 if (data == nullptr || size < sizeof(int64_t)) {
99 return;
100 }
101 uint8_t *ptr = static_cast<uint8_t *>(SoftBusCalloc(size + 1));
102 if (ptr == nullptr) {
103 return;
104 }
105 if (memcpy_s(ptr, size, data, size) != EOK) {
106 SoftBusFree(ptr);
107 return;
108 }
109 int32_t channelId = *(reinterpret_cast<const int32_t *>(ptr));
110 VtpStreamOpenParam param = {
111 .pkgName = reinterpret_cast<const char *>(ptr),
112 .myIp = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
113 .peerIp = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
114 .peerPort = *(reinterpret_cast<const int32_t *>(ptr)),
115 .type = *(reinterpret_cast<const StreamType *>(ptr)),
116 .sessionKey = const_cast<uint8_t *>(ptr),
117 .keyLen = *(reinterpret_cast<const uint32_t *>(ptr)),
118 .isRawStreamEncrypt = size % 2,
119 };
120 IStreamListener *callback = nullptr;
121
122 StartVtpStreamChannelClient(channelId, ¶m, callback);
123 SoftBusFree(ptr);
124 }
125
CloseVtpStreamChannelTest(const uint8_t * data,size_t size)126 void CloseVtpStreamChannelTest(const uint8_t* data, size_t size)
127 {
128 if (data == nullptr || size < PKG_NAME_SIZE_MAX) {
129 return;
130 }
131 char tmp[PKG_NAME_SIZE_MAX + 1] = {0};
132 if (memcpy_s(tmp, PKG_NAME_SIZE_MAX, data, PKG_NAME_SIZE_MAX) != EOK) {
133 return;
134 };
135
136 CloseVtpStreamChannel(size, tmp);
137 }
138 } // namespace OHOS
139
140 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)141 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
142 {
143 /* Run your code on data */
144 OHOS::SendVtpStreamTest(data, size);
145 OHOS::StartVtpStreamChannelServerTest(data, size);
146 OHOS::StartVtpStreamChannelClientTest(data, size);
147 return 0;
148 }
149