1 /*
2 * Copyright (c) 2022 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 "preferences_xml_utils.h"
17
18 #include <gtest/gtest.h>
19 #include <string>
20
21 #include "preferences.h"
22 #include "preferences_errno.h"
23 #include "preferences_helper.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::NativePreferences;
27
28 class PreferencesXmlUtilsTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase(void)36 void PreferencesXmlUtilsTest::SetUpTestCase(void)
37 {
38 }
39
TearDownTestCase(void)40 void PreferencesXmlUtilsTest::TearDownTestCase(void)
41 {
42 }
43
SetUp(void)44 void PreferencesXmlUtilsTest::SetUp(void)
45 {
46 }
47
TearDown(void)48 void PreferencesXmlUtilsTest::TearDown(void)
49 {
50 }
51
52 /**
53 * @tc.name: NativePreferencesImplTest_001
54 * @tc.desc: normal testcase of DeletePreferences
55 * @tc.type: FUNC
56 */
57 HWTEST_F(PreferencesXmlUtilsTest, NativePreferencesHelperTest_001, TestSize.Level1)
58 {
59 std::vector<Element> settings = {};
60 bool ret = PreferencesXmlUtils::ReadSettingXml("", settings);
61 EXPECT_EQ(ret, false);
62
63 std::string path = "/data/test/test_helper" + std::string(4096, 't');
64 ret = PreferencesXmlUtils::ReadSettingXml(path, settings);
65 EXPECT_EQ(ret, false);
66
67 ret = PreferencesXmlUtils::ReadSettingXml("data/test/test_helper", settings);
68 EXPECT_EQ(ret, false);
69 }
70
71 /**
72 * @tc.name: StringNodeElementTest_001
73 * @tc.desc: StringNodeElement testcase of PreferencesXmlUtils
74 * @tc.type: FUNC
75 */
76 HWTEST_F(PreferencesXmlUtilsTest, StringNodeElementTest_001, TestSize.Level1)
77 {
78 std::string file = "/data/test/test";
79 std::remove(file.c_str());
80
81 std::vector<Element> settings;
82 Element elem;
83 elem.key_ = "stringKey";
84 elem.tag_ = std::string("string");
85 elem.value_ = "test";
86 settings.push_back(elem);
87 PreferencesXmlUtils::WriteSettingXml(file, settings);
88
89 int errCode = E_OK;
90 std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode);
91 EXPECT_EQ(errCode, E_OK);
92 std::string retString = pref->GetString("stringKey", "");
93 EXPECT_EQ(retString, elem.value_);
94
95 int ret = PreferencesHelper::DeletePreferences(file);
96 EXPECT_EQ(ret, E_OK);
97 }
98
99 /**
100 * @tc.name: ArrayNodeElementTest_001
101 * @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils
102 * @tc.type: FUNC
103 */
104 HWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_001, TestSize.Level1)
105 {
106 std::string file = "/data/test/test";
107 std::remove(file.c_str());
108 std::vector<Element> settings;
109
110 Element elem;
111 elem.key_ = "stringArrayKey";
112 elem.tag_ = std::string("stringArray");
113 elem.value_ = "testStringArray";
114
115 Element elemChild;
116 elemChild.key_ = "stringKey";
117 elemChild.tag_ = std::string("string");
118
119 elemChild.value_ = "test_child1";
120 elem.children_.push_back(elemChild);
121 elemChild.value_ = "test_child2";
122 elem.children_.push_back(elemChild);
123 settings.push_back(elem);
124 std::vector<std::string> inputStringArray = { "test_child1", "test_child2" };
125 PreferencesXmlUtils::WriteSettingXml(file, settings);
126
127 int errCode = E_OK;
128 std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode);
129 EXPECT_EQ(errCode, E_OK);
130
131 auto retStringArray = pref->Get("stringArrayKey", "");
132 EXPECT_EQ(retStringArray.operator std::vector<std::string>(), inputStringArray);
133
134 int ret = PreferencesHelper::DeletePreferences(file);
135 EXPECT_EQ(ret, E_OK);
136 }