1 /*
2 * Copyright (c) 2023 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 "clienttranschannelcallback_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <securec.h>
21
22 #include "client_trans_auth_manager.h"
23 #include "client_trans_channel_callback.h"
24 #include "client_trans_proxy_manager.h"
25 #include "client_trans_session_callback.h"
26 #include "client_trans_session_manager.h"
27 #include "client_trans_socket_manager.h"
28 #include "client_trans_tcp_direct_manager.h"
29 #include "client_trans_udp_manager.h"
30 #include "session.h"
31 #include "softbus_def.h"
32 #include "softbus_error_code.h"
33 #include "softbus_utils.h"
34
35 namespace OHOS {
36 class TestEnv {
37 public:
TestEnv()38 TestEnv()
39 {
40 isInited_ = false;
41 IClientSessionCallBack *cb = GetClientSessionCb();
42 TransTdcManagerInit(cb);
43 ClientTransAuthInit(cb);
44 ClientTransProxyInit(cb);
45 ClientTransUdpMgrInit(cb);
46 isInited_ = true;
47 }
48
~TestEnv()49 ~TestEnv()
50 {
51 isInited_ = false;
52 TransTdcManagerDeinit();
53 ClientTransUdpMgrDeinit();
54 ClientTransProxyDeinit();
55 }
56
IsInited(void)57 bool IsInited(void)
58 {
59 return isInited_;
60 }
61 private:
62 volatile bool isInited_;
63 };
64
ClientTransChannelCallbackTest(const uint8_t * data,size_t size)65 void ClientTransChannelCallbackTest(const uint8_t *data, size_t size)
66 {
67 #define TEST_DATA_LENGTH 1024
68 if (data == nullptr || size < sizeof(int32_t)) {
69 return;
70 }
71
72 char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
73 ChannelInfo channel = { 0 };
74
75 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
76 int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
77 int32_t errCode = *(reinterpret_cast<const int32_t *>(data));
78 int32_t messageType = *(reinterpret_cast<const int32_t *>(data));
79 int32_t shutdownReason = *(reinterpret_cast<const int32_t *>(data));
80
81 char *networkId = const_cast<char *>(reinterpret_cast<const char *>(data));
82 int32_t routeType = *(reinterpret_cast<const int32_t *>(data));
83
84 int32_t eventId = *(reinterpret_cast<const int32_t *>(data));
85 int32_t tvCount = *(reinterpret_cast<const int32_t *>(data));
86 QosTv tvList = {};
87
88 int32_t sessionId = *(reinterpret_cast<const int32_t *>(data));
89
90 TransOnChannelOpened(sessionName, &channel);
91
92 TransOnChannelOpenFailed(channelId, channelType, errCode);
93
94 TransOnChannelClosed(channelId, channelType, messageType, (ShutdownReason)shutdownReason);
95
96 TransOnChannelLinkDown(networkId, routeType);
97
98 TransOnChannelMsgReceived(channelId, CHANNEL_TYPE_PROXY, (void *)data, TEST_DATA_LENGTH, TRANS_SESSION_BYTES);
99
100 TransOnChannelQosEvent(channelId, channelType, eventId, tvCount, &tvList);
101
102 TransSetChannelInfo(sessionName, sessionId, channelId, channelType);
103
104 TransOnChannelBind(channelId, channelType);
105 }
106 } // namespace OHOS
107
108 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)109 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
110 {
111 /* Run your code on data */
112 static OHOS::TestEnv env;
113 if (!env.IsInited()) {
114 return 0;
115 }
116 OHOS::ClientTransChannelCallbackTest(data, size);
117 return 0;
118 }
119