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