1fc223305Sopenharmony_ci/* 2fc223305Sopenharmony_ci* Copyright (c) 2022 Huawei Device Co., Ltd. 3fc223305Sopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License"); 4fc223305Sopenharmony_ci* you may not use this file except in compliance with the License. 5fc223305Sopenharmony_ci* You may obtain a copy of the License at 6fc223305Sopenharmony_ci* 7fc223305Sopenharmony_ci* http://www.apache.org/licenses/LICENSE-2.0 8fc223305Sopenharmony_ci* 9fc223305Sopenharmony_ci* Unless required by applicable law or agreed to in writing, software 10fc223305Sopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS, 11fc223305Sopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fc223305Sopenharmony_ci* See the License for the specific language governing permissions and 13fc223305Sopenharmony_ci* limitations under the License. 14fc223305Sopenharmony_ci*/ 15fc223305Sopenharmony_ci 16fc223305Sopenharmony_ci#include "preferences_xml_utils.h" 17fc223305Sopenharmony_ci 18fc223305Sopenharmony_ci#include <gtest/gtest.h> 19fc223305Sopenharmony_ci 20fc223305Sopenharmony_ci#include <fstream> 21fc223305Sopenharmony_ci#include <string> 22fc223305Sopenharmony_ci 23fc223305Sopenharmony_ci#include "preferences.h" 24fc223305Sopenharmony_ci#include "preferences_errno.h" 25fc223305Sopenharmony_ci#include "preferences_helper.h" 26fc223305Sopenharmony_ci#include "preferences_impl.h" 27fc223305Sopenharmony_ci 28fc223305Sopenharmony_ciusing namespace testing::ext; 29fc223305Sopenharmony_ciusing namespace OHOS::NativePreferences; 30fc223305Sopenharmony_ci 31fc223305Sopenharmony_cinamespace { 32fc223305Sopenharmony_ciclass PreferencesXmlUtilsTest : public testing::Test { 33fc223305Sopenharmony_cipublic: 34fc223305Sopenharmony_ci static void SetUpTestCase(void); 35fc223305Sopenharmony_ci static void TearDownTestCase(void); 36fc223305Sopenharmony_ci void SetUp(); 37fc223305Sopenharmony_ci void TearDown(); 38fc223305Sopenharmony_ci}; 39fc223305Sopenharmony_ci 40fc223305Sopenharmony_civoid PreferencesXmlUtilsTest::SetUpTestCase(void) 41fc223305Sopenharmony_ci{ 42fc223305Sopenharmony_ci} 43fc223305Sopenharmony_ci 44fc223305Sopenharmony_civoid PreferencesXmlUtilsTest::TearDownTestCase(void) 45fc223305Sopenharmony_ci{ 46fc223305Sopenharmony_ci} 47fc223305Sopenharmony_ci 48fc223305Sopenharmony_civoid PreferencesXmlUtilsTest::SetUp(void) 49fc223305Sopenharmony_ci{ 50fc223305Sopenharmony_ci} 51fc223305Sopenharmony_ci 52fc223305Sopenharmony_civoid PreferencesXmlUtilsTest::TearDown(void) 53fc223305Sopenharmony_ci{ 54fc223305Sopenharmony_ci} 55fc223305Sopenharmony_ci 56fc223305Sopenharmony_ci/** 57fc223305Sopenharmony_ci* @tc.name: ReadSettingXmlTest_001 58fc223305Sopenharmony_ci* @tc.desc: normal testcase of ReadSettingXml 59fc223305Sopenharmony_ci* @tc.type: FUNC 60fc223305Sopenharmony_ci*/ 61fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ReadSettingXmlTest_001, TestSize.Level1) 62fc223305Sopenharmony_ci{ 63fc223305Sopenharmony_ci std::vector<Element> settings = {}; 64fc223305Sopenharmony_ci bool ret = PreferencesXmlUtils::ReadSettingXml("", "", "", settings); 65fc223305Sopenharmony_ci EXPECT_EQ(ret, false); 66fc223305Sopenharmony_ci 67fc223305Sopenharmony_ci std::string path = "/data/test/test_helper" + std::string(4096, 't'); 68fc223305Sopenharmony_ci ret = PreferencesXmlUtils::ReadSettingXml(path, "", "", settings); 69fc223305Sopenharmony_ci EXPECT_EQ(ret, false); 70fc223305Sopenharmony_ci 71fc223305Sopenharmony_ci ret = PreferencesXmlUtils::ReadSettingXml("data/test/test_helper", "", "", settings); 72fc223305Sopenharmony_ci EXPECT_EQ(ret, false); 73fc223305Sopenharmony_ci} 74fc223305Sopenharmony_ci 75fc223305Sopenharmony_ci/** 76fc223305Sopenharmony_ci* @tc.name: ReadSettingXmlTest_002 77fc223305Sopenharmony_ci* @tc.desc: ReadSettingXml testcase of PreferencesXmlUtils, reading a corrupt file 78fc223305Sopenharmony_ci* @tc.type: FUNC 79fc223305Sopenharmony_ci*/ 80fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ReadSettingXmlTest_002, TestSize.Level1) 81fc223305Sopenharmony_ci{ 82fc223305Sopenharmony_ci std::string fileName = "/data/test/test01"; 83fc223305Sopenharmony_ci 84fc223305Sopenharmony_ci std::ofstream oss(fileName); 85fc223305Sopenharmony_ci oss << "corrupted"; 86fc223305Sopenharmony_ci 87fc223305Sopenharmony_ci int errCode = E_OK; 88fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(fileName, errCode); 89fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 90fc223305Sopenharmony_ci 91fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(fileName); 92fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 93fc223305Sopenharmony_ci} 94fc223305Sopenharmony_ci 95fc223305Sopenharmony_ci/** 96fc223305Sopenharmony_ci* @tc.name: ReadSettingXmlTest_003 97fc223305Sopenharmony_ci* @tc.desc: ReadSettingXml testcase of PreferencesXmlUtils, no empty dataGroupId 98fc223305Sopenharmony_ci* @tc.type: FUNC 99fc223305Sopenharmony_ci*/ 100fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ReadSettingXmlTest_003, TestSize.Level1) 101fc223305Sopenharmony_ci{ 102fc223305Sopenharmony_ci std::string file = "/data/test/test01"; 103fc223305Sopenharmony_ci 104fc223305Sopenharmony_ci std::vector<Element> settings; 105fc223305Sopenharmony_ci Element elem; 106fc223305Sopenharmony_ci elem.key_ = "testKet"; 107fc223305Sopenharmony_ci elem.tag_ = "int"; 108fc223305Sopenharmony_ci elem.value_ = "999"; 109fc223305Sopenharmony_ci settings.push_back(elem); 110fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "123456", settings); 111fc223305Sopenharmony_ci 112fc223305Sopenharmony_ci std::vector<Element> settingsRes = {}; 113fc223305Sopenharmony_ci bool ret = PreferencesXmlUtils::ReadSettingXml(file, "", "123456", settingsRes); 114fc223305Sopenharmony_ci EXPECT_EQ(ret, true); 115fc223305Sopenharmony_ci EXPECT_EQ(settingsRes.empty(), false); 116fc223305Sopenharmony_ci EXPECT_EQ(elem.key_, settingsRes.front().key_); 117fc223305Sopenharmony_ci EXPECT_EQ(elem.tag_, settingsRes.front().tag_); 118fc223305Sopenharmony_ci EXPECT_EQ(elem.value_, settingsRes.front().value_); 119fc223305Sopenharmony_ci 120fc223305Sopenharmony_ci std::remove(file.c_str()); 121fc223305Sopenharmony_ci} 122fc223305Sopenharmony_ci 123fc223305Sopenharmony_ci/** 124fc223305Sopenharmony_ci* @tc.name: UnnormalReadSettingXml_001 125fc223305Sopenharmony_ci* @tc.desc: unnormal testcase of ReadSettingXml 126fc223305Sopenharmony_ci* @tc.type: FUNC 127fc223305Sopenharmony_ci*/ 128fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, UnnormalReadSettingXml_001, TestSize.Level1) 129fc223305Sopenharmony_ci{ 130fc223305Sopenharmony_ci std::vector<Element> settings = {}; 131fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml("", "", "", settings); 132fc223305Sopenharmony_ci bool ret = PreferencesXmlUtils::ReadSettingXml("", "", "", settings); 133fc223305Sopenharmony_ci EXPECT_EQ(ret, false); 134fc223305Sopenharmony_ci 135fc223305Sopenharmony_ci std::string path = "/data/test/test_helper" + std::string(4096, 't'); 136fc223305Sopenharmony_ci ret = PreferencesXmlUtils::ReadSettingXml(path, "", "", settings); 137fc223305Sopenharmony_ci EXPECT_EQ(ret, false); 138fc223305Sopenharmony_ci 139fc223305Sopenharmony_ci ret = PreferencesXmlUtils::ReadSettingXml("data/test/test_helper", "", "", settings); 140fc223305Sopenharmony_ci EXPECT_EQ(ret, false); 141fc223305Sopenharmony_ci 142fc223305Sopenharmony_ci Element elem; 143fc223305Sopenharmony_ci settings.push_back(elem); 144fc223305Sopenharmony_ci path = "data/test/test_helper"; 145fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(path, "", "", settings); 146fc223305Sopenharmony_ci ret = PreferencesXmlUtils::ReadSettingXml(path, "", "", settings); 147fc223305Sopenharmony_ci EXPECT_EQ(ret, false); 148fc223305Sopenharmony_ci} 149fc223305Sopenharmony_ci 150fc223305Sopenharmony_ci/** 151fc223305Sopenharmony_ci* @tc.name: StringNodeElementTest_001 152fc223305Sopenharmony_ci* @tc.desc: StringNodeElement testcase of PreferencesXmlUtils 153fc223305Sopenharmony_ci* @tc.type: FUNC 154fc223305Sopenharmony_ci*/ 155fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, StringNodeElementTest_001, TestSize.Level1) 156fc223305Sopenharmony_ci{ 157fc223305Sopenharmony_ci std::string file = "/data/test/test01"; 158fc223305Sopenharmony_ci std::remove(file.c_str()); 159fc223305Sopenharmony_ci 160fc223305Sopenharmony_ci std::vector<Element> settings; 161fc223305Sopenharmony_ci Element elem; 162fc223305Sopenharmony_ci elem.key_ = "stringKey"; 163fc223305Sopenharmony_ci elem.tag_ = std::string("string"); 164fc223305Sopenharmony_ci elem.value_ = "test"; 165fc223305Sopenharmony_ci settings.push_back(elem); 166fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "", settings); 167fc223305Sopenharmony_ci 168fc223305Sopenharmony_ci int errCode = E_OK; 169fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode); 170fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 171fc223305Sopenharmony_ci std::string retString = pref->GetString("stringKey", ""); 172fc223305Sopenharmony_ci EXPECT_EQ(retString, elem.value_); 173fc223305Sopenharmony_ci 174fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(file); 175fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 176fc223305Sopenharmony_ci} 177fc223305Sopenharmony_ci 178fc223305Sopenharmony_ci/** 179fc223305Sopenharmony_ci* @tc.name: ArrayNodeElementTest_001 180fc223305Sopenharmony_ci* @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils 181fc223305Sopenharmony_ci* @tc.type: FUNC 182fc223305Sopenharmony_ci*/ 183fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_001, TestSize.Level1) 184fc223305Sopenharmony_ci{ 185fc223305Sopenharmony_ci std::string file = "/data/test/test02"; 186fc223305Sopenharmony_ci std::remove(file.c_str()); 187fc223305Sopenharmony_ci std::vector<Element> settings; 188fc223305Sopenharmony_ci 189fc223305Sopenharmony_ci Element elem; 190fc223305Sopenharmony_ci elem.key_ = "stringArrayKey"; 191fc223305Sopenharmony_ci elem.tag_ = std::string("stringArray"); 192fc223305Sopenharmony_ci elem.value_ = "testStringArray"; 193fc223305Sopenharmony_ci 194fc223305Sopenharmony_ci Element elemChild; 195fc223305Sopenharmony_ci elemChild.key_ = "stringKey"; 196fc223305Sopenharmony_ci elemChild.tag_ = std::string("string"); 197fc223305Sopenharmony_ci 198fc223305Sopenharmony_ci elemChild.value_ = "test_child1"; 199fc223305Sopenharmony_ci elem.children_.push_back(elemChild); 200fc223305Sopenharmony_ci elemChild.value_ = "test_child2"; 201fc223305Sopenharmony_ci elem.children_.push_back(elemChild); 202fc223305Sopenharmony_ci settings.push_back(elem); 203fc223305Sopenharmony_ci std::vector<std::string> inputStringArray = { "test_child1", "test_child2" }; 204fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "", settings); 205fc223305Sopenharmony_ci 206fc223305Sopenharmony_ci int errCode = E_OK; 207fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode); 208fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 209fc223305Sopenharmony_ci 210fc223305Sopenharmony_ci auto retStringArray = pref->Get("stringArrayKey", ""); 211fc223305Sopenharmony_ci EXPECT_EQ(retStringArray.operator std::vector<std::string>(), inputStringArray); 212fc223305Sopenharmony_ci 213fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(file); 214fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 215fc223305Sopenharmony_ci} 216fc223305Sopenharmony_ci 217fc223305Sopenharmony_ci/** 218fc223305Sopenharmony_ci* @tc.name: ArrayNodeElementTest_001 219fc223305Sopenharmony_ci* @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils 220fc223305Sopenharmony_ci* @tc.type: FUNC 221fc223305Sopenharmony_ci*/ 222fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_002, TestSize.Level1) 223fc223305Sopenharmony_ci{ 224fc223305Sopenharmony_ci std::string file = "/data/test/test03"; 225fc223305Sopenharmony_ci std::remove(file.c_str()); 226fc223305Sopenharmony_ci std::vector<Element> settings; 227fc223305Sopenharmony_ci 228fc223305Sopenharmony_ci Element elem; 229fc223305Sopenharmony_ci elem.key_ = "doubleArrayKey"; 230fc223305Sopenharmony_ci elem.tag_ = std::string("doubleArray"); 231fc223305Sopenharmony_ci elem.value_ = std::to_string(10.0); 232fc223305Sopenharmony_ci 233fc223305Sopenharmony_ci Element elemChild; 234fc223305Sopenharmony_ci elemChild.key_ = "doubleKey"; 235fc223305Sopenharmony_ci elemChild.tag_ = std::string("double"); 236fc223305Sopenharmony_ci 237fc223305Sopenharmony_ci elemChild.value_ = std::to_string(1.0); 238fc223305Sopenharmony_ci elem.children_.push_back(elemChild); 239fc223305Sopenharmony_ci 240fc223305Sopenharmony_ci elemChild.value_ = std::to_string(2.0); 241fc223305Sopenharmony_ci elem.children_.push_back(elemChild); 242fc223305Sopenharmony_ci settings.push_back(elem); 243fc223305Sopenharmony_ci std::vector<double> inputDoubleArray = { 1.0, 2.0 }; 244fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "", settings); 245fc223305Sopenharmony_ci 246fc223305Sopenharmony_ci int errCode = E_OK; 247fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode); 248fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 249fc223305Sopenharmony_ci 250fc223305Sopenharmony_ci auto retDoubleArray = pref->Get("doubleArrayKey", 10.0); 251fc223305Sopenharmony_ci EXPECT_EQ(retDoubleArray.operator std::vector<double>(), inputDoubleArray); 252fc223305Sopenharmony_ci 253fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(file); 254fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 255fc223305Sopenharmony_ci} 256fc223305Sopenharmony_ci 257fc223305Sopenharmony_ci/** 258fc223305Sopenharmony_ci* @tc.name: ArrayNodeElementTest_003 259fc223305Sopenharmony_ci* @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils 260fc223305Sopenharmony_ci* @tc.type: FUNC 261fc223305Sopenharmony_ci*/ 262fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_003, TestSize.Level1) 263fc223305Sopenharmony_ci{ 264fc223305Sopenharmony_ci std::string file = "/data/test/test04"; 265fc223305Sopenharmony_ci std::remove(file.c_str()); 266fc223305Sopenharmony_ci std::vector<Element> settings; 267fc223305Sopenharmony_ci 268fc223305Sopenharmony_ci Element elem; 269fc223305Sopenharmony_ci elem.key_ = "boolArrayKey"; 270fc223305Sopenharmony_ci elem.tag_ = std::string("boolArray"); 271fc223305Sopenharmony_ci elem.value_ = std::to_string(false); 272fc223305Sopenharmony_ci 273fc223305Sopenharmony_ci Element elemChild; 274fc223305Sopenharmony_ci elemChild.key_ = "boolKey"; 275fc223305Sopenharmony_ci elemChild.tag_ = std::string("bool"); 276fc223305Sopenharmony_ci 277fc223305Sopenharmony_ci elemChild.value_ = "false"; 278fc223305Sopenharmony_ci elem.children_.push_back(elemChild); 279fc223305Sopenharmony_ci 280fc223305Sopenharmony_ci elemChild.value_ = "true"; 281fc223305Sopenharmony_ci elem.children_.push_back(elemChild); 282fc223305Sopenharmony_ci settings.push_back(elem); 283fc223305Sopenharmony_ci std::vector<bool> inputBoolArray = { false, true }; 284fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "", settings); 285fc223305Sopenharmony_ci 286fc223305Sopenharmony_ci int errCode = E_OK; 287fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode); 288fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 289fc223305Sopenharmony_ci 290fc223305Sopenharmony_ci auto retBoolArray = pref->Get("boolArrayKey", false); 291fc223305Sopenharmony_ci EXPECT_EQ(retBoolArray.operator std::vector<bool>(), inputBoolArray); 292fc223305Sopenharmony_ci 293fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(file); 294fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 295fc223305Sopenharmony_ci} 296fc223305Sopenharmony_ci 297fc223305Sopenharmony_ci/** 298fc223305Sopenharmony_ci* @tc.name: ArrayNodeElementTest_004 299fc223305Sopenharmony_ci* @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils 300fc223305Sopenharmony_ci* @tc.type: FUNC 301fc223305Sopenharmony_ci*/ 302fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_004, TestSize.Level1) 303fc223305Sopenharmony_ci{ 304fc223305Sopenharmony_ci std::string file = "/data/test/test05"; 305fc223305Sopenharmony_ci std::remove(file.c_str()); 306fc223305Sopenharmony_ci std::vector<Element> settings; 307fc223305Sopenharmony_ci 308fc223305Sopenharmony_ci Element elem; 309fc223305Sopenharmony_ci elem.key_ = "boolArrayKey"; 310fc223305Sopenharmony_ci elem.tag_ = std::string("boolArray"); 311fc223305Sopenharmony_ci elem.value_ = std::to_string(false); 312fc223305Sopenharmony_ci 313fc223305Sopenharmony_ci settings.push_back(elem); 314fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "", settings); 315fc223305Sopenharmony_ci 316fc223305Sopenharmony_ci int errCode = E_OK; 317fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode); 318fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 319fc223305Sopenharmony_ci 320fc223305Sopenharmony_ci auto retBoolArray = pref->Get("boolArrayKey", false); 321fc223305Sopenharmony_ci EXPECT_EQ(retBoolArray.IsBoolArray(), true); 322fc223305Sopenharmony_ci auto array = static_cast<std::vector<bool>>(retBoolArray); 323fc223305Sopenharmony_ci EXPECT_EQ(array.empty(), true); 324fc223305Sopenharmony_ci 325fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(file); 326fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 327fc223305Sopenharmony_ci} 328fc223305Sopenharmony_ci 329fc223305Sopenharmony_ci/** 330fc223305Sopenharmony_ci* @tc.name: ArrayNodeElementTest_005 331fc223305Sopenharmony_ci* @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils 332fc223305Sopenharmony_ci* @tc.type: FUNC 333fc223305Sopenharmony_ci*/ 334fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_005, TestSize.Level1) 335fc223305Sopenharmony_ci{ 336fc223305Sopenharmony_ci std::string file = "/data/test/test06"; 337fc223305Sopenharmony_ci std::remove(file.c_str()); 338fc223305Sopenharmony_ci std::vector<Element> settings; 339fc223305Sopenharmony_ci 340fc223305Sopenharmony_ci Element elem; 341fc223305Sopenharmony_ci elem.key_ = "stringArrayKey"; 342fc223305Sopenharmony_ci elem.tag_ = std::string("stringArray"); 343fc223305Sopenharmony_ci elem.value_ = std::to_string(false); 344fc223305Sopenharmony_ci 345fc223305Sopenharmony_ci settings.push_back(elem); 346fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "", settings); 347fc223305Sopenharmony_ci 348fc223305Sopenharmony_ci int errCode = E_OK; 349fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode); 350fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 351fc223305Sopenharmony_ci 352fc223305Sopenharmony_ci auto retStringArray = pref->Get("stringArrayKey", false); 353fc223305Sopenharmony_ci EXPECT_EQ(retStringArray.IsStringArray(), true); 354fc223305Sopenharmony_ci auto array = static_cast<std::vector<std::string>>(retStringArray); 355fc223305Sopenharmony_ci EXPECT_EQ(array.empty(), true); 356fc223305Sopenharmony_ci 357fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(file); 358fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 359fc223305Sopenharmony_ci} 360fc223305Sopenharmony_ci 361fc223305Sopenharmony_ci/** 362fc223305Sopenharmony_ci* @tc.name: ArrayNodeElementTest_006 363fc223305Sopenharmony_ci* @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils 364fc223305Sopenharmony_ci* @tc.type: FUNC 365fc223305Sopenharmony_ci*/ 366fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_006, TestSize.Level1) 367fc223305Sopenharmony_ci{ 368fc223305Sopenharmony_ci std::string file = "/data/test/test07"; 369fc223305Sopenharmony_ci std::remove(file.c_str()); 370fc223305Sopenharmony_ci std::vector<Element> settings; 371fc223305Sopenharmony_ci 372fc223305Sopenharmony_ci Element elem; 373fc223305Sopenharmony_ci elem.key_ = "doubleArrayKey"; 374fc223305Sopenharmony_ci elem.tag_ = std::string("doubleArray"); 375fc223305Sopenharmony_ci elem.value_ = std::to_string(1); 376fc223305Sopenharmony_ci 377fc223305Sopenharmony_ci settings.push_back(elem); 378fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(file, "", "", settings); 379fc223305Sopenharmony_ci 380fc223305Sopenharmony_ci int errCode = E_OK; 381fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode); 382fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 383fc223305Sopenharmony_ci 384fc223305Sopenharmony_ci auto retDoubleArray = pref->Get("doubleArrayKey", 0); 385fc223305Sopenharmony_ci EXPECT_EQ(retDoubleArray.IsDoubleArray(), true); 386fc223305Sopenharmony_ci auto array = static_cast<std::vector<double>>(retDoubleArray); 387fc223305Sopenharmony_ci EXPECT_EQ(array.empty(), true); 388fc223305Sopenharmony_ci 389fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(file); 390fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 391fc223305Sopenharmony_ci} 392fc223305Sopenharmony_ci 393fc223305Sopenharmony_ci/** 394fc223305Sopenharmony_ci* @tc.name: RenameToBrokenFileTest_001 395fc223305Sopenharmony_ci* @tc.desc: RenameToBrokenFile testcase of PreferencesXmlUtils 396fc223305Sopenharmony_ci* @tc.type: FUNC 397fc223305Sopenharmony_ci*/ 398fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, RenameToBrokenFileTest_001, TestSize.Level1) 399fc223305Sopenharmony_ci{ 400fc223305Sopenharmony_ci std::string fileName = "/data/test/test01"; 401fc223305Sopenharmony_ci // construct an unreadable file 402fc223305Sopenharmony_ci std::ofstream oss(fileName); 403fc223305Sopenharmony_ci oss << "corrupted"; 404fc223305Sopenharmony_ci 405fc223305Sopenharmony_ci std::vector<Element> settings; 406fc223305Sopenharmony_ci Element elem; 407fc223305Sopenharmony_ci elem.key_ = "intKey"; 408fc223305Sopenharmony_ci elem.tag_ = "int"; 409fc223305Sopenharmony_ci elem.value_ = "2"; 410fc223305Sopenharmony_ci 411fc223305Sopenharmony_ci settings.push_back(elem); 412fc223305Sopenharmony_ci PreferencesXmlUtils::WriteSettingXml(MakeFilePath(fileName, STR_BACKUP), "", "", settings); 413fc223305Sopenharmony_ci 414fc223305Sopenharmony_ci int errCode = E_OK; 415fc223305Sopenharmony_ci std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(fileName, errCode); 416fc223305Sopenharmony_ci EXPECT_EQ(errCode, E_OK); 417fc223305Sopenharmony_ci 418fc223305Sopenharmony_ci int value = pref->Get("intKey", 0); 419fc223305Sopenharmony_ci EXPECT_EQ(value, 2); 420fc223305Sopenharmony_ci 421fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(fileName); 422fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 423fc223305Sopenharmony_ci} 424fc223305Sopenharmony_ci 425fc223305Sopenharmony_ci/** 426fc223305Sopenharmony_ci* @tc.name: ReadSettingXmlTest_004 427fc223305Sopenharmony_ci* @tc.desc: RenameToBrokenFile testcase of PreferencesXmlUtils 428fc223305Sopenharmony_ci* @tc.type: FUNC 429fc223305Sopenharmony_ci*/ 430fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, ReadSettingXmlTest_004, TestSize.Level1) 431fc223305Sopenharmony_ci{ 432fc223305Sopenharmony_ci std::string fileName = "/data/test/test01"; 433fc223305Sopenharmony_ci // construct an unreadable file 434fc223305Sopenharmony_ci std::ofstream oss(fileName); 435fc223305Sopenharmony_ci oss << "corrupted"; 436fc223305Sopenharmony_ci 437fc223305Sopenharmony_ci std::ofstream ossBak(MakeFilePath(fileName, STR_BACKUP)); 438fc223305Sopenharmony_ci ossBak << "corruptedBak"; 439fc223305Sopenharmony_ci 440fc223305Sopenharmony_ci std::vector<Element> settings; 441fc223305Sopenharmony_ci bool res = PreferencesXmlUtils::ReadSettingXml(fileName, "", "", settings); 442fc223305Sopenharmony_ci EXPECT_EQ(res, false); 443fc223305Sopenharmony_ci 444fc223305Sopenharmony_ci int ret = PreferencesHelper::DeletePreferences(fileName); 445fc223305Sopenharmony_ci EXPECT_EQ(ret, E_OK); 446fc223305Sopenharmony_ci} 447fc223305Sopenharmony_ci 448fc223305Sopenharmony_ci/** 449fc223305Sopenharmony_ci* @tc.name: WriteSettingXmlWhenFileIsNotExistTest_001 450fc223305Sopenharmony_ci* @tc.desc: RenameToBrokenFile testcase of PreferencesXmlUtils 451fc223305Sopenharmony_ci* @tc.type: FUNC 452fc223305Sopenharmony_ci*/ 453fc223305Sopenharmony_ciHWTEST_F(PreferencesXmlUtilsTest, WriteSettingXmlWhenFileIsNotExistTest_001, TestSize.Level1) 454fc223305Sopenharmony_ci{ 455fc223305Sopenharmony_ci std::string fileName = "/data/test/test01"; 456fc223305Sopenharmony_ci std::vector<Element> settings; 457fc223305Sopenharmony_ci Element elem; 458fc223305Sopenharmony_ci elem.key_ = "stringKey"; 459fc223305Sopenharmony_ci elem.tag_ = "string"; 460fc223305Sopenharmony_ci elem.value_ = ""; 461fc223305Sopenharmony_ci 462fc223305Sopenharmony_ci settings.push_back(elem); 463fc223305Sopenharmony_ci bool result = PreferencesXmlUtils::WriteSettingXml("/data/test/preferences/test01", "", "", settings); 464fc223305Sopenharmony_ci EXPECT_EQ(result, false); 465fc223305Sopenharmony_ci 466fc223305Sopenharmony_ci result = PreferencesXmlUtils::WriteSettingXml(fileName, "", "", settings); 467fc223305Sopenharmony_ci EXPECT_EQ(result, true); 468fc223305Sopenharmony_ci} 469fc223305Sopenharmony_ci} 470