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