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 #define LOG_TAG "SerializableTest" 16 #include "serializable.h" 17 18 #include <type_traits> 19 20 #include "gtest/gtest.h" 21 #include "logger.h" 22 23 using namespace testing::ext; 24 namespace OHOS::Test { 25 class SerializableTest : public testing::Test { 26 public: 27 struct Normal final : public Serializable { 28 public: 29 std::string name = "Test"; 30 int32_t count = 0; 31 uint32_t status = 1; 32 int64_t value = 2; 33 bool isClear = false; 34 std::vector<std::string> cols{ "123", "345", "789" }; 35 std::vector<std::vector<int32_t>> colRow{ { 123, 345, 789 }, { 123, 345, 789 } }; 36 MarshalOHOS::Test::SerializableTest::Normal37 bool Marshal(json &node) const override 38 { 39 SetValue(node[GET_NAME(name)], name); 40 SetValue(node[GET_NAME(count)], count); 41 SetValue(node[GET_NAME(status)], status); 42 SetValue(node[GET_NAME(value)], value); 43 SetValue(node[GET_NAME(isClear)], isClear); 44 SetValue(node[GET_NAME(cols)], cols); 45 SetValue(node[GET_NAME(colRow)], colRow); 46 return true; 47 } UnmarshalOHOS::Test::SerializableTest::Normal48 bool Unmarshal(const json &node) override 49 { 50 GetValue(node, GET_NAME(name), name); 51 GetValue(node, GET_NAME(count), count); 52 GetValue(node, GET_NAME(status), status); 53 GetValue(node, GET_NAME(value), value); 54 GetValue(node, GET_NAME(isClear), isClear); 55 GetValue(node, GET_NAME(cols), cols); 56 GetValue(node, GET_NAME(colRow), colRow); 57 return true; 58 } operator ==OHOS::Test::SerializableTest::Normal59 bool operator==(const Normal &ref) const 60 { 61 return name == ref.name && count == ref.count && status == ref.status && value == ref.value && 62 isClear == ref.isClear && cols == ref.cols; 63 } 64 }; 65 66 struct NormalEx final : public Serializable { 67 public: 68 std::vector<Normal> normals{ Normal(), Normal() }; 69 Normal normal; 70 int32_t count = 123; 71 std::string name = "wdt"; MarshalOHOS::Test::SerializableTest::NormalEx72 bool Marshal(json &node) const override 73 { 74 SetValue(node[GET_NAME(normals)], normals); 75 SetValue(node[GET_NAME(normal)], normal); 76 SetValue(node[GET_NAME(count)], count); 77 SetValue(node[GET_NAME(name)], name); 78 return true; 79 } UnmarshalOHOS::Test::SerializableTest::NormalEx80 bool Unmarshal(const json &node) override 81 { 82 GetValue(node, GET_NAME(normals), normals); 83 GetValue(node, GET_NAME(normal), normal); 84 GetValue(node, GET_NAME(count), count); 85 GetValue(node, GET_NAME(name), name); 86 return true; 87 } operator ==OHOS::Test::SerializableTest::NormalEx88 bool operator==(const NormalEx &normalEx) const 89 { 90 return normals == normalEx.normals && count == normalEx.count && name == normalEx.name; 91 } 92 }; SetUpTestCase(void)93 static void SetUpTestCase(void) 94 { 95 } TearDownTestCase(void)96 static void TearDownTestCase(void) 97 { 98 } SetUp()99 void SetUp() 100 { 101 Test::SetUp(); 102 } TearDown()103 void TearDown() 104 { 105 Test::TearDown(); 106 } 107 108 template<typename T> EqualPtr(const T * src,const T * target)109 static inline bool EqualPtr(const T *src, const T *target) 110 { 111 return (((src) == (target)) || ((src) != nullptr && (target) != nullptr && *(src) == *(target))); 112 } 113 }; 114 115 /** 116 * @tc.name: SerializableSuiteGetVal 117 * @tc.desc: Get Value. 118 * @tc.type: FUNC 119 * @tc.require: 120 */ 121 HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) 122 { 123 Normal normal; 124 normal.name = "normal"; 125 normal.count = -1; 126 normal.status = 12; 127 normal.value = -56; 128 normal.isClear = true; 129 normal.cols = { "adfasdfas" }; 130 auto jstr = to_string(normal.Marshall()); 131 Normal normal1; 132 normal1.Unmarshall(jstr); 133 ASSERT_TRUE(normal == normal1) << normal1.name; 134 } 135 136 /** 137 * @tc.name: Delete Serializable 138 * @tc.desc: can delete child class, but not delete parent class point. 139 * @tc.type: FUNC 140 * @tc.require: 141 */ 142 HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2) 143 { 144 ASSERT_FALSE(std::is_destructible<Serializable>::value); 145 ASSERT_TRUE(std::is_destructible<NormalEx>::value); 146 } 147 148 /** 149 * @tc.name: SerializableSuiteGetMutilVal 150 * @tc.desc: mutil value case. 151 * @tc.type: FUNC 152 * @tc.require: 153 */ 154 HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2) 155 { 156 NormalEx normalEx; 157 normalEx.normals = { Normal() }; 158 normalEx.name = "normalEx"; 159 auto jstr = to_string(normalEx.Marshall()); 160 NormalEx normal1; 161 normal1.Unmarshall(jstr); 162 ASSERT_TRUE(normalEx == normal1) << normal1.name; 163 } 164 165 /** 166 * @tc.name: IsJson 167 * @tc.desc: is json. 168 * @tc.type: FUNC 169 */ 170 HWTEST_F(SerializableTest, IsJson, TestSize.Level1) 171 { 172 std::string str = "test"; 173 std::string jsonStr = "\"test\""; 174 ASSERT_FALSE(Serializable::IsJson(str)); 175 ASSERT_TRUE(Serializable::IsJson(jsonStr)); 176 } 177 178 /** 179 * @tc.name: ToJson_01 180 * @tc.desc: to json. 181 * @tc.type: FUNC 182 */ 183 HWTEST_F(SerializableTest, ToJson_01, TestSize.Level1) 184 { 185 std::string jsonStr = "{\"key\":\"value\"}"; 186 Serializable::json result = Serializable::ToJson(jsonStr); 187 ASSERT_FALSE(result.is_discarded()); 188 } 189 190 /** 191 * @tc.name: ToJson_02 192 * @tc.desc: to json. 193 * @tc.type: FUNC 194 */ 195 HWTEST_F(SerializableTest, ToJson_02, TestSize.Level1) 196 { 197 std::string jsonStr = "invalid_json"; 198 Serializable::json result = Serializable::ToJson(jsonStr); 199 ASSERT_FALSE(result.is_discarded()); 200 } 201 202 /** 203 * @tc.name: ToJson_03 204 * @tc.desc: to json. 205 * @tc.type: FUNC 206 */ 207 HWTEST_F(SerializableTest, ToJson_03, TestSize.Level1) 208 { 209 std::string jsonStr = ""; 210 Serializable::json result = Serializable::ToJson(jsonStr); 211 ASSERT_TRUE(result.empty()); 212 } 213 214 /** 215 * @tc.name: ToJson_04 216 * @tc.desc: to json. 217 * @tc.type: FUNC 218 */ 219 HWTEST_F(SerializableTest, ToJson_04, TestSize.Level1) 220 { 221 std::string jsonStr = "{invalid_json}"; 222 Serializable::json result = Serializable::ToJson(jsonStr); 223 ASSERT_FALSE(result.is_discarded()); 224 } 225 226 /** 227 * @tc.name: ToJson_05 228 * @tc.desc: test string to json of value with numeric type. 229 * @tc.type: FUNC 230 */ 231 HWTEST_F(SerializableTest, ToJson_05, TestSize.Level1) 232 { 233 std::string jsonStr = "{\"key\": 10}"; 234 Serializable::json result = Serializable::ToJson(jsonStr); 235 uint64_t uint64Value; 236 bool ret = Serializable::GetValue(result, "key", uint64Value); 237 ASSERT_TRUE(ret); 238 239 std::string jsonStr2 = "{\"key\": 10.0}"; 240 Serializable::json result2 = Serializable::ToJson(jsonStr2); 241 double doubleValue; 242 ret = Serializable::GetValue(result2, "key", doubleValue); 243 ASSERT_TRUE(ret); 244 } 245 246 /** 247 * @tc.name: GetValueTest001 248 * @tc.desc: Test to json when type not match. 249 * @tc.type: FUNC 250 */ 251 HWTEST_F(SerializableTest, GetValueTest001, TestSize.Level1) 252 { 253 std::string jsonStr = "{\"key\": 10}"; 254 Serializable::json result = Serializable::ToJson(jsonStr); 255 256 std::string value; 257 bool ret = Serializable::GetValue(result, "key", value); 258 ASSERT_FALSE(ret); 259 ret = Serializable::GetValue(result, "notExist", value); 260 ASSERT_FALSE(ret); 261 262 std::string jsonStr2 = "{\"key\": \"str\"}"; 263 Serializable::json strResult = Serializable::ToJson(jsonStr2); 264 int32_t intValue; 265 ret = Serializable::GetValue(strResult, "key", intValue); 266 ASSERT_FALSE(ret); 267 ret = Serializable::GetValue(strResult, "notExist", intValue); 268 ASSERT_FALSE(ret); 269 270 uint32_t uintValue; 271 ret = Serializable::GetValue(strResult, "key", uintValue); 272 ASSERT_FALSE(ret); 273 ret = Serializable::GetValue(strResult, "notExist", uintValue); 274 ASSERT_FALSE(ret); 275 276 uint64_t uint64Value; 277 ret = Serializable::GetValue(strResult, "key", uint64Value); 278 ASSERT_FALSE(ret); 279 ret = Serializable::GetValue(strResult, "notExist", uint64Value); 280 ASSERT_FALSE(ret); 281 282 int64_t int64Value; 283 ret = Serializable::GetValue(strResult, "key", int64Value); 284 ASSERT_FALSE(ret); 285 ret = Serializable::GetValue(strResult, "notExist", int64Value); 286 ASSERT_FALSE(ret); 287 288 bool boolValue; 289 ret = Serializable::GetValue(strResult, "key", boolValue); 290 ASSERT_FALSE(ret); 291 ret = Serializable::GetValue(strResult, "notExist", boolValue); 292 ASSERT_FALSE(ret); 293 294 double doubleValue; 295 ret = Serializable::GetValue(strResult, "key", doubleValue); 296 ASSERT_FALSE(ret); 297 ret = Serializable::GetValue(strResult, "notExist", doubleValue); 298 ASSERT_FALSE(ret); 299 300 std::vector<uint8_t> arrayValue; 301 ret = Serializable::GetValue(strResult, "key", arrayValue); 302 ASSERT_FALSE(ret); 303 ret = Serializable::GetValue(strResult, "notExist", arrayValue); 304 ASSERT_FALSE(ret); 305 } 306 307 /** 308 * @tc.name: BoundaryTest 309 * @tc.desc: test boundary. 310 * @tc.type: FUNC 311 */ 312 HWTEST_F(SerializableTest, BoundaryTest, TestSize.Level1) 313 { 314 struct TestBoundary : public Serializable { 315 int32_t int32Val; 316 uint32_t uint32Val; 317 int64_t int64Val; 318 uint64_t uint64Val; MarshalOHOS::Test::TestBoundary319 bool Marshal(json &node) const override 320 { 321 SetValue(node[GET_NAME(int32Val)], int32Val); 322 SetValue(node[GET_NAME(uint32Val)], uint32Val); 323 SetValue(node[GET_NAME(int64Val)], int64Val); 324 SetValue(node[GET_NAME(uint64Val)], uint64Val); 325 return true; 326 } UnmarshalOHOS::Test::TestBoundary327 bool Unmarshal(const json &node) override 328 { 329 bool success = true; 330 success = GetValue(node, GET_NAME(int32Val), int32Val) && success; 331 success = GetValue(node, GET_NAME(uint32Val), uint32Val) && success; 332 success = GetValue(node, GET_NAME(int64Val), int64Val) && success; 333 success = GetValue(node, GET_NAME(uint64Val), uint64Val) && success; 334 return success; 335 } 336 }; 337 TestBoundary in, out; 338 in.int32Val = INT32_MIN; 339 in.uint32Val = 0; 340 in.int64Val = INT64_MIN; 341 in.uint64Val = 0; 342 343 auto json = to_string(in.Marshall()); 344 out.Unmarshall(json); 345 EXPECT_EQ(out.int32Val, in.int32Val); 346 EXPECT_EQ(out.uint32Val, in.uint32Val); 347 EXPECT_EQ(out.int64Val, in.int64Val); 348 EXPECT_EQ(out.uint64Val, in.uint64Val); 349 350 in.int32Val = INT32_MAX; 351 in.uint32Val = UINT32_MAX; 352 in.int64Val = INT64_MAX; 353 in.uint64Val = UINT64_MAX; 354 355 json = to_string(in.Marshall()); 356 out.Unmarshall(json); 357 EXPECT_EQ(out.int32Val, in.int32Val); 358 EXPECT_EQ(out.uint32Val, in.uint32Val); 359 EXPECT_EQ(out.int64Val, in.int64Val); 360 EXPECT_EQ(out.uint64Val, in.uint64Val); 361 } 362 } // namespace OHOS::Test