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 18namespace OHOS { 19namespace DistributedHardware { 20constexpr const char* FIELD_CREDENTIAL_DATA = "credentialData"; 21void DmAnonymousTest::SetUp() 22{ 23} 24void DmAnonymousTest::TearDown() 25{ 26} 27void DmAnonymousTest::SetUpTestCase() 28{ 29} 30void DmAnonymousTest::TearDownTestCase() 31{ 32} 33 34namespace { 35/** 36 * @tc.name: GetAnonyString_001 37 * @tc.desc: Return size of anony string 38 * @tc.type: FUNC 39 * @tc.require: AR000GHSJK 40 */ 41HWTEST_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 */ 55HWTEST_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 */ 69HWTEST_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 */ 83HWTEST_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 */ 97HWTEST_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 */ 111HWTEST_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 */ 125HWTEST_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 */ 138HWTEST_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 */ 151HWTEST_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 */ 164HWTEST_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 */ 178HWTEST_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 */ 196HWTEST_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 */ 214HWTEST_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 */ 232HWTEST_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 */ 250HWTEST_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 */ 268HWTEST_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 */ 286HWTEST_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 */ 304HWTEST_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 */ 335HWTEST_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 */ 355HWTEST_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 */ 375HWTEST_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 */ 395HWTEST_F(DmAnonymousTest, ConvertCharArray2String_001, testing::ext::TestSize.Level0) 396{ 397 constexpr uint32_t MAX_MESSAGE_LEN = 40 * 1024 * 1024; 398 char *srcData = nullptr; 399 uint32_t srcLen = 0; 400 std::string ret = ConvertCharArray2String(srcData, srcLen); 401 EXPECT_EQ(ret, ""); 402 ret = ConvertCharArray2String(srcData, MAX_MESSAGE_LEN + 1); 403 EXPECT_EQ(ret, ""); 404 char srcData2[20] = {"1234"}; 405 ret = ConvertCharArray2String(srcData2, MAX_MESSAGE_LEN + 1); 406 EXPECT_EQ(ret, ""); 407 uint32_t srcLen2 = 20; 408 ret = ConvertCharArray2String(srcData2, srcLen); 409 EXPECT_EQ(ret, ""); 410 ret = ConvertCharArray2String(srcData, srcLen2); 411 EXPECT_EQ(ret, ""); 412 ret = ConvertCharArray2String(srcData2, srcLen2); 413 EXPECT_EQ(ret, "1234"); 414} 415 416HWTEST_F(DmAnonymousTest, StringToInt_001, testing::ext::TestSize.Level0) 417{ 418 std::string str = "12344"; 419 int32_t base = 10; 420 int32_t ret = StringToInt(str, base); 421 EXPECT_EQ(ret, 12344); 422} 423 424HWTEST_F(DmAnonymousTest, StringToInt_002, testing::ext::TestSize.Level0) 425{ 426 std::string str; 427 int32_t base = 10; 428 int32_t ret = StringToInt(str, base); 429 EXPECT_EQ(ret, 0); 430} 431 432HWTEST_F(DmAnonymousTest, StringToInt64_001, testing::ext::TestSize.Level0) 433{ 434 std::string str; 435 int32_t base = 10; 436 int64_t ret = StringToInt64(str, base); 437 EXPECT_EQ(ret, 0); 438} 439 440 441HWTEST_F(DmAnonymousTest, ParseMapFromJsonString_001, testing::ext::TestSize.Level0) 442{ 443 std::string jsonStr = R"( 444 { 445 "authType" : 1, 446 "userId" : "123", 447 "credentialData" : 448 [ 449 { 450 "CRYPTOSUPPORT" : "cryptosupportTest", 451 "credentialType" : 1, 452 "credentialId" : "104", 453 "authCode" : "1234567812345678123456781234567812345678123456781234567812345678", 454 "serverPk" : "hello", 455 "pkInfoSignature" : "world", 456 "pkInfo" : "pkginfo", 457 "peerDeviceId" : "3515656546" 458 } 459 ] 460 } 461 )"; 462 std::map<std::string, std::string> paramMap; 463 nlohmann::json jsonObj = nlohmann::json::parse(jsonStr, nullptr, false); 464 ParseMapFromJsonString(jsonStr, paramMap); 465 EXPECT_GE(paramMap.size(), 0); 466} 467 468HWTEST_F(DmAnonymousTest, ParseMapFromJsonString_002, testing::ext::TestSize.Level0) 469{ 470 std::string jsonStr = R"( 471 { 472 "authType" : 1, 473 "userId" : "123", 474 "credentialData" : 475 [ 476 { 477 "CRYPTOSUPPORT" : "cryptosupportTest", 478 "credentialType" : 1, 479 "credentialId" : "104", 480 "authCode" : "1234567812345678123456781234567812345678123456781234567812345678", 481 "serverPk" : "hello", 482 "pkInfoSignature" : "world", 483 "pkInfo" : "pkginfo", 484 "peerDeviceId" : "3515656546" 485 } 486 ] 487 )"; 488 std::map<std::string, std::string> paramMap; 489 nlohmann::json jsonObj = nlohmann::json::parse(jsonStr, nullptr, false); 490 ParseMapFromJsonString(jsonStr, paramMap); 491 EXPECT_EQ(paramMap.size(), 0); 492} 493 494HWTEST_F(DmAnonymousTest, ParseMapFromJsonString_003, testing::ext::TestSize.Level0) 495{ 496 std::string jsonStr = ""; 497 std::map<std::string, std::string> paramMap; 498 nlohmann::json jsonObj = nlohmann::json::parse(jsonStr, nullptr, false); 499 ParseMapFromJsonString(jsonStr, paramMap); 500 EXPECT_EQ(paramMap.size(), 0); 501} 502} // namespace 503} // namespace DistributedHardware 504} // namespace OHOS