1 /* 2 * Copyright (c) 2021 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 #define LOG_TAG "SerializableTest" 16 #include <type_traits> 17 #include "log_print.h" 18 #include "serializable/serializable.h" 19 #include "gtest/gtest.h" 20 21 using namespace testing::ext; 22 using namespace OHOS::DistributedData; 23 namespace OHOS::Test { 24 class SerializableTest : public testing::Test { 25 public: 26 struct Normal final : public Serializable { 27 public: 28 std::string name = "Test"; 29 int32_t count = 0; 30 uint32_t status = 1; 31 int64_t value = 2; 32 bool isClear = false; 33 std::vector<std::string> cols{ "123", "345", "789" }; 34 std::vector<std::vector<int32_t>> colRow{ { 123, 345, 789 }, { 123, 345, 789 } }; 35 MarshalOHOS::Test::SerializableTest::Normal36 bool Marshal(json &node) const override 37 { 38 SetValue(node[GET_NAME(name)], name); 39 SetValue(node[GET_NAME(count)], count); 40 SetValue(node[GET_NAME(status)], status); 41 SetValue(node[GET_NAME(value)], value); 42 SetValue(node[GET_NAME(isClear)], isClear); 43 SetValue(node[GET_NAME(cols)], cols); 44 SetValue(node[GET_NAME(colRow)], colRow); 45 return true; 46 } UnmarshalOHOS::Test::SerializableTest::Normal47 bool Unmarshal(const json &node) override 48 { 49 GetValue(node, GET_NAME(name), name); 50 GetValue(node, GET_NAME(count), count); 51 GetValue(node, GET_NAME(status), status); 52 GetValue(node, GET_NAME(value), value); 53 GetValue(node, GET_NAME(isClear), isClear); 54 GetValue(node, GET_NAME(cols), cols); 55 GetValue(node, GET_NAME(colRow), colRow); 56 return true; 57 } operator ==OHOS::Test::SerializableTest::Normal58 bool operator == (const Normal &ref) const 59 { 60 return name == ref.name && count == ref.count && status == ref.status && value == ref.value 61 && isClear == ref.isClear && cols == ref.cols; 62 } 63 }; 64 65 struct NormalEx final : public Serializable { 66 public: 67 std::vector<Normal> normals {Normal(), Normal()}; 68 Normal normal; 69 int32_t count = 123; 70 std::string name = "wdt"; MarshalOHOS::Test::SerializableTest::NormalEx71 bool Marshal(json &node) const override 72 { 73 SetValue(node[GET_NAME(normals)], normals); 74 SetValue(node[GET_NAME(normal)], normal); 75 SetValue(node[GET_NAME(count)], count); 76 SetValue(node[GET_NAME(name)], name); 77 return true; 78 } UnmarshalOHOS::Test::SerializableTest::NormalEx79 bool Unmarshal(const json &node) override 80 { 81 GetValue(node, GET_NAME(normals), normals); 82 GetValue(node, GET_NAME(normal), normal); 83 GetValue(node, GET_NAME(count), count); 84 GetValue(node, GET_NAME(name), name); 85 return true; 86 } operator ==OHOS::Test::SerializableTest::NormalEx87 bool operator==(const NormalEx &normalEx) const 88 { 89 return normals == normalEx.normals && count == normalEx.count && name == normalEx.name; 90 } 91 }; SetUpTestCase(void)92 static void SetUpTestCase(void) 93 { 94 } TearDownTestCase(void)95 static void TearDownTestCase(void) 96 { 97 } SetUp()98 void SetUp() 99 { 100 Test::SetUp(); 101 } TearDown()102 void TearDown() 103 { 104 Test::TearDown(); 105 } 106 107 template<typename T> EqualPtr(const T * src,const T * target)108 static inline bool EqualPtr(const T *src, const T *target) 109 { 110 return (((src) == (target)) || ((src) != nullptr && (target) != nullptr && *(src) == *(target))); 111 } 112 }; 113 114 /** 115 * @tc.name: SerializableSuiteGetVal 116 * @tc.desc: Get Value. 117 * @tc.type: FUNC 118 * @tc.require: 119 * @tc.author: Sven Wang 120 */ 121 HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) 122 { 123 ZLOGI("SerializableSuite GetNormalVal begin."); 124 Normal normal; 125 normal.name = "normal"; 126 normal.count = -1; 127 normal.status = 12; 128 normal.value = -56; 129 normal.isClear = true; 130 normal.cols = {"adfasdfas"}; 131 auto json = normal.Marshall(); 132 auto jstr = to_string(normal.Marshall()); 133 Normal normal1; 134 normal1.Unmarshall(jstr); 135 ASSERT_TRUE(normal == normal1) << normal1.name; 136 ASSERT_FALSE(normal1.Unmarshall("")); 137 ASSERT_FALSE(normal1.Unmarshall("{")); 138 std::vector<std::string> testVec = {"adfasdfas", "banana"}; 139 ASSERT_FALSE(json["cols"] == testVec); 140 } 141 142 /** 143 * @tc.name: Delete Serializable 144 * @tc.desc: can delete child class, but not delete parent class point. 145 * @tc.type: FUNC 146 * @tc.require: 147 * @tc.author: Sven Wang 148 */ 149 HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2) 150 { 151 ZLOGI("SerializableSuite DeleteSerializable begin."); 152 ASSERT_FALSE(std::is_destructible<Serializable>::value); 153 ASSERT_TRUE(std::is_destructible<NormalEx>::value); 154 } 155 156 /** 157 * @tc.name: SerializableSuiteGetMutilVal 158 * @tc.desc: mutil value case. 159 * @tc.type: FUNC 160 * @tc.require: 161 * @tc.author: Sven Wang 162 */ 163 HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2) 164 { 165 ZLOGI("SerializableSuite GetMutilVal begin."); 166 NormalEx normalEx; 167 normalEx.normals = {Normal()}; 168 normalEx.name = "normalEx"; 169 auto jstr = to_string(normalEx.Marshall()); 170 NormalEx normal1; 171 normal1.Unmarshall(jstr); 172 ASSERT_TRUE(normalEx == normal1) << normal1.name; 173 } 174 175 /** 176 * @tc.name: GetMap 177 * @tc.desc: mutil value case. 178 * @tc.type: FUNC 179 * @tc.require: 180 * @tc.author: Sven Wang 181 */ 182 HWTEST_F(SerializableTest, GetMap, TestSize.Level2) 183 { 184 ZLOGI("SerializableSuite GetMapVals begin."); 185 std::map<std::string, NormalEx> marshData; 186 NormalEx normalEx; 187 normalEx.normals = { Normal() }; 188 normalEx.name = "normalEx"; 189 marshData.insert(std::pair{ "test1", normalEx }); 190 auto jsonData = NormalEx::Marshall(marshData); 191 192 std::map<std::string, NormalEx> unmarshData; 193 NormalEx::Unmarshall(jsonData, unmarshData); 194 ASSERT_TRUE((marshData["test1"] == unmarshData["test1"])) << jsonData; 195 } 196 197 /** 198 * @tc.name: GetMapInStruct 199 * @tc.desc: mutil value case. 200 * @tc.type: FUNC 201 * @tc.require: 202 * @tc.author: Sven Wang 203 */ 204 HWTEST_F(SerializableTest, GetMapInStruct, TestSize.Level2) 205 { 206 struct TestMeta : public Serializable { 207 std::map<std::string, NormalEx> data; 208 std::map<std::string, bool> *index = nullptr; 209 std::vector<std::map<std::string, NormalEx>> others; ~TestMetaOHOS::Test::TestMeta210 ~TestMeta() 211 { 212 delete index; 213 index = nullptr; 214 } MarshalOHOS::Test::TestMeta215 bool Marshal(json &node) const 216 { 217 SetValue(node[GET_NAME(data)], data); 218 SetValue(node[GET_NAME(index)], index); 219 SetValue(node[GET_NAME(others)], others); 220 return true; 221 } 222 UnmarshalOHOS::Test::TestMeta223 bool Unmarshal(const json &node) 224 { 225 GetValue(node, GET_NAME(data), data); 226 GetValue(node, GET_NAME(index), index); 227 GetValue(node, GET_NAME(others), others); 228 return true; 229 } 230 }; 231 ZLOGI("SerializableSuite GetMapVals begin."); 232 TestMeta marData; 233 NormalEx normalEx; 234 normalEx.normals = { Normal() }; 235 normalEx.name = "normalEx"; 236 marData.data.insert(std::pair{ "test1", normalEx }); 237 marData.others.push_back({ std::pair{ "test2", normalEx } }); 238 marData.index = new (std::nothrow) std::map<std::string, bool>; 239 ASSERT_NE(marData.index, nullptr); 240 marData.index->insert(std::pair{ "test1", true }); 241 marData.index->insert(std::pair{ "test2", true }); 242 auto jsonData = NormalEx::Marshall(marData); 243 TestMeta unmarData; 244 NormalEx::Unmarshall(jsonData, unmarData); 245 ASSERT_TRUE((marData.data == unmarData.data)) << jsonData; 246 ASSERT_TRUE((marData.others == unmarData.others)) << jsonData; 247 ASSERT_NE(unmarData.index, nullptr); 248 ASSERT_TRUE((*marData.index == *unmarData.index)) << jsonData; 249 } 250 251 /** 252 * @tc.name: GetTestValue 253 * @tc.desc: set value with point param. 254 * @tc.type: FUNC 255 * @tc.require: 256 * @tc.author: Sven Wang 257 */ 258 HWTEST_F(SerializableTest, SetPointerValue, TestSize.Level2) 259 { 260 struct Test final : public Serializable { 261 public: 262 int32_t *count = nullptr; 263 int64_t *value = nullptr; 264 uint32_t *status = nullptr; 265 bool *isClear = nullptr; ~TestOHOS::Test::Test266 ~Test() 267 { 268 delete count; 269 count = nullptr; 270 delete value; 271 value = nullptr; 272 delete status; 273 status = nullptr; 274 delete isClear; 275 isClear = nullptr; 276 } MarshalOHOS::Test::Test277 bool Marshal(json &node) const override 278 { 279 SetValue(node[GET_NAME(count)], count); 280 SetValue(node[GET_NAME(status)], status); 281 SetValue(node[GET_NAME(value)], value); 282 SetValue(node[GET_NAME(isClear)], isClear); 283 return true; 284 } UnmarshalOHOS::Test::Test285 bool Unmarshal(const json &node) override 286 { 287 GetValue(node, GET_NAME(count), count); 288 GetValue(node, GET_NAME(status), status); 289 GetValue(node, GET_NAME(value), value); 290 GetValue(node, GET_NAME(isClear), isClear); 291 return true; 292 } operator ==OHOS::Test::Test293 bool operator==(const Test &test) const 294 { 295 return (EqualPtr(count, test.count)) && (EqualPtr(status, test.status)) && 296 (EqualPtr(value, test.value)) && (EqualPtr(isClear, test.isClear)); 297 } 298 }; 299 Test in; 300 in.count = new int32_t(100); 301 in.value = new int64_t(-100); 302 in.status = new uint32_t(110); 303 in.isClear = new bool(true); 304 auto json = to_string(in.Marshall()); 305 Test out; 306 out.Unmarshall(json); 307 ASSERT_TRUE(in == out) << in.count; 308 } 309 310 /** 311 * @tc.name: IsJson 312 * @tc.desc: is json. 313 * @tc.type: FUNC 314 */ 315 HWTEST_F(SerializableTest, IsJson, TestSize.Level1) 316 { 317 std::string str = "test"; 318 std::string jsonStr = "\"test\""; 319 ASSERT_FALSE(Serializable::IsJson(str)); 320 ASSERT_TRUE(Serializable::IsJson(jsonStr)); 321 } 322 323 /** 324 * @tc.name: ToJson_01 325 * @tc.desc: to json. 326 * @tc.type: FUNC 327 */ 328 HWTEST_F(SerializableTest, ToJson_01, TestSize.Level1) 329 { 330 std::string jsonStr = "{\"key\":\"value\"}"; 331 Serializable::json result = Serializable::ToJson(jsonStr); 332 ASSERT_FALSE(result.is_discarded()); 333 } 334 335 /** 336 * @tc.name: ToJson_02 337 * @tc.desc: to json. 338 * @tc.type: FUNC 339 */ 340 HWTEST_F(SerializableTest, ToJson_02, TestSize.Level1) 341 { 342 std::string jsonStr = "invalid_json"; 343 Serializable::json result = Serializable::ToJson(jsonStr); 344 ASSERT_FALSE(result.is_discarded()); 345 } 346 347 /** 348 * @tc.name: ToJson_03 349 * @tc.desc: to json. 350 * @tc.type: FUNC 351 */ 352 HWTEST_F(SerializableTest, ToJson_03, TestSize.Level1) 353 { 354 std::string jsonStr = ""; 355 Serializable::json result = Serializable::ToJson(jsonStr); 356 ASSERT_TRUE(result.empty()); 357 } 358 359 /** 360 * @tc.name: ToJson_04 361 * @tc.desc: to json. 362 * @tc.type: FUNC 363 */ 364 HWTEST_F(SerializableTest, ToJson_04, TestSize.Level1) 365 { 366 std::string jsonStr = "{invalid_json}"; 367 Serializable::json result = Serializable::ToJson(jsonStr); 368 ASSERT_FALSE(result.is_discarded()); 369 } 370 371 /** 372 * @tc.name: ToJson_05 373 * @tc.desc: test string to json of value with numeric type. 374 * @tc.type: FUNC 375 */ 376 HWTEST_F(SerializableTest, ToJson_05, TestSize.Level1) 377 { 378 std::string jsonStr = "{\"key\": 10}"; 379 Serializable::json result = Serializable::ToJson(jsonStr); 380 uint64_t uint64Value; 381 bool ret = Serializable::GetValue(result, "key", uint64Value); 382 ASSERT_TRUE(ret); 383 384 std::string jsonStr2 = "{\"key\": 10.0}"; 385 Serializable::json result2 = Serializable::ToJson(jsonStr2); 386 double doubleValue; 387 ret = Serializable::GetValue(result2, "key", doubleValue); 388 ASSERT_TRUE(ret); 389 } 390 391 /** 392 * @tc.name: GetValueTest001 393 * @tc.desc: Test to json when type not match. 394 * @tc.type: FUNC 395 */ 396 HWTEST_F(SerializableTest, GetValueTest001, TestSize.Level1) 397 { 398 std::string jsonStr = "{\"key\": 10}"; 399 Serializable::json result = Serializable::ToJson(jsonStr); 400 401 std::string value; 402 bool ret = Serializable::GetValue(result, "key", value); 403 ASSERT_FALSE(ret); 404 ret = Serializable::GetValue(result, "notExist", value); 405 ASSERT_FALSE(ret); 406 407 std::string jsonStr2 = "{\"key\": \"str\"}"; 408 Serializable::json strResult = Serializable::ToJson(jsonStr2); 409 int32_t intValue; 410 ret = Serializable::GetValue(strResult, "key", intValue); 411 ASSERT_FALSE(ret); 412 ret = Serializable::GetValue(strResult, "notExist", intValue); 413 ASSERT_FALSE(ret); 414 415 uint32_t uintValue; 416 ret = Serializable::GetValue(strResult, "key", uintValue); 417 ASSERT_FALSE(ret); 418 ret = Serializable::GetValue(strResult, "notExist", uintValue); 419 ASSERT_FALSE(ret); 420 421 uint64_t uint64Value; 422 ret = Serializable::GetValue(strResult, "key", uint64Value); 423 ASSERT_FALSE(ret); 424 ret = Serializable::GetValue(strResult, "notExist", uint64Value); 425 ASSERT_FALSE(ret); 426 427 int64_t int64Value; 428 ret = Serializable::GetValue(strResult, "key", int64Value); 429 ASSERT_FALSE(ret); 430 ret = Serializable::GetValue(strResult, "notExist", int64Value); 431 ASSERT_FALSE(ret); 432 433 bool boolValue; 434 ret = Serializable::GetValue(strResult, "key", boolValue); 435 ASSERT_FALSE(ret); 436 ret = Serializable::GetValue(strResult, "notExist", boolValue); 437 ASSERT_FALSE(ret); 438 439 double doubleValue; 440 ret = Serializable::GetValue(strResult, "key", doubleValue); 441 ASSERT_FALSE(ret); 442 ret = Serializable::GetValue(strResult, "notExist", doubleValue); 443 ASSERT_FALSE(ret); 444 445 std::vector<uint8_t> arrayValue; 446 ret = Serializable::GetValue(strResult, "key", arrayValue); 447 ASSERT_FALSE(ret); 448 ret = Serializable::GetValue(strResult, "notExist", arrayValue); 449 ASSERT_FALSE(ret); 450 } 451 452 /** 453 * @tc.name: SetUintValue 454 * @tc.desc: set value with uint param. 455 * @tc.type: FUNC 456 */ 457 HWTEST_F(SerializableTest, SetUintValue, TestSize.Level2) 458 { 459 struct TestUint final : public Serializable { 460 public: 461 std::vector<uint8_t> testBytes = { 0x01, 0x02, 0x03, 0x04 }; MarshalOHOS::Test::TestUint462 bool Marshal(json &node) const override 463 { 464 SetValue(node[GET_NAME(testBytes)], testBytes); 465 return true; 466 } 467 UnmarshalOHOS::Test::TestUint468 bool Unmarshal(const json &node) override 469 { 470 bool success = true; 471 success = GetValue(node, GET_NAME(testBytes), testBytes) && success; 472 return success; 473 } 474 operator ==OHOS::Test::TestUint475 bool operator==(const TestUint &other) const 476 { 477 return testBytes == other.testBytes; 478 } 479 }; 480 481 std::string jsonStr2 = "{\"key\": \"str\"}"; 482 Serializable::json strResult = Serializable::ToJson(jsonStr2); 483 TestUint serialValue; 484 bool ret = Serializable::GetValue(strResult, "key", serialValue); 485 ASSERT_FALSE(ret); 486 ret = Serializable::GetValue(strResult, "notExist", serialValue); 487 ASSERT_FALSE(ret); 488 } 489 490 /** 491 * @tc.name: SetStringMapValue 492 * @tc.desc: set map value with string param. 493 * @tc.type: FUNC 494 */ 495 HWTEST_F(SerializableTest, SetStringMapValue, TestSize.Level2) 496 { 497 struct TestStringMap final : public Serializable { 498 public: 499 std::map<std::string, std::string> testMap = { 500 {"name", "John"}, 501 {"email", "john@example.com"} 502 }; MarshalOHOS::Test::TestStringMap503 bool Marshal(json &node) const override 504 { 505 SetValue(node[GET_NAME(testMap)], testMap); 506 return true; 507 } UnmarshalOHOS::Test::TestStringMap508 bool Unmarshal(const json &node) override 509 { 510 GetValue(node, GET_NAME(testMap), testMap); 511 return true; 512 } operator ==OHOS::Test::TestStringMap513 bool operator==(const TestStringMap &other) const 514 { 515 return testMap == other.testMap; 516 } 517 }; 518 519 TestStringMap in; 520 in.testMap["name"] = "New York"; 521 in.testMap["email"] = "john@sample.com"; 522 auto json = to_string(in.Marshall()); 523 TestStringMap out; 524 out.Unmarshall(json); 525 ASSERT_TRUE(in == out); 526 } 527 528 /** 529 * @tc.name: SetStringMapValue 530 * @tc.desc: set map value with int param. 531 * @tc.type: FUNC 532 */ 533 HWTEST_F(SerializableTest, SetMapValue, TestSize.Level2) 534 { 535 struct TestMap final : public Serializable { 536 public: 537 std::map<std::string, uint64_t> testMap = { 538 {"id", 123456}, 539 {"version", 42} 540 }; MarshalOHOS::Test::TestMap541 bool Marshal(json &node) const override 542 { 543 SetValue(node[GET_NAME(testMap)], testMap); 544 return true; 545 } UnmarshalOHOS::Test::TestMap546 bool Unmarshal(const json &node) override 547 { 548 GetValue(node, GET_NAME(testMap), testMap); 549 return true; 550 } operator ==OHOS::Test::TestMap551 bool operator==(const TestMap &other) const 552 { 553 return testMap == other.testMap; 554 } 555 }; 556 557 TestMap in; 558 in.testMap["version"] = 552; 559 auto json = to_string(in.Marshall()); 560 TestMap out; 561 out.Unmarshall(json); 562 ASSERT_TRUE(in == out); 563 } 564 565 /** 566 * @tc.name: BoundaryTest 567 * @tc.desc: test boundary. 568 * @tc.type: FUNC 569 */ 570 HWTEST_F(SerializableTest, BoundaryTest, TestSize.Level1) 571 { 572 struct TestBoundary : public Serializable { 573 int32_t int32Val; 574 uint32_t uint32Val; 575 int64_t int64Val; 576 uint64_t uint64Val; MarshalOHOS::Test::TestBoundary577 bool Marshal(json &node) const override 578 { 579 SetValue(node[GET_NAME(int32Val)], int32Val); 580 SetValue(node[GET_NAME(uint32Val)], uint32Val); 581 SetValue(node[GET_NAME(int64Val)], int64Val); 582 SetValue(node[GET_NAME(uint64Val)], uint64Val); 583 return true; 584 } UnmarshalOHOS::Test::TestBoundary585 bool Unmarshal(const json &node) override 586 { 587 bool success = true; 588 success = GetValue(node, GET_NAME(int32Val), int32Val) && success; 589 success = GetValue(node, GET_NAME(uint32Val), uint32Val) && success; 590 success = GetValue(node, GET_NAME(int64Val), int64Val) && success; 591 success = GetValue(node, GET_NAME(uint64Val), uint64Val) && success; 592 return success; 593 } 594 }; 595 TestBoundary in, out; 596 in.int32Val = INT32_MIN; 597 in.uint32Val = 0; 598 in.int64Val = INT64_MIN; 599 in.uint64Val = 0; 600 601 auto json = to_string(in.Marshall()); 602 out.Unmarshall(json); 603 EXPECT_EQ(out.int32Val, in.int32Val); 604 EXPECT_EQ(out.uint32Val, in.uint32Val); 605 EXPECT_EQ(out.int64Val, in.int64Val); 606 EXPECT_EQ(out.uint64Val, in.uint64Val); 607 608 in.int32Val = INT32_MAX; 609 in.uint32Val = UINT32_MAX; 610 in.int64Val = INT64_MAX; 611 in.uint64Val = UINT64_MAX; 612 613 json = to_string(in.Marshall()); 614 out.Unmarshall(json); 615 EXPECT_EQ(out.int32Val, in.int32Val); 616 EXPECT_EQ(out.uint32Val, in.uint32Val); 617 EXPECT_EQ(out.int64Val, in.int64Val); 618 EXPECT_EQ(out.uint64Val, in.uint64Val); 619 } 620 } // namespace OHOS::Test