• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fuzz_data_generator.h"
31 #include <fuzzer/FuzzedDataProvider.h>
32 #include "session.h"
33 #include "softbus_def.h"
34 #include "softbus_error_code.h"
35 #include "softbus_utils.h"
36 
37 namespace OHOS {
38 class TestEnv {
39 public:
TestEnv()40     TestEnv()
41     {
42         isInited_ = false;
43         IClientSessionCallBack *cb = GetClientSessionCb();
44         TransTdcManagerInit(cb);
45         ClientTransAuthInit(cb);
46         ClientTransProxyInit(cb);
47         ClientTransUdpMgrInit(cb);
48         isInited_ = true;
49     }
50 
~TestEnv()51     ~TestEnv()
52     {
53         isInited_ = false;
54         TransTdcManagerDeinit();
55         ClientTransUdpMgrDeinit();
56         ClientTransProxyDeinit();
57     }
58 
IsInited(void)59     bool IsInited(void)
60     {
61         return isInited_;
62     }
63 private:
64     volatile bool isInited_;
65 };
66 
ClientTransChannelCallbackTest(const uint8_t * data,size_t size)67 void ClientTransChannelCallbackTest(const uint8_t *data, size_t size)
68 {
69 #define TEST_DATA_LENGTH 1024
70 #define NETWORKID_LENGTH 65
71     if (data == nullptr || size < sizeof(int32_t)) {
72         return;
73     }
74     DataGenerator::Write(data, size);
75     FuzzedDataProvider dataProvider(data, size);
76     char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
77     ChannelInfo channel = { 0 };
78     int32_t channelId = 0;
79     int32_t channelType = 0;
80     int32_t errCode = 0;
81     int32_t messageType = 0;
82     int32_t shutdownReason = 0;
83     std::string networkId = dataProvider.ConsumeRandomLengthString(NETWORKID_LENGTH - 1);
84     int32_t routeType = 0;
85     int32_t eventId = 0;
86     int32_t tvCount = 0;
87     QosTv tvList = {};
88     int32_t sessionId = 0;
89 
90     GenerateInt32(channelId);
91     GenerateInt32(channelType);
92     GenerateInt32(errCode);
93     GenerateInt32(messageType);
94     GenerateInt32(shutdownReason);
95     GenerateInt32(routeType);
96     GenerateInt32(eventId);
97     GenerateInt32(tvCount);
98     GenerateInt32(sessionId);
99 
100     TransOnChannelOpened(sessionName, &channel);
101 
102     TransOnChannelOpenFailed(channelId, channelType, errCode);
103 
104     TransOnChannelClosed(channelId, channelType, messageType, (ShutdownReason)shutdownReason);
105 
106     TransOnChannelLinkDown(networkId.c_str(), routeType);
107 
108     TransOnChannelMsgReceived(channelId, CHANNEL_TYPE_PROXY, (void *)data, TEST_DATA_LENGTH, TRANS_SESSION_BYTES);
109 
110     TransOnChannelQosEvent(channelId, channelType, eventId, tvCount, &tvList);
111 
112     TransSetChannelInfo(sessionName, sessionId, channelId, channelType);
113 
114     TransOnChannelBind(channelId, channelType);
115     DataGenerator::Clear();
116 }
117 } // namespace OHOS
118 
119 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)120 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
121 {
122     /* Run your code on data */
123     static OHOS::TestEnv env;
124     if (!env.IsInited()) {
125         return 0;
126     }
127     OHOS::ClientTransChannelCallbackTest(data, size);
128     return 0;
129 }
130