1 /*
2  * Copyright (c) 2020-2021 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 <cstdio>
17 #include "gtest/gtest.h"
18 #include "parameter.h"
19 #include "sysparam_errno.h"
20 using namespace std;
21 using namespace testing::ext;
22 
23 namespace StartUpLite {
24     static const int GET_DEF_PARA_FUN_MAX = 22;
25     static const int MAX_LEN = 96;
26     static const int WRONG_LEN = 2;
27 
28 class ParameterTest : public testing::Test {
29 protected:
SetUpTestCase(void)30     static void SetUpTestCase(void)
31     {
32         mkdir("/storage", S_IRUSR | S_IWUSR);
33         mkdir("/storage/data", S_IRUSR | S_IWUSR);
34         mkdir("/storage/data/system", S_IRUSR | S_IWUSR);
35         mkdir("/storage/data/system/param", S_IRUSR | S_IWUSR);
36     }
TearDownTestCase(void)37     static void TearDownTestCase(void) {}
SetUp()38     virtual void SetUp() {}
TearDown()39     virtual void TearDown() {}
40     string defSysParam = "data of sys param ***...";
41     using GetRdonlyPara = const char* (*)();
42     using GetDefParaNode = struct TagGetDefParaNode {
43         char const *funName;
44         GetRdonlyPara fun;
45     };
46     GetDefParaNode getDefPara[StartUpLite::GET_DEF_PARA_FUN_MAX] = {
47         {"GetDeviceType", GetDeviceType},
48         {"GetManufacture", GetManufacture},
49         {"GetBrand", GetBrand},
50         {"GetMarketName", GetMarketName},
51         {"GetProductSeries", GetProductSeries},
52         {"GetProductModel", GetProductModel},
53         {"GetSoftwareModel", GetSoftwareModel},
54         {"GetHardwareModel", GetHardwareModel},
55         {"GetHardwareProfile", GetHardwareProfile},
56         {"GetOSFullName", GetOSFullName},
57         {"GetDisplayVersion", GetDisplayVersion},
58         {"GetBootloaderVersion", GetBootloaderVersion},
59         {"GetSecurityPatchTag", GetSecurityPatchTag},
60         {"GetAbiList", GetAbiList},
61         {"GetIncrementalVersion", GetIncrementalVersion},
62         {"GetVersionId", GetVersionId},
63         {"GetBuildType", GetBuildType},
64         {"GetBuildUser", GetBuildUser},
65         {"GetBuildHost", GetBuildHost},
66         {"GetBuildTime", GetBuildTime},
67         {"GetBuildRootHash", GetBuildRootHash},
68         {"GetSerial", GetSerial},
69     };
70 };
71 
72 /**
73  * @tc.number    : SUB_START_Para_Setting_Legal_0010
74  * @tc.name      : SetParameter legal test with Lowercase alphanumeric, underscore, dot
75  * @tc.desc      : [C- SOFTWARE -0200]
76  */
HWTEST_F(ParameterTest, SUB_START_Para_Setting_Legal_0010, Function | MediumTest | Level0)77 HWTEST_F(ParameterTest, SUB_START_Para_Setting_Legal_0010, Function | MediumTest | Level0)
78 {
79     int ret;
80 
81     char key[] = "rw.sys.version";
82     char value[] = "OEM-10.1.0";
83     ret = SetParameter(key, value);
84     EXPECT_EQ(ret, 0);
85 }
86 
87 /**
88  * @tc.number    : SUB_START_Para_Setting_Legal_0020
89  * @tc.name      : SetParameter legal test with key 31 bytes, value 127 bytes
90  * @tc.desc      : [C- SOFTWARE -0200]
91  */
HWTEST_F(ParameterTest, SUB_START_Para_Setting_Legal_0020, Function | MediumTest | Level0)92 HWTEST_F(ParameterTest, SUB_START_Para_Setting_Legal_0020, Function | MediumTest | Level0)
93 {
94     int ret;
95 
96     char key1[] = "rw.sys.version.version.version";
97     char value1[] = "set with key = 31";
98     ret = SetParameter(key1, value1);
99     EXPECT_EQ(ret, 0);
100 
101     char key2[] = "rw.sys.version.version";
102     char value2[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuv";
103     ret = SetParameter(key2, value2);
104     EXPECT_EQ(ret, 0);
105 }
106 
107 /**
108  * @tc.number    : SUB_START_Para_Setting_ilLegal_0010
109  * @tc.name      : SetParameter legal test with key is nullptr, value is nullptr
110  * @tc.desc      : [C- SOFTWARE -0200]
111  */
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_0010, Function | MediumTest | Level2)112 HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_0010, Function | MediumTest | Level2)
113 {
114     int ret;
115 
116     char value[] = "test with null";
117     ret = SetParameter(nullptr, value);
118     EXPECT_EQ(ret, -9);
119 
120     char key[] = "rw.sys.version";
121     ret = SetParameter(key, nullptr);
122     EXPECT_EQ(ret, -9);
123 }
124 
125 /**
126  * @tc.number    : SUB_START_Para_Setting_ilLegal_0020
127  * @tc.name      : SetParameter legal test with key is NULL, value is NULL
128  * @tc.desc      : [C- SOFTWARE -0200]
129  */
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_0020, Function | MediumTest | Level2)130 HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_0020, Function | MediumTest | Level2)
131 {
132     int ret = SetParameter("\0", "\0");
133     EXPECT_EQ(ret, -9);
134 }
135 
136 /**
137  * @tc.number    : SUB_START_Para_Setting_ilLegal_key_0010
138  * @tc.name      : SetParameter legal test with key 96 or more than 96 bytes
139  * @tc.desc      : [C- SOFTWARE -0200]
140  */
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_key_0010, Function | MediumTest | Level2)141 HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_key_0010, Function | MediumTest | Level2)
142 {
143     int ret;
144 
145     char key1[] = "rw.sys.version.version.version.version.version.version.version.version.version.version.version.v";
146     char value1[] = "set with key = 96";
147     ret = SetParameter(key1, value1);
148     EXPECT_EQ(ret, -9);
149 
150     char key2[] = "rw.sys.version.version.version.version.version.version.version.version.version.version.version.v1";
151     char value2[] = "set with key > 96";
152     ret = SetParameter(key2, value2);
153     EXPECT_EQ(ret, -9);
154 }
155 
156 /**
157  * @tc.number    : SUB_START_Para_Setting_ilLegal_key_0030
158  * @tc.name      : SetParameter legal test with illegal characters
159  * @tc.desc      : [C- SOFTWARE -0200]
160  */
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_key_0030, Function | MediumTest | Level2)161 HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_key_0030, Function | MediumTest | Level2)
162 {
163     int ret;
164 
165     char key[] = "rw.sys.version*%version";
166     char value[] = "set value with illegal key";
167     ret = SetParameter(key, value);
168     EXPECT_EQ(ret, -9);
169 }
170 
171 /**
172  * @tc.number    : SUB_START_Para_Setting_ilLegal_value_0010
173  * @tc.name      : SetParameter legal test with value is 96 or more than 96 bytes
174  * @tc.desc      : [C- SOFTWARE -0200]
175  */
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_value_0010, Function | MediumTest | Level2)176 HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_value_0010, Function | MediumTest | Level2)
177 {
178     int ret;
179 
180     char key1[] = "rw.sys.version.version";
181     char value1[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuv11";
182     ret = SetParameter(key1, value1);
183     EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
184 
185     char key2[] = "rw.sys.version.version";
186     char value2[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuv222";
187     ret = SetParameter(key2, value2);
188     EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
189 }
190 
191 /**
192  * @tc.number    : SUB_START_Para_Getting_Legal_0010
193  * @tc.name      : GetParameter legal test with Lowercase alphanumeric, underscore, dot
194  * @tc.desc      : [C- SOFTWARE -0200]
195  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0010, Function | MediumTest | Level0)196 HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0010, Function | MediumTest | Level0)
197 {
198     int ret;
199 
200     char key[] = "rw.sys.version";
201     char rightVal[] = "OEM-10.1.0";
202     char value[StartUpLite::MAX_LEN] = {0};
203     ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
204     EXPECT_EQ(ret, (int)strlen(rightVal));
205     value[MAX_LEN - 1] = '\0';
206     EXPECT_STREQ(value, rightVal);
207 }
208 
209 /**
210  * @tc.number    : SUB_START_Para_Getting_Legal_0020
211  * @tc.name      : GetParameter legal test with defaut value point is nullptr
212  * @tc.desc      : [C- SOFTWARE -0200]
213  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0020, Function | MediumTest | Level0)214 HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0020, Function | MediumTest | Level0)
215 {
216     int ret;
217 
218     char key[] = "rw.sys.version";
219     char rightVal[] = "OEM-10.1.0";
220     char value[StartUpLite::MAX_LEN] = {0};
221     ret = GetParameter(key, nullptr, value, StartUpLite::MAX_LEN);
222     EXPECT_EQ(ret, (int)strlen(rightVal));
223     value[MAX_LEN - 1] = '\0';
224     EXPECT_STREQ(value, rightVal);
225 }
226 
227 /**
228  * @tc.number    : SUB_START_Para_Getting_Legal_0030
229  * @tc.name      : GetParameter legal test with length is 95 bytes, value is 95 bytes
230  * @tc.desc      : [C- SOFTWARE -0200]
231  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0030, Function | MediumTest | Level0)232 HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0030, Function | MediumTest | Level0)
233 {
234     int ret;
235 
236     char key1[] = "rw.sys.version.version.version";
237     char rightVal1[] = "set with key = 31";
238     char value1[StartUpLite::MAX_LEN] = {0};
239     ret = GetParameter(key1, defSysParam.c_str(), value1, StartUpLite::MAX_LEN);
240     EXPECT_EQ(ret, (int)strlen(rightVal1));
241     value1[MAX_LEN - 1] = '\0';
242     EXPECT_STREQ(value1, rightVal1);
243 
244     char key2[] = "rw.sys.version.version";
245     char rightVal2[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuv";
246     char value2[StartUpLite::MAX_LEN] = {0};
247     ret = GetParameter(key2, defSysParam.c_str(), value2, StartUpLite::MAX_LEN);
248     value2[MAX_LEN - 1] = '\0';
249     EXPECT_EQ(ret, (int)strlen(rightVal2));
250     EXPECT_STREQ(value2, rightVal2);
251 }
252 
253 /**
254  * @tc.number    : SUB_START_Para_Getting_ilLegal_0010
255  * @tc.name      : GetParameter legal test with value length is too short
256  * @tc.desc      : [C- SOFTWARE -0200]
257  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0010, Function | MediumTest | Level2)258 HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0010, Function | MediumTest | Level2)
259 {
260     int ret;
261 
262     char key[] = "rw.sys.version";
263     char value[StartUpLite::WRONG_LEN] = {0};
264     ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::WRONG_LEN);
265     EXPECT_EQ(ret, -9);
266 }
267 
268 /**
269  * @tc.number    : SUB_START_Para_Getting_ilLegal_0020
270  * @tc.name      : GetParameter legal test with value point is nullptr
271  * @tc.desc      : [C- SOFTWARE -0200]
272  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0020, Function | MediumTest | Level2)273 HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0020, Function | MediumTest | Level2)
274 {
275     int ret;
276 
277     char key[] = "rw.sys.version";
278     ret = GetParameter(key, defSysParam.c_str(), nullptr, StartUpLite::MAX_LEN);
279     EXPECT_EQ(ret, -9);
280 }
281 
282 /**
283  * @tc.number    : SUB_START_Para_Getting_ilLegal_0030
284  * @tc.name      : GetParameter legal test with key is not exist and vlan len is too short
285  * @tc.desc      : [C- SOFTWARE -0200]
286  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0030, Function | MediumTest | Level2)287 HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0030, Function | MediumTest | Level2)
288 {
289     int ret;
290 
291     char key1[] = "rw.product.not.exist";
292     char value1[StartUpLite::MAX_LEN] = {0};
293     ret = GetParameter(key1, defSysParam.c_str(), value1, StartUpLite::MAX_LEN);
294     EXPECT_EQ(ret, (int)strlen(defSysParam.c_str()));
295     value1[MAX_LEN - 1] = '\0';
296     EXPECT_STREQ(value1, defSysParam.c_str());
297 
298     char value2[StartUpLite::WRONG_LEN] = {0};
299     ret = GetParameter(key1, defSysParam.c_str(), value2, StartUpLite::WRONG_LEN);
300     EXPECT_EQ(ret, -9);
301 }
302 
303 /**
304  * @tc.number    : SUB_START_Para_Getting_ilLegal_0040
305  * @tc.name      : GetParameter legal test with key is 96 bytes
306  * @tc.desc      : [C- SOFTWARE -0200]
307  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0040, Function | MediumTest | Level2)308 HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0040, Function | MediumTest | Level2)
309 {
310     int ret;
311 
312     char key[] = "rw.sys.version.version.version.v";
313     char value[StartUpLite::MAX_LEN] = {0};
314     ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
315     EXPECT_STREQ(value, defSysParam.c_str());
316 }
317 
318 /**
319  * @tc.number    : SUB_START_Para_Getting_ilLegal_0050
320  * @tc.name      : GetParameter legal test with key is nullptr
321  * @tc.desc      : [C- SOFTWARE -0200]
322  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0050, Function | MediumTest | Level2)323 HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0050, Function | MediumTest | Level2)
324 {
325     int ret;
326 
327     char value[StartUpLite::MAX_LEN] = {0};
328     ret = GetParameter(nullptr, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
329     EXPECT_EQ(ret, -9);
330 }
331 
332 /**
333  * @tc.number    : SUB_START_Para_Getting_ilLegal_0060
334  * @tc.name      : GetParameter legal test with key is illegal with Special characters
335  * @tc.desc      : [C- SOFTWARE -0200]
336  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0060, Function | MediumTest | Level2)337 HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0060, Function | MediumTest | Level2)
338 {
339     int ret;
340 
341     char key[] = "rw.sys.version*%version";
342     char value[StartUpLite::MAX_LEN] = {0};
343     ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
344     EXPECT_STREQ(value, defSysParam.c_str());
345 }
346 
347 /**
348  * @tc.number    : SUB_START_Para_Getting_ReadOnly_0010
349  * @tc.name      : GetParameter read only parameter legal test
350  * @tc.desc      : [C- SOFTWARE -0200]
351  */
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ReadOnly_0010, Function | MediumTest | Level0)352 HWTEST_F(ParameterTest, SUB_START_Para_Getting_ReadOnly_0010, Function | MediumTest | Level0)
353 {
354     const char *value = nullptr;
355 
356     for (int loop = 0; loop < StartUpLite::GET_DEF_PARA_FUN_MAX; loop++) {
357         value = getDefPara[loop].fun();
358         EXPECT_STRNE(value, (char *)nullptr);
359     }
360     EXPECT_GT(GetFirstApiVersion(), 0);
361     EXPECT_GT(GetSdkApiVersion(), 0);
362 }
363 }