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 "clienttransstream_fuzzer.h"
17
18 #include <securec.h>
19 #include "client_trans_stream.h"
20 #include "softbus_adapter_mem.h"
21
22 namespace OHOS {
TransOnstreamChannelOpenedTest(const uint8_t * data,size_t size)23 void TransOnstreamChannelOpenedTest(const uint8_t* data, size_t size)
24 {
25 if (data == nullptr || size < sizeof(int32_t)) {
26 return;
27 }
28 ChannelInfo *channel = nullptr;
29 int32_t streamPort = *(reinterpret_cast<const int32_t *>(data));
30
31 TransOnstreamChannelOpened(channel, &streamPort);
32 }
33
TransSendStreamTest(const uint8_t * data,size_t size)34 void TransSendStreamTest(const uint8_t* data, size_t size)
35 {
36 if (data == nullptr || size < sizeof(int64_t)) {
37 return;
38 }
39 uint8_t *ptr = static_cast<uint8_t *>(SoftBusCalloc(size + 1));
40 if (ptr == nullptr) {
41 return;
42 }
43 if (memcpy_s(ptr, size, data, size) != EOK) {
44 SoftBusFree(ptr);
45 return;
46 }
47 int32_t channelId = *(reinterpret_cast<const int32_t *>(ptr));
48 StreamData streamdata = {
49 .buf = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
50 .bufLen = size,
51 };
52 StreamData ext = {
53 .buf = const_cast<char *>(reinterpret_cast<const char *>(ptr)),
54 .bufLen = size,
55 };
56 TV tv = {
57 .type = *(reinterpret_cast<const int32_t *>(ptr)),
58 .value = *(reinterpret_cast<const int64_t *>(ptr)),
59 };
60 StreamFrameInfo param = {
61 .frameType = *(reinterpret_cast<const int32_t *>(ptr)),
62 .timeStamp = *(reinterpret_cast<const int32_t *>(ptr)),
63 .seqNum = *(reinterpret_cast<const int32_t *>(ptr)),
64 .seqSubNum = *(reinterpret_cast<const int32_t *>(ptr)),
65 .level = *(reinterpret_cast<const int32_t *>(ptr)),
66 .bitMap = *(reinterpret_cast<const int32_t *>(ptr)),
67 .tvCount = 1,
68 .tvList = &tv,
69 };
70
71 TransSendStream(channelId, &streamdata, &ext, ¶m);
72 SoftBusFree(ptr);
73 }
74
TransCloseStreamChannelTest(const uint8_t * data,size_t size)75 void TransCloseStreamChannelTest(const uint8_t* data, size_t size)
76 {
77 if (data == nullptr || size < sizeof(int32_t)) {
78 return;
79 }
80 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
81
82 TransCloseStreamChannel(channelId);
83 }
84 } // namespace OHOS
85
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)87 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
88 {
89 /* Run your code on data */
90 OHOS::TransOnstreamChannelOpenedTest(data, size);
91 OHOS::TransSendStreamTest(data, size);
92 OHOS::TransCloseStreamChannelTest(data, size);
93 return 0;
94 }
95