• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "UTTest_dm_anonymous.h"
17 
18 namespace OHOS {
19 namespace DistributedHardware {
20 constexpr const char* FIELD_CREDENTIAL_DATA = "credentialData";
SetUp()21 void DmAnonymousTest::SetUp()
22 {
23 }
TearDown()24 void DmAnonymousTest::TearDown()
25 {
26 }
SetUpTestCase()27 void DmAnonymousTest::SetUpTestCase()
28 {
29 }
TearDownTestCase()30 void DmAnonymousTest::TearDownTestCase()
31 {
32 }
33 
34 namespace {
35 /**
36  * @tc.name: GetAnonyString_001
37  * @tc.desc: Return size of anony string
38  * @tc.type: FUNC
39  * @tc.require: AR000GHSJK
40  */
41 HWTEST_F(DmAnonymousTest, GetAnonyString_001, testing::ext::TestSize.Level0)
42 {
43     const std::string value = "valueTest";
44     std::string str = GetAnonyString(value);
45     int32_t ret = str.size();
46     EXPECT_EQ(ret, 8);
47 }
48 
49 /**
50  * @tc.name: GetAnonyString_002
51  * @tc.desc: Return size of anony string
52  * @tc.type: FUNC
53  * @tc.require: AR000GHSJK
54  */
55 HWTEST_F(DmAnonymousTest, GetAnonyString_002, testing::ext::TestSize.Level0)
56 {
57     const std::string value = "va";
58     std::string str = GetAnonyString(value);
59     int32_t ret = str.size();
60     EXPECT_EQ(ret, 6);
61 }
62 
63 /**
64  * @tc.name: GetAnonyString_003
65  * @tc.desc: Return size of anony string
66  * @tc.type: FUNC
67  * @tc.require: AR000GHSJK
68  */
69 HWTEST_F(DmAnonymousTest, GetAnonyString_003, testing::ext::TestSize.Level0)
70 {
71     const std::string value = "ohos.distributedhardware.devicemanager.resident";
72     std::string str = GetAnonyString(value);
73     int32_t ret = str.size();
74     EXPECT_EQ(ret, 14);
75 }
76 
77 /**
78  * @tc.name: GetAnonyInt32_001
79  * @tc.desc: Return size of anony string
80  * @tc.type: FUNC
81  * @tc.require: AR000GHSJK
82  */
83 HWTEST_F(DmAnonymousTest, GetAnonyInt32_001, testing::ext::TestSize.Level0)
84 {
85     const int32_t value = 1;
86     std::string str = GetAnonyInt32(value);
87     int32_t ret = str.size();
88     EXPECT_EQ(ret, 1);
89 }
90 
91 /**
92  * @tc.name: GetAnonyInt32_002
93  * @tc.desc: Return size of anony string
94  * @tc.type: FUNC
95  * @tc.require: AR000GHSJK
96  */
97 HWTEST_F(DmAnonymousTest, GetAnonyInt32_002, testing::ext::TestSize.Level0)
98 {
99     const int32_t value = 12;
100     std::string str = GetAnonyInt32(value);
101     int32_t ret = str.size();
102     EXPECT_EQ(ret, 2);
103 }
104 
105 /**
106  * @tc.name: GetAnonyInt32_003
107  * @tc.desc: Return size of anony string
108  * @tc.type: FUNC
109  * @tc.require: AR000GHSJK
110  */
111 HWTEST_F(DmAnonymousTest, GetAnonyInt32_003, testing::ext::TestSize.Level0)
112 {
113     const int32_t value = 123456;
114     std::string str = GetAnonyInt32(value);
115     int32_t ret = str.size();
116     EXPECT_EQ(ret, 6);
117 }
118 
119 /**
120  * @tc.name: IsNumberString_001
121  * @tc.desc: Return false if the string is not a number
122  * @tc.type: FUNC
123  * @tc.require: AR000GHSJK
124  */
125 HWTEST_F(DmAnonymousTest, IsNumberString_001, testing::ext::TestSize.Level0)
126 {
127     const std::string inputString = "";
128     bool ret = IsNumberString(inputString);
129     EXPECT_EQ(ret, false);
130 }
131 
132 /**
133  * @tc.name: IsNumberString_002
134  * @tc.desc: Return false if the string is not a number
135  * @tc.type: FUNC
136  * @tc.require: AR000GHSJK
137  */
138 HWTEST_F(DmAnonymousTest, IsNumberString_002, testing::ext::TestSize.Level0)
139 {
140     const std::string inputString = "123inputstring";
141     bool ret = IsNumberString(inputString);
142     EXPECT_EQ(ret, false);
143 }
144 
145 /**
146  * @tc.name: IsNumberString_003
147  * @tc.desc: Return true if the string is a number
148  * @tc.type: FUNC
149  * @tc.require: AR000GHSJK
150  */
151 HWTEST_F(DmAnonymousTest, IsNumberString_003, testing::ext::TestSize.Level0)
152 {
153     const std::string inputString = "25633981";
154     bool ret = IsNumberString(inputString);
155     EXPECT_EQ(ret, true);
156 }
157 
158 /**
159  * @tc.name: GetErrorString_001
160  * @tc.desc: Return true if the string is a number
161  * @tc.type: FUNC
162  * @tc.require: AR000GHSJK
163  */
164 HWTEST_F(DmAnonymousTest, GetErrorString_001, testing::ext::TestSize.Level0)
165 {
166     int failedReason = -20000;
167     std::string errorMessage = "dm process execution failed.";
168     std::string ret = GetErrorString(failedReason);
169     EXPECT_EQ(ret, errorMessage);
170 }
171 
172 /**
173  * @tc.name: IsString_001
174  * @tc.desc: Return true
175  * @tc.type: FUNC
176  * @tc.require: AR000GHSJK
177  */
178 HWTEST_F(DmAnonymousTest, IsString_001, testing::ext::TestSize.Level0)
179 {
180     std::string str = R"(
181     {
182         "pinToken" : "IsString"
183     }
184     )";
185     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
186     bool ret = IsString(jsonObj, PIN_TOKEN);
187     EXPECT_EQ(ret, true);
188 }
189 
190 /**
191  * @tc.name: IsString_002
192  * @tc.desc: Return false
193  * @tc.type: FUNC
194  * @tc.require: AR000GHSJK
195  */
196 HWTEST_F(DmAnonymousTest, IsString_002, testing::ext::TestSize.Level0)
197 {
198     std::string str = R"(
199     {
200         "pinToken" : 123
201     }
202     )";
203     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
204     bool ret = IsString(jsonObj, PIN_TOKEN);
205     EXPECT_EQ(ret, false);
206 }
207 
208 /**
209  * @tc.name: IsInt32_001
210  * @tc.desc: Return true
211  * @tc.type: FUNC
212  * @tc.require: AR000GHSJK
213  */
214 HWTEST_F(DmAnonymousTest, IsInt32_001, testing::ext::TestSize.Level0)
215 {
216     std::string str = R"(
217     {
218         "AUTHTYPE" : 369
219     }
220     )";
221     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
222     bool ret = IsInt32(jsonObj, TAG_AUTH_TYPE);
223     EXPECT_EQ(ret, true);
224 }
225 
226 /**
227  * @tc.name: IsInt32_002
228  * @tc.desc: Return false
229  * @tc.type: FUNC
230  * @tc.require: AR000GHSJK
231  */
232 HWTEST_F(DmAnonymousTest, IsInt32_002, testing::ext::TestSize.Level0)
233 {
234     std::string str = R"(
235     {
236         "AUTHTYPE" : "authtypeTest"
237     }
238     )";
239     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
240     bool ret = IsInt32(jsonObj, TAG_AUTH_TYPE);
241     EXPECT_EQ(ret, false);
242 }
243 
244 /**
245  * @tc.name: IsInt64_001
246  * @tc.desc: Return true
247  * @tc.type: FUNC
248  * @tc.require: AR000GHSJK
249  */
250 HWTEST_F(DmAnonymousTest, IsInt64_001, testing::ext::TestSize.Level0)
251 {
252     std::string str = R"(
253     {
254         "REQUESTID" : 789
255     }
256     )";
257     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
258     bool ret = IsInt64(jsonObj, TAG_REQUEST_ID);
259     EXPECT_EQ(ret, true);
260 }
261 
262 /**
263  * @tc.name: IsInt64_002
264  * @tc.desc: Return false
265  * @tc.type: FUNC
266  * @tc.require: AR000GHSJK
267  */
268 HWTEST_F(DmAnonymousTest, IsInt64_002, testing::ext::TestSize.Level0)
269 {
270     std::string str = R"(
271     {
272         "REQUESTID" : "requestidTest"
273     }
274     )";
275     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
276     bool ret = IsInt64(jsonObj, TAG_REQUEST_ID);
277     EXPECT_EQ(ret, false);
278 }
279 
280 /**
281  * @tc.name: IsArray_001
282  * @tc.desc: Return true
283  * @tc.type: FUNC
284  * @tc.require: AR000GHSJK
285  */
286 HWTEST_F(DmAnonymousTest, IsArray_001, testing::ext::TestSize.Level0)
287 {
288     std::string str = R"(
289     {
290         "authType" : 1,
291         "userId" : "123",
292         "credentialData" :
293         [
294             {
295                 "credentialType" : 1,
296                 "credentialId" : "104",
297                 "authCode" : "1234567812345678123456781234567812345678123456781234567812345678",
298                 "serverPk" : "",
299                 "pkInfoSignature" : "",
300                 "pkInfo" : "",
301                 "peerDeviceId" : ""
302             }
303         ]
304     }
305     )";
306     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
307     bool ret = IsArray(jsonObj, FIELD_CREDENTIAL_DATA);
308     EXPECT_EQ(ret, true);
309 }
310 
311 /**
312  * @tc.name: IsArray_002
313  * @tc.desc: Return false
314  * @tc.type: FUNC
315  * @tc.require: AR000GHSJK
316  */
317 HWTEST_F(DmAnonymousTest, IsArray_002, testing::ext::TestSize.Level0)
318 {
319     std::string str = R"(
320     {
321         "authType" : 1,
322         "userId" : "123",
323         "credentialData" : "credentialDataTest"
324     }
325     )";
326     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
327     bool ret = IsArray(jsonObj, FIELD_CREDENTIAL_DATA);
328     EXPECT_EQ(ret, false);
329 }
330 
331 /**
332  * @tc.name: IsBool_001
333  * @tc.desc: Return true
334  * @tc.type: FUNC
335  * @tc.require: AR000GHSJK
336  */
337 HWTEST_F(DmAnonymousTest, IsBool_001, testing::ext::TestSize.Level0)
338 {
339     std::string str = R"(
340     {
341         "CRYPTOSUPPORT" : false,
342         "userId" : "123",
343         "credentialData" : "credentialDataTest"
344     }
345     )";
346     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
347     bool ret = IsBool(jsonObj, TAG_CRYPTO_SUPPORT);
348     EXPECT_EQ(ret, true);
349 }
350 
351 /**
352  * @tc.name: IsBool_002
353  * @tc.desc: Return false
354  * @tc.type: FUNC
355  * @tc.require: AR000GHSJK
356  */
357 HWTEST_F(DmAnonymousTest, IsBool_002, testing::ext::TestSize.Level0)
358 {
359     std::string str = R"(
360     {
361         "CRYPTOSUPPORT" : "cryptosupportTest",
362         "userId" : "123",
363         "credentialData" : "credentialDataTest"
364     }
365     )";
366     nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
367     bool ret = IsBool(jsonObj, TAG_CRYPTO_SUPPORT);
368     EXPECT_EQ(ret, false);
369 }
370 } // namespace
371 } // namespace DistributedHardware
372 } // namespace OHOS