• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "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.Level0)
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: ToJsonAndFromJson01
67  * @tc.type: FUNC
68  */
69 HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson01, testing::ext::TestSize.Level0)
70 {
71     CommMsg commMsg;
72     commMsg.code = 200;
73     commMsg.msg = "Success";
74 
75     cJSON *jsonObject = cJSON_CreateObject();
76     ToJson(jsonObject, commMsg);
77 
78     CommMsg newCommMsg;
79     FromJson(jsonObject, newCommMsg);
80 
81     EXPECT_EQ(newCommMsg.code, 200);
82     EXPECT_EQ(newCommMsg.msg, "Success");
83 
84     cJSON_Delete(jsonObject);
85 
86     cJSON *emptyCommJsonObject = cJSON_CreateObject();
87     CommMsg emptyCommMsg;
88     FromJson(emptyCommJsonObject, emptyCommMsg);
89 
90     EXPECT_EQ(emptyCommMsg.code, -1);
91     EXPECT_EQ(emptyCommMsg.msg, "");
92     cJSON_Delete(emptyCommJsonObject);
93 }
94 
95 /**
96  * @tc.name: PerformanceTest
97  * @tc.type: FUNC
98  */
99 HWTEST_F(DMTransportMsgTest, PerformanceTest, testing::ext::TestSize.Level0)
100 {
101     UserIdsMsg userIdsMsg;
102     for (int i = 0; i < 10000; ++i) {
103         userIdsMsg.foregroundUserIds.push_back(i);
104     }
105 
106     cJSON *jsonObject = cJSON_CreateObject();
107     auto start = std::chrono::high_resolution_clock::now();
108     ToJson(jsonObject, userIdsMsg);
109     auto end = std::chrono::high_resolution_clock::now();
110     std::chrono::duration<double> elapsed = end - start;
111 
112     EXPECT_LT(elapsed.count(), 1.0);
113     cJSON_Delete(jsonObject);
114 }
115 
116 /**
117  * @tc.name: GetCommMsgString_EmptyInput
118  * @tc.type: FUNC
119  */
120 HWTEST_F(DMTransportMsgTest, GetCommMsgString_EmptyInput, testing::ext::TestSize.Level0)
121 {
122     CommMsg commMsg;
123     std::string result = GetCommMsgString(commMsg);
124     EXPECT_FALSE(result.empty());
125 }
126 
127 /**
128  * @tc.name: ToJson_ValidInput
129  * @tc.type: FUNC
130  */
131 HWTEST_F(DMTransportMsgTest, ToJson_ValidInput, testing::ext::TestSize.Level0)
132 {
133     cJSON *jsonObject = cJSON_CreateObject();
134     NotifyUserIds notifyUserIds;
135     notifyUserIds.remoteUdid = "test_udid";
136     notifyUserIds.userIds = {1, 2, 3};
137 
138     ToJson(jsonObject, notifyUserIds);
139     cJSON *udidItem = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_UDIDKEY);
140     EXPECT_STREQ(udidItem->valuestring, "test_udid");
141     cJSON *userIdsArr = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY);
142     EXPECT_EQ(cJSON_GetArraySize(userIdsArr), 3);
143 }
144 
145 /**
146  * @tc.name: FromJson_InvalidJson
147  * @tc.type: FUNC
148  */
149 HWTEST_F(DMTransportMsgTest, FromJson_InvalidJson, testing::ext::TestSize.Level0)
150 {
151     const char *jsonString = "{\"udid\":123,\"userIds\":\"invalid\"}";
152     cJSON *jsonObject = cJSON_Parse(jsonString);
153     NotifyUserIds notifyUserIds;
154 
155     FromJson(jsonObject, notifyUserIds);
156     EXPECT_TRUE(notifyUserIds.remoteUdid.empty());
157     EXPECT_TRUE(notifyUserIds.userIds.empty());
158 
159     cJSON_Delete(jsonObject);
160 }
161 
162 /**
163  * @tc.name: NotifyUserIds_ToString_ValidInput
164  * @tc.type: FUNC
165  */
166 HWTEST_F(DMTransportMsgTest, NotifyUserIds_ToString_ValidInput, testing::ext::TestSize.Level0)
167 {
168     NotifyUserIds notifyUserIds;
169     notifyUserIds.remoteUdid = "test_udid";
170     notifyUserIds.userIds = {1, 2, 3};
171 
172     std::string result = notifyUserIds.ToString();
173     EXPECT_FALSE(result.empty());
174 }
175 
176 /**
177  * @tc.name: NotifyUserIds_ToString_EmptyInput
178  * @tc.type: FUNC
179  */
180 HWTEST_F(DMTransportMsgTest, NotifyUserIds_ToString_EmptyInput, testing::ext::TestSize.Level0)
181 {
182     NotifyUserIds notifyUserIds;
183     std::string result = notifyUserIds.ToString();
184     EXPECT_FALSE(result.empty());
185 }
186 
187 HWTEST_F(DMTransportMsgTest, ToJson_UserIdsMsg, testing::ext::TestSize.Level0)
188 {
189     std::vector<uint32_t> foregroundUserIds{1, 2, 3};
190     std::vector<uint32_t> backgroundUserIds{4, 5, 6};
191     UserIdsMsg userIdsMsg(foregroundUserIds, backgroundUserIds);
192     const char* jsonStr = R"({
193         "MsgType": "0",
194         "msg": "messgaeinfo",
195         "code": 145,
196         "userIds": [
197             {"type": 1, "userId": 111},
198             {"type": 0, "userId": 222}
199         ]
200     })";
201     cJSON *jsonObject = cJSON_Parse(jsonStr);
202     ToJson(jsonObject, userIdsMsg);
203     cJSON_Delete(jsonObject);
204     EXPECT_FALSE(userIdsMsg.foregroundUserIds.empty());
205 }
206 } // DistributedHardware
207 } // OHOS