1bc03f14fSopenharmony_ci/* 2bc03f14fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License. 5bc03f14fSopenharmony_ci * You may obtain a copy of the License at 6bc03f14fSopenharmony_ci * 7bc03f14fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bc03f14fSopenharmony_ci * 9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and 13bc03f14fSopenharmony_ci * limitations under the License. 14bc03f14fSopenharmony_ci */ 15bc03f14fSopenharmony_ci#include <gtest/gtest.h> 16bc03f14fSopenharmony_ci 17bc03f14fSopenharmony_ci#include "clip/clip_plugin.h" 18bc03f14fSopenharmony_ci#include "config.h" 19bc03f14fSopenharmony_ci#include "pasteboard_hilog.h" 20bc03f14fSopenharmony_ci#include "serializable.h" 21bc03f14fSopenharmony_ci 22bc03f14fSopenharmony_cinamespace OHOS::DistributedData { 23bc03f14fSopenharmony_ciusing namespace testing::ext; 24bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices; 25bc03f14fSopenharmony_ciclass SerializableTest : public testing::Test { 26bc03f14fSopenharmony_cipublic: 27bc03f14fSopenharmony_ci static void SetUpTestCase(void); 28bc03f14fSopenharmony_ci static void TearDownTestCase(void); 29bc03f14fSopenharmony_ci void SetUp(); 30bc03f14fSopenharmony_ci void TearDown(); 31bc03f14fSopenharmony_ci void CreateConfig(Config &config, const std::string &prefix = ""); 32bc03f14fSopenharmony_ci void CreateComponent(Config::Component &component, int32_t index, const std::string &prefix); 33bc03f14fSopenharmony_ci Serializable::json ToJson(const std::string &str); 34bc03f14fSopenharmony_ci bool IsSame(Config &oldConfig, Config &newConfig); 35bc03f14fSopenharmony_ci static bool IsSame(Config::Component &oldComp, Config::Component &newComp); 36bc03f14fSopenharmony_ci 37bc03f14fSopenharmony_ci template<typename T> 38bc03f14fSopenharmony_ci static bool IsSame(std::vector<T> &olds, std::vector<T> &news) 39bc03f14fSopenharmony_ci { 40bc03f14fSopenharmony_ci if (olds.size() != news.size()) { 41bc03f14fSopenharmony_ci return false; 42bc03f14fSopenharmony_ci } 43bc03f14fSopenharmony_ci 44bc03f14fSopenharmony_ci bool isSame = true; 45bc03f14fSopenharmony_ci auto len = olds.size(); 46bc03f14fSopenharmony_ci for (int i = 0; i < len; ++i) { 47bc03f14fSopenharmony_ci isSame = IsSame(olds[i], news[i]) && isSame; 48bc03f14fSopenharmony_ci } 49bc03f14fSopenharmony_ci return isSame; 50bc03f14fSopenharmony_ci } 51bc03f14fSopenharmony_ci}; 52bc03f14fSopenharmony_ci 53bc03f14fSopenharmony_civoid SerializableTest::SetUpTestCase(void) {} 54bc03f14fSopenharmony_ci 55bc03f14fSopenharmony_civoid SerializableTest::TearDownTestCase(void) {} 56bc03f14fSopenharmony_ci 57bc03f14fSopenharmony_civoid SerializableTest::SetUp(void) {} 58bc03f14fSopenharmony_ci 59bc03f14fSopenharmony_civoid SerializableTest::TearDown(void) {} 60bc03f14fSopenharmony_ci 61bc03f14fSopenharmony_ciSerializable::json SerializableTest::ToJson(const std::string &str) 62bc03f14fSopenharmony_ci{ 63bc03f14fSopenharmony_ci return cJSON_Parse(str.c_str()); 64bc03f14fSopenharmony_ci} 65bc03f14fSopenharmony_ci 66bc03f14fSopenharmony_cibool SerializableTest::IsSame(Config::Component &oldComp, Config::Component &newComp) 67bc03f14fSopenharmony_ci{ 68bc03f14fSopenharmony_ci bool isSame = true; 69bc03f14fSopenharmony_ci isSame = oldComp.description == newComp.description; 70bc03f14fSopenharmony_ci isSame = oldComp.lib == newComp.lib && isSame; 71bc03f14fSopenharmony_ci isSame = oldComp.constructor == newComp.constructor && isSame; 72bc03f14fSopenharmony_ci isSame = oldComp.destructor == newComp.destructor && isSame; 73bc03f14fSopenharmony_ci isSame = oldComp.params == newComp.params && isSame; 74bc03f14fSopenharmony_ci return isSame; 75bc03f14fSopenharmony_ci} 76bc03f14fSopenharmony_ci 77bc03f14fSopenharmony_cibool SerializableTest::IsSame(Config &oldConfig, Config &newConfig) 78bc03f14fSopenharmony_ci{ 79bc03f14fSopenharmony_ci bool isSame = true; 80bc03f14fSopenharmony_ci isSame = oldConfig.processLabel == newConfig.processLabel; 81bc03f14fSopenharmony_ci isSame = oldConfig.version == newConfig.version && isSame; 82bc03f14fSopenharmony_ci isSame = oldConfig.features == newConfig.features && isSame; 83bc03f14fSopenharmony_ci isSame = oldConfig.plugins == newConfig.plugins && isSame; 84bc03f14fSopenharmony_ci isSame = IsSame(oldConfig.components, newConfig.components) && isSame; 85bc03f14fSopenharmony_ci isSame = oldConfig.bundles == newConfig.bundles && isSame; 86bc03f14fSopenharmony_ci return isSame; 87bc03f14fSopenharmony_ci} 88bc03f14fSopenharmony_ci 89bc03f14fSopenharmony_civoid SerializableTest::CreateComponent(Config::Component &component, int32_t index, const std::string &prefix) 90bc03f14fSopenharmony_ci{ 91bc03f14fSopenharmony_ci component.description = prefix + "description" + std::to_string(index); 92bc03f14fSopenharmony_ci component.lib = prefix + "lib" + std::to_string(index); 93bc03f14fSopenharmony_ci component.constructor = prefix + "constructor" + std::to_string(index); 94bc03f14fSopenharmony_ci component.destructor = prefix + "destructor" + std::to_string(index); 95bc03f14fSopenharmony_ci component.params = prefix + "params" + std::to_string(index); 96bc03f14fSopenharmony_ci} 97bc03f14fSopenharmony_ci 98bc03f14fSopenharmony_civoid SerializableTest::CreateConfig(Config &config, const std::string &prefix) 99bc03f14fSopenharmony_ci{ 100bc03f14fSopenharmony_ci config.processLabel = prefix + "processLabel"; 101bc03f14fSopenharmony_ci config.version = prefix + "version"; 102bc03f14fSopenharmony_ci config.features = { prefix + "feature1", prefix + "feature2" }; 103bc03f14fSopenharmony_ci config.plugins = { prefix + "plugin1", prefix + "plugin2" }; 104bc03f14fSopenharmony_ci Config::Component component1; 105bc03f14fSopenharmony_ci Config::Component component2; 106bc03f14fSopenharmony_ci int32_t index = 0; 107bc03f14fSopenharmony_ci CreateComponent(component1, index++, prefix); 108bc03f14fSopenharmony_ci CreateComponent(component2, index++, prefix); 109bc03f14fSopenharmony_ci config.components = { component1, component2 }; 110bc03f14fSopenharmony_ci} 111bc03f14fSopenharmony_ci 112bc03f14fSopenharmony_ci/** 113bc03f14fSopenharmony_ci* @tc.name: SerializableTest001 114bc03f14fSopenharmony_ci* @tc.desc: test serializable with invalid jsonStr . 115bc03f14fSopenharmony_ci* @tc.type: FUNC 116bc03f14fSopenharmony_ci* @tc.require: 117bc03f14fSopenharmony_ci* @tc.author: 118bc03f14fSopenharmony_ci*/ 119bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest001, TestSize.Level0) 120bc03f14fSopenharmony_ci{ 121bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "SerializableTest001 Start."); 122bc03f14fSopenharmony_ci Config config; 123bc03f14fSopenharmony_ci std::string jsonStr = ""; 124bc03f14fSopenharmony_ci auto ret = config.Unmarshall(jsonStr); 125bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Unmarshall NULL."); 126bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 127bc03f14fSopenharmony_ci 128bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Unmarshall not null."); 129bc03f14fSopenharmony_ci jsonStr = "{ a }"; 130bc03f14fSopenharmony_ci ret = config.Unmarshall(jsonStr); 131bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 132bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 133bc03f14fSopenharmony_ci} 134bc03f14fSopenharmony_ci 135bc03f14fSopenharmony_ci/** 136bc03f14fSopenharmony_ci* @tc.name: SerializableTest002 137bc03f14fSopenharmony_ci* @tc.desc: test serializable with valid jsonStr . 138bc03f14fSopenharmony_ci* @tc.type: FUNC 139bc03f14fSopenharmony_ci* @tc.require: 140bc03f14fSopenharmony_ci* @tc.author: 141bc03f14fSopenharmony_ci*/ 142bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest002, TestSize.Level0) 143bc03f14fSopenharmony_ci{ 144bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "SerializableTest002 Start."); 145bc03f14fSopenharmony_ci 146bc03f14fSopenharmony_ci std::vector<uint8_t> vec = { 1, 2, 3, 4, 5 }; 147bc03f14fSopenharmony_ci std::string jsonStr = "{\n" 148bc03f14fSopenharmony_ci " \"param1\": 2, \n" 149bc03f14fSopenharmony_ci " \"param2\": 3, \n" 150bc03f14fSopenharmony_ci " \"param3\": 4, \n" 151bc03f14fSopenharmony_ci " \"param4\": true, \n" 152bc03f14fSopenharmony_ci " \"param5\": [1, 2, 3, 4, 5] \n" 153bc03f14fSopenharmony_ci "}"; 154bc03f14fSopenharmony_ci 155bc03f14fSopenharmony_ci uint32_t res1; 156bc03f14fSopenharmony_ci int32_t res2; 157bc03f14fSopenharmony_ci int64_t res3; 158bc03f14fSopenharmony_ci bool res4; 159bc03f14fSopenharmony_ci std::vector<uint8_t> res5; 160bc03f14fSopenharmony_ci 161bc03f14fSopenharmony_ci auto node = ToJson(jsonStr); 162bc03f14fSopenharmony_ci auto ret = Serializable::GetValue(node, GET_NAME(param1), res1); 163bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 164bc03f14fSopenharmony_ci ASSERT_EQ(res1, 2); 165bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param2), res2); 166bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 167bc03f14fSopenharmony_ci ASSERT_EQ(res2, 3); 168bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param3), res3); 169bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 170bc03f14fSopenharmony_ci ASSERT_EQ(res3, 4); 171bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param4), res4); 172bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 173bc03f14fSopenharmony_ci ASSERT_TRUE(res4); 174bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param5), res5); 175bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 176bc03f14fSopenharmony_ci ASSERT_EQ(res5.size(), vec.size()); 177bc03f14fSopenharmony_ci for (uint64_t i = 0; i < res5.size(); i++) { 178bc03f14fSopenharmony_ci ASSERT_EQ(res5[i], vec[i]); 179bc03f14fSopenharmony_ci } 180bc03f14fSopenharmony_ci 181bc03f14fSopenharmony_ci cJSON_Delete(node); 182bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 183bc03f14fSopenharmony_ci} 184bc03f14fSopenharmony_ci 185bc03f14fSopenharmony_ci/** 186bc03f14fSopenharmony_ci* @tc.name: SerializableTest003 187bc03f14fSopenharmony_ci* @tc.desc: test serializable GetValue and SetValue with invalid string value . 188bc03f14fSopenharmony_ci* @tc.type: FUNC 189bc03f14fSopenharmony_ci* @tc.require: 190bc03f14fSopenharmony_ci* @tc.author: 191bc03f14fSopenharmony_ci*/ 192bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest003, TestSize.Level0) 193bc03f14fSopenharmony_ci{ 194bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 195bc03f14fSopenharmony_ci std::string jsonStr = R"({"key":null})"; 196bc03f14fSopenharmony_ci auto json = ToJson(jsonStr); 197bc03f14fSopenharmony_ci std::string value; 198bc03f14fSopenharmony_ci auto ret = Serializable::GetValue(json, "key", value); 199bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 200bc03f14fSopenharmony_ci cJSON_Delete(json); 201bc03f14fSopenharmony_ci 202bc03f14fSopenharmony_ci jsonStr = R"({"key":1})"; 203bc03f14fSopenharmony_ci json = ToJson(jsonStr); 204bc03f14fSopenharmony_ci ret = Serializable::GetValue(json, "key", value); 205bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 206bc03f14fSopenharmony_ci cJSON_Delete(json); 207bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 208bc03f14fSopenharmony_ci} 209bc03f14fSopenharmony_ci 210bc03f14fSopenharmony_ci/** 211bc03f14fSopenharmony_ci* @tc.name: SerializableTest004 212bc03f14fSopenharmony_ci* @tc.desc: test serializable GetValue and SetValue with invalid uint32_t value . 213bc03f14fSopenharmony_ci* @tc.type: FUNC 214bc03f14fSopenharmony_ci* @tc.require: 215bc03f14fSopenharmony_ci* @tc.author: 216bc03f14fSopenharmony_ci*/ 217bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest004, TestSize.Level0) 218bc03f14fSopenharmony_ci{ 219bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 220bc03f14fSopenharmony_ci std::string jsonStr = R"({"key":null})"; 221bc03f14fSopenharmony_ci auto json = ToJson(jsonStr); 222bc03f14fSopenharmony_ci uint32_t value; 223bc03f14fSopenharmony_ci auto ret = Serializable::GetValue(json, "key", value); 224bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 225bc03f14fSopenharmony_ci cJSON_Delete(json); 226bc03f14fSopenharmony_ci 227bc03f14fSopenharmony_ci jsonStr = R"({"key":"1"})"; 228bc03f14fSopenharmony_ci json = ToJson(jsonStr); 229bc03f14fSopenharmony_ci ret = Serializable::GetValue(json, "key", value); 230bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 231bc03f14fSopenharmony_ci cJSON_Delete(json); 232bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 233bc03f14fSopenharmony_ci} 234bc03f14fSopenharmony_ci 235bc03f14fSopenharmony_ci/** 236bc03f14fSopenharmony_ci* @tc.name: SerializableTest005 237bc03f14fSopenharmony_ci* @tc.desc: test serializable GetValue and SetValue with invalid int32_t value . 238bc03f14fSopenharmony_ci* @tc.type: FUNC 239bc03f14fSopenharmony_ci* @tc.require: 240bc03f14fSopenharmony_ci* @tc.author: 241bc03f14fSopenharmony_ci*/ 242bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest005, TestSize.Level0) 243bc03f14fSopenharmony_ci{ 244bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 245bc03f14fSopenharmony_ci std::string jsonStr = R"({"key":null})"; 246bc03f14fSopenharmony_ci auto json = ToJson(jsonStr); 247bc03f14fSopenharmony_ci int32_t value; 248bc03f14fSopenharmony_ci auto ret = Serializable::GetValue(json, "key", value); 249bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 250bc03f14fSopenharmony_ci cJSON_Delete(json); 251bc03f14fSopenharmony_ci 252bc03f14fSopenharmony_ci jsonStr = R"({"key":"1"})"; 253bc03f14fSopenharmony_ci json = ToJson(jsonStr); 254bc03f14fSopenharmony_ci ret = Serializable::GetValue(json, "key", value); 255bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 256bc03f14fSopenharmony_ci cJSON_Delete(json); 257bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 258bc03f14fSopenharmony_ci} 259bc03f14fSopenharmony_ci 260bc03f14fSopenharmony_ci/** 261bc03f14fSopenharmony_ci* @tc.name: SerializableTest006 262bc03f14fSopenharmony_ci* @tc.desc: test serializable GetValue and SetValue with invalid int64_t value . 263bc03f14fSopenharmony_ci* @tc.type: FUNC 264bc03f14fSopenharmony_ci* @tc.require: 265bc03f14fSopenharmony_ci* @tc.author: 266bc03f14fSopenharmony_ci*/ 267bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest006, TestSize.Level0) 268bc03f14fSopenharmony_ci{ 269bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 270bc03f14fSopenharmony_ci std::string jsonStr = R"({"key":null})"; 271bc03f14fSopenharmony_ci auto json = ToJson(jsonStr); 272bc03f14fSopenharmony_ci int64_t value; 273bc03f14fSopenharmony_ci auto ret = Serializable::GetValue(json, "key", value); 274bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 275bc03f14fSopenharmony_ci cJSON_Delete(json); 276bc03f14fSopenharmony_ci 277bc03f14fSopenharmony_ci jsonStr = R"({"key":"1"})"; 278bc03f14fSopenharmony_ci json = ToJson(jsonStr); 279bc03f14fSopenharmony_ci ret = Serializable::GetValue(json, "key", value); 280bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 281bc03f14fSopenharmony_ci cJSON_Delete(json); 282bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 283bc03f14fSopenharmony_ci} 284bc03f14fSopenharmony_ci 285bc03f14fSopenharmony_ci/** 286bc03f14fSopenharmony_ci* @tc.name: SerializableTest007 287bc03f14fSopenharmony_ci* @tc.desc: test serializable GetValue and SetValue with invalid bool value . 288bc03f14fSopenharmony_ci* @tc.type: FUNC 289bc03f14fSopenharmony_ci* @tc.require: 290bc03f14fSopenharmony_ci* @tc.author: 291bc03f14fSopenharmony_ci*/ 292bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest007, TestSize.Level0) 293bc03f14fSopenharmony_ci{ 294bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 295bc03f14fSopenharmony_ci std::string jsonStr = R"({"key":null})"; 296bc03f14fSopenharmony_ci auto json = ToJson(jsonStr); 297bc03f14fSopenharmony_ci bool value; 298bc03f14fSopenharmony_ci auto ret = Serializable::GetValue(json, "key", value); 299bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 300bc03f14fSopenharmony_ci cJSON_Delete(json); 301bc03f14fSopenharmony_ci 302bc03f14fSopenharmony_ci jsonStr = R"({"key":1})"; 303bc03f14fSopenharmony_ci json = ToJson(jsonStr); 304bc03f14fSopenharmony_ci ret = Serializable::GetValue(json, "key", value); 305bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 306bc03f14fSopenharmony_ci cJSON_Delete(json); 307bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 308bc03f14fSopenharmony_ci} 309bc03f14fSopenharmony_ci 310bc03f14fSopenharmony_ci/** 311bc03f14fSopenharmony_ci* @tc.name: SerializableTest008 312bc03f14fSopenharmony_ci* @tc.desc: test serializable GetValue and SetValue with invalid std::vector<uint8_t> value . 313bc03f14fSopenharmony_ci* @tc.type: FUNC 314bc03f14fSopenharmony_ci* @tc.require: 315bc03f14fSopenharmony_ci* @tc.author: 316bc03f14fSopenharmony_ci*/ 317bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest008, TestSize.Level0) 318bc03f14fSopenharmony_ci{ 319bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 320bc03f14fSopenharmony_ci std::string jsonStr = R"({"key":null})"; 321bc03f14fSopenharmony_ci auto json = ToJson(jsonStr); 322bc03f14fSopenharmony_ci std::vector<uint8_t> value; 323bc03f14fSopenharmony_ci auto ret = Serializable::GetValue(json, "key", value); 324bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 325bc03f14fSopenharmony_ci cJSON_Delete(json); 326bc03f14fSopenharmony_ci 327bc03f14fSopenharmony_ci jsonStr = R"({"key":"{1, 2, 3}"})"; 328bc03f14fSopenharmony_ci json = ToJson(jsonStr); 329bc03f14fSopenharmony_ci ret = Serializable::GetValue(json, "key", value); 330bc03f14fSopenharmony_ci ASSERT_FALSE(ret); 331bc03f14fSopenharmony_ci cJSON_Delete(json); 332bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 333bc03f14fSopenharmony_ci} 334bc03f14fSopenharmony_ci 335bc03f14fSopenharmony_ci/** 336bc03f14fSopenharmony_ci* @tc.name: SerializableTest009 337bc03f14fSopenharmony_ci* @tc.desc: test serializable SetValue with valid value. 338bc03f14fSopenharmony_ci* @tc.type: FUNC 339bc03f14fSopenharmony_ci* @tc.require: 340bc03f14fSopenharmony_ci* @tc.author: 341bc03f14fSopenharmony_ci*/ 342bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest009, TestSize.Level0) 343bc03f14fSopenharmony_ci{ 344bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 345bc03f14fSopenharmony_ci uint32_t param1 = 2; 346bc03f14fSopenharmony_ci int32_t param2 = 3; 347bc03f14fSopenharmony_ci int64_t param3 = 4; 348bc03f14fSopenharmony_ci bool param4 = false; 349bc03f14fSopenharmony_ci std::vector<uint8_t> param5 = { 1, 2, 3, 4, 5 }; 350bc03f14fSopenharmony_ci Config config; 351bc03f14fSopenharmony_ci CreateConfig(config); 352bc03f14fSopenharmony_ci 353bc03f14fSopenharmony_ci Serializable::json node = nullptr; 354bc03f14fSopenharmony_ci config.Marshal(node); 355bc03f14fSopenharmony_ci auto ret = Serializable::SetValue(node, param1, GET_NAME(param1)); 356bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 357bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, param2, GET_NAME(param2)); 358bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 359bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, param3, GET_NAME(param3)); 360bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 361bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, param4, GET_NAME(param4)); 362bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 363bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, param5, GET_NAME(param5)); 364bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 365bc03f14fSopenharmony_ci ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param1))); 366bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param1), param1); 367bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 368bc03f14fSopenharmony_ci ASSERT_EQ(param1, 2); 369bc03f14fSopenharmony_ci ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param2))); 370bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param2), param2); 371bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 372bc03f14fSopenharmony_ci ASSERT_EQ(param2, 3); 373bc03f14fSopenharmony_ci ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param3))); 374bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param3), param3); 375bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 376bc03f14fSopenharmony_ci ASSERT_EQ(param3, 4); 377bc03f14fSopenharmony_ci ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param4))); 378bc03f14fSopenharmony_ci ret = Serializable::GetValue(node, GET_NAME(param4), param4); 379bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 380bc03f14fSopenharmony_ci ASSERT_EQ(param4, false); 381bc03f14fSopenharmony_ci ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param5))); 382bc03f14fSopenharmony_ci cJSON_Delete(node); 383bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 384bc03f14fSopenharmony_ci} 385bc03f14fSopenharmony_ci 386bc03f14fSopenharmony_ci/** 387bc03f14fSopenharmony_ci* @tc.name: SerializableTest010 388bc03f14fSopenharmony_ci* @tc.desc: test serializable SetValue with valid value. 389bc03f14fSopenharmony_ci* @tc.type: FUNC 390bc03f14fSopenharmony_ci* @tc.require: 391bc03f14fSopenharmony_ci* @tc.author: 392bc03f14fSopenharmony_ci*/ 393bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest010, TestSize.Level0) 394bc03f14fSopenharmony_ci{ 395bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "SerializableTest010 Start."); 396bc03f14fSopenharmony_ci Config config; 397bc03f14fSopenharmony_ci CreateConfig(config); 398bc03f14fSopenharmony_ci 399bc03f14fSopenharmony_ci Config newConfig; 400bc03f14fSopenharmony_ci CreateConfig(newConfig, "new"); 401bc03f14fSopenharmony_ci 402bc03f14fSopenharmony_ci Serializable::json node = nullptr; 403bc03f14fSopenharmony_ci config.Marshal(node); 404bc03f14fSopenharmony_ci auto ret = Serializable::SetValue(node, newConfig.version, GET_NAME(version)); 405bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 406bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, newConfig.processLabel, GET_NAME(processLabel)); 407bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 408bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, newConfig.features, GET_NAME(features)); 409bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 410bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, newConfig.plugins, GET_NAME(plugins)); 411bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 412bc03f14fSopenharmony_ci ret = Serializable::SetValue(node, newConfig.components, GET_NAME(components)); 413bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 414bc03f14fSopenharmony_ci config.Unmarshal(node); 415bc03f14fSopenharmony_ci ASSERT_EQ(IsSame(config, newConfig), true); 416bc03f14fSopenharmony_ci cJSON_Delete(node); 417bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 418bc03f14fSopenharmony_ci} 419bc03f14fSopenharmony_ci 420bc03f14fSopenharmony_ci/** 421bc03f14fSopenharmony_ci* @tc.name: SerializableTest011 422bc03f14fSopenharmony_ci* @tc.desc: test serializable Unmarshall with valid jsonstr. 423bc03f14fSopenharmony_ci* @tc.type: FUNC 424bc03f14fSopenharmony_ci* @tc.require: 425bc03f14fSopenharmony_ci* @tc.author: 426bc03f14fSopenharmony_ci*/ 427bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, SerializableTest011, TestSize.Level0) 428bc03f14fSopenharmony_ci{ 429bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start."); 430bc03f14fSopenharmony_ci std::string jsonStr = "{\n" 431bc03f14fSopenharmony_ci " \"processLabel\": \"processLabel\", \n" 432bc03f14fSopenharmony_ci " \"version\": \"version\", \n" 433bc03f14fSopenharmony_ci " \"features\": [\"features1\", \"features2\"], \n" 434bc03f14fSopenharmony_ci " \"plugins\": [\"plugins1\", \"plugins2\"], \n" 435bc03f14fSopenharmony_ci " \"components\": [{ \n" 436bc03f14fSopenharmony_ci " \"description\": \"description1\",\n" 437bc03f14fSopenharmony_ci " \"lib\": \"lib1\", \n" 438bc03f14fSopenharmony_ci " \"constructor\": \"constructor1\",\n" 439bc03f14fSopenharmony_ci " \"destructor\": \"destructor1\", \n" 440bc03f14fSopenharmony_ci " \"params\": \"params1\" \n" 441bc03f14fSopenharmony_ci " }, { \n" 442bc03f14fSopenharmony_ci " \"description\": \"description2\",\n" 443bc03f14fSopenharmony_ci " \"lib\": \"lib2\", \n" 444bc03f14fSopenharmony_ci " \"constructor\": \"constructor2\",\n" 445bc03f14fSopenharmony_ci " \"destructor\": \"destructor2\", \n" 446bc03f14fSopenharmony_ci " \"params\": \"params2\" \n" 447bc03f14fSopenharmony_ci " }] \n" 448bc03f14fSopenharmony_ci "}"; 449bc03f14fSopenharmony_ci 450bc03f14fSopenharmony_ci Config config; 451bc03f14fSopenharmony_ci auto ret = config.Unmarshall(jsonStr); 452bc03f14fSopenharmony_ci ASSERT_TRUE(ret); 453bc03f14fSopenharmony_ci ASSERT_EQ(config.processLabel, "processLabel"); 454bc03f14fSopenharmony_ci ASSERT_EQ(config.version, "version"); 455bc03f14fSopenharmony_ci ASSERT_EQ(config.features[0], "features1"); 456bc03f14fSopenharmony_ci ASSERT_EQ(config.features[1], "features2"); 457bc03f14fSopenharmony_ci ASSERT_EQ(config.plugins[0], "plugins1"); 458bc03f14fSopenharmony_ci ASSERT_EQ(config.plugins[1], "plugins2"); 459bc03f14fSopenharmony_ci ASSERT_EQ(config.components[0].description, "description1"); 460bc03f14fSopenharmony_ci ASSERT_EQ(config.components[0].lib, "lib1"); 461bc03f14fSopenharmony_ci ASSERT_EQ(config.components[0].constructor, "constructor1"); 462bc03f14fSopenharmony_ci ASSERT_EQ(config.components[0].destructor, "destructor1"); 463bc03f14fSopenharmony_ci ASSERT_EQ(config.components[0].params, "params1"); 464bc03f14fSopenharmony_ci ASSERT_EQ(config.components[1].description, "description2"); 465bc03f14fSopenharmony_ci ASSERT_EQ(config.components[1].lib, "lib2"); 466bc03f14fSopenharmony_ci ASSERT_EQ(config.components[1].constructor, "constructor2"); 467bc03f14fSopenharmony_ci ASSERT_EQ(config.components[1].destructor, "destructor2"); 468bc03f14fSopenharmony_ci ASSERT_EQ(config.components[1].params, "params2"); 469bc03f14fSopenharmony_ci PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End."); 470bc03f14fSopenharmony_ci} 471bc03f14fSopenharmony_ci 472bc03f14fSopenharmony_ci/** 473bc03f14fSopenharmony_ci* @tc.name: GlobalEventTest001 474bc03f14fSopenharmony_ci* @tc.desc: test GlobalEvent serializable. 475bc03f14fSopenharmony_ci* @tc.type: FUNC 476bc03f14fSopenharmony_ci* @tc.require: 477bc03f14fSopenharmony_ci* @tc.author: 478bc03f14fSopenharmony_ci*/ 479bc03f14fSopenharmony_ciHWTEST_F(SerializableTest, GlobalEventTest001, TestSize.Level0) 480bc03f14fSopenharmony_ci{ 481bc03f14fSopenharmony_ci ClipPlugin::GlobalEvent event; 482bc03f14fSopenharmony_ci event.deviceId = "deviceId"; 483bc03f14fSopenharmony_ci event.expiration = 1; 484bc03f14fSopenharmony_ci event.seqId = 0; 485bc03f14fSopenharmony_ci std::string data = event.Marshall(); 486bc03f14fSopenharmony_ci ClipPlugin::GlobalEvent event1; 487bc03f14fSopenharmony_ci if (!event1.Unmarshall(data)) { 488bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_INNERKIT, "Unmarshall event failed."); 489bc03f14fSopenharmony_ci } 490bc03f14fSopenharmony_ci ASSERT_TRUE(event == event1); 491bc03f14fSopenharmony_ci} 492bc03f14fSopenharmony_ci} // namespace OHOS::DistributedData