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 #include <gtest/gtest.h>
16
17 #include "init_param.h"
18 #include "init_utils.h"
19 #include "param_stub.h"
20
21 using namespace std;
22 using namespace testing::ext;
23
ClientCheckParamValue(const char * name,const char * expectValue)24 static void ClientCheckParamValue(const char *name, const char *expectValue)
25 {
26 char tmp[PARAM_BUFFER_SIZE] = {0};
27 u_int32_t len = sizeof(tmp);
28 int ret = SystemGetParameter(name, tmp, &len);
29 PARAM_LOGI("ClientCheckParamValue name %s value: \'%s\' expectValue:\'%s\' ", name, tmp, expectValue);
30 if (ret == 0 && len > 0) {
31 EXPECT_NE((int)strlen(tmp), 0);
32 if (expectValue != nullptr) {
33 EXPECT_EQ(strcmp(tmp, expectValue), 0);
34 }
35 }
36 }
37
38 // 多线程测试
TestSendParamSetMsg(void * args)39 static void *TestSendParamSetMsg(void *args)
40 {
41 if (args == nullptr) {
42 return nullptr;
43 }
44 std::string name = (char *)args;
45 PARAM_LOGI("TestSendParamSetMsg name :\'%s\' ", name.c_str());
46 int ret = SystemSetParameter(name.c_str(), name.c_str());
47 EXPECT_EQ(ret, 0);
48 return nullptr;
49 }
50
TestSendParamWaitMsg(void * args)51 static void *TestSendParamWaitMsg(void *args)
52 {
53 if (args == nullptr) {
54 return nullptr;
55 }
56 std::string name = "Wati.";
57 name = name + (char *)args;
58 PARAM_LOGI("TestSendParamWaitMsg name :\'%s\' \n", name.c_str());
59 int ret = SystemWaitParameter(name.c_str(), name.c_str(), 1);
60 EXPECT_GE(ret, 0);
61 return nullptr;
62 }
63
TestForMultiThread()64 static void TestForMultiThread()
65 {
66 static const int threadMaxNumer = 2;
67 PARAM_LOGI("TestForMultiThread \n");
68 pthread_t tids[threadMaxNumer + threadMaxNumer];
69 const char *names[] = {
70 "thread.1111.2222.3333.4444.5555",
71 "thread.2222.1111.2222.3333.4444",
72 "thread.3333.1111.2222.4444.5555",
73 "thread.4444.5555.1111.2222.3333",
74 "thread.5555.1111.2222.3333.4444"
75 };
76 for (size_t i = 0; i < threadMaxNumer; i++) {
77 pthread_create(&tids[i], nullptr, TestSendParamSetMsg,
78 reinterpret_cast<void *>(const_cast<char *>(names[i % ARRAY_LENGTH(names)])));
79 }
80 for (size_t i = threadMaxNumer; i < threadMaxNumer + threadMaxNumer; i++) {
81 pthread_create(&tids[i], nullptr, TestSendParamWaitMsg,
82 reinterpret_cast<void *>(const_cast<char *>(names[i % ARRAY_LENGTH(names)])));
83 }
84 for (size_t i = 0; i < threadMaxNumer + threadMaxNumer; i++) {
85 pthread_join(tids[i], nullptr);
86 }
87 }
88
TestParamTraversal()89 static void TestParamTraversal()
90 {
91 SystemTraversalParameter(
92 "",
93 [](ParamHandle handle, void *cookie) {
94 char value[PARAM_BUFFER_SIZE + PARAM_BUFFER_SIZE] = {0};
95 uint32_t commitId = 0;
96 int ret = SystemGetParameterCommitId(handle, &commitId);
97 EXPECT_EQ(ret, 0);
98 SystemGetParameterName(handle, value, PARAM_BUFFER_SIZE);
99 u_int32_t len = PARAM_BUFFER_SIZE;
100 SystemGetParameterValue(handle, ((char *)value) + PARAM_BUFFER_SIZE, &len);
101 printf("$$$$$$$$Param %s=%s \n", (char *)value, ((char *)value) + PARAM_BUFFER_SIZE);
102 },
103 nullptr);
104 }
105
TestPermission()106 static void TestPermission()
107 {
108 const char *testName = "persist.111.ffff.bbbb.cccc.dddd.eeee.55555";
109 char tmp[PARAM_BUFFER_SIZE] = {0};
110 int ret;
111
112 ParamSecurityOps *paramSecurityOps = GetParamSecurityOps(0);
113 EXPECT_NE(paramSecurityOps, nullptr);
114 paramSecurityOps->securityCheckParamPermission = TestCheckParamPermission;
115 SetTestPermissionResult(DAC_RESULT_FORBIDED);
116 if ((GetParamSecurityLabel() != nullptr)) {
117 GetParamSecurityLabel()->flags[0] = LABEL_CHECK_IN_ALL_PROCESS;
118 ret = SystemSetParameter(testName, "22202");
119 #ifdef __LITEOS_A__
120 EXPECT_EQ(ret, DAC_RESULT_FORBIDED);
121 #else
122 EXPECT_EQ(ret, 0); // 本地不在校验
123 #endif
124 }
125 paramSecurityOps->securityFreeLabel = TestFreeLocalSecurityLabel;
126 paramSecurityOps->securityCheckParamPermission = TestCheckParamPermission;
127 SetTestPermissionResult(0);
128 SystemWriteParam(testName, "22202");
129 ret = SystemSetParameter(testName, "22202");
130 ClientCheckParamValue(testName, "22202");
131
132 const int testResult = 201;
133 SetTestPermissionResult(testResult);
134 ret = SystemSetParameter(testName, "3333");
135 #ifdef __LITEOS_A__
136 EXPECT_EQ(ret, testResult);
137 #else
138 EXPECT_EQ(ret, 0); // 本地不在校验
139 #endif
140
141 u_int32_t len = sizeof(tmp);
142 SetTestPermissionResult(DAC_RESULT_FORBIDED);
143 ret = SystemGetParameter(testName, tmp, &len);
144 EXPECT_EQ(ret, DAC_RESULT_FORBIDED);
145 RegisterSecurityOps(0);
146 SetTestPermissionResult(0); // recover testpermission result
147 }
148
TestClientApi(char testBuffer[],uint32_t size,const char * name,const char * value)149 void TestClientApi(char testBuffer[], uint32_t size, const char *name, const char *value)
150 {
151 ParamHandle handle;
152 int ret = SystemFindParameter(name, &handle);
153 SystemWriteParam(name, value);
154 SystemSetParameter(name, value);
155 ret = SystemFindParameter(name, &handle);
156 EXPECT_EQ(ret, 0);
157 uint32_t commitId = 0;
158 ret = SystemGetParameterCommitId(handle, &commitId);
159 EXPECT_EQ(ret, 0);
160 ret = SystemGetParameterName(handle, testBuffer, size);
161 EXPECT_EQ(ret, 0);
162 EXPECT_EQ(strcmp(testBuffer, name), 0);
163 ret = SystemGetParameterValue(handle, testBuffer, &size);
164 EXPECT_EQ(ret, 0);
165 EXPECT_EQ(strcmp(testBuffer, value), 0);
166 }
167
168 namespace init_ut {
169 class ClientUnitTest : public ::testing::Test {
170 public:
ClientUnitTest()171 ClientUnitTest() {}
~ClientUnitTest()172 virtual ~ClientUnitTest() {}
SetUpTestCase(void)173 static void SetUpTestCase(void)
174 {
175 PrepareInitUnitTestEnv();
176 };
177
SetUp(void)178 void SetUp(void)
179 {
180 if (GetParamSecurityLabel() != nullptr) {
181 GetParamSecurityLabel()->cred.uid = 1000; // 1000 test uid
182 GetParamSecurityLabel()->cred.gid = 1000; // 1000 test gid
183 }
184 }
TearDown(void)185 void TearDown(void) {}
TestBody(void)186 void TestBody(void) {}
187 };
188
189 HWTEST_F(ClientUnitTest, TestClient_01, TestSize.Level0)
190 {
191 const std::string name = "test.add.client.001.001";
192 const std::string value = "test.add.client.value.001.001";
193 // direct write
194 SystemWriteParam(name.c_str(), value.c_str());
195 SystemSetParameter(name.c_str(), value.c_str());
196 ClientCheckParamValue(name.c_str(), value.c_str());
197 SystemWaitParameter(name.c_str(), value.c_str(), 1);
198 // wait
199 SystemWaitParameter(name.c_str(), value.c_str(), 1);
200 SystemWaitParameter(name.c_str(), nullptr, 0);
201
202 // error
203 SystemWaitParameter(nullptr, nullptr, 0);
204 SystemWaitParameter("@@@@", value.c_str(), 1);
205 }
206
207 HWTEST_F(ClientUnitTest, TestParamValue, TestSize.Level0)
208 {
209 // support empty string
210 const char *name = "test_readonly.dddddddddddddddddd.fffffffffffffffffff";
211 int ret = SystemSetParameter(name, "");
212 EXPECT_EQ(ret, 0);
213 ret = SystemSetParameter(name, "111111111");
214 EXPECT_EQ(ret, 0);
215 ret = SystemSetParameter(name, "");
216 EXPECT_EQ(ret, 0);
217 }
218
219 HWTEST_F(ClientUnitTest, TestClient_02, TestSize.Level0)
220 {
221 char testBuffer[PARAM_BUFFER_SIZE] = {0};
222 const std::string value = "test.add.client.value.001";
223 const std::string name = "test.add.client.001.003";
224 TestClientApi(testBuffer, PARAM_BUFFER_SIZE, name.c_str(), value.c_str());
225 }
226
227 HWTEST_F(ClientUnitTest, TestClient_03, TestSize.Level0)
228 {
229 // 3 Traversal test
230 TestParamTraversal();
231 SystemDumpParameters(1, -1, nullptr);
232 }
233
234 HWTEST_F(ClientUnitTest, TestClient_04, TestSize.Level0)
235 {
236 const std::string name = "test.add.client.001.004";
237 int ret = WatchParamCheck(name.c_str());
238 #ifndef OHOS_LITE
239 EXPECT_EQ(ret, 0);
240 #endif
241 ret = WatchParamCheck("&&&&&.test.tttt");
242 EXPECT_NE(ret, 0);
243
244 ret = WatchParamCheck(nullptr);
245 #ifndef OHOS_LITE
246 EXPECT_EQ(ret, 100);
247 #endif
248 // test permission
249 TestPermission();
250 }
251
252 HWTEST_F(ClientUnitTest, TestClient_05, TestSize.Level0)
253 {
254 TestForMultiThread();
255 }
256 } // namespace init_ut