1 /*
2 * Copyright (c) 2021-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 "config_policy_utils_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "config_policy_utils.h"
21 #include "config_policy_impl.h"
22 #ifndef __LITEOS__
23 #include "init_param.h"
24
25 constexpr const char *CUST_OPKEY0 = "telephony.sim.opkey0";
26 constexpr const char *CUST_OPKEY1 = "telephony.sim.opkey1";
27 #endif
28
29 using namespace testing::ext;
30
31 namespace OHOS {
32 class ConfigPolicyUtilsTest : public testing::Test {
33 public:
34 static void SetUpTestCase(void);
35 };
36
37 #ifndef __LITEOS__
SetUpTestCase(void)38 void ConfigPolicyUtilsTest::SetUpTestCase(void)
39 {
40 SystemSetParameter(CUST_OPKEY0, "46060");
41 SystemSetParameter(CUST_OPKEY1, "46061");
42 SystemSetParameter(CUST_FOLLOW_X_RULES,
43 ":etc/custxmltest/user.xml,10:etc/custxmltest/both.xml,100,etc/carrier/${test:-46061}");
44 }
45 #endif
46
TestGetCfgFile(const char * testPathSuffix,int type,const char * extra)47 bool TestGetCfgFile(const char *testPathSuffix, int type, const char *extra)
48 {
49 CfgFiles *cfgFiles = GetCfgFilesEx(testPathSuffix, type, extra);
50 EXPECT_TRUE(cfgFiles != NULL);
51 bool flag = false;
52 char *filePath = nullptr;
53 for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) {
54 filePath = cfgFiles->paths[i];
55 if (filePath && *filePath != '\0') {
56 std::cout << "type: " << type << ", filePath: " << filePath << std::endl;
57 flag = true;
58 }
59 }
60 FreeCfgFiles(cfgFiles);
61 char buf[MAX_PATH_LEN] = {0};
62 filePath = GetOneCfgFileEx(testPathSuffix, buf, MAX_PATH_LEN, type, extra);
63 if (filePath && *filePath != '\0') {
64 std::cout << "type: " << type << ", one filePath: " << filePath << std::endl;
65 flag = flag && true;
66 }
67 return flag;
68 }
69
70 /**
71 * @tc.name: CfgPolicyUtilsFuncTest001
72 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, none file case.
73 * @tc.type: FUNC
74 * @tc.require: issueI5NYDH
75 */
76 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest001, TestSize.Level1)
77 {
78 const char *testPathSuffix = "etc/custxmltest/none.xml";
79 EXPECT_FALSE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
80 }
81
82 /**
83 * @tc.name: CfgPolicyUtilsFuncTest002
84 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, system file case.
85 * @tc.type: FUNC
86 * @tc.require: issueI5NYDH
87 */
88 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest002, TestSize.Level1)
89 {
90 const char *testPathSuffix = "etc/custxmltest/system.xml";
91 EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
92 }
93
94 /**
95 * @tc.name: CfgPolicyUtilsFuncTest003
96 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, user file case.
97 * @tc.type: FUNC
98 * @tc.require: issueI5NYDH
99 */
100 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest003, TestSize.Level1)
101 {
102 const char *testPathSuffix = "etc/custxmltest/user.xml";
103 EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
104 }
105
106 /**
107 * @tc.name: CfgPolicyUtilsFuncTest004
108 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, both files case.
109 * @tc.type: FUNC
110 * @tc.require: issueI5NYDH
111 */
112 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest004, TestSize.Level1)
113 {
114 const char *testPathSuffix = "etc/custxmltest/both.xml";
115 EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
116 }
117
118 /**
119 * @tc.name: CfgPolicyUtilsFuncTest005
120 * @tc.desc: Test struct CfgDir *GetCfgDirList(void) function.
121 * @tc.type: FUNC
122 * @tc.require: issueI5NYDH
123 */
124 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest005, TestSize.Level1)
125 {
126 CfgDir *cfgDir = GetCfgDirList();
127 EXPECT_TRUE(cfgDir != NULL);
128 bool flag = false;
129 for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) {
130 char *filePath = cfgDir->paths[i];
131 if (filePath && *filePath != '\0') {
132 std::cout << "filePath: " << filePath << std::endl;
133 flag = true;
134 }
135 }
136 FreeCfgDirList(cfgDir);
137 EXPECT_TRUE(flag);
138 }
139
140 #ifndef __LITEOS__
141 /**
142 * @tc.name: CfgPolicyUtilsFuncTest006
143 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow default sim rule.
144 * @tc.type: FUNC
145 * @tc.require: issueI5NYDH
146 */
147 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest006, TestSize.Level1)
148 {
149 const char *testPathSuffix = "etc/custxmltest/user.xml";
150 EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_DEFAULT, NULL));
151 }
152
153 /**
154 * @tc.name: CfgPolicyUtilsFuncTest007
155 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow sim1 rule.
156 * @tc.type: FUNC
157 * @tc.require: issueI5NYDH
158 */
159 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest007, TestSize.Level1)
160 {
161 const char *testPathSuffix = "etc/custxmltest/user.xml";
162 EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_1, NULL));
163 }
164
165 /**
166 * @tc.name: CfgPolicyUtilsFuncTest008
167 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow sim1 rule.
168 * @tc.type: FUNC
169 * @tc.require: issueI5NYDH
170 */
171 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest008, TestSize.Level1)
172 {
173 const char *testPathSuffix = "etc/custxmltest/user.xml";
174 EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_2, NULL));
175 }
176
177 /**
178 * @tc.name: CfgPolicyUtilsFuncTest009
179 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow default x rule rule.
180 * @tc.type: FUNC
181 * @tc.require: issueI5NYDH
182 */
183 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest009, TestSize.Level1)
184 {
185 EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/user.xml", FOLLOWX_MODE_DEFAULT, NULL));
186 EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/both.xml", FOLLOWX_MODE_DEFAULT, NULL));
187 }
188
189 /**
190 * @tc.name: CfgPolicyUtilsFuncTest010
191 * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow default x rule rule.
192 * @tc.type: FUNC
193 * @tc.require: issueI5NYDH
194 */
195 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest010, TestSize.Level1)
196 {
197 std::string extraString;
198 extraString.append("etc/carrier/${").append(CUST_OPKEY1).append(":-46060},etc/carrier/${")
199 .append(CUST_OPKEY0).append("}/etc/${testkey:-custxmltest}");
200 std::cout << "extra: " << extraString << std::endl;
201 EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/user.xml", FOLLOWX_MODE_USER_DEFINED, extraString.c_str()));
202 EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/both.xml", FOLLOWX_MODE_USER_DEFINED, extraString.c_str()));
203 }
204 #endif // __LITEOS__
205 } // namespace OHOS
206