1 /* 2 * Copyright (c) 2022-2023 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 <gtest/gtest.h> 17 #include "array_map_serializer.h" 18 #include "array_string_serializer.h" 19 #include "bool_serializer.h" 20 #include "func_code_utils.h" 21 #include "long_serializer.h" 22 #include "json_serializer.h" 23 #include "map_string_serializer.h" 24 #include "string_serializer.h" 25 26 using namespace testing::ext; 27 using namespace OHOS; 28 using namespace OHOS::EDM; 29 30 namespace OHOS { 31 namespace EDM { 32 namespace TEST { 33 class PolicySerializerTest : public testing::Test {}; 34 35 /** 36 * @tc.name: BOOL 37 * @tc.desc: Test BoolSerializer. 38 * @tc.type: FUNC 39 */ 40 HWTEST_F(PolicySerializerTest, BOOL, TestSize.Level1) 41 { 42 auto serializer = BoolSerializer::GetInstance(); 43 string boolJsonString; 44 ASSERT_TRUE(serializer->Serialize(true, boolJsonString)); 45 ASSERT_EQ(boolJsonString, "true"); 46 ASSERT_NE(boolJsonString, "false"); 47 48 bool boolValue = false; 49 ASSERT_FALSE(serializer->Deserialize("", boolValue)); 50 ASSERT_TRUE(serializer->Deserialize("true", boolValue)); 51 ASSERT_EQ(boolValue, true); 52 ASSERT_FALSE(serializer->Deserialize("truee", boolValue)); 53 54 MessageParcel messageParcel1; 55 messageParcel1.WriteBool(false); 56 ASSERT_TRUE(serializer->GetPolicy(messageParcel1, boolValue)); 57 ASSERT_EQ(boolValue, false); 58 59 MessageParcel messageParcel2; 60 boolValue = true; 61 ASSERT_TRUE(serializer->WritePolicy(messageParcel2, boolValue)); 62 ASSERT_EQ(messageParcel2.ReadBool(), true); 63 64 65 boolValue = false; 66 vector<bool> policyValues { false, true, false, false, true }; 67 ASSERT_TRUE(serializer->MergePolicy(policyValues, boolValue)); 68 ASSERT_EQ(boolValue, true); 69 70 boolValue = true; 71 policyValues = { false, false, false, false, false }; 72 ASSERT_TRUE(serializer->MergePolicy(policyValues, boolValue)); 73 ASSERT_EQ(boolValue, false); 74 } 75 76 /** 77 * @tc.name: STRING 78 * @tc.desc: Test StringSerializer. 79 * @tc.type: FUNC 80 */ 81 HWTEST_F(PolicySerializerTest, STRING, TestSize.Level1) 82 { 83 auto serializer = StringSerializer::GetInstance(); 84 string value = "s1"; 85 string jsonString; 86 ASSERT_TRUE(serializer->Serialize(value, jsonString)); 87 ASSERT_EQ(jsonString, "s1"); 88 89 jsonString = "s2"; 90 ASSERT_TRUE(serializer->Deserialize(jsonString, value)); 91 ASSERT_TRUE(value.length() == 2); 92 93 MessageParcel messageParcel1; 94 std::string value2 = "s一"; 95 messageParcel1.WriteString(value2); 96 value = ""; 97 ASSERT_TRUE(serializer->GetPolicy(messageParcel1, value)); 98 ASSERT_TRUE(value == "s一"); 99 100 MessageParcel messageParcel2; 101 value = "s二"; 102 ASSERT_TRUE(serializer->WritePolicy(messageParcel2, value)); 103 value2 = ""; 104 ASSERT_TRUE(messageParcel2.ReadString(value2)); 105 ASSERT_TRUE(value2 == "s二"); 106 107 vector<string> policyValues { "v1", "v2", "v3" }; 108 ASSERT_TRUE(serializer->MergePolicy(policyValues, value)); 109 ASSERT_TRUE(value == "v3"); 110 } 111 112 /** 113 * @tc.name: ARRAY_STRING 114 * @tc.desc: Test ArrayStringSerializer. 115 * @tc.type: FUNC 116 */ 117 HWTEST_F(PolicySerializerTest, ARRAY_STRING, TestSize.Level1) 118 { 119 auto serializer = ArrayStringSerializer::GetInstance(); 120 vector<string> value = { "v1", "v2", "v3", "v4", "v5" }; 121 string jsonString; 122 ASSERT_TRUE(serializer->Serialize(value, jsonString)); 123 auto sd = remove_if(jsonString.begin(), jsonString.end(), isspace); 124 jsonString.erase(sd, jsonString.end()); 125 ASSERT_EQ(jsonString, R"(["v1","v2","v3","v4","v5"])"); 126 127 jsonString = R"(["v1","v2","v3","v4","v5","v6"])"; 128 ASSERT_TRUE(serializer->Deserialize(jsonString, value)); 129 ASSERT_TRUE(value.size() == 6); 130 ASSERT_FALSE(serializer->Deserialize(R"(["v1","v2","v3","v4","v5""v6"])", value)); 131 132 MessageParcel messageParcel1; 133 vector<std::string> value2 = { "一", "二", "三", "四", "五", "六", "七" }; 134 messageParcel1.WriteStringVector(value2); 135 value = {}; 136 ASSERT_TRUE(serializer->GetPolicy(messageParcel1, value)); 137 ASSERT_TRUE(value.size() == 7); 138 139 MessageParcel messageParcel2; 140 value = { "v1", "v2", "v3" }; 141 ASSERT_TRUE(serializer->WritePolicy(messageParcel2, value)); 142 value2 = {}; 143 messageParcel2.ReadStringVector(&value2); 144 ASSERT_TRUE(value2.size() == 3); 145 146 vector<vector<string>> policyValues {{ "v1", "v2", "v3" }, 147 { "vv1", "vv2", "vv3" }, 148 { "vvv1", "vvv2", "vvv3" }, 149 { "v1", "v2", "v3" }, 150 { "vv1", "vv2", "vv3" }}; 151 ASSERT_TRUE(serializer->MergePolicy(policyValues, value)); 152 ASSERT_TRUE(value.size() == 9); 153 } 154 155 /** 156 * @tc.name: ArrayMapStringSerializer 157 * @tc.desc: Test ArrayMapSerializer::Serialize. 158 * @tc.type: FUNC 159 */ 160 HWTEST_F(PolicySerializerTest, ArrayMapStringSerializer, TestSize.Level1) 161 { 162 auto serializer = ArrayMapSerializer::GetInstance(); 163 vector<map<string, string>> value = { 164 { 165 { "id", "1" }, 166 { "name", "leon" }, 167 { "desc", "hello" }, 168 }, 169 { 170 { "id", "2" }, 171 { "name", "job" }, 172 { "desc", "world" }, 173 } 174 }; 175 string exceptStr; 176 string jsonString; 177 ASSERT_TRUE(serializer->Serialize(value, jsonString)); 178 auto sd = remove_if(jsonString.begin(), jsonString.end(), isspace); 179 jsonString.erase(sd, jsonString.end()); 180 exceptStr = R"(["{\n\"desc\":\"hello\",\n\"id\":\"1\",\n\"name\":\"leon\"\n}")"; 181 exceptStr.append(R"(,"{\n\"desc\":\"world\",\n\"id\":\"2\",\n\"name\":\"job\"\n}"])"); 182 ASSERT_EQ(jsonString, exceptStr); 183 } 184 185 /** 186 * @tc.name: ArrayMapStringDeserialize 187 * @tc.desc: Test ArrayMapSerializer::Deserialize. 188 * @tc.type: FUNC 189 */ 190 HWTEST_F(PolicySerializerTest, ArrayMapStringDeserialize, TestSize.Level1) 191 { 192 auto serializer = ArrayMapSerializer::GetInstance(); 193 vector<map<string, string>> value; 194 std::string jsonString = R"([{"desc":"hello","id":"1","name":"leon"},{"desc":"world","id":"2","name":"job"}])"; 195 ASSERT_TRUE(serializer->Deserialize(jsonString, value)); 196 ASSERT_TRUE(value.size() == 2); 197 198 map<string, string> expectZero = { 199 { "id", "1" }, 200 { "name", "leon" }, 201 { "desc", "hello" }, 202 }; 203 ASSERT_TRUE(value.at(0) == expectZero); 204 map<string, string> expectOne = { 205 { "id", "2" }, 206 { "name", "job" }, 207 { "desc", "world" }, 208 }; 209 ASSERT_TRUE(value.at(1) == expectOne); 210 } 211 212 /** 213 * @tc.name: ArrayMapStringGetPolicy 214 * @tc.desc: Test ArrayMapSerializer::GetPolicy. 215 * @tc.type: FUNC 216 */ 217 HWTEST_F(PolicySerializerTest, ArrayMapStringGetPolicy, TestSize.Level1) 218 { 219 auto serializer = ArrayMapSerializer::GetInstance(); 220 vector<map<string, string>> value; 221 MessageParcel messageParcel1; 222 vector<std::string> value2 = { 223 R"({"id":"1","name":"leon","desc":"hello world"})", 224 R"({"id":"2","name":"job","desc":"two"})", 225 R"({"id":"3","name":"james","desc":"three"})", 226 R"({"id":"4","name":"bao","desc":"four"})", 227 R"({"id":"5","name":"fox","desc":"five"})" 228 }; 229 messageParcel1.WriteStringVector(value2); 230 ASSERT_TRUE(serializer->GetPolicy(messageParcel1, value)); 231 ASSERT_TRUE(value.size() == 5); 232 } 233 234 /** 235 * @tc.name: ArrayMapStringWritePolicy 236 * @tc.desc: Test ArrayMapSerializer::WritePolicy. 237 * @tc.type: FUNC 238 */ 239 HWTEST_F(PolicySerializerTest, ArrayMapStringWritePolicy, TestSize.Level1) 240 { 241 MessageParcel messageParcel2; 242 auto serializer = ArrayMapSerializer::GetInstance(); 243 vector<map<string, string>> value = { 244 { 245 { "id", "1" }, 246 { "name", "leon" }, 247 { "desc", "hello" }, 248 }, 249 { 250 { "id", "2" }, 251 { "name", "job" }, 252 { "desc", "world" }, 253 } 254 }; 255 ASSERT_TRUE(serializer->WritePolicy(messageParcel2, value)); 256 257 value = {}; 258 ASSERT_TRUE(serializer->GetPolicy(messageParcel2, value)); 259 ASSERT_TRUE(value.size() == 2); 260 } 261 262 /** 263 * @tc.name: ArrayMapStringMergePolicy 264 * @tc.desc: Test ArrayMapSerializer::MergePolicy. 265 * @tc.type: FUNC 266 */ 267 HWTEST_F(PolicySerializerTest, ArrayMapStringMergePolicy, TestSize.Level1) 268 { 269 auto serializer = ArrayMapSerializer::GetInstance(); 270 vector<map<string, string>> value; 271 vector<vector<map<string, string>>> policyValues { 272 { 273 { 274 { "id", "1" }, 275 { "name", "leon" }, 276 { "desc", "hello" }, 277 }, 278 { 279 { "id", "2" }, 280 { "name", "job" }, 281 { "desc", "world" }, 282 } 283 }, 284 { 285 { 286 { "id", "1" }, 287 { "name", "leon" }, 288 { "desc", "hello" }, 289 }, 290 { 291 { "id", "3" }, 292 { "name", "james" }, 293 } 294 } 295 }; 296 ASSERT_TRUE(serializer->MergePolicy(policyValues, value)); 297 ASSERT_TRUE(value.size() == 3); 298 } 299 300 /** 301 * @tc.name: MAP_STRING 302 * @tc.desc: Test MapStringSerializer. 303 * @tc.type: FUNC 304 */ 305 HWTEST_F(PolicySerializerTest, MAP_STRING, TestSize.Level1) 306 { 307 auto serializer = MapStringSerializer::GetInstance(); 308 map<string, string> value = { 309 { "k1", "v1" }, 310 { "k2", "v2" }, 311 { "k3", "v3" }, 312 { "k4", "v4" }, 313 }; 314 string jsonString; 315 ASSERT_TRUE(serializer->Serialize(value, jsonString)); 316 auto sd = remove_if(jsonString.begin(), jsonString.end(), isspace); 317 jsonString.erase(sd, jsonString.end()); 318 ASSERT_EQ(jsonString, R"({"k1":"v1","k2":"v2","k3":"v3","k4":"v4"})"); 319 320 jsonString = R"({"k1":"v1","k2":"v2","k3":"v3","k4":"v4"})"; 321 ASSERT_TRUE(serializer->Deserialize(jsonString, value)); 322 ASSERT_TRUE(value.size() == 4); 323 ASSERT_FALSE(serializer->Deserialize(R"(["v1","v2","v3","v4","v5","v6"])", value)); 324 325 MessageParcel messageParcel1; 326 vector<std::string> key2 = { 327 "一", "二", "三", 328 "四", "五", "六", 329 "七" }; 330 messageParcel1.WriteStringVector(key2); 331 vector<std::string> value2 = { 332 "值", "值", "值", 333 "值", "值", "值", 334 "值" }; 335 messageParcel1.WriteStringVector(value2); 336 value = {}; 337 ASSERT_TRUE(serializer->GetPolicy(messageParcel1, value)); 338 ASSERT_TRUE(value.size() == 7); 339 for (auto const &entry:value) { 340 ASSERT_EQ(entry.second, "值"); 341 } 342 } 343 344 /** 345 * @tc.name: MAP_STRING_002 346 * @tc.desc: Test MapStringSerializer. 347 * @tc.type: FUNC 348 */ 349 HWTEST_F(PolicySerializerTest, MAP_STRING_002, TestSize.Level1) 350 { 351 auto serializer = MapStringSerializer::GetInstance(); 352 map<string, string> value = { 353 { "k1", "v1" }, 354 { "k2", "v2" }, 355 { "k3", "v3" }, 356 { "k4", "v4" }, 357 }; 358 MessageParcel messageParcel2; 359 value = { 360 { "k1", "v1" }, 361 { "k2", "v2" }, 362 { "k3", "v3" }, 363 { "k4", "v4" }, 364 }; 365 ASSERT_TRUE(serializer->WritePolicy(messageParcel2, value)); 366 vector<std::string> key4; 367 vector<std::string> value4; 368 messageParcel2.ReadStringVector(&key4); 369 messageParcel2.ReadStringVector(&value4); 370 ASSERT_TRUE(key4.size() == 4); 371 for (std::uint32_t i = 0; i < key4.size(); ++i) { 372 ASSERT_EQ(key4.at(i), "k" + std::to_string(i + 1)); 373 } 374 ASSERT_TRUE(value4.size() == 4); 375 for (std::uint32_t i = 0; i < value4.size(); ++i) { 376 ASSERT_EQ(value4.at(i), "v" + std::to_string(i + 1)); 377 } 378 379 map<string, string> value1 = { 380 { "k1", "v1" }, 381 }; 382 map<string, string> value2 = { 383 { "k1", "v1" }, 384 { "k2", "v2" }, 385 { "k3", "v3" }, 386 }; 387 map<string, string> value3 = { 388 { "k3", "v3" }, 389 { "k4", "v4" }, 390 }; 391 value = {}; 392 vector<map<string, string>> policyValues { value1, value2, value3 }; 393 ASSERT_TRUE(serializer->MergePolicy(policyValues, value)); 394 ASSERT_TRUE(value3 == value); 395 } 396 397 /** 398 * @tc.name: MAP_STRING_Serializer_Deserialize 399 * @tc.desc: Test MapStringSerializer. 400 * @tc.type: FUNC 401 */ 402 HWTEST_F(PolicySerializerTest, MAP_STRING_Serializer_Deserialize, TestSize.Level1) 403 { 404 auto serializer = MapStringSerializer::GetInstance(); 405 Json::Value root; 406 root["string"] = "str"; 407 root["bool"] = true; 408 root["int"] = 1; 409 Json::StreamWriterBuilder builder; 410 builder["indentation"] = " "; 411 std::string jsonString = Json::writeString(builder, root); 412 std::map<std::string, std::string> result; 413 ASSERT_TRUE(serializer->Deserialize(jsonString, result)); 414 ASSERT_TRUE(result["string"] == "str"); 415 ASSERT_TRUE(result["bool"] == "true"); 416 ASSERT_TRUE(result["int"] == "1"); 417 } 418 419 /** 420 * @tc.name: MAP_STRING_Serializer_DeserializeFail 421 * @tc.desc: Test MapStringSerializer. 422 * @tc.type: FUNC 423 */ 424 HWTEST_F(PolicySerializerTest, MAP_STRING_Serializer_DeserializeFail, TestSize.Level1) 425 { 426 auto serializer = MapStringSerializer::GetInstance(); 427 Json::Value root; 428 Json::Value sub; 429 sub["key"] = "value"; 430 root["object"] = sub; 431 Json::StreamWriterBuilder builder; 432 builder["indentation"] = " "; 433 std::string jsonString = Json::writeString(builder, root); 434 std::map<std::string, std::string> result; 435 ASSERT_FALSE(serializer->Deserialize(jsonString, result)); 436 ASSERT_TRUE(result.empty()); 437 } 438 439 /** 440 * @tc.name: JSON 441 * @tc.desc: Test JsonSerializer. 442 * @tc.type: FUNC 443 */ 444 HWTEST_F(PolicySerializerTest, JSON, TestSize.Level1) 445 { 446 auto serializer = JsonSerializer::GetInstance(); 447 Json::Value value; 448 value["k1"] = "v1"; 449 value["k2"] = "v2"; 450 value["k3"] = 3; 451 string jsonString; 452 ASSERT_TRUE(serializer->Serialize(value, jsonString)); 453 auto sd = remove_if(jsonString.begin(), jsonString.end(), isspace); 454 jsonString.erase(sd, jsonString.end()); 455 ASSERT_EQ(jsonString, R"({"k1":"v1","k2":"v2","k3":3})"); 456 457 jsonString = R"(["v1","v2","v3","v4","v5","v6"])"; 458 ASSERT_TRUE(serializer->Deserialize(jsonString, value)); 459 ASSERT_TRUE(value.isArray() && value.size() == 6); 460 461 MessageParcel messageParcel1; 462 std::string value2 = R"(["v1","v2"])"; 463 messageParcel1.WriteString(value2); 464 value = {}; 465 ASSERT_TRUE(serializer->GetPolicy(messageParcel1, value)); 466 ASSERT_TRUE(value.isArray() && value.size() == 2); 467 468 MessageParcel messageParcel2; 469 value = Json::Value(Json::arrayValue); 470 value[0] = 1; 471 value[1] = 2; 472 value[3] = 3; 473 ASSERT_TRUE(serializer->WritePolicy(messageParcel2, value)); 474 value2 = ""; 475 messageParcel2.ReadString(value2); 476 jsonString = value2; 477 sd = remove_if(jsonString.begin(), jsonString.end(), isspace); 478 jsonString.erase(sd, jsonString.end()); 479 ASSERT_EQ(jsonString, R"([1,2,null,3])"); 480 481 std::vector<Json::Value> vec = {jsonString}; 482 ASSERT_TRUE(serializer->MergePolicy(vec, value)); 483 ASSERT_TRUE(vec.size() == 1); 484 } 485 486 /** 487 * @tc.name: Long 488 * @tc.desc: Test LongSerializer. 489 * @tc.type: FUNC 490 */ 491 HWTEST_F(PolicySerializerTest, Long, TestSize.Level1) 492 { 493 auto serializer = LongSerializer::GetInstance(); 494 std::string testString = "123456"; 495 std::string jsonString = testString; 496 int64_t res = 0; 497 ASSERT_TRUE(serializer->Deserialize(jsonString, res)); 498 ASSERT_EQ(res, 123456); 499 500 jsonString = ""; 501 ASSERT_TRUE(serializer->Serialize(res, jsonString)); 502 ASSERT_EQ(jsonString, testString); 503 504 MessageParcel data; 505 data.WriteInt64(1); 506 ASSERT_TRUE(serializer->GetPolicy(data, res)); 507 ASSERT_TRUE(res == 1); 508 509 MessageParcel reply; 510 res = 1; 511 ASSERT_TRUE(serializer->WritePolicy(reply, res)); 512 int64_t temp = 0; 513 reply.ReadInt64(temp); 514 ASSERT_TRUE(temp == 1); 515 516 std::vector<int64_t> vec = {1, 2, 3, 4, 5}; 517 serializer->MergePolicy(vec, res); 518 ASSERT_TRUE(res == 5); 519 } 520 } // namespace TEST 521 } // namespace EDM 522 } // namespace OHOS 523