1 /*
2 * Copyright (c) 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 <thread>
17 #include <gtest/gtest.h>
18 #include <cstdint>
19 #include "param_wrapper.h"
20 #include "parameter.h"
21 #include "parameters.h"
22 #include "sysversion.h"
23 #include "param_comm.h"
24 #include "init_param.h"
25 #include "sysparam_errno.h"
26 #include "sys_param.h"
27
28 using namespace std;
29 using namespace testing::ext;
30 using namespace OHOS;
31
32 const int THREAD_NUM = 5;
33
34 namespace initModuleTest {
GetAllParameterTestFunc(void)35 static void GetAllParameterTestFunc(void)
36 {
37 EXPECT_STRNE(GetSecurityPatchTag(), nullptr);
38 EXPECT_STRNE(GetOSFullName(), nullptr);
39 EXPECT_STRNE(GetVersionId(), nullptr);
40 EXPECT_STRNE(GetBuildRootHash(), nullptr);
41 EXPECT_STRNE(GetOsReleaseType(), nullptr);
42 GetSdkApiVersion();
43 EXPECT_STRNE(GetDeviceType(), nullptr);
44 EXPECT_STRNE(GetProductModel(), nullptr);
45 EXPECT_STRNE(GetManufacture(), nullptr);
46 EXPECT_STRNE(GetBrand(), nullptr);
47 EXPECT_STRNE(GetMarketName(), nullptr);
48 EXPECT_STRNE(GetProductSeries(), nullptr);
49 EXPECT_STRNE(GetSoftwareModel(), nullptr);
50 EXPECT_STRNE(GetHardwareModel(), nullptr);
51 EXPECT_STRNE(GetHardwareProfile(), nullptr);
52 EXPECT_STRNE(GetSerial(), nullptr);
53 EXPECT_STRNE(GetAbiList(), nullptr);
54 EXPECT_STRNE(GetDisplayVersion(), nullptr);
55 EXPECT_STRNE(GetIncrementalVersion(), nullptr);
56 EXPECT_STRNE(GetBootloaderVersion(), nullptr);
57 EXPECT_STRNE(GetBuildType(), nullptr);
58 EXPECT_STRNE(GetBuildUser(), nullptr);
59 EXPECT_STRNE(GetBuildHost(), nullptr);
60 EXPECT_STRNE(GetBuildTime(), nullptr);
61 GetFirstApiVersion();
62 EXPECT_STRNE(system::GetDeviceType().c_str(), nullptr);
63 }
64
GetUdidTestFunc(char * udid,int size)65 static void GetUdidTestFunc(char* udid, int size)
66 {
67 int ret = GetDevUdid(udid, size);
68 EXPECT_EQ(ret, 0);
69 EXPECT_STRNE(udid, nullptr);
70 }
71
SetParameterTestFunc(const char * key,const char * value)72 static void SetParameterTestFunc(const char *key, const char *value)
73 {
74 EXPECT_EQ(SetParameter(key, value), 0);
75 uint32_t handle = FindParameter(key);
76 EXPECT_NE(handle, static_cast<unsigned int>(-1));
77 uint32_t result = GetParameterCommitId(handle);
78 EXPECT_NE(result, static_cast<unsigned int>(-1));
79 char nameGet[PARAM_NAME_LEN_MAX] = {0};
80 int ret = GetParameterName(handle, nameGet, PARAM_NAME_LEN_MAX);
81 EXPECT_EQ(ret, strlen(nameGet));
82 EXPECT_STREQ(key, nameGet);
83 char valueGet[PARAM_VALUE_LEN_MAX] = {0};
84 ret = GetParameterValue(handle, valueGet, PARAM_VALUE_LEN_MAX);
85 EXPECT_EQ(ret, strlen(valueGet));
86 EXPECT_STREQ(value, valueGet);
87 EXPECT_NE(GetSystemCommitId(), 0);
88 }
89
GetParameterTestReInt(const char * key,const char * def,char * value,uint32_t len)90 static void GetParameterTestReInt(const char *key, const char *def, char *value, uint32_t len)
91 {
92 int ret = GetParameter(key, def, value, len);
93 EXPECT_EQ(ret, strlen(value));
94 EXPECT_STREQ(value, "v10.1.1");
95 }
96
GetParameterTestFuncReStr(string key,string def)97 static void GetParameterTestFuncReStr(string key, string def)
98 {
99 string ret = system::GetParameter(key, def);
100 EXPECT_STREQ(ret.c_str(), "v10.1.1");
101 }
102
ParamSetFun(string key,string value)103 static void ParamSetFun(string key, string value)
104 {
105 bool ret = system::SetParameter(key, value);
106 EXPECT_TRUE(ret);
107 string testValue = system::GetParameter(key, "");
108 EXPECT_STREQ(testValue.c_str(), value.c_str());
109 }
110
TestParameterChange(const char * key,const char * value,void * context)111 static void TestParameterChange(const char *key, const char *value, void *context)
112 {
113 std::cout<<"TestParameterChange key: "<<key<<"value: "<<value<<endl;
114 }
115
TestParameterWatchChange(void)116 static void TestParameterWatchChange(void)
117 {
118 size_t index = 1;
119 int ret = WatchParameter("test.param.watcher.test1", TestParameterChange, reinterpret_cast<void *>(index));
120 EXPECT_EQ(ret, 0);
121 ret = RemoveParameterWatcher("test.param.watcher.test1", TestParameterChange, reinterpret_cast<void *>(index));
122 EXPECT_EQ(ret, 0);
123 }
124
125 class SysparaModuleTest : public testing::Test {
126 public:
SetUpTestCase(void)127 static void SetUpTestCase(void) {};
TearDownTestCase(void)128 static void TearDownTestCase(void) {};
SetUp(void)129 void SetUp(void) {};
TearDown(void)130 void TearDown(void) {};
131 };
132
133 HWTEST_F(SysparaModuleTest, Syspara_SysVersion_test_001, TestSize.Level0)
134 {
135 GTEST_LOG_(INFO) << "Syspara_SysVersion_test_001 start";
136 GetMajorVersion();
137 GetSeniorVersion();
138 GetFeatureVersion();
139 GetBuildVersion();
140 GTEST_LOG_(INFO) << "Syspara_SysVersion_test_001 end";
141 }
142
143 HWTEST_F(SysparaModuleTest, Syspara_GetParam_test_002, TestSize.Level0)
144 {
145 GTEST_LOG_(INFO) << "Syspara_GetParam_test_002 start";
146 for (int i = 0; i < THREAD_NUM; ++i) {
147 std::thread(GetAllParameterTestFunc).join();
148 }
149 GTEST_LOG_(INFO) << "Syspara_GetParam_test_002 end";
150 }
151
152 HWTEST_F(SysparaModuleTest, Syspara_GetUdid_test_003, TestSize.Level0)
153 {
154 GTEST_LOG_(INFO) << "Syspara_GetUdid_test_003 start";
155 for (int i = 0; i < THREAD_NUM; ++i) {
156 char udid[UDID_LEN] = {0};
157 std::thread(GetUdidTestFunc, udid, UDID_LEN).join();
158 }
159 GTEST_LOG_(INFO) << "Syspara_GetUdid_test_003 end";
160 }
161
162 HWTEST_F(SysparaModuleTest, Syspara_SetParameter_test_004, TestSize.Level0)
163 {
164 GTEST_LOG_(INFO) << "Syspara_SetParameter_test_004 start";
165 char key1[] = "test1.param.version";
166 char value1[] = "v10.1.1";
167 char key2[] = "test2.param.version";
168 char value2[] = "v10.2.2";
169 char key3[] = "test3.param.version";
170 char value3[] = "v10.3.3";
171 std::thread(SetParameterTestFunc, key1, value1).join();
172 std::thread(SetParameterTestFunc, key2, value2).join();
173 std::thread(SetParameterTestFunc, key3, value3).join();
174 GTEST_LOG_(INFO) << "Syspara_SetParameter_test_004 end";
175 }
176
177 HWTEST_F(SysparaModuleTest, Syspara_SetParameter_test_005, TestSize.Level0)
178 {
179 GTEST_LOG_(INFO) << "Syspara_SetParameter_test_005 start";
180 // check param name length
181 char key1[] = "test.param.name.xxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxx";
182 char value[] = "test.value.xxxx";
183 int ret = SetParameter(key1, value);
184 EXPECT_EQ(ret, EC_INVALID);
185 // check param name, Illegal param name
186 char key2[] = ".test.param.name.xxxxx";
187 ret = SetParameter(key2, value);
188 EXPECT_EQ(ret, EC_INVALID);
189 char key3[] = "test..param.name.xxxxx";
190 ret = SetParameter(key3, value);
191 EXPECT_EQ(ret, EC_INVALID);
192 char key4[] = "test..param. .name";
193 ret = SetParameter(key4, value);
194 EXPECT_EQ(ret, EC_INVALID);
195 // check param name, legal param name
196 char key5[] = "test.param.name.--__.:::";
197 ret = SetParameter(key5, value);
198 EXPECT_EQ(ret, 0);
199 EXPECT_STREQ(value, "test.value.xxxx");
200 char key6[] = "test.param.name.@@@.---";
201 ret = SetParameter(key6, value);
202 EXPECT_EQ(ret, 0);
203 EXPECT_STREQ(value, "test.value.xxxx");
204 // not const param, check param value, bool 8, int 32, other 96
205 char key7[] = "test.param.name.xxxx";
206 char value1[] = "test.value.xxxxxxxxx.xxxxxxxxxxxxx.xxxxxxxxxxxx.xxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxx";
207 ret = SetParameter(key7, value1);
208 EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
209 char key8[] = "startup.service.ctl.test.int";
210 char value2[] = "111111111111111111111111111111111";
211 ret = SetParameter(key8, value2);
212 EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
213 GTEST_LOG_(INFO) << "Syspara_SetParameter_test_005 end";
214 }
215
216 HWTEST_F(SysparaModuleTest, Syspara_Getparameter_test_006, TestSize.Level0)
217 {
218 GTEST_LOG_(INFO) << "Syspara_Getparameter_test_006 start";
219 string key = "test.param.set.001";
220 string value = "xxx.xxx.xxx";
221 bool ret = system::SetParameter(key, value);
222 EXPECT_TRUE(ret);
223 string testValue = system::GetParameter(key, "");
224 EXPECT_STREQ(testValue.c_str(), value.c_str());
225 // not read param value,the result is default
226 testValue = system::GetParameter("test.param.set.002", "aaa.aaa.aaa");
227 EXPECT_STREQ(testValue.c_str(), "aaa.aaa.aaa");
228 testValue = system::GetParameter("test.param.set.003", "");
229 EXPECT_STREQ(testValue.c_str(), "");
230 // correct set value
231 string key1 = "test.param.set.bool";
232 ret = system::SetParameter(key1, "1");
233 EXPECT_TRUE(ret);
234 EXPECT_TRUE(system::GetBoolParameter(key1, false));
235 ret = system::SetParameter(key1, "y");
236 EXPECT_TRUE(ret);
237 EXPECT_TRUE(system::GetBoolParameter(key1, false));
238 ret = system::SetParameter(key1, "yes");
239 EXPECT_TRUE(ret);
240 EXPECT_TRUE(system::GetBoolParameter(key1, false));
241 ret = system::SetParameter(key1, "on");
242 EXPECT_TRUE(ret);
243 EXPECT_TRUE(system::GetBoolParameter(key1, false));
244 ret = system::SetParameter(key1, "true");
245 EXPECT_TRUE(ret);
246 EXPECT_TRUE(system::GetBoolParameter(key1, false));
247 ret = system::SetParameter(key1, "0");
248 EXPECT_TRUE(ret);
249 EXPECT_FALSE(system::GetBoolParameter(key1, true));
250 ret = system::SetParameter(key1, "off");
251 EXPECT_TRUE(ret);
252 EXPECT_FALSE(system::GetBoolParameter(key1, true));
253 ret = system::SetParameter(key1, "n");
254 EXPECT_TRUE(ret);
255 EXPECT_FALSE(system::GetBoolParameter(key1, true));
256 ret = system::SetParameter(key1, "no");
257 EXPECT_TRUE(ret);
258 EXPECT_FALSE(system::GetBoolParameter(key1, true));
259 ret = system::SetParameter(key1, "false");
260 EXPECT_TRUE(ret);
261 EXPECT_FALSE(system::GetBoolParameter(key1, true));
262 // set value type not bool,the result get form def
263 ret = system::SetParameter(key1, "test");
264 EXPECT_TRUE(ret);
265 EXPECT_TRUE(system::GetBoolParameter(key1, true));
266 EXPECT_FALSE(system::GetBoolParameter(key1, false));
267 GTEST_LOG_(INFO) << "Syspara_Getparameter_test_006 end";
268 }
269
270 HWTEST_F(SysparaModuleTest, Syspara_SetParameter_test_007, TestSize.Level0)
271 {
272 GTEST_LOG_(INFO) << "Syspara_SetParameter_test_007 start";
273 string key1 = "const.param.test";
274 string value1 = "test.param.value.001";
275
276 int ret = 0;
277 string vRet = "";
278 uint32_t handle1 = FindParameter(key1.c_str());
279 cout<<"handle1 is: "<<handle1<<std::endl;
280 if (handle1 == static_cast<uint32_t>(-1)) {
281 ret = SetParameter(key1.c_str(), value1.c_str());
282 EXPECT_EQ(ret, 0);
283 vRet = system::GetParameter(key1, "");
284 EXPECT_STREQ(vRet.c_str(), value1.c_str());
285 }
286 string value2 = "test.param.value.002";
287 ret = SetParameter(key1.c_str(), value2.c_str());
288 EXPECT_EQ(ret, EC_INVALID);
289
290 string key2 = "ro.param.test";
291 string value3 = "test.param.value.003";
292 uint32_t handle2 = FindParameter(key2.c_str());
293 cout<<"handle2 is: "<<handle2<<std::endl;
294 if (handle2 == static_cast<uint32_t>(-1)) {
295 ret = SetParameter(key2.c_str(), value3.c_str());
296 EXPECT_EQ(ret, 0);
297 vRet = system::GetParameter(key2, "");
298 EXPECT_STREQ(vRet.c_str(), value3.c_str());
299 }
300 string value4 = "test.param.value.004";
301 ret = SetParameter(key2.c_str(), value4.c_str());
302 EXPECT_EQ(ret, EC_INVALID);
303 GTEST_LOG_(INFO) << "Syspara_SetParameter_test_007 end";
304 }
305
306 HWTEST_F(SysparaModuleTest, Syspara_GetParameterReIntOrStr_test_008, TestSize.Level0)
307 {
308 GTEST_LOG_(INFO) << "Syspara_GetParameterReIntOrStr_test_008 start";
309 string key = "test1.param.version";
310 string value = "v10.1.1";
311 int ret = SetParameter(key.c_str(), value.c_str());
312 EXPECT_EQ(ret, 0);
313 char retValue[PARAM_VALUE_LEN_MAX] = {0};
314 for (int i = 0; i < THREAD_NUM; ++i) {
315 std::thread(GetParameterTestReInt, key.c_str(), "", retValue, PARAM_VALUE_LEN_MAX).join();
316 }
317 for (int j = 0; j < THREAD_NUM; ++j) {
318 std::thread(GetParameterTestFuncReStr, key, "").join();
319 }
320 GTEST_LOG_(INFO) << "Syspara_GetParameterReIntOrStr_test_008 end";
321 }
322
323 HWTEST_F(SysparaModuleTest, Syspara_WaitParameter_test_009, TestSize.Level0)
324 {
325 GTEST_LOG_(INFO) << "Syspara_WaitParameter_test_009 start";
326 // param already set succeed,waitParamter succeed.
327 char key[] = "test1.param.wait";
328 char value[] = "aaa.bbb.ccc";
329 int ret = SetParameter(key, value);
330 EXPECT_EQ(ret, 0);
331 ret = WaitParameter(key, value, 5);
332 EXPECT_EQ(ret, 0);
333 // param not set,waitParamter will wait param set,return succeed.
334 char key1[] = "test2.param.wait";
335 char value1[] = "aaa.aaa.aaa";
336 std::thread(ParamSetFun, key1, value1).join();
337 ret = WaitParameter(key1, value1, 5);
338 EXPECT_EQ(ret, 0);
339 char key2[] = "test3.param.wait";
340 std::thread(ParamSetFun, key2, "*****").join();
341 ret = WaitParameter(key2, "*****", 5);
342 EXPECT_EQ(ret, 0);
343 // param not set,waitParamter will timeout,return failed.
344 char key3[] = "test4.param.wait";
345 ret = WaitParameter(key3, "*****", 5);
346 EXPECT_EQ(ret, SYSPARAM_WAIT_TIMEOUT);
347 GTEST_LOG_(INFO) << "Syspara_WaitParameter_test_009 end";
348 }
349
350 HWTEST_F(SysparaModuleTest, Syspara_watcherParameter_test_010, TestSize.Level0)
351 {
352 GTEST_LOG_(INFO) << "Syspara_watcherParameter_test_010 start";
353 for (int i = 0; i < THREAD_NUM; ++i) {
354 std::thread(TestParameterWatchChange).join();
355 }
356
357 std::thread(ParamSetFun, "test.param.watcher.test1", "test.param.value.xxx").join();
358
359 GTEST_LOG_(INFO) << "Syspara_watcherParameter_test_010 end";
360 }
361
362 HWTEST_F(SysparaModuleTest, Syspara_GetParameter_test_011, TestSize.Level0)
363 {
364 GTEST_LOG_(INFO) << "Syspara_GetParameter_test_011 start";
365
366 char key1[] = "test.param.int1";
367 char value1[] = "0x111111";
368 EXPECT_EQ(SetParameter(key1, value1), 0);
369 EXPECT_EQ(GetIntParameter(key1, 0), 1118481); // 0x111111 decimalism result
370 EXPECT_EQ(GetUintParameter(key1, 0), 1118481);
371
372 char key2[] = "test.param.int2";
373 char value2[] = "-0x111111";
374 EXPECT_EQ(SetParameter(key2, value2), 0);
375 EXPECT_EQ(GetIntParameter(key2, 0), -1118481); // 0x111111 decimalism result
376
377 GetUintParameter(key2, 0);
378
379 char key3[] = "test.param.int3";
380 char value3[] = "9999999";
381 EXPECT_EQ(SetParameter(key3, value3), 0);
382 EXPECT_EQ(GetIntParameter(key3, 0), 9999999); // value3 int result
383 EXPECT_EQ(GetUintParameter(key3, 0), 9999999); // value3 uint result
384
385 char key4[] = "test.param.int4";
386 char value4[] = "-9999999";
387 EXPECT_EQ(SetParameter(key4, value4), 0);
388 EXPECT_EQ(GetIntParameter(key4, 0), -9999999); // value4 int result
389 EXPECT_EQ(GetUintParameter(key4, 0), 0);
390
391 char key5[] = "test.param.int5";
392 char value5[] = "-2147483648"; // INT32_MIN
393 EXPECT_EQ(SetParameter(key5, value5), 0);
394 EXPECT_EQ(GetIntParameter(key5, 0), 0);
395
396 char key6[] = "test.param.int6";
397 char value6[] = "2147483647"; // INT32_MAX
398 EXPECT_EQ(SetParameter(key6, value6), 0);
399 EXPECT_EQ(GetIntParameter(key6, 0), 0);
400
401 char key7[] = "test.param.uint7";
402 char value7[] = "4294967295"; // UINT32_MAX
403 EXPECT_EQ(SetParameter(key7, value7), 0);
404 EXPECT_EQ(GetUintParameter(key7, 0), 0);
405
406 GTEST_LOG_(INFO) << "Syspara_GetParameter_test_011 end";
407 }
408
409 HWTEST_F(SysparaModuleTest, Syspara_CacheParameter_test_001, TestSize.Level0)
410 {
411 GTEST_LOG_(INFO) << "Syspara_CacheParameter_test_001 start";
412
413 const char *name = "test.write.1111111.222222";
414 CachedHandle cacheHandle = CachedParameterCreate(name, "true");
415 EXPECT_NE(cacheHandle, nullptr);
416 const char *value = CachedParameterGet(cacheHandle);
417 int ret = SetParameter(name, "false");
418 EXPECT_EQ(ret, 0);
419
420 value = CachedParameterGet(cacheHandle);
421 EXPECT_EQ(strcmp(value, "false"), 0);
422 CachedParameterDestroy(cacheHandle);
423
424 GTEST_LOG_(INFO) << "Syspara_CacheParameter_test_001 end";
425 }
426
427 HWTEST_F(SysparaModuleTest, Syspara_CacheParameter_test_002, TestSize.Level0)
428 {
429 GTEST_LOG_(INFO) << "Syspara_CacheParameter_test_002 start";
430
431 const char *name = "test.write.1111111.333333";
432 int ret = SetParameter(name, "3333");
433 EXPECT_EQ(ret, 0);
434 CachedHandle cacheHandle3 = CachedParameterCreate(name, "true");
435 EXPECT_NE(cacheHandle3, nullptr);
436 const char *value = CachedParameterGet(cacheHandle3);
437 EXPECT_EQ(strcmp(value, "3333"), 0);
438
439 ret = SetParameter(name, "2222222");
440 EXPECT_EQ(ret, 0);
441 int valueChange = 0;
442 value = CachedParameterGetChanged(cacheHandle3, &valueChange);
443 EXPECT_EQ(strcmp(value, "2222222"), 0);
444 EXPECT_EQ(valueChange, 1);
445 CachedParameterDestroy(cacheHandle3);
446 GTEST_LOG_(INFO) << "Syspara_CacheParameter_test_002 end";
447 }
448 }