• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: NativePreferencesImplTest_002
73 * @tc.desc: normal testcase of DeletePreferences
74 * @tc.type: FUNC
75 */
76 HWTEST_F(PreferencesXmlUtilsTest, NativePreferencesHelperTest_002, TestSize.Level1)
77 {
78     std::vector<Element> settings = {};
79     bool ret = PreferencesXmlUtils::ReadSettingXml("", settings);
80     EXPECT_EQ(ret, false);
81 
82     std::string path = "/data/test/test_helper" + std::string(4096, 't');
83     ret = PreferencesXmlUtils::ReadSettingXml(path, settings);
84     EXPECT_EQ(ret, false);
85 
86     ret = PreferencesXmlUtils::ReadSettingXml("data/test/test_helper", settings);
87     EXPECT_EQ(ret, false);
88 }
89 
90 /**
91 * @tc.name: StringNodeElementTest_001
92 * @tc.desc: StringNodeElement testcase of PreferencesXmlUtils
93 * @tc.type: FUNC
94 */
95 HWTEST_F(PreferencesXmlUtilsTest, StringNodeElementTest_001, TestSize.Level1)
96 {
97     std::string file = "/data/test/test";
98     std::remove(file.c_str());
99 
100     std::vector<Element> settings;
101     Element elem;
102     elem.key_ = "stringKey";
103     elem.tag_ = std::string("string");
104     elem.value_ = "test";
105     settings.push_back(elem);
106     PreferencesXmlUtils::WriteSettingXml(file, settings);
107 
108     int errCode = E_OK;
109     std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode);
110     EXPECT_EQ(errCode, E_OK);
111     std::string retString = pref->GetString("stringKey", "");
112     EXPECT_EQ(retString, elem.value_);
113 
114     int ret = PreferencesHelper::DeletePreferences(file);
115     EXPECT_EQ(ret, E_OK);
116 }
117 
118 /**
119 * @tc.name: ArrayNodeElementTest_001
120 * @tc.desc: ArrayNodeElement testcase of PreferencesXmlUtils
121 * @tc.type: FUNC
122 */
123 HWTEST_F(PreferencesXmlUtilsTest, ArrayNodeElementTest_001, TestSize.Level1)
124 {
125     std::string file = "/data/test/test";
126     std::remove(file.c_str());
127     std::vector<Element> settings;
128 
129     Element elem;
130     elem.key_ = "stringArrayKey";
131     elem.tag_ = std::string("stringArray");
132     elem.value_ = "testStringArray";
133 
134     Element elemChild;
135     elemChild.key_ = "stringKey";
136     elemChild.tag_ = std::string("string");
137 
138     elemChild.value_ = "test_child1";
139     elem.children_.push_back(elemChild);
140     elemChild.value_ = "test_child2";
141     elem.children_.push_back(elemChild);
142     settings.push_back(elem);
143     std::vector<std::string> inputStringArray = { "test_child1", "test_child2" };
144     PreferencesXmlUtils::WriteSettingXml(file, settings);
145 
146     int errCode = E_OK;
147     std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences(file, errCode);
148     EXPECT_EQ(errCode, E_OK);
149 
150     auto retStringArray = pref->Get("stringArrayKey", "");
151     EXPECT_EQ(retStringArray.operator std::vector<std::string>(), inputStringArray);
152 
153     int ret = PreferencesHelper::DeletePreferences(file);
154     EXPECT_EQ(ret, E_OK);
155 }