1 /* 2 * Copyright (c) 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 "gtest/gtest.h" 17 18 #include "init_param.h" 19 #include "init_utils.h" 20 #include "parameter.h" 21 #include "param_comm.h" 22 #include "param_stub.h" 23 #ifndef OHOS_LITE 24 #include "param_wrapper.h" 25 #include "parameters.h" 26 #endif 27 #include "sysversion.h" 28 #include "sysparam_errno.h" 29 30 using namespace testing::ext; 31 32 namespace OHOS { 33 constexpr int TEST_VALUE = 101; 34 class SysparaUnitTest : public testing::Test { 35 public: SetUpTestCase()36 static void SetUpTestCase() {} TearDownTestCase()37 static void TearDownTestCase() {} SetUp()38 void SetUp() 39 { 40 SetTestPermissionResult(0); 41 } TearDown()42 void TearDown() {} 43 }; 44 45 HWTEST_F(SysparaUnitTest, parameterTest001, TestSize.Level0) 46 { 47 printf("Device type =%s\n", GetDeviceType()); 48 printf("Manufacture =%s\n", GetManufacture()); 49 printf("GetBrand =%s\n", GetBrand()); 50 printf("MarketName =%s\n", GetMarketName()); 51 printf("ProductSeries =%s\n", GetProductSeries()); 52 printf("ProductModel =%s\n", GetProductModel()); 53 printf("SoftwareModel =%s\n", GetSoftwareModel()); 54 printf("HardwareModel =%s\n", GetHardwareModel()); 55 printf("Software profile =%s\n", GetHardwareProfile()); 56 printf("Serial =%s\n", GetSerial()); 57 printf("OS full name =%s\n", GetOSFullName()); 58 printf("OS Release type =%s\n", GetOsReleaseType()); 59 printf("Display version =%s\n", GetDisplayVersion()); 60 printf("bootloader version =%s\n", GetBootloaderVersion()); 61 printf("secure patch level =%s\n", GetSecurityPatchTag()); 62 } 63 64 HWTEST_F(SysparaUnitTest, parameterTest001_1, TestSize.Level0) 65 { 66 printf("secure patch level =%s\n", GetSecurityPatchTag()); 67 printf("abi list =%s\n", GetAbiList()); 68 printf("first api version =%d\n", GetFirstApiVersion()); 69 printf("SDK api version =%d\n", GetSdkApiVersion()); 70 printf("Incremental version = %s\n", GetIncrementalVersion()); 71 printf("formal id =%s\n", GetVersionId()); 72 printf("build type =%s\n", GetBuildType()); 73 printf("build user =%s\n", GetBuildUser()); 74 printf("Build host = %s\n", GetBuildHost()); 75 printf("build time =%s\n", GetBuildTime()); 76 printf("build root later..., %s\n", GetBuildRootHash()); 77 } 78 79 HWTEST_F(SysparaUnitTest, parameterTest001_2, TestSize.Level0) 80 { 81 EXPECT_STRNE(GetDeviceType(), nullptr); 82 EXPECT_STRNE(GetManufacture(), nullptr); 83 EXPECT_STRNE(GetBrand(), nullptr); 84 EXPECT_STRNE(GetMarketName(), nullptr); 85 EXPECT_STRNE(GetProductSeries(), nullptr); 86 EXPECT_STRNE(GetProductModel(), nullptr); 87 EXPECT_STRNE(GetSoftwareModel(), nullptr); 88 EXPECT_STRNE(GetHardwareModel(), nullptr); 89 EXPECT_STRNE(GetHardwareProfile(), nullptr); 90 EXPECT_STRNE(GetOSFullName(), nullptr); 91 EXPECT_STRNE(GetOsReleaseType(), nullptr); 92 EXPECT_STRNE(GetDisplayVersion(), nullptr); 93 EXPECT_STRNE(GetBootloaderVersion(), nullptr); 94 EXPECT_STRNE(GetSecurityPatchTag(), nullptr); 95 } 96 97 HWTEST_F(SysparaUnitTest, parameterTest001_3, TestSize.Level0) 98 { 99 EXPECT_STRNE(GetSecurityPatchTag(), nullptr); 100 EXPECT_STRNE(GetAbiList(), nullptr); 101 EXPECT_GT(GetFirstApiVersion(), 0); 102 EXPECT_GT(GetSdkApiVersion(), 0); 103 EXPECT_STRNE(GetIncrementalVersion(), nullptr); 104 EXPECT_STRNE(GetVersionId(), nullptr); 105 EXPECT_STRNE(GetBuildType(), nullptr); 106 EXPECT_STRNE(GetBuildUser(), nullptr); 107 EXPECT_STRNE(GetBuildHost(), nullptr); 108 EXPECT_STRNE(GetBuildTime(), nullptr); 109 EXPECT_STRNE(GetBuildRootHash(), nullptr); 110 } 111 112 HWTEST_F(SysparaUnitTest, parameterTest002, TestSize.Level0) 113 { 114 char key1[] = "test.ro.sys.version"; 115 char value1[] = "set read only key"; 116 int ret = SetParameter(key1, value1); 117 EXPECT_EQ(ret, EC_SUCCESS); 118 ret = SetParameter(nullptr, nullptr); 119 EXPECT_EQ(ret, EC_INVALID); 120 } 121 122 HWTEST_F(SysparaUnitTest, parameterTest003, TestSize.Level0) 123 { 124 char key2[] = "rw.sys.version*%version"; 125 char value2[] = "set value with illegal key"; 126 int ret = SetParameter(key2, value2); 127 EXPECT_EQ(ret, EC_INVALID); 128 } 129 130 /* key = 32 */ 131 HWTEST_F(SysparaUnitTest, parameterTest004, TestSize.Level0) 132 { 133 char key3[] = "rw.sys.version.utilskvparameter0"; 134 char value3[] = "set with key = 32"; 135 int ret = SetParameter(key3, value3); 136 EXPECT_EQ(ret, EC_SUCCESS); 137 } 138 139 /* value > 128 */ 140 HWTEST_F(SysparaUnitTest, parameterTest005, TestSize.Level0) 141 { 142 char key4[] = "rw.sys.version.version"; 143 char value4[] = "rw.sys.version.version.version.version flash_offset = *(hi_u32 *)DT_SetGetU32(&g_Element[0], 0)a\ 144 size = *(hi_u32 *)DT_SetGetU32(&g_Element[1], 0)a"; 145 int ret = SetParameter(key4, value4); 146 EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE); 147 } 148 149 HWTEST_F(SysparaUnitTest, parameterTest006, TestSize.Level0) 150 { 151 char key1[] = "rw.product.not.exist"; 152 char value1[64] = {0}; 153 char defValue1[] = "value of key not exist..."; 154 int ret = GetParameter(key1, defValue1, value1, 64); 155 EXPECT_EQ(ret, static_cast<int>(strlen(defValue1))); 156 } 157 158 HWTEST_F(SysparaUnitTest, parameterTest007, TestSize.Level0) 159 { 160 char key2[] = "rw.sys.version.version.version.version"; 161 char value2[64] = {0}; 162 char defValue2[] = "value of key > 32 ..."; 163 int ret = GetParameter(key2, defValue2, value2, 64); 164 EXPECT_EQ(ret, static_cast<int>(strlen(defValue2))); 165 } 166 167 HWTEST_F(SysparaUnitTest, parameterTest008, TestSize.Level0) 168 { 169 char key4[] = "test.rw.sys.version"; 170 char* value4 = nullptr; 171 char defValue3[] = "value of key > 32 ..."; 172 int ret = GetParameter(key4, defValue3, value4, 0); 173 EXPECT_EQ(ret, EC_INVALID); 174 } 175 176 HWTEST_F(SysparaUnitTest, parameterTest009, TestSize.Level0) 177 { 178 char key5[] = "test.rw.product.type.2222222"; 179 char value5[] = "rw.sys.version.version.version.version \ 180 flash_offset = *(hi_u32 *)DT_SetGetU32(&g_Element[0], 0)"; 181 int ret = SetParameter(key5, value5); 182 EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE); 183 char valueGet[2] = {0}; 184 char defValue3[] = "value of key > 32 ..."; 185 ret = GetParameter(key5, defValue3, valueGet, 2); 186 EXPECT_EQ(ret, EC_INVALID); 187 } 188 189 HWTEST_F(SysparaUnitTest, parameterTest0010, TestSize.Level0) 190 { 191 char key1[] = "test.rw.sys.version"; 192 char value1[] = "10.1.0"; 193 int ret = SetParameter(key1, value1); 194 EXPECT_EQ(ret, 0); 195 ret = SystemWriteParam(key1, value1); 196 EXPECT_EQ(ret, 0); 197 char valueGet1[32] = {0}; 198 ret = GetParameter(key1, "version=10.1.0", valueGet1, 32); 199 EXPECT_EQ(ret, static_cast<int>(strlen(valueGet1))); 200 201 char key2[] = "test.rw.product.type"; 202 char value2[] = "wifi_iot"; 203 ret = SetParameter(key2, value2); 204 EXPECT_EQ(ret, 0); 205 ret = SystemWriteParam(key2, value2); 206 EXPECT_EQ(ret, 0); 207 char valueGet2[32] = {0}; 208 ret = GetParameter(key2, "version=10.1.0", valueGet2, 32); 209 EXPECT_EQ(ret, static_cast<int>(strlen(valueGet2))); 210 211 char key3[] = "test.rw.product.manufacturer"; 212 char value3[] = "TEST MANUFACTURER"; 213 ret = SetParameter(key3, value3); 214 EXPECT_EQ(ret, 0); 215 ret = SystemWriteParam(key3, value3); 216 EXPECT_EQ(ret, 0); 217 char valueGet3[32] = {0}; 218 ret = GetParameter(key3, "version=10.1.0", valueGet3, 32); 219 EXPECT_EQ(ret, static_cast<int>(strlen(valueGet3))); 220 221 char key4[] = "test.rw.product.marketname"; 222 char value4[] = "TEST MARKETNAME"; 223 ret = SetParameter(key4, value4); 224 EXPECT_EQ(ret, 0); 225 ret = SystemWriteParam(key4, value4); 226 EXPECT_EQ(ret, 0); 227 char valueGet4[32] = {0}; 228 ret = GetParameter(key4, "version=10.1.0", valueGet4, 32); 229 EXPECT_EQ(ret, static_cast<int>(strlen(valueGet4))); 230 } 231 232 HWTEST_F(SysparaUnitTest, parameterTest0011, TestSize.Level0) 233 { 234 char key1[] = "test.rw.sys.version.wait1"; 235 char value1[] = "10.1.0"; 236 int ret = SetParameter(key1, value1); 237 EXPECT_EQ(ret, 0); 238 ret = SystemWriteParam(key1, value1); 239 EXPECT_EQ(ret, 0); 240 ret = WaitParameter(key1, value1, 10); 241 EXPECT_EQ(ret, 0); 242 ret = WaitParameter(key1, "*", 10); 243 EXPECT_EQ(ret, 0); 244 char key2[] = "test.rw.sys.version.wait2"; 245 ret = WaitParameter(key2, "*", 1); 246 EXPECT_EQ(ret, SYSPARAM_WAIT_TIMEOUT); 247 } 248 249 HWTEST_F(SysparaUnitTest, parameterTest0012, TestSize.Level0) 250 { 251 char key1[] = "test.rw.sys.version.version1"; 252 char value1[] = "10.1.0"; 253 int ret = SetParameter(key1, value1); 254 EXPECT_EQ(ret, 0); 255 256 ret = SystemWriteParam(key1, value1); 257 EXPECT_EQ(ret, 0); 258 // success 259 unsigned int handle = FindParameter(key1); 260 EXPECT_NE(handle, static_cast<unsigned int>(-1)); 261 char valueGet1[32] = {0}; 262 ret = GetParameterValue(handle, valueGet1, 32); 263 EXPECT_EQ(ret, static_cast<int>(strlen(valueGet1))); 264 char nameGet1[32] = {0}; 265 ret = GetParameterName(handle, nameGet1, 32); 266 EXPECT_EQ(ret, static_cast<int>(strlen(nameGet1))); 267 268 // fail 269 char key2[] = "test.rw.sys.version.version2"; 270 handle = FindParameter(key2); 271 EXPECT_EQ(handle, static_cast<unsigned int>(-1)); 272 ret = GetParameterValue(handle, valueGet1, 32); 273 EXPECT_EQ(ret, SYSPARAM_NOT_FOUND); 274 ret = GetParameterName(handle, nameGet1, 32); 275 EXPECT_EQ(ret, SYSPARAM_NOT_FOUND); 276 ret = GetParameterValue(handle, nullptr, 32); 277 EXPECT_EQ(ret, EC_INVALID); 278 ret = GetParameterCommitId(handle); 279 EXPECT_EQ(ret, -1); 280 } 281 282 HWTEST_F(SysparaUnitTest, parameterTest0013, TestSize.Level0) 283 { 284 long long int out = 0; 285 unsigned long long int uout = 0; 286 GetParameter_(nullptr, nullptr, nullptr, 0); 287 EXPECT_EQ(GetIntParameter("test.int.get", 0) == -TEST_VALUE, 1); 288 EXPECT_EQ(GetUintParameter("test.int.get", 0), 0); 289 EXPECT_EQ(GetIntParameter("test.uint.get", 0), TEST_VALUE); 290 EXPECT_EQ(GetUintParameter("test.uint.get", 0), TEST_VALUE); 291 EXPECT_EQ(GetIntParameter("test.int.default", 10), 10); // key not find,value = default 292 EXPECT_EQ(GetUintParameter("test.uint.default", 10), 10); // key not find,value = default 293 EXPECT_EQ(IsValidParamValue(nullptr, 0), 0); 294 EXPECT_EQ(IsValidParamValue("testvalue", strlen("testvalue") + 1), 1); 295 EXPECT_EQ(StringToLL("0x11", &out), 0); 296 EXPECT_EQ(StringToULL("0x11", &uout), 0); 297 EXPECT_EQ(StringToLL("not vailed", &out), -1); 298 EXPECT_EQ(StringToULL("not vailed", &uout), -1); 299 char udid[UDID_LEN] = {0}; 300 GetDevUdid(udid, UDID_LEN); 301 EXPECT_NE(GetMajorVersion(), 0); 302 int ret = GetDevUdid(nullptr, UDID_LEN); 303 EXPECT_EQ(ret, EC_FAILURE); 304 GetSeniorVersion(); 305 GetFeatureVersion(); 306 GetBuildVersion(); 307 } 308 309 #ifndef OHOS_LITE 310 // for test param_wrapper.cpp 311 HWTEST_F(SysparaUnitTest, parameterTest0014, TestSize.Level0) 312 { 313 const std::string key1 = "test.int.get"; 314 OHOS::system::SetParameter(std::string("testKey"), std::string("testValue")); 315 int v = OHOS::system::GetIntParameter(key1, 0); 316 EXPECT_EQ(v, -TEST_VALUE); 317 int8_t v1 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range 318 EXPECT_EQ(v1, -TEST_VALUE); 319 int16_t v2 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range 320 EXPECT_EQ(v2, -TEST_VALUE); 321 int32_t v3 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range 322 EXPECT_EQ(v3, -TEST_VALUE); 323 int64_t v4 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range 324 EXPECT_EQ(v4, -TEST_VALUE); 325 326 int8_t v5 = OHOS::system::GetIntParameter(key1, 0, -10, 10); // -10, 10 range 327 EXPECT_EQ(v5, 0); 328 329 const std::string key2 = "test.uint.get"; 330 uint8_t u1 = OHOS::system::GetUintParameter<uint8_t>(key2, 0, (uint8_t)255); // 255 max value 331 EXPECT_EQ(u1, TEST_VALUE); 332 uint16_t u2 = OHOS::system::GetUintParameter<uint16_t>(key2, 0, (uint16_t)255); // 255 max value 333 EXPECT_EQ(u2, TEST_VALUE); 334 uint32_t u3 = OHOS::system::GetUintParameter<uint32_t>(key2, 0, (uint32_t)255); // 255 max value 335 EXPECT_EQ(u3, TEST_VALUE); 336 uint64_t u4 = OHOS::system::GetUintParameter<uint64_t>(key2, 0, (uint64_t)255); // 255 max value 337 EXPECT_EQ(u4 == TEST_VALUE, 1); 338 const std::string key3 = "test.uint.get3"; 339 u1 = OHOS::system::GetUintParameter<uint8_t>(key3, 0, (uint8_t)255); // 255 max value 340 EXPECT_EQ(u1, 0); 341 u1 = OHOS::system::GetUintParameter<uint8_t>(key2, 0, (uint8_t)10); // 10 max value 342 EXPECT_EQ(u1, 0); 343 } 344 345 HWTEST_F(SysparaUnitTest, parameterTest0015, TestSize.Level0) 346 { 347 std::string type = OHOS::system::GetDeviceType(); 348 printf("device type %s \n", type.c_str()); 349 350 const std::string key1 = "test.string.get"; 351 std::string v1 = OHOS::system::GetParameter(key1, ""); 352 EXPECT_EQ(strcmp(v1.c_str(), "101"), 0); 353 354 const std::string key2 = "test.string.get2"; 355 v1 = OHOS::system::GetParameter(key2, "test2"); 356 EXPECT_EQ(strcmp(v1.c_str(), "test2"), 0); 357 358 int ret = OHOS::system::GetStringParameter(key1, v1, ""); 359 EXPECT_EQ(ret, 0); 360 EXPECT_EQ(strcmp(v1.c_str(), "101"), 0); 361 ret = OHOS::system::GetStringParameter(key2, v1, "test2"); 362 EXPECT_EQ(ret, 0); 363 EXPECT_EQ(strcmp(v1.c_str(), "test2"), 0); 364 } 365 366 HWTEST_F(SysparaUnitTest, parameterTest0016, TestSize.Level0) 367 { 368 const std::string key1 = "test.bool.get.true"; 369 bool ret = OHOS::system::GetBoolParameter(key1, false); 370 EXPECT_EQ(ret, true); 371 const std::string key2 = "test.bool.get.false"; 372 ret = OHOS::system::GetBoolParameter(key2, true); 373 EXPECT_EQ(ret, false); 374 const std::string key3 = "test.bool.get3"; 375 ret = OHOS::system::GetBoolParameter(key3, false); 376 EXPECT_EQ(ret, false); 377 } 378 379 HWTEST_F(SysparaUnitTest, parameterTest0017, TestSize.Level0) 380 { 381 GetDistributionOSName(); 382 GetDistributionOSVersion(); 383 GetDistributionOSApiVersion(); 384 GetDistributionOSReleaseType(); 385 } 386 #endif 387 } // namespace OHOS 388