1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci#include <gtest/gtest.h> 16d9f0492fSopenharmony_ci 17d9f0492fSopenharmony_ci#include "init_param.h" 18d9f0492fSopenharmony_ci#include "init_utils.h" 19d9f0492fSopenharmony_ci#include "param_stub.h" 20d9f0492fSopenharmony_ci#include "param_init.h" 21d9f0492fSopenharmony_ci 22d9f0492fSopenharmony_ciusing namespace std; 23d9f0492fSopenharmony_ciusing namespace testing::ext; 24d9f0492fSopenharmony_ci 25d9f0492fSopenharmony_cistatic void ClientCheckParamValue(const char *name, const char *expectValue) 26d9f0492fSopenharmony_ci{ 27d9f0492fSopenharmony_ci char tmp[PARAM_BUFFER_SIZE] = {0}; 28d9f0492fSopenharmony_ci u_int32_t len = sizeof(tmp); 29d9f0492fSopenharmony_ci int ret = SystemGetParameter(name, tmp, &len); 30d9f0492fSopenharmony_ci PARAM_LOGI("ClientCheckParamValue name %s value: \'%s\' expectValue:\'%s\' ", name, tmp, expectValue); 31d9f0492fSopenharmony_ci if (ret == 0 && len > 0) { 32d9f0492fSopenharmony_ci EXPECT_NE((int)strlen(tmp), 0); 33d9f0492fSopenharmony_ci if (expectValue != nullptr) { 34d9f0492fSopenharmony_ci EXPECT_EQ(strcmp(tmp, expectValue), 0); 35d9f0492fSopenharmony_ci } 36d9f0492fSopenharmony_ci } 37d9f0492fSopenharmony_ci} 38d9f0492fSopenharmony_ci 39d9f0492fSopenharmony_ci// 多线程测试 40d9f0492fSopenharmony_cistatic void *TestSendParamSetMsg(void *args) 41d9f0492fSopenharmony_ci{ 42d9f0492fSopenharmony_ci if (args == nullptr) { 43d9f0492fSopenharmony_ci return nullptr; 44d9f0492fSopenharmony_ci } 45d9f0492fSopenharmony_ci std::string name = (char *)args; 46d9f0492fSopenharmony_ci PARAM_LOGI("TestSendParamSetMsg name :\'%s\' ", name.c_str()); 47d9f0492fSopenharmony_ci int ret = SystemSetParameter(name.c_str(), name.c_str()); 48d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 49d9f0492fSopenharmony_ci return nullptr; 50d9f0492fSopenharmony_ci} 51d9f0492fSopenharmony_ci 52d9f0492fSopenharmony_cistatic void *TestSendParamWaitMsg(void *args) 53d9f0492fSopenharmony_ci{ 54d9f0492fSopenharmony_ci if (args == nullptr) { 55d9f0492fSopenharmony_ci return nullptr; 56d9f0492fSopenharmony_ci } 57d9f0492fSopenharmony_ci std::string name = "Wati."; 58d9f0492fSopenharmony_ci name = name + (char *)args; 59d9f0492fSopenharmony_ci PARAM_LOGI("TestSendParamWaitMsg name :\'%s\' \n", name.c_str()); 60d9f0492fSopenharmony_ci int ret = SystemWaitParameter(name.c_str(), name.c_str(), 1); 61d9f0492fSopenharmony_ci EXPECT_GE(ret, 0); 62d9f0492fSopenharmony_ci return nullptr; 63d9f0492fSopenharmony_ci} 64d9f0492fSopenharmony_ci 65d9f0492fSopenharmony_cistatic void TestForMultiThread() 66d9f0492fSopenharmony_ci{ 67d9f0492fSopenharmony_ci static const int threadMaxNumer = 2; 68d9f0492fSopenharmony_ci PARAM_LOGI("TestForMultiThread \n"); 69d9f0492fSopenharmony_ci pthread_t tids[threadMaxNumer + threadMaxNumer]; 70d9f0492fSopenharmony_ci const char *names[] = { 71d9f0492fSopenharmony_ci "thread.1111.2222.3333.4444.5555", 72d9f0492fSopenharmony_ci "thread.2222.1111.2222.3333.4444", 73d9f0492fSopenharmony_ci "thread.3333.1111.2222.4444.5555", 74d9f0492fSopenharmony_ci "thread.4444.5555.1111.2222.3333", 75d9f0492fSopenharmony_ci "thread.5555.1111.2222.3333.4444" 76d9f0492fSopenharmony_ci }; 77d9f0492fSopenharmony_ci for (size_t i = 0; i < threadMaxNumer; i++) { 78d9f0492fSopenharmony_ci pthread_create(&tids[i], nullptr, TestSendParamSetMsg, 79d9f0492fSopenharmony_ci reinterpret_cast<void *>(const_cast<char *>(names[i % ARRAY_LENGTH(names)]))); 80d9f0492fSopenharmony_ci } 81d9f0492fSopenharmony_ci for (size_t i = threadMaxNumer; i < threadMaxNumer + threadMaxNumer; i++) { 82d9f0492fSopenharmony_ci pthread_create(&tids[i], nullptr, TestSendParamWaitMsg, 83d9f0492fSopenharmony_ci reinterpret_cast<void *>(const_cast<char *>(names[i % ARRAY_LENGTH(names)]))); 84d9f0492fSopenharmony_ci } 85d9f0492fSopenharmony_ci for (size_t i = 0; i < threadMaxNumer + threadMaxNumer; i++) { 86d9f0492fSopenharmony_ci pthread_join(tids[i], nullptr); 87d9f0492fSopenharmony_ci } 88d9f0492fSopenharmony_ci} 89d9f0492fSopenharmony_ci 90d9f0492fSopenharmony_cistatic void TestParamTraversal() 91d9f0492fSopenharmony_ci{ 92d9f0492fSopenharmony_ci SystemTraversalParameter( 93d9f0492fSopenharmony_ci "", 94d9f0492fSopenharmony_ci [](ParamHandle handle, void *cookie) { 95d9f0492fSopenharmony_ci char value[PARAM_BUFFER_SIZE + PARAM_BUFFER_SIZE] = {0}; 96d9f0492fSopenharmony_ci uint32_t commitId = 0; 97d9f0492fSopenharmony_ci int ret = SystemGetParameterCommitId(handle, &commitId); 98d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 99d9f0492fSopenharmony_ci SystemGetParameterName(handle, value, PARAM_BUFFER_SIZE); 100d9f0492fSopenharmony_ci u_int32_t len = PARAM_BUFFER_SIZE; 101d9f0492fSopenharmony_ci SystemGetParameterValue(handle, ((char *)value) + PARAM_BUFFER_SIZE, &len); 102d9f0492fSopenharmony_ci printf("$$$$$$$$Param %s=%s \n", (char *)value, ((char *)value) + PARAM_BUFFER_SIZE); 103d9f0492fSopenharmony_ci }, 104d9f0492fSopenharmony_ci nullptr); 105d9f0492fSopenharmony_ci} 106d9f0492fSopenharmony_ci 107d9f0492fSopenharmony_cistatic void TestPermission() 108d9f0492fSopenharmony_ci{ 109d9f0492fSopenharmony_ci const char *testName = "persist.111.ffff.bbbb.cccc.dddd.eeee.55555"; 110d9f0492fSopenharmony_ci char tmp[PARAM_BUFFER_SIZE] = {0}; 111d9f0492fSopenharmony_ci int ret; 112d9f0492fSopenharmony_ci 113d9f0492fSopenharmony_ci ParamSecurityOps *paramSecurityOps = GetParamSecurityOps(0); 114d9f0492fSopenharmony_ci EXPECT_NE(paramSecurityOps, nullptr); 115d9f0492fSopenharmony_ci paramSecurityOps->securityCheckParamPermission = TestCheckParamPermission; 116d9f0492fSopenharmony_ci SetTestPermissionResult(DAC_RESULT_FORBIDED); 117d9f0492fSopenharmony_ci if ((GetParamSecurityLabel() != nullptr)) { 118d9f0492fSopenharmony_ci GetParamSecurityLabel()->flags[0] = LABEL_CHECK_IN_ALL_PROCESS; 119d9f0492fSopenharmony_ci ret = SystemSetParameter(testName, "22202"); 120d9f0492fSopenharmony_ci#ifdef __LITEOS_A__ 121d9f0492fSopenharmony_ci EXPECT_EQ(ret, DAC_RESULT_FORBIDED); 122d9f0492fSopenharmony_ci#else 123d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); // 本地不在校验 124d9f0492fSopenharmony_ci#endif 125d9f0492fSopenharmony_ci } 126d9f0492fSopenharmony_ci paramSecurityOps->securityFreeLabel = TestFreeLocalSecurityLabel; 127d9f0492fSopenharmony_ci paramSecurityOps->securityCheckParamPermission = TestCheckParamPermission; 128d9f0492fSopenharmony_ci SetTestPermissionResult(0); 129d9f0492fSopenharmony_ci SystemWriteParam(testName, "22202"); 130d9f0492fSopenharmony_ci ret = SystemSetParameter(testName, "22202"); 131d9f0492fSopenharmony_ci ClientCheckParamValue(testName, "22202"); 132d9f0492fSopenharmony_ci 133d9f0492fSopenharmony_ci const int testResult = 201; 134d9f0492fSopenharmony_ci SetTestPermissionResult(testResult); 135d9f0492fSopenharmony_ci ret = SystemSetParameter(testName, "3333"); 136d9f0492fSopenharmony_ci#ifdef __LITEOS_A__ 137d9f0492fSopenharmony_ci EXPECT_EQ(ret, testResult); 138d9f0492fSopenharmony_ci#else 139d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); // 本地不在校验 140d9f0492fSopenharmony_ci#endif 141d9f0492fSopenharmony_ci 142d9f0492fSopenharmony_ci u_int32_t len = sizeof(tmp); 143d9f0492fSopenharmony_ci SetTestPermissionResult(DAC_RESULT_FORBIDED); 144d9f0492fSopenharmony_ci ret = SystemGetParameter(testName, tmp, &len); 145d9f0492fSopenharmony_ci EXPECT_EQ(ret, DAC_RESULT_FORBIDED); 146d9f0492fSopenharmony_ci RegisterSecurityOps(0); 147d9f0492fSopenharmony_ci SetTestPermissionResult(0); // recover testpermission result 148d9f0492fSopenharmony_ci} 149d9f0492fSopenharmony_ci 150d9f0492fSopenharmony_civoid TestClientApi(char testBuffer[], uint32_t size, const char *name, const char *value) 151d9f0492fSopenharmony_ci{ 152d9f0492fSopenharmony_ci ParamHandle handle; 153d9f0492fSopenharmony_ci int ret = SystemFindParameter(name, &handle); 154d9f0492fSopenharmony_ci SystemWriteParam(name, value); 155d9f0492fSopenharmony_ci SystemSetParameter(name, value); 156d9f0492fSopenharmony_ci ret = SystemFindParameter(name, &handle); 157d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 158d9f0492fSopenharmony_ci uint32_t commitId = 0; 159d9f0492fSopenharmony_ci ret = SystemGetParameterCommitId(handle, &commitId); 160d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 161d9f0492fSopenharmony_ci ret = SystemGetParameterName(handle, testBuffer, size); 162d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 163d9f0492fSopenharmony_ci EXPECT_EQ(strcmp(testBuffer, name), 0); 164d9f0492fSopenharmony_ci ret = SystemGetParameterValue(handle, testBuffer, &size); 165d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 166d9f0492fSopenharmony_ci EXPECT_EQ(strcmp(testBuffer, value), 0); 167d9f0492fSopenharmony_ci} 168d9f0492fSopenharmony_ci 169d9f0492fSopenharmony_cinamespace init_ut { 170d9f0492fSopenharmony_ciclass ClientUnitTest : public ::testing::Test { 171d9f0492fSopenharmony_cipublic: 172d9f0492fSopenharmony_ci ClientUnitTest() {} 173d9f0492fSopenharmony_ci virtual ~ClientUnitTest() {} 174d9f0492fSopenharmony_ci static void SetUpTestCase(void) {} 175d9f0492fSopenharmony_ci void SetUp(void) 176d9f0492fSopenharmony_ci { 177d9f0492fSopenharmony_ci if (GetParamSecurityLabel() != nullptr) { 178d9f0492fSopenharmony_ci GetParamSecurityLabel()->cred.uid = 1000; // 1000 test uid 179d9f0492fSopenharmony_ci GetParamSecurityLabel()->cred.gid = 1000; // 1000 test gid 180d9f0492fSopenharmony_ci } 181d9f0492fSopenharmony_ci } 182d9f0492fSopenharmony_ci void TearDown(void) {} 183d9f0492fSopenharmony_ci void TestBody(void) {} 184d9f0492fSopenharmony_ci}; 185d9f0492fSopenharmony_ci 186d9f0492fSopenharmony_ciHWTEST_F(ClientUnitTest, Init_TestClient_001, TestSize.Level0) 187d9f0492fSopenharmony_ci{ 188d9f0492fSopenharmony_ci const std::string name = "test.add.client.001.001"; 189d9f0492fSopenharmony_ci const std::string value = "test.add.client.value.001.001"; 190d9f0492fSopenharmony_ci // direct write 191d9f0492fSopenharmony_ci SystemWriteParam(name.c_str(), value.c_str()); 192d9f0492fSopenharmony_ci SystemSetParameter(name.c_str(), value.c_str()); 193d9f0492fSopenharmony_ci ClientCheckParamValue(name.c_str(), value.c_str()); 194d9f0492fSopenharmony_ci SystemWaitParameter(name.c_str(), value.c_str(), 1); 195d9f0492fSopenharmony_ci // wait 196d9f0492fSopenharmony_ci SystemWaitParameter(name.c_str(), value.c_str(), 1); 197d9f0492fSopenharmony_ci SystemWaitParameter(name.c_str(), nullptr, 0); 198d9f0492fSopenharmony_ci 199d9f0492fSopenharmony_ci // error 200d9f0492fSopenharmony_ci SystemWaitParameter(nullptr, nullptr, 0); 201d9f0492fSopenharmony_ci SystemWaitParameter("@@@@", value.c_str(), 1); 202d9f0492fSopenharmony_ci} 203d9f0492fSopenharmony_ci 204d9f0492fSopenharmony_ciHWTEST_F(ClientUnitTest, Init_TestParamValue_001, TestSize.Level0) 205d9f0492fSopenharmony_ci{ 206d9f0492fSopenharmony_ci // support empty string 207d9f0492fSopenharmony_ci const char *name = "test_readonly.dddddddddddddddddd.fffffffffffffffffff"; 208d9f0492fSopenharmony_ci int ret = SystemSetParameter(name, ""); 209d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 210d9f0492fSopenharmony_ci ret = SystemSetParameter(name, "111111111"); 211d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 212d9f0492fSopenharmony_ci ret = SystemSetParameter(name, ""); 213d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 214d9f0492fSopenharmony_ci} 215d9f0492fSopenharmony_ci 216d9f0492fSopenharmony_ciHWTEST_F(ClientUnitTest, Init_TestClient_002, TestSize.Level0) 217d9f0492fSopenharmony_ci{ 218d9f0492fSopenharmony_ci char testBuffer[PARAM_BUFFER_SIZE] = {0}; 219d9f0492fSopenharmony_ci const std::string value = "test.add.client.value.001"; 220d9f0492fSopenharmony_ci const std::string name = "test.add.client.001.003"; 221d9f0492fSopenharmony_ci TestClientApi(testBuffer, PARAM_BUFFER_SIZE, name.c_str(), value.c_str()); 222d9f0492fSopenharmony_ci} 223d9f0492fSopenharmony_ci 224d9f0492fSopenharmony_ciHWTEST_F(ClientUnitTest, Init_TestClient_003, TestSize.Level0) 225d9f0492fSopenharmony_ci{ 226d9f0492fSopenharmony_ci // 3 Traversal test 227d9f0492fSopenharmony_ci TestParamTraversal(); 228d9f0492fSopenharmony_ci SystemDumpParameters(1, -1, nullptr); 229d9f0492fSopenharmony_ci} 230d9f0492fSopenharmony_ci 231d9f0492fSopenharmony_ciHWTEST_F(ClientUnitTest, Init_TestClient_004, TestSize.Level0) 232d9f0492fSopenharmony_ci{ 233d9f0492fSopenharmony_ci const std::string name = "test.add.client.001.004"; 234d9f0492fSopenharmony_ci int ret = WatchParamCheck(name.c_str()); 235d9f0492fSopenharmony_ci#ifndef OHOS_LITE 236d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 237d9f0492fSopenharmony_ci#endif 238d9f0492fSopenharmony_ci ret = WatchParamCheck("&&&&&.test.tttt"); 239d9f0492fSopenharmony_ci EXPECT_NE(ret, 0); 240d9f0492fSopenharmony_ci 241d9f0492fSopenharmony_ci ret = WatchParamCheck(nullptr); 242d9f0492fSopenharmony_ci#ifndef OHOS_LITE 243d9f0492fSopenharmony_ci EXPECT_EQ(ret, 100); 244d9f0492fSopenharmony_ci#endif 245d9f0492fSopenharmony_ci // test permission 246d9f0492fSopenharmony_ci TestPermission(); 247d9f0492fSopenharmony_ci} 248d9f0492fSopenharmony_ci 249d9f0492fSopenharmony_ciHWTEST_F(ClientUnitTest, Init_TestClient_005, TestSize.Level0) 250d9f0492fSopenharmony_ci{ 251d9f0492fSopenharmony_ci TestForMultiThread(); 252d9f0492fSopenharmony_ci} 253d9f0492fSopenharmony_ci 254d9f0492fSopenharmony_ciHWTEST_F(ClientUnitTest, Init_TestClient_006, TestSize.Level0) 255d9f0492fSopenharmony_ci{ 256d9f0492fSopenharmony_ci int ret = SystemSetParameter("test.type.string.xxx", "xxxxxxx"); 257d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 258d9f0492fSopenharmony_ci ret = SystemSetParameter("test.type.string...xxx", "xxxxxxx"); 259d9f0492fSopenharmony_ci EXPECT_EQ(ret, PARAM_CODE_INVALID_NAME); 260d9f0492fSopenharmony_ci ret = SystemSetParameter("test.type.string*xxx", "xxxxxxx"); 261d9f0492fSopenharmony_ci EXPECT_EQ(ret, PARAM_CODE_INVALID_NAME); 262d9f0492fSopenharmony_ci#if !(defined __LITEOS_A__ || defined __LITEOS_M__) 263d9f0492fSopenharmony_ci ret = SystemSetParameter("test.type.bool.xxx", "xxxxxxxxxxxxxxxx"); 264d9f0492fSopenharmony_ci EXPECT_EQ(ret, PARAM_CODE_INVALID_VALUE); 265d9f0492fSopenharmony_ci char value[PARAM_VALUE_LEN_MAX] = {0}; 266d9f0492fSopenharmony_ci u_int32_t len = sizeof(value); 267d9f0492fSopenharmony_ci ret = SystemGetParameter("test.permission.watcher.xxx", value, &len); 268d9f0492fSopenharmony_ci EXPECT_EQ(ret, DAC_RESULT_FORBIDED); 269d9f0492fSopenharmony_ci ret = SystemGetParameter("test.type.xxx", value, &len); 270d9f0492fSopenharmony_ci EXPECT_EQ(ret, PARAM_CODE_NOT_FOUND); 271d9f0492fSopenharmony_ci#endif 272d9f0492fSopenharmony_ci} 273d9f0492fSopenharmony_ci} // namespace init_ut