• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "UTTest_dm_transport_msg.h"
17 
18 namespace OHOS {
19 namespace DistributedHardware {
20 const char* const DSOFTBUS_NOTIFY_USERIDS_UDIDKEY = "remoteUdid";
21 const char* const DSOFTBUS_NOTIFY_USERIDS_USERIDKEY = "foregroundUserIds";
SetUp()22 void DMTransportMsgTest::SetUp()
23 {
24 }
TearDown()25 void DMTransportMsgTest::TearDown()
26 {
27 }
SetUpTestCase()28 void DMTransportMsgTest::SetUpTestCase()
29 {
30 }
TearDownTestCase()31 void DMTransportMsgTest::TearDownTestCase()
32 {
33 }
34 
35 /**
36  * @tc.name: ToJsonAndFromJson
37  * @tc.type: FUNC
38  */
39 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson, testing::ext::TestSize.Level1)
40 {
41     UserIdsMsg userIdsMsg;
42     userIdsMsg.foregroundUserIds = {1, 2, 3};
43 
44     cJSON *jsonObject = cJSON_CreateObject();
45     ToJson(jsonObject, userIdsMsg);
46 
47     UserIdsMsg newUserIdsMsg;
48     FromJson(jsonObject, newUserIdsMsg);
49 
50     EXPECT_EQ(newUserIdsMsg.foregroundUserIds.size(), 3);
51     EXPECT_EQ(newUserIdsMsg.foregroundUserIds[0], 1);
52     EXPECT_EQ(newUserIdsMsg.foregroundUserIds[1], 2);
53     EXPECT_EQ(newUserIdsMsg.foregroundUserIds[2], 3);
54 
55     cJSON_Delete(jsonObject);
56 
57     cJSON *emptyJsonObject = cJSON_CreateObject();
58     UserIdsMsg emptyUserIdsMsg;
59     FromJson(emptyJsonObject, emptyUserIdsMsg);
60 
61     EXPECT_EQ(emptyUserIdsMsg.foregroundUserIds.size(), 0);
62     cJSON_Delete(emptyJsonObject);
63 }
64 
65 /**
66  * @tc.name: ToJsonAndFromJson_Invaild
67  * @tc.type: FUNC
68  */
69 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson_Invaild, testing::ext::TestSize.Level1)
70 {
71     UserIdsMsg userIdsMsg;
72     userIdsMsg.foregroundUserIds = {1, 2, 3};
73 
74     cJSON *jsonObject = nullptr;
75     ToJson(jsonObject, userIdsMsg);
76 
77     UserIdsMsg newUserIdsMsg;
78     FromJson(jsonObject, newUserIdsMsg);
79     EXPECT_EQ(jsonObject, nullptr);
80     EXPECT_TRUE(newUserIdsMsg.foregroundUserIds.empty());
81 }
82 
83 /**
84  * @tc.name: ToJsonAndFromJson01
85  * @tc.type: FUNC
86  */
87 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson01, testing::ext::TestSize.Level1)
88 {
89     CommMsg commMsg;
90     commMsg.code = 200;
91     commMsg.msg = "Success";
92 
93     cJSON *jsonObject = cJSON_CreateObject();
94     ToJson(jsonObject, commMsg);
95 
96     CommMsg newCommMsg;
97     FromJson(jsonObject, newCommMsg);
98 
99     EXPECT_EQ(newCommMsg.code, 200);
100     EXPECT_EQ(newCommMsg.msg, "Success");
101 
102     cJSON_Delete(jsonObject);
103 
104     cJSON *emptyCommJsonObject = cJSON_CreateObject();
105     CommMsg emptyCommMsg;
106     FromJson(emptyCommJsonObject, emptyCommMsg);
107 
108     EXPECT_EQ(emptyCommMsg.code, -1);
109     EXPECT_EQ(emptyCommMsg.msg, "");
110     cJSON_Delete(emptyCommJsonObject);
111 }
112 
113 /**
114  * @tc.name: ToJsonAndFromJson01_Invaild
115  * @tc.type: FUNC
116  */
117 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson01_Invaild, testing::ext::TestSize.Level1)
118 {
119     CommMsg commMsg;
120     commMsg.code = 200;
121     commMsg.msg = "Success";
122 
123     cJSON *jsonObject = nullptr;
124     ToJson(jsonObject, commMsg);
125 
126     CommMsg newCommMsg;
127     FromJson(jsonObject, newCommMsg);
128 
129     EXPECT_EQ(jsonObject, nullptr);
130     EXPECT_TRUE(newCommMsg.msg.empty());
131 }
132 
133 /**
134  * @tc.name: ToJsonAndFromJson02
135  * @tc.type: FUNC
136  */
137 
138 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson02, testing::ext::TestSize.Level1)
139 {
140     std::string remoteUdid = "test_udid";
141     std::vector<uint32_t> userIds = {10, 20, 30};
142     NotifyUserIds notifyUserIds(remoteUdid, userIds);
143     cJSON *jsonObject = cJSON_CreateObject();
144     ToJson(jsonObject, notifyUserIds);
145 
146     NotifyUserIds newNotifyUserIds;
147     FromJson(jsonObject, newNotifyUserIds);
148     EXPECT_EQ(newNotifyUserIds.remoteUdid, remoteUdid);
149     EXPECT_EQ(newNotifyUserIds.userIds.size(), 3);
150     EXPECT_EQ(newNotifyUserIds.userIds[0], 10);
151     EXPECT_EQ(newNotifyUserIds.userIds[1], 20);
152     EXPECT_EQ(newNotifyUserIds.userIds[2], 30);
153     cJSON_Delete(jsonObject);
154 
155     cJSON *emptyCommJsonObject = cJSON_CreateObject();
156     NotifyUserIds emptyNotifyUserIds;
157     FromJson(emptyCommJsonObject, emptyNotifyUserIds);
158     EXPECT_EQ(emptyNotifyUserIds.userIds.size(), 0);
159     EXPECT_EQ(emptyNotifyUserIds.remoteUdid, "");
160     cJSON_Delete(emptyCommJsonObject);
161 }
162 
163 /**
164  * @tc.name: ToJsonAndFromJson02_Invaild
165  * @tc.type: FUNC
166  */
167 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson02_Invaild, testing::ext::TestSize.Level1)
168 {
169     std::string remoteUdid = "test_udid";
170     std::vector<uint32_t> userIds = {10, 20, 30};
171     NotifyUserIds notifyUserIds(remoteUdid, userIds);
172     cJSON *jsonObject = nullptr;
173     ToJson(jsonObject, notifyUserIds);
174     NotifyUserIds newNotifyUserIds;
175     FromJson(jsonObject, newNotifyUserIds);
176     EXPECT_EQ(jsonObject, nullptr);
177     EXPECT_EQ(newNotifyUserIds.remoteUdid, "");
178     EXPECT_EQ(newNotifyUserIds.userIds.size(), 0);
179 }
180 
181 /**
182  * @tc.name: ToJsonAndFromJson03
183  * @tc.type: FUNC
184  */
185 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson03, testing::ext::TestSize.Level1)
186 {
187     LogoutAccountMsg logoutAccountMsg;
188     logoutAccountMsg.accountId = "test_account_id";
189     logoutAccountMsg.userId = 123;
190     cJSON *jsonObject = cJSON_CreateObject();
191     ToJson(jsonObject, logoutAccountMsg);
192     LogoutAccountMsg newLogoutAccountMsg;
193     FromJson(jsonObject, newLogoutAccountMsg);
194     EXPECT_EQ(newLogoutAccountMsg.accountId, "test_account_id");
195     EXPECT_EQ(newLogoutAccountMsg.userId, 123);
196     cJSON_Delete(jsonObject);
197 
198     cJSON *emptyJsonObject = cJSON_CreateObject();
199     LogoutAccountMsg emptyLogoutAccountMsg;
200     FromJson(emptyJsonObject, emptyLogoutAccountMsg);
201     EXPECT_EQ(emptyLogoutAccountMsg.userId, -1);
202 }
203 
204 /**
205  * @tc.name: ToJsonAndFromJson03_Invaild
206  * @tc.type: FUNC
207  */
208 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson03_Invaild, testing::ext::TestSize.Level1)
209 {
210     LogoutAccountMsg logoutAccountMsg;
211     logoutAccountMsg.accountId = "test_account_id";
212     logoutAccountMsg.userId = 123;
213     cJSON *jsonObject = nullptr;
214     ToJson(jsonObject, logoutAccountMsg);
215     LogoutAccountMsg newLogoutAccountMsg;
216     FromJson(jsonObject, newLogoutAccountMsg);
217     EXPECT_EQ(jsonObject, nullptr);
218     EXPECT_EQ(newLogoutAccountMsg.userId, -1);
219 }
220 
221 /**
222  * @tc.name: PerformanceTest
223  * @tc.type: FUNC
224  */
225 HWTEST_F(DMTransportMsgTest, PerformanceTest, testing::ext::TestSize.Level1)
226 {
227     UserIdsMsg userIdsMsg;
228     for (int i = 0; i < 10000; ++i) {
229         userIdsMsg.foregroundUserIds.push_back(i);
230     }
231 
232     cJSON *jsonObject = cJSON_CreateObject();
233     auto start = std::chrono::high_resolution_clock::now();
234     ToJson(jsonObject, userIdsMsg);
235     auto end = std::chrono::high_resolution_clock::now();
236     std::chrono::duration<double> elapsed = end - start;
237 
238     EXPECT_LT(elapsed.count(), 1.0);
239     cJSON_Delete(jsonObject);
240 }
241 
242 /**
243  * @tc.name: GetCommMsgString_EmptyInput
244  * @tc.type: FUNC
245  */
246 HWTEST_F(DMTransportMsgTest, GetCommMsgString_EmptyInput, testing::ext::TestSize.Level1)
247 {
248     CommMsg commMsg;
249     std::string result = GetCommMsgString(commMsg);
250     EXPECT_FALSE(result.empty());
251 }
252 
253 /**
254  * @tc.name: ToJson_ValidInput
255  * @tc.type: FUNC
256  */
257 HWTEST_F(DMTransportMsgTest, ToJson_ValidInput, testing::ext::TestSize.Level1)
258 {
259     cJSON *jsonObject = cJSON_CreateObject();
260     NotifyUserIds notifyUserIds;
261     notifyUserIds.remoteUdid = "test_udid";
262     notifyUserIds.userIds = {1, 2, 3};
263 
264     ToJson(jsonObject, notifyUserIds);
265     cJSON *udidItem = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_UDIDKEY);
266     EXPECT_STREQ(udidItem->valuestring, "test_udid");
267     cJSON *userIdsArr = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY);
268     EXPECT_EQ(cJSON_GetArraySize(userIdsArr), 3);
269 }
270 
271 /**
272  * @tc.name: FromJson_InvalidJson
273  * @tc.type: FUNC
274  */
275 HWTEST_F(DMTransportMsgTest, FromJson_InvalidJson, testing::ext::TestSize.Level1)
276 {
277     const char *jsonString = "{\"udid\":123,\"userIds\":\"invalid\"}";
278     cJSON *jsonObject = cJSON_Parse(jsonString);
279     NotifyUserIds notifyUserIds;
280 
281     FromJson(jsonObject, notifyUserIds);
282     EXPECT_TRUE(notifyUserIds.remoteUdid.empty());
283     EXPECT_TRUE(notifyUserIds.userIds.empty());
284 
285     cJSON_Delete(jsonObject);
286 }
287 
288 /**
289  * @tc.name: NotifyUserIds_ToString_ValidInput
290  * @tc.type: FUNC
291  */
292 HWTEST_F(DMTransportMsgTest, NotifyUserIds_ToString_ValidInput, testing::ext::TestSize.Level1)
293 {
294     NotifyUserIds notifyUserIds;
295     notifyUserIds.remoteUdid = "test_udid";
296     notifyUserIds.userIds = {1, 2, 3};
297 
298     std::string result = notifyUserIds.ToString();
299     EXPECT_FALSE(result.empty());
300 }
301 
302 /**
303  * @tc.name: NotifyUserIds_ToString_EmptyInput
304  * @tc.type: FUNC
305  */
306 HWTEST_F(DMTransportMsgTest, NotifyUserIds_ToString_EmptyInput, testing::ext::TestSize.Level1)
307 {
308     NotifyUserIds notifyUserIds;
309     std::string result = notifyUserIds.ToString();
310     EXPECT_FALSE(result.empty());
311 }
312 
313 HWTEST_F(DMTransportMsgTest, ToJson_UserIdsMsg, testing::ext::TestSize.Level1)
314 {
315     std::vector<uint32_t> foregroundUserIds{1, 2, 3};
316     std::vector<uint32_t> backgroundUserIds{4, 5, 6};
317     UserIdsMsg userIdsMsg(foregroundUserIds, backgroundUserIds, true);
318     const char* jsonStr = R"({
319         "MsgType": "0",
320         "msg": "messgaeinfo",
321         "code": 145,
322         "userIds": [
323             {"type": 1, "userId": 111},
324             {"type": 0, "userId": 222}
325         ]
326     })";
327     cJSON *jsonObject = cJSON_Parse(jsonStr);
328     ToJson(jsonObject, userIdsMsg);
329     cJSON_Delete(jsonObject);
330     EXPECT_FALSE(userIdsMsg.foregroundUserIds.empty());
331 }
332 
333 /**
334  * @tc.name: GetCommMsgString_01
335  * @tc.type: FUNC
336  */
337 HWTEST_F(DMTransportMsgTest, GetCommMsgString_01, testing::ext::TestSize.Level1)
338 {
339     const char* jsonstr = R"({"code":123,"msg":"messageinfo"})";
340     std::string jsonObj(jsonstr);
341     CommMsg commMsg(123, "messageinfo");
342     auto CommMsgString = GetCommMsgString(commMsg);
343     EXPECT_EQ(CommMsgString, jsonObj);
344 }
345 
346 /**
347  * @tc.name: ToString_01
348  * @tc.type: FUNC
349  */
350 HWTEST_F(DMTransportMsgTest, ToString_01, testing::ext::TestSize.Level1)
351 {
352     const char* jsonstr = R"({"remoteUdid":"test_udid","foregroundUserIds":[10,20,30]})";
353     std::string jsonObj(jsonstr);
354     std::string remoteUdid = "test_udid";
355     std::vector<uint32_t> userIds = {10, 20, 30};
356     NotifyUserIds notifyUserIds(remoteUdid, userIds);
357     auto notifyUserIdsString = notifyUserIds.ToString();
358     EXPECT_EQ(notifyUserIdsString, jsonObj);
359 }
360 
361 HWTEST_F(DMTransportMsgTest, UninstAppToJsonAndFromJson, testing::ext::TestSize.Level1)
362 {
363     UninstAppMsg uninstAppMsg(2, 3);
364 
365     cJSON *jsonObject = cJSON_CreateObject();
366     ToJson(jsonObject, uninstAppMsg);
367 
368     UninstAppMsg newUninstAppMsg;
369     FromJson(jsonObject, newUninstAppMsg);
370 
371     EXPECT_EQ(newUninstAppMsg.userId_, 2);
372     EXPECT_EQ(newUninstAppMsg.tokenId_, 3);
373 
374     cJSON_Delete(jsonObject);
375 
376     cJSON *nullJsonObject = nullptr;
377     ToJson(nullJsonObject, uninstAppMsg);
378 
379     UninstAppMsg emptyMsg;
380     FromJson(nullJsonObject, emptyMsg);
381 
382     EXPECT_EQ(emptyMsg.userId_, -1);
383     cJSON *emptyObject = cJSON_CreateObject();
384     FromJson(emptyObject, emptyMsg);
385     EXPECT_EQ(emptyMsg.userId_, -1);
386 
387     cJSON_Delete(nullJsonObject);
388     cJSON_Delete(emptyObject);
389 }
390 
391 HWTEST_F(DMTransportMsgTest, UnbindAppToJsonAndFromJson, testing::ext::TestSize.Level1)
392 {
393     UnBindAppMsg unBindAppMsg(2, 3, "", "test_udid");
394 
395     cJSON *jsonObject = cJSON_CreateObject();
396     ToJson(jsonObject, unBindAppMsg);
397 
398     UnBindAppMsg newUnBindAppMsg;
399     FromJson(jsonObject, newUnBindAppMsg);
400 
401     EXPECT_EQ(newUnBindAppMsg.userId_, 2);
402     EXPECT_EQ(newUnBindAppMsg.tokenId_, 3);
403     EXPECT_EQ(newUnBindAppMsg.extra_, "");
404     EXPECT_EQ(newUnBindAppMsg.udid_, "test_udid");
405 
406     cJSON_Delete(jsonObject);
407 
408     cJSON *nullJsonObject = nullptr;
409     ToJson(nullJsonObject, unBindAppMsg);
410 
411     UnBindAppMsg emptyMsg;
412     FromJson(nullJsonObject, emptyMsg);
413 
414     EXPECT_EQ(emptyMsg.userId_, -1);
415     cJSON *emptyObject = cJSON_CreateObject();
416     FromJson(emptyObject, emptyMsg);
417     EXPECT_EQ(emptyMsg.userId_, -1);
418 
419     cJSON_Delete(nullJsonObject);
420     cJSON_Delete(emptyObject);
421 }
422 } // DistributedHardware
423 } // OHOS