1 /*
2  * Copyright (c) 2022-2024 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 "screen_data_channel_impl_test.h"
17 #include "dscreen_util.h"
18 #include "dscreen_json_util.h"
19 #include "accesstoken_kit.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 namespace {
29     const std::string PEER_SESSION_NAME = "ohos.dhardware.dscreen.session8647073e02e7a78f09473aa123";
30     const std::string REMOTE_DEV_ID = "f6d4c0864707aefte7a78f09473aa122ff57fc81c00981fcf5be989e7d112123";
31     const std::string DSCREEN_PKG_NAME_TEST = "ohos.dhardware.dscreen";
32 }
SetUpTestCase(void)33 void ScreenDataChannelImplTest::SetUpTestCase(void) {}
34 
TearDownTestCase(void)35 void ScreenDataChannelImplTest::TearDownTestCase(void) {}
36 
SetUp(void)37 void ScreenDataChannelImplTest::SetUp(void)
38 {
39     std::string peerDevId = "test";
40     dataChannelImpl_ = std::make_shared<ScreenDataChannelImpl>(peerDevId);
41 }
42 
TearDown(void)43 void ScreenDataChannelImplTest::TearDown(void)
44 {
45     if (dataChannelImpl_ != nullptr) {
46         dataChannelImpl_->CloseSession();
47         dataChannelImpl_->ReleaseSession();
48         dataChannelImpl_ = nullptr;
49     }
50 }
51 
EnablePermissionAccess(const char * perms[],size_t permsNum,uint64_t & tokenId)52 void EnablePermissionAccess(const char* perms[], size_t permsNum, uint64_t &tokenId)
53 {
54     NativeTokenInfoParams infoInstance = {
55         .dcapsNum = 0,
56         .permsNum = permsNum,
57         .aclsNum = 0,
58         .dcaps = nullptr,
59         .perms = perms,
60         .acls = nullptr,
61         .aplStr = "system_basic",
62     };
63 
64     infoInstance.processName = "DataChannelTest";
65     tokenId = GetAccessTokenId(&infoInstance);
66     SetSelfTokenID(tokenId);
67     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
68 }
69 
DisablePermissionAccess(const uint64_t & tokenId)70 void DisablePermissionAccess(const uint64_t &tokenId)
71 {
72     OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(tokenId);
73 }
74 
75 /**
76  * @tc.name: CreateSession_001
77  * @tc.desc: Verify the CreateSession function.
78  * @tc.type: FUNC
79  * @tc.require: Issue Number
80  */
81 HWTEST_F(ScreenDataChannelImplTest, CreateSession_001, TestSize.Level1)
82 {
83     std::shared_ptr<IScreenChannelListener> listener = nullptr;
84     int32_t ret = dataChannelImpl_->CreateSession(listener);
85 
86     EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, ret);
87 }
88 
89 /**
90  * @tc.name: CreateSession_002
91  * @tc.desc: Verify the CreateSession function.
92  * @tc.type: FUNC
93  * @tc.require: Issue Number
94  */
95 HWTEST_F(ScreenDataChannelImplTest, CreateSession_002, TestSize.Level1)
96 {
97     const char* perms[] = {
98         "ohos.permission.DISTRIBUTED_DATASYNC",
99         "ohos.permission.CAPTURE_SCREEN",
100     };
101     EnablePermissionAccess(perms, sizeof(perms) / sizeof(perms[0]), tokenId_);
102     std::shared_ptr<IScreenChannelListener> listener = std::make_shared<MockIScreenChannelListener>();
103     dataChannelImpl_->jpegSessionFlag_ = false;
104     int32_t ret = dataChannelImpl_->CreateSession(listener);
105     DisablePermissionAccess(tokenId_);
106     EXPECT_EQ(DH_SUCCESS, ret);
107 }
108 
109 /**
110  * @tc.name: OpenSession_001
111  * @tc.desc: Verify the OpenSession function.
112  * @tc.type: FUNC
113  * @tc.require: Issue Number
114  */
115 HWTEST_F(ScreenDataChannelImplTest, OpenSession_001, TestSize.Level1)
116 {
117     std::shared_ptr<IScreenChannelListener> listener = std::make_shared<MockIScreenChannelListener>();
118     int32_t ret = dataChannelImpl_->OpenSession(listener);
119     EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, ret);
120 }
121 
122 /**
123  * @tc.name: OpenSession_002
124  * @tc.desc: Verify the OpenSession function.
125  * @tc.type: FUNC
126  * @tc.require: Issue Number
127  */
128 HWTEST_F(ScreenDataChannelImplTest, OpenSession_002, TestSize.Level1)
129 {
130     int32_t ret = dataChannelImpl_->OpenSession(nullptr);
131     EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, ret);
132 }
133 
134 /**
135  * @tc.name: SendFullData_001
136  * @tc.desc: Verify the SendFullData function.
137  * @tc.type: FUNC
138  * @tc.require: Issue Number
139  */
140 HWTEST_F(ScreenDataChannelImplTest, SendFullData_001, TestSize.Level1)
141 {
142     std::shared_ptr<DataBuffer> screenData = nullptr;
143     int32_t ret = dataChannelImpl_->SendFullData(screenData);
144     EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, ret);
145 }
146 
147 /**
148  * @tc.name: SendFullData_002
149  * @tc.desc: Verify the SendFullData function.
150  * @tc.type: FUNC
151  * @tc.require: Issue Number
152  */
153 HWTEST_F(ScreenDataChannelImplTest, SendFullData_002, TestSize.Level1)
154 {
155     std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(10);
156     dataChannelImpl_->sessionId_ = 1;
157     int32_t ret = dataChannelImpl_->SendFullData(data);
158     EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, ret);
159 }
160 
161 /**
162  * @tc.name: SendDirtyData_001
163  * @tc.desc: Verify the SendDirtyData function.
164  * @tc.type: FUNC
165  * @tc.require: Issue Number
166  */
167 HWTEST_F(ScreenDataChannelImplTest, SendDirtyData_001, TestSize.Level1)
168 {
169     std::shared_ptr<DataBuffer> screenData = nullptr;
170     int32_t ret = dataChannelImpl_->SendDirtyData(screenData);
171     EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, ret);
172 }
173 
174 /**
175  * @tc.name: SendDirtyData_002
176  * @tc.desc: Verify the SendDirtyData function.
177  * @tc.type: FUNC
178  * @tc.require: Issue Number
179  */
180 HWTEST_F(ScreenDataChannelImplTest, SendDirtyData_002, TestSize.Level1)
181 {
182     std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(10);
183     dataChannelImpl_->jpegSessionId_ = 1;
184     int32_t ret = dataChannelImpl_->SendFullData(data);
185     EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, ret);
186 }
187 
188 /**
189  * @tc.name: release_session_test_001
190  * @tc.desc: Verify the ReleaseSession function.
191  * @tc.type: FUNC
192  * @tc.require: Issue Number
193  */
194 HWTEST_F(ScreenDataChannelImplTest, release_session_test_001, TestSize.Level1)
195 {
196     const char* perms[] = {
197         "ohos.permission.DISTRIBUTED_DATASYNC",
198         "ohos.permission.CAPTURE_SCREEN",
199     };
200     EnablePermissionAccess(perms, sizeof(perms) / sizeof(perms[0]), tokenId_);
201     EXPECT_EQ(DH_SUCCESS, dataChannelImpl_->ReleaseSession());
202     DisablePermissionAccess(tokenId_);
203 }
204 
205 /**
206  * @tc.name: close_session_test_001
207  * @tc.desc: Verify the CloseSession function.
208  * @tc.type: FUNC
209  * @tc.require: Issue Number
210  */
211 HWTEST_F(ScreenDataChannelImplTest, close_session_test_001, TestSize.Level1)
212 {
213     dataChannelImpl_->sessionId_ = 1;
214     EXPECT_EQ(DH_SUCCESS, dataChannelImpl_->CloseSession());
215 }
216 
217 /**
218  * @tc.name: close_session_test_002
219  * @tc.desc: Verify the CloseSession function.
220  * @tc.type: FUNC
221  * @tc.require: Issue Number
222  */
223 HWTEST_F(ScreenDataChannelImplTest, close_session_test_002, TestSize.Level1)
224 {
225     dataChannelImpl_->channelListener_ = std::make_shared<MockIScreenChannelListener>();
226 
227     StreamData ext = {0};
228     StreamFrameInfo *param = nullptr;
229 
230     int32_t sessionId = 0;
231     StreamData data;
232     data.buf = new char[DATA_LEN];
233     data.bufLen = DATA_LEN;
234 
235     dataChannelImpl_->OnStreamReceived(sessionId, &data, &ext, param);
236     delete[] data.buf;
237     dataChannelImpl_->sessionId_ = 0;
238     EXPECT_EQ(ERR_DH_SCREEN_TRANS_SESSION_NOT_OPEN, dataChannelImpl_->CloseSession());
239 }
240 
241 /**
242  * @tc.name: send_data_test_001
243  * @tc.desc: Verify the SendData function.
244  * @tc.type: FUNC
245  * @tc.require: Issue Number
246  */
247 HWTEST_F(ScreenDataChannelImplTest, send_data_test_001, TestSize.Level1)
248 {
249     std::shared_ptr<IScreenChannelListener> listener = nullptr;
250     dataChannelImpl_->channelListener_ = listener;
251     int32_t sessionId = 0;
252     PeerSocketInfo peerSocketInfo = {
253         .name = const_cast<char*>(PEER_SESSION_NAME.c_str()),
254         .networkId = const_cast<char*>(REMOTE_DEV_ID.c_str()),
255         .pkgName = const_cast<char*>(DSCREEN_PKG_NAME_TEST.c_str()),
256         .dataType = DATA_TYPE_BYTES
257     };
258     dataChannelImpl_->OnSessionOpened(sessionId, peerSocketInfo);
259     ShutdownReason reason = SHUTDOWN_REASON_UNKNOWN;
260     dataChannelImpl_->OnSessionClosed(sessionId, reason);
261     std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(10);
262     data->SetDataType(VIDEO_PART_SCREEN_DATA);
263     EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, dataChannelImpl_->SendData(data));
264 }
265 
266 /**
267  * @tc.name: send_data_test_002
268  * @tc.desc: Verify the SendData function.
269  * @tc.type: FUNC
270  * @tc.require: Issue Number
271  */
272 HWTEST_F(ScreenDataChannelImplTest, send_data_test_002, TestSize.Level1)
273 {
274     std::shared_ptr<IScreenChannelListener> listener = nullptr;
275     dataChannelImpl_->channelListener_ = listener;
276     std::shared_ptr<DataBuffer> data = nullptr;
277     EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, dataChannelImpl_->SendData(data));
278 }
279 
280 /**
281  * @tc.name: send_data_test_003
282  * @tc.desc: Verify the SendData function.
283  * @tc.type: FUNC
284  * @tc.require: Issue Number
285  */
286 HWTEST_F(ScreenDataChannelImplTest, send_data_test_003, TestSize.Level1)
287 {
288     std::shared_ptr<IScreenChannelListener> listener = nullptr;
289     dataChannelImpl_->channelListener_ = listener;
290     std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(10);
291     data->SetDataType(VIDEO_FULL_SCREEN_DATA);
292     EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, dataChannelImpl_->SendData(data));
293 }
294 
295 /**
296  * @tc.name: on_session_opened_test_001
297  * @tc.desc: Verify the OnSessionOpened function.
298  * @tc.type: FUNC
299  * @tc.require: Issue Number
300  */
301 HWTEST_F(ScreenDataChannelImplTest, on_session_opened_test_001, TestSize.Level1)
302 {
303     std::shared_ptr<IScreenChannelListener> listener = std::make_shared<MockIScreenChannelListener>();
304     dataChannelImpl_->channelListener_ = listener;
305     int32_t sessionId = 1;
306     PeerSocketInfo peerSocketInfo = {
307         .name = const_cast<char*>(PEER_SESSION_NAME.c_str()),
308         .networkId = const_cast<char*>(REMOTE_DEV_ID.c_str()),
309         .pkgName = const_cast<char*>(DSCREEN_PKG_NAME_TEST.c_str()),
310         .dataType = DATA_TYPE_BYTES
311     };
312     dataChannelImpl_->OnSessionOpened(sessionId, peerSocketInfo);
313     EXPECT_EQ(sessionId, dataChannelImpl_->sessionId_);
314 }
315 
316 /**
317  * @tc.name: on_session_opened_test_003
318  * @tc.desc: Verify the OnSessionOpened function.
319  * @tc.type: FUNC
320  * @tc.require: Issue Number
321  */
322 HWTEST_F(ScreenDataChannelImplTest, on_session_opened_test_003, TestSize.Level1)
323 {
324     std::shared_ptr<IScreenChannelListener>  mockListener = std::make_shared<MockIScreenChannelListener>();
325     dataChannelImpl_->channelListener_ = mockListener;
326     int32_t sessionId = 1;
327     dataChannelImpl_->jpegSessionFlag_ = true;
328     dataChannelImpl_->sessionId_ = 1;
329     dataChannelImpl_->jpegSessionId_ = 1;
330     dataChannelImpl_->jpegSessionOpened = false;
331     PeerSocketInfo peerSocketInfo = {
332         .name = const_cast<char*>(PEER_SESSION_NAME.c_str()),
333         .networkId = const_cast<char*>(REMOTE_DEV_ID.c_str()),
334         .pkgName = const_cast<char*>(DSCREEN_PKG_NAME_TEST.c_str()),
335         .dataType = DATA_TYPE_BYTES
336     };
337     dataChannelImpl_->OnSessionOpened(sessionId, peerSocketInfo);
338 
339     dataChannelImpl_->sessionId_ = 2;
340     dataChannelImpl_->dataSessionOpened = false;
341     dataChannelImpl_->OnSessionOpened(sessionId, peerSocketInfo);
342     EXPECT_NE(nullptr, dataChannelImpl_->channelListener_.lock());
343 }
344 
345 /**
346  * @tc.name: JsonToDirtyJson_001
347  * @tc.desc: Verify the JsonToDirtyJson function.
348  * @tc.type: FUNC
349  * @tc.require: Issue Number
350  */
351 HWTEST_F(ScreenDataChannelImplTest, JsonToDirtyJson_001, TestSize.Level1)
352 {
353     std::shared_ptr<DataBuffer> screenData = std::make_shared<DataBuffer>(10);
354     nlohmann::json rectJson;
355     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
356 
357     rectJson["dirtySize"] = 2;
358     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
359 
360     rectJson["dirtySize"] = INT32_MAX + 1;
361     rectJson["dataType"] = 2;
362     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
363 
364     rectJson["dirtySize"] = DIRTY_MAX_SIZE;
365     rectJson["dataType"] = 2;
366     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
367 
368     rectJson["dirtySize"] = 2;
369     rectJson["dataType"] = 2;
370     rectJson["3"] = 2;
371     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
372 
373     nlohmann::json testJson;
374     rectJson["0"] = testJson;
375     rectJson["0"][KEY_POINT_DIRTY_X] = INT32_MAX + 1;
376     rectJson["0"][KEY_POINT_DIRTY_Y] = INT32_MAX + 1;
377     rectJson["0"][KEY_POINT_DIRTY_W] = INT32_MAX + 1;
378     rectJson["0"][KEY_POINT_DIRTY_H] = INT32_MAX + 1;
379     rectJson["0"][KEY_POINT_DIRTY_SIZE] = INT32_MAX + 1;
380     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
381 
382     rectJson["0"][KEY_POINT_DIRTY_SIZE] = 2;
383     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
384 
385     rectJson["0"][KEY_POINT_DIRTY_H] = 2;
386     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
387 
388     rectJson["0"][KEY_POINT_DIRTY_W] = 2;
389     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
390 
391     rectJson["0"][KEY_POINT_DIRTY_Y] = 2;
392     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
393 
394     rectJson["0"][KEY_POINT_DIRTY_X] = 2;
395     dataChannelImpl_->JsonToDirtyJson(rectJson, screenData);
396     EXPECT_EQ(nullptr, dataChannelImpl_->channelListener_.lock());
397 }
398 
399 } // DistributedHardware
400 } // OHOS