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 } else {
36 EXPECT_NE(0, 0);
37 }
38 }
39
40 // 多线程测试
TestSendParamSetMsg(void * args)41 static void *TestSendParamSetMsg(void *args)
42 {
43 if (args == nullptr) {
44 return nullptr;
45 }
46 std::string name = (char *)args;
47 PARAM_LOGI("TestSendParamSetMsg name :\'%s\' ", name.c_str());
48 SystemSetParameter(name.c_str(), name.c_str());
49 return nullptr;
50 }
51
TestSendParamWaitMsg(void * args)52 static void *TestSendParamWaitMsg(void *args)
53 {
54 if (args == nullptr) {
55 return nullptr;
56 }
57 std::string name = "Wati.";
58 name = name + (char *)args;
59 PARAM_LOGI("TestSendParamWaitMsg name :\'%s\' \n", name.c_str());
60 SystemWaitParameter(name.c_str(), name.c_str(), 1);
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
203 HWTEST_F(ClientUnitTest, TestClient_02, TestSize.Level0)
204 {
205 char testBuffer[PARAM_BUFFER_SIZE] = {0};
206 const std::string value = "test.add.client.value.001";
207 const std::string name = "test.add.client.001.003";
208 TestClientApi(testBuffer, PARAM_BUFFER_SIZE, name.c_str(), value.c_str());
209 }
210
211 HWTEST_F(ClientUnitTest, TestClient_03, TestSize.Level0)
212 {
213 // 3 Traversal test
214 TestParamTraversal();
215 SystemDumpParameters(1, NULL);
216 }
217
218 HWTEST_F(ClientUnitTest, TestClient_04, TestSize.Level0)
219 {
220 const std::string name = "test.add.client.001.004";
221 int ret = WatchParamCheck(name.c_str());
222 #ifndef OHOS_LITE
223 EXPECT_EQ(ret, 0);
224 #endif
225 ret = WatchParamCheck("&&&&&.test.tttt");
226 EXPECT_NE(ret, 0);
227 // test permission
228 TestPermission();
229 }
230
231 HWTEST_F(ClientUnitTest, TestClient_05, TestSize.Level0)
232 {
233 TestForMultiThread();
234 }
235 } // namespace init_ut