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 = 96929744;
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: IsUint32_001
246 * @tc.desc: Return false
247 * @tc.type: FUNC
248 * @tc.require: AR000GHSJK
249 */
250 HWTEST_F(DmAnonymousTest, IsUint32_001, testing::ext::TestSize.Level0)
251 {
252 std::string str = R"(
253 {
254 "AUTHTYPE" : "authtypeTest"
255 }
256 )";
257 nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
258 bool ret = IsUint32(jsonObj, TAG_AUTH_TYPE);
259 EXPECT_EQ(ret, false);
260 }
261
262 /**
263 * @tc.name: IsInt64_001
264 * @tc.desc: Return true
265 * @tc.type: FUNC
266 * @tc.require: AR000GHSJK
267 */
268 HWTEST_F(DmAnonymousTest, IsInt64_001, testing::ext::TestSize.Level0)
269 {
270 std::string str = R"(
271 {
272 "REQUESTID" : 789
273 }
274 )";
275 nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
276 bool ret = IsInt64(jsonObj, TAG_REQUEST_ID);
277 EXPECT_EQ(ret, true);
278 }
279
280 /**
281 * @tc.name: IsInt64_002
282 * @tc.desc: Return false
283 * @tc.type: FUNC
284 * @tc.require: AR000GHSJK
285 */
286 HWTEST_F(DmAnonymousTest, IsInt64_002, testing::ext::TestSize.Level0)
287 {
288 std::string str = R"(
289 {
290 "REQUESTID" : "requestidTest"
291 }
292 )";
293 nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
294 bool ret = IsInt64(jsonObj, TAG_REQUEST_ID);
295 EXPECT_EQ(ret, false);
296 }
297
298 /**
299 * @tc.name: IsArray_001
300 * @tc.desc: Return true
301 * @tc.type: FUNC
302 * @tc.require: AR000GHSJK
303 */
304 HWTEST_F(DmAnonymousTest, IsArray_001, testing::ext::TestSize.Level0)
305 {
306 std::string str = R"(
307 {
308 "authType" : 1,
309 "userId" : "123",
310 "credentialData" :
311 [
312 {
313 "credentialType" : 1,
314 "credentialId" : "104",
315 "authCode" : "1234567812345678123456781234567812345678123456781234567812345678",
316 "serverPk" : "",
317 "pkInfoSignature" : "",
318 "pkInfo" : "",
319 "peerDeviceId" : ""
320 }
321 ]
322 }
323 )";
324 nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
325 bool ret = IsArray(jsonObj, FIELD_CREDENTIAL_DATA);
326 EXPECT_EQ(ret, true);
327 }
328
329 /**
330 * @tc.name: IsArray_002
331 * @tc.desc: Return false
332 * @tc.type: FUNC
333 * @tc.require: AR000GHSJK
334 */
335 HWTEST_F(DmAnonymousTest, IsArray_002, testing::ext::TestSize.Level0)
336 {
337 std::string str = R"(
338 {
339 "authType" : 1,
340 "userId" : "123",
341 "credentialData" : "credentialDataTest"
342 }
343 )";
344 nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
345 bool ret = IsArray(jsonObj, FIELD_CREDENTIAL_DATA);
346 EXPECT_EQ(ret, false);
347 }
348
349 /**
350 * @tc.name: IsBool_001
351 * @tc.desc: Return true
352 * @tc.type: FUNC
353 * @tc.require: AR000GHSJK
354 */
355 HWTEST_F(DmAnonymousTest, IsBool_001, testing::ext::TestSize.Level0)
356 {
357 std::string str = R"(
358 {
359 "CRYPTOSUPPORT" : false,
360 "userId" : "123",
361 "credentialData" : "credentialDataTest"
362 }
363 )";
364 nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
365 bool ret = IsBool(jsonObj, TAG_CRYPTO_SUPPORT);
366 EXPECT_EQ(ret, true);
367 }
368
369 /**
370 * @tc.name: IsBool_002
371 * @tc.desc: Return false
372 * @tc.type: FUNC
373 * @tc.require: AR000GHSJK
374 */
375 HWTEST_F(DmAnonymousTest, IsBool_002, testing::ext::TestSize.Level0)
376 {
377 std::string str = R"(
378 {
379 "CRYPTOSUPPORT" : "cryptosupportTest",
380 "userId" : "123",
381 "credentialData" : "credentialDataTest"
382 }
383 )";
384 nlohmann::json jsonObj = nlohmann::json::parse(str, nullptr, false);
385 bool ret = IsBool(jsonObj, TAG_CRYPTO_SUPPORT);
386 EXPECT_EQ(ret, false);
387 }
388
389 /**
390 * @tc.name: ConvertCharArray2String_001
391 * @tc.desc: Return false
392 * @tc.type: FUNC
393 * @tc.require: AR000GHSJK
394 */
395 HWTEST_F(DmAnonymousTest, ConvertCharArray2String_001, testing::ext::TestSize.Level0)
396 {
397 char *srcData = nullptr;
398 uint32_t srcLen = 0;
399 std::string ret = ConvertCharArray2String(srcData, srcLen);
400 EXPECT_EQ(ret, "");
401 srcLen = 40 * 1024 * 1024 * 2;
402 char *srcData1 = new char[srcLen]();
403 if (srcData1 == nullptr) {
404 return;
405 }
406 ret = ConvertCharArray2String(srcData1, srcLen);
407 EXPECT_EQ(ret, "");
408 delete[] srcData1;
409 }
410 } // namespace
411 } // namespace DistributedHardware
412 } // namespace OHOS