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 <gtest/gtest.h>
17 #include "securec.h"
18
19 #include "client_trans_proxy_manager.h"
20 #include "client_trans_session_manager.h"
21 #include "session.h"
22 #include "softbus_def.h"
23 #include "softbus_errcode.h"
24 #include "softbus_access_token_test.h"
25 #include "client_trans_proxy_file_manager.h"
26
27 #define TEST_CHANNEL_ID (-10)
28 #define TEST_ERR_CODE (-1)
29 #define TEST_DATA "testdata"
30 #define TEST_DATA_LENGTH 9
31 #define TEST_FILE_CNT 4
32
33 using namespace std;
34 using namespace testing::ext;
35
36 namespace OHOS {
37 const char *g_proxyPkgName = "dms";
38 const char *g_proxySessionName = "ohos.distributedschedule.dms.test";
39 const char *g_testProxyFileName[] = {
40 "/data/test.txt",
41 "/data/ss.txt",
42 "/data/test.tar",
43 "/data/test.mp3",
44 };
45 const char *g_proxyFileSet[] = {
46 "/data/data/test.txt",
47 "/path/max/length/512/"
48 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
49 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
50 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
51 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
52 "111111111111111111111111111111111111111111111111111",
53 "ss",
54 "/data/ss",
55 };
56
TransOnSessionOpened(const char * sessionName,const ChannelInfo * channel,SessionType flag)57 int32_t TransOnSessionOpened(const char *sessionName, const ChannelInfo *channel, SessionType flag)
58 {
59 return SOFTBUS_OK;
60 }
61
TransOnSessionClosed(int32_t channelId,int32_t channelType)62 int32_t TransOnSessionClosed(int32_t channelId, int32_t channelType)
63 {
64 return SOFTBUS_OK;
65 }
66
TransOnSessionOpenFailed(int32_t channelId,int32_t channelType,int32_t errCode)67 int32_t TransOnSessionOpenFailed(int32_t channelId, int32_t channelType, int32_t errCode)
68 {
69 return SOFTBUS_OK;
70 }
71
TransOnBytesReceived(int32_t channelId,int32_t channelType,const void * data,uint32_t len,SessionPktType type)72 int32_t TransOnBytesReceived(int32_t channelId, int32_t channelType,
73 const void *data, uint32_t len, SessionPktType type)
74 {
75 return SOFTBUS_OK;
76 }
77
TransOnOnStreamRecevied(int32_t channelId,int32_t channelType,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)78 int32_t TransOnOnStreamRecevied(int32_t channelId, int32_t channelType,
79 const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
80 {
81 return SOFTBUS_OK;
82 }
83
TransOnGetSessionId(int32_t channelId,int32_t channelType,int32_t * sessionId)84 int32_t TransOnGetSessionId(int32_t channelId, int32_t channelType, int32_t *sessionId)
85 {
86 return SOFTBUS_OK;
87 }
TransOnQosEvent(int32_t channelId,int32_t channelType,int32_t eventId,int32_t tvCount,const QosTv * tvList)88 int32_t TransOnQosEvent(int32_t channelId, int32_t channelType, int32_t eventId,
89 int32_t tvCount, const QosTv *tvList)
90 {
91 return SOFTBUS_OK;
92 }
93
94 static IClientSessionCallBack g_clientSessionCb = {
95 .OnSessionOpened = TransOnSessionOpened,
96 .OnSessionClosed = TransOnSessionClosed,
97 .OnSessionOpenFailed = TransOnSessionOpenFailed,
98 .OnDataReceived = TransOnBytesReceived,
99 .OnStreamReceived = TransOnOnStreamRecevied,
100 .OnQosEvent = TransOnQosEvent,
101 };
102
OnSessionOpened(const char * sessionName,const ChannelInfo * channel,SessionType flag)103 int32_t OnSessionOpened(const char *sessionName, const ChannelInfo *channel, SessionType flag)
104 {
105 return SOFTBUS_ERR;
106 }
107
OnSessionClosed(int32_t channelId,int32_t channelType)108 int32_t OnSessionClosed(int32_t channelId, int32_t channelType)
109 {
110 return SOFTBUS_ERR;
111 }
112
OnSessionOpenFailed(int32_t channelId,int32_t channelType,int32_t errCode)113 int32_t OnSessionOpenFailed(int32_t channelId, int32_t channelType, int32_t errCode)
114 {
115 return SOFTBUS_ERR;
116 }
117
OnBytesReceived(int32_t channelId,int32_t channelType,const void * data,uint32_t len,SessionPktType type)118 int32_t OnBytesReceived(int32_t channelId, int32_t channelType,
119 const void *data, uint32_t len, SessionPktType type)
120 {
121 return SOFTBUS_ERR;
122 }
123
124 static IClientSessionCallBack g_sessionCb = {
125 .OnSessionOpened = OnSessionOpened,
126 .OnSessionClosed = OnSessionClosed,
127 .OnSessionOpenFailed = OnSessionOpenFailed,
128 .OnDataReceived = OnBytesReceived,
129 };
130
131 class ClientTransProxyManagerTest : public testing::Test {
132 public:
ClientTransProxyManagerTest()133 ClientTransProxyManagerTest() {}
~ClientTransProxyManagerTest()134 ~ClientTransProxyManagerTest() {}
135 static void SetUpTestCase(void);
136 static void TearDownTestCase(void);
SetUp()137 void SetUp() override {}
TearDown()138 void TearDown() override {}
139 };
140
SetUpTestCase(void)141 void ClientTransProxyManagerTest::SetUpTestCase(void)
142 {
143 int ret = ClientTransProxyInit(&g_clientSessionCb);
144 EXPECT_EQ(SOFTBUS_OK, ret);
145 SetAceessTokenPermission("dsoftbusTransTest");
146 }
TearDownTestCase(void)147 void ClientTransProxyManagerTest::TearDownTestCase(void) {}
148
149 /**
150 * @tc.name: ClientTransProxyInitTest
151 * @tc.desc: client trans proxy init test, use the wrong or normal parameter.
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 HWTEST_F(ClientTransProxyManagerTest, ClientTransProxyInitTest, TestSize.Level0)
156 {
157 int ret = ClientTransProxyInit(nullptr);
158 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
159 }
160
161 /**
162 * @tc.name: ClientTransProxyOnChannelOpenedTest
163 * @tc.desc: client trans proxy on channel opened test, use the wrong or normal parameter.
164 * @tc.type: FUNC
165 * @tc.require:
166 */
167 HWTEST_F(ClientTransProxyManagerTest, ClientTransProxyOnChannelOpenedTest, TestSize.Level0)
168 {
169 int ret = ClientTransProxyOnChannelOpened(g_proxySessionName, nullptr);
170 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
171
172 ChannelInfo channelInfo = {0};
173 ret = ClientTransProxyOnChannelOpened(g_proxySessionName, &channelInfo);
174 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
175 }
176
177 /**
178 * @tc.name: ClientTransProxyOnChannelClosedTest
179 * @tc.desc: client trans proxy on channel closed test, use the normal parameter.
180 * @tc.type: FUNC
181 * @tc.require:
182 */
183 HWTEST_F(ClientTransProxyManagerTest, ClientTransProxyOnChannelClosedTest, TestSize.Level0)
184 {
185 int32_t channelId = 1;
186 int ret = ClientTransProxyOnChannelClosed(channelId);
187 EXPECT_EQ(SOFTBUS_OK, ret);
188 }
189
190 /**
191 * @tc.name: ClientTransProxyOnChannelOpenFailedTest
192 * @tc.desc: client trans proxy on channel open failed test, use the normal parameter.
193 * @tc.type: FUNC
194 * @tc.require:
195 */
196 HWTEST_F(ClientTransProxyManagerTest, ClientTransProxyOnChannelOpenFailedTest, TestSize.Level0)
197 {
198 int32_t channelId = 1;
199 int ret = ClientTransProxyOnChannelOpenFailed(channelId, TEST_ERR_CODE);
200 EXPECT_EQ(SOFTBUS_OK, ret);
201 }
202
203 /**
204 * @tc.name: ClientTransProxyOnDataReceivedTest
205 * @tc.desc: client trans proxy on data received test, use the wrong or normal parameter.
206 * @tc.type: FUNC
207 * @tc.require:
208 */
209 HWTEST_F(ClientTransProxyManagerTest, ClientTransProxyOnDataReceivedTest, TestSize.Level0)
210 {
211 int32_t channelId = 1;
212 int ret = ClientTransProxyOnDataReceived(channelId, nullptr, TEST_DATA_LENGTH, TRANS_SESSION_BYTES);
213 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
214
215 ret = ClientTransProxyOnDataReceived(channelId, TEST_DATA, TEST_DATA_LENGTH, TRANS_SESSION_BYTES);
216 EXPECT_EQ(SOFTBUS_ERR, ret);
217 }
218
219 /**
220 * @tc.name: ClientTransProxyErrorCallBackTest
221 * @tc.desc: client trans proxy error callback test, use the wrong or normal parameter.
222 * @tc.type: FUNC
223 * @tc.require:
224 */
225 HWTEST_F(ClientTransProxyManagerTest, ClientTransProxyErrorCallBackTest, TestSize.Level0)
226 {
227 int ret = ClientTransProxyInit(&g_sessionCb);
228 EXPECT_EQ(SOFTBUS_OK, ret);
229
230 ChannelInfo channelInfo = {0};
231 ret = ClientTransProxyOnChannelOpened(g_proxySessionName, &channelInfo);
232 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
233
234 int32_t channelId = 1;
235 ret = ClientTransProxyOnChannelClosed(channelId);
236 EXPECT_NE(SOFTBUS_OK, ret);
237
238 ret = ClientTransProxyOnChannelOpenFailed(channelId, TEST_ERR_CODE);
239 EXPECT_NE(SOFTBUS_OK, ret);
240
241 ret = ClientTransProxyOnDataReceived(channelId, TEST_DATA, TEST_DATA_LENGTH, TRANS_SESSION_BYTES);
242 EXPECT_NE(SOFTBUS_OK, ret);
243 }
244
245 /**
246 * @tc.name: ClientTransProxyCloseChannelTest
247 * @tc.desc: client trans proxy close channel test, use the normal parameter.
248 * @tc.type: FUNC
249 * @tc.require:
250 */
251 HWTEST_F(ClientTransProxyManagerTest, ClientTransProxyCloseChannelTest, TestSize.Level0)
252 {
253 int32_t ret = ClientTransProxyInit(&g_sessionCb);
254 EXPECT_EQ(SOFTBUS_OK, ret);
255 int32_t channelId = 1;
256 ClientTransProxyCloseChannel(TEST_CHANNEL_ID);
257
258 ClientTransProxyCloseChannel(channelId);
259 }
260
261 /**
262 * @tc.name: TransProxyChannelSendFileTest
263 * @tc.desc: trans proxy channel send file test, use the wrong parameter.
264 * @tc.type: FUNC
265 * @tc.require:
266 */
267 HWTEST_F(ClientTransProxyManagerTest, TransProxyChannelSendFileTest, TestSize.Level0)
268 {
269 int32_t channelId = 1;
270 int ret = TransProxyChannelSendFile(channelId, nullptr, g_proxyFileSet, TEST_FILE_CNT);
271 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
272
273 ret = TransProxyChannelSendFile(channelId, g_testProxyFileName, g_proxyFileSet, MAX_SEND_FILE_NUM + TEST_FILE_CNT);
274 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
275
276 ret = TransProxyChannelSendFile(channelId, g_testProxyFileName, nullptr, TEST_FILE_CNT);
277 EXPECT_EQ(SOFTBUS_ERR, ret);
278
279 ret = TransProxyChannelSendFile(channelId, g_testProxyFileName, g_proxyFileSet, TEST_FILE_CNT);
280 EXPECT_EQ(SOFTBUS_ERR, ret);
281 }
282 } // namespace OHOS