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