1 /*
2  * Copyright (c) 2021-2024 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 }
43 #endif
44 
TestGetCfgFile(const char * testPathSuffix,int type,const char * extra)45 bool TestGetCfgFile(const char *testPathSuffix, int type, const char *extra)
46 {
47     CfgFiles *cfgFiles = GetCfgFilesEx(testPathSuffix, type, extra);
48     EXPECT_TRUE(cfgFiles != NULL);
49     bool flag = false;
50     char *filePath = nullptr;
51     for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) {
52         filePath = cfgFiles->paths[i];
53         if (filePath && *filePath != '\0') {
54             std::cout << "type: " << type << ", filePath: " << filePath << std::endl;
55             flag = true;
56         }
57     }
58     FreeCfgFiles(cfgFiles);
59     char buf[MAX_PATH_LEN] = {0};
60     filePath = GetOneCfgFileEx(testPathSuffix, buf, MAX_PATH_LEN, type, extra);
61     if (filePath && *filePath != '\0') {
62         std::cout << "type: " << type << ", one filePath: " << filePath << std::endl;
63         flag = flag && true;
64     }
65     return flag;
66 }
67 
68 /**
69  * @tc.name: CfgPolicyUtilsFuncTest001
70  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, none file case.
71  * @tc.type: FUNC
72  * @tc.require: issueI5NYDH
73  */
74 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest001, TestSize.Level1)
75 {
76     const char *testPathSuffix = "etc/custxmltest/none.xml";
77     EXPECT_FALSE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
78 }
79 
80 /**
81  * @tc.name: CfgPolicyUtilsFuncTest002
82  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, system file case.
83  * @tc.type: FUNC
84  * @tc.require: issueI5NYDH
85  */
86 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest002, TestSize.Level1)
87 {
88     const char *testPathSuffix = "etc/custxmltest/system.xml";
89     EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
90 }
91 
92 /**
93  * @tc.name: CfgPolicyUtilsFuncTest003
94  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, user file case.
95  * @tc.type: FUNC
96  * @tc.require: issueI5NYDH
97  */
98 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest003, TestSize.Level1)
99 {
100     const char *testPathSuffix = "etc/custxmltest/user.xml";
101     EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
102 }
103 
104 /**
105  * @tc.name: CfgPolicyUtilsFuncTest004
106  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, both files case.
107  * @tc.type: FUNC
108  * @tc.require: issueI5NYDH
109  */
110 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest004, TestSize.Level1)
111 {
112     const char *testPathSuffix = "etc/custxmltest/both.xml";
113     EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL));
114 }
115 
116 /**
117  * @tc.name: CfgPolicyUtilsFuncTest005
118  * @tc.desc: Test struct CfgDir *GetCfgDirList(void) function.
119  * @tc.type: FUNC
120  * @tc.require: issueI5NYDH
121  */
122 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest005, TestSize.Level1)
123 {
124     CfgDir *cfgDir = GetCfgDirList();
125     EXPECT_TRUE(cfgDir != NULL);
126     bool flag = false;
127     for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) {
128         char *filePath = cfgDir->paths[i];
129         if (filePath && *filePath != '\0') {
130             std::cout << "filePath: " << filePath << std::endl;
131             flag = true;
132         }
133     }
134     FreeCfgDirList(cfgDir);
135     EXPECT_TRUE(flag);
136 }
137 
138 #ifndef __LITEOS__
139 /**
140  * @tc.name: CfgPolicyUtilsFuncTest006
141  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow default sim rule.
142  * @tc.type: FUNC
143  * @tc.require: issueI5NYDH
144  */
145 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest006, TestSize.Level1)
146 {
147     SystemSetParameter(CUST_OPKEY0, "-1");
148     const char *testPathSuffix = "etc/custxmltest/user.xml";
149     EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_DEFAULT, NULL));
150 }
151 
152 /**
153  * @tc.name: CfgPolicyUtilsFuncTest007
154  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow sim1 rule.
155  * @tc.type: FUNC
156  * @tc.require: issueI5NYDH
157  */
158 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest007, TestSize.Level1)
159 {
160     const char *testPathSuffix = "etc/custxmltest/user.xml";
161     EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_1, NULL));
162 }
163 
164 /**
165  * @tc.name: CfgPolicyUtilsFuncTest008
166  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow sim1 rule.
167  * @tc.type: FUNC
168  * @tc.require: issueI5NYDH
169  */
170 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest008, TestSize.Level1)
171 {
172     const char *testPathSuffix = "etc/custxmltest/user.xml";
173     EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_2, NULL));
174 }
175 
176 /**
177  * @tc.name: CfgPolicyUtilsFuncTest009
178  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow default x rule rule.
179  * @tc.type: FUNC
180  * @tc.require: issueI5NYDH
181  */
182 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest009, TestSize.Level1)
183 {
184     int ret = SystemSetParameter(CUST_FOLLOW_X_RULES,
185         ":etc/custxmltest/user.xml,10:etc/custxmltest/both.xml,100,etc/carrier/${test:-46061}");
186     if (ret == 0) {
187         std::cout << "set " << CUST_FOLLOW_X_RULES << " success." << std::endl;
188         EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/user.xml", FOLLOWX_MODE_DEFAULT, NULL));
189         EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/both.xml", FOLLOWX_MODE_DEFAULT, NULL));
190     } else {
191         std::cout << "set " << CUST_FOLLOW_X_RULES << " failed, use OpkeyInfo.json for test." << std::endl;
192         EXPECT_TRUE(TestGetCfgFile("etc/telephony/OpkeyInfo.json", FOLLOWX_MODE_DEFAULT, NULL));
193     }
194 }
195 
196 /**
197  * @tc.name: CfgPolicyUtilsFuncTest010
198  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, follow default x rule rule.
199  * @tc.type: FUNC
200  * @tc.require: issueI5NYDH
201  */
202 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest010, TestSize.Level1)
203 {
204     std::string extraString;
205     extraString.append("etc/carrier/${").append(CUST_OPKEY1).append(":-46060},etc/carrier/${")
206         .append(CUST_OPKEY0).append("}/etc/${testkey:-custxmltest}");
207     std::cout << "extra: " << extraString << std::endl;
208     EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/user.xml", FOLLOWX_MODE_USER_DEFINED, extraString.c_str()));
209     EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/both.xml", FOLLOWX_MODE_USER_DEFINED, extraString.c_str()));
210 }
211 
212 /**
213  * @tc.name: CfgPolicyUtilsFuncTest011
214  * @tc.desc: Test GetOneCfgFile & GetCfgFiles function, with nullptr input
215  * @tc.type: FUNC
216  * @tc.require: issueI5NYDH
217  */
218 HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest011, TestSize.Level1)
219 {
220     char buf[MAX_PATH_LEN] = {0};
221     char *filePath = nullptr;
222     filePath = GetOneCfgFileEx(nullptr, buf, MAX_PATH_LEN, FOLLOWX_MODE_DEFAULT, nullptr);
223     EXPECT_TRUE(filePath == nullptr);
224     filePath = GetOneCfgFileEx("etc/custxmltest/user.xml", nullptr, MAX_PATH_LEN, FOLLOWX_MODE_DEFAULT, nullptr);
225     EXPECT_TRUE(filePath == nullptr);
226     filePath = GetOneCfgFileEx("etc/custxmltest/user.xml", buf, MAX_PATH_LEN - 1, FOLLOWX_MODE_DEFAULT, nullptr);
227     EXPECT_TRUE(filePath == nullptr);
228     CfgFiles *cfgFiles = GetCfgFilesEx(nullptr, FOLLOWX_MODE_DEFAULT, nullptr);
229     EXPECT_TRUE(cfgFiles == nullptr);
230     FreeCfgFiles(cfgFiles);
231     EXPECT_TRUE(cfgFiles == nullptr);
232     CfgDir *res = nullptr;
233     FreeCfgDirList(res);
234     EXPECT_TRUE(res == nullptr);
235 }
236 #endif // __LITEOS__
237 } // namespace OHOS
238