1 /**
2 * Copyright (c) 2022 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 <dlfcn.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "dlns_test.h"
22 #include "functionalext.h"
23
24
25 /**
26 * @tc.name : dlns_inherit_0100
27 * @tc.desc : Under the INI configuration file, when shared_libs in dlns_inherit is empty,
28 * all the libraries of the inheritor can be shared by it
29 * @tc.level : Level 1
30 */
dlns_inherit_0100(void)31 void dlns_inherit_0100(void)
32 {
33 Dl_namespace dlnsA, dlnsB;
34 dlns_init(&dlnsA, "for_inherit_A");
35 dlns_init(&dlnsB, "inherited_class");
36
37 EXPECT_EQ("dlns_inherit_0100", dlns_inherit(&dlnsA, &dlnsB, NULL), EOK);
38
39 void* handle = dlopen_ns(&dlnsA, dllName, RTLD_LAZY);
40 EXPECT_TRUE("dlns_inherit_0100", handle);
41 dlclose(handle);
42
43 void* handle1 = dlopen_ns(&dlnsA, sharedLib, RTLD_LAZY);
44 EXPECT_TRUE("dlns_inherit_0100", handle1);
45 dlclose(handle1);
46 }
47
48 /**
49 * @tc.name : dlns_inherit_0200
50 * @tc.desc : Under the INI configuration file, when shared_libs in dlns_inherit is set, the set shared library
51 * name can be shared, and other than the shared library name, it will not be shared
52 * @tc.level : Level 2
53 */
dlns_inherit_0200(void)54 void dlns_inherit_0200(void)
55 {
56 Dl_namespace dlnsA, dlnsB;
57 dlns_init(&dlnsA, "for_inherit_A");
58 dlns_init(&dlnsB, "inherited_class");
59
60 EXPECT_EQ("dlns_inherit_0200", dlns_inherit(&dlnsA, &dlnsB, dllName), EOK);
61
62 void* handle = dlopen_ns(&dlnsA, dllName, RTLD_LAZY);
63 EXPECT_TRUE("dlns_inherit_0200", handle);
64 dlclose(handle);
65
66 void* handle1 = dlopen_ns(&dlnsA, sharedLib, RTLD_LAZY);
67 EXPECT_FALSE("dlns_inherit_0200", handle1);
68 }
69
70 /**
71 * @tc.name : dlns_inherit_0300
72 * @tc.desc : Under the INI configuration file, the library that cannot be opened
73 * by itself is opened through inheritance
74 * @tc.level : Level 1
75 */
dlns_inherit_0300(void)76 void dlns_inherit_0300(void)
77 {
78 Dl_namespace dlnsA, dlnsB;
79 dlns_init(&dlnsA, "inherit_A");
80 dlns_init(&dlnsB, "inherited_class");
81
82 EXPECT_EQ("dlns_inherit_0300", dlns_create(&dlnsA, NULL), EOK);
83
84 void* handle1 = dlopen_ns(&dlnsA, dllName_inh_003, RTLD_LAZY);
85 EXPECT_FALSE("dlns_inherit_0300", handle1);
86
87 EXPECT_EQ("dlns_inherit_0300", dlns_inherit(&dlnsA, &dlnsB, dllName_inh_003), EOK);
88
89 void* handle = dlopen_ns(&dlnsA, dllName_inh_003, RTLD_LAZY);
90 EXPECT_TRUE("dlns_inherit_0300", handle);
91 dlclose(handle);
92 }
93
94 /**
95 * @tc.name : dlns_inherit_0400
96 * @tc.desc : In the INI configuration file, inheritance cannot be passed, and cannot be opened after generation
97 * @tc.level : Level 2
98 */
dlns_inherit_0400(void)99 void dlns_inherit_0400(void)
100 {
101 Dl_namespace dlnsA, dlnsAA, dlnsB;
102 dlns_init(&dlnsA, "for_inherit_A");
103 dlns_init(&dlnsAA, "for_inherit_AA");
104 dlns_init(&dlnsB, "inherited_class");
105
106 EXPECT_EQ("dlns_inherit_0400", dlns_inherit(&dlnsA, &dlnsB, dllName), EOK);
107
108 void* handle = dlopen_ns(&dlnsA, dllName, RTLD_LAZY);
109 EXPECT_PTRNE("dlns_inherit_0400", handle, NULL);
110 dlclose(handle);
111
112 EXPECT_EQ("dlns_inherit_0400", dlns_inherit(&dlnsAA, &dlnsA, NULL), EOK);
113
114 void* handle1 = dlopen_ns(&dlnsAA, dllName, RTLD_LAZY);
115 EXPECT_FALSE("dlns_inherit_0400", handle1);
116 }
117
118 /**
119 * @tc.name : dlns_inherit_0500
120 * @tc.desc : Create ns through the interface, when shared_libs in dlns_inherit is empty,
121 * all the libraries of the inheritor can be shared by it
122 * @tc.level : Level 1
123 */
dlns_inherit_0500(void)124 void dlns_inherit_0500(void)
125 {
126 Dl_namespace dlnsA, dlnsB;
127 dlns_init(&dlnsA, "dlns_inherit_0500_A");
128 dlns_init(&dlnsB, "dlns_inherit_0500_B");
129
130 EXPECT_EQ("dlns_inherit_0500", dlns_create(&dlnsA, NULL), EOK);
131 EXPECT_EQ("dlns_inherit_0500", dlns_create(&dlnsB, path), EOK);
132
133 EXPECT_EQ("dlns_inherit_0500", dlns_set_namespace_separated("dlns_inherit_0500_A", true), EOK);
134 EXPECT_EQ("dlns_inherit_0500", dlns_set_namespace_separated("dlns_inherit_0500_B", true), EOK);
135
136 EXPECT_EQ("dlns_inherit_0500", dlns_inherit(&dlnsA, &dlnsB, NULL), EOK);
137
138 void* handle = dlopen_ns(&dlnsA, dllName, RTLD_LAZY);
139 EXPECT_TRUE("dlns_inherit_0500", handle);
140 dlclose(handle);
141
142 handle = dlopen_ns(&dlnsA, sharedLib, RTLD_LAZY);
143 EXPECT_TRUE("dlns_inherit_0500", handle);
144 dlclose(handle);
145 }
146
147 /**
148 * @tc.name : dlns_inherit_0600
149 * @tc.desc : Create ns through the interface, when shared_libs in dlns_inherit is set, the set shared
150 * library name can be shared, other than the shared library name, it will not be shared
151 * @tc.level : Level 2
152 */
dlns_inherit_0600(void)153 void dlns_inherit_0600(void)
154 {
155 Dl_namespace dlnsA, dlnsB;
156 dlns_init(&dlnsA, "dlns_inherit_0600_A");
157 dlns_init(&dlnsB, "dlns_inherit_0600_B");
158
159 EXPECT_EQ("dlns_inherit_0600", dlns_create(&dlnsA, NULL), EOK);
160 EXPECT_EQ("dlns_inherit_0600", dlns_create(&dlnsB, path), EOK);
161
162 EXPECT_EQ("dlns_inherit_0600", dlns_set_namespace_separated("dlns_inherit_0600_A", true), EOK);
163 EXPECT_EQ("dlns_inherit_0600", dlns_set_namespace_separated("dlns_inherit_0600_B", true), EOK);
164
165 EXPECT_EQ("dlns_inherit_0600", dlns_inherit(&dlnsA, &dlnsB, dllName), EOK);
166
167 void* handle = dlopen_ns(&dlnsA, dllName, RTLD_LAZY);
168 EXPECT_TRUE("dlns_inherit_0600", handle);
169 dlclose(handle);
170
171 handle = dlopen_ns(&dlnsA, sharedLib, RTLD_LAZY);
172 EXPECT_FALSE("dlns_inherit_0600", handle);
173 }
174
175 /**
176 * @tc.name : dlns_inherit_0700
177 * @tc.desc : Create ns through interfaces, and open libraries that cannot be
178 * opened by themselves through inheritance.
179 * @tc.level : Level 2
180 */
dlns_inherit_0700(void)181 void dlns_inherit_0700(void)
182 {
183 Dl_namespace dlnsA, dlnsB;
184 dlns_init(&dlnsA, "dlns_inherit_0700_A");
185 dlns_init(&dlnsB, "dlns_inherit_0700_B");
186
187 EXPECT_EQ("dlns_inherit_0700", dlns_create(&dlnsA, NULL), EOK);
188 EXPECT_EQ("dlns_inherit_0700", dlns_create(&dlnsB, path), EOK);
189
190 EXPECT_EQ("dlns_inherit_0700", dlns_set_namespace_separated("dlns_inherit_0700_A", true), EOK);
191 EXPECT_EQ("dlns_inherit_0700", dlns_set_namespace_separated("dlns_inherit_0700_B", true), EOK);
192
193 EXPECT_EQ("dlns_inherit_0700", dlns_set_namespace_allowed_libs("dlns_inherit_0700_A", dllName2), EOK);
194
195 void* handle = dlopen_ns(&dlnsA, dllName_inh_007, RTLD_LAZY);
196 EXPECT_FALSE("dlns_inherit_0700", handle);
197
198 EXPECT_EQ("dlns_inherit_0700", dlns_inherit(&dlnsA, &dlnsB, NULL), EOK);
199
200 handle = dlopen_ns(&dlnsA, dllName_inh_007, RTLD_LAZY);
201 EXPECT_TRUE("dlns_inherit_0700", handle);
202 dlclose(handle);
203 }
204
205 /**
206 * @tc.name : dlns_inherit_0800
207 * @tc.desc : Create ns through an interface, inheritance cannot be passed,
208 * and cannot be opened between generations.
209 * @tc.level : Level 2
210 */
dlns_inherit_0800(void)211 void dlns_inherit_0800(void)
212 {
213 Dl_namespace dlnsA, dlnsB, dlnsC;
214 dlns_init(&dlnsA, "dlns_inherit_0800_A");
215 dlns_init(&dlnsB, "dlns_inherit_0800_B");
216 dlns_init(&dlnsC, "dlns_inherit_0800_C");
217
218 EXPECT_EQ("dlns_inherit_0800", dlns_create(&dlnsA, NULL), EOK);
219 EXPECT_EQ("dlns_inherit_0800", dlns_create(&dlnsB, NULL), EOK);
220 EXPECT_EQ("dlns_inherit_0800", dlns_create(&dlnsC, path), EOK);
221
222 EXPECT_EQ("dlns_inherit_0800", dlns_set_namespace_separated("dlns_inherit_0800_A", true), EOK);
223 EXPECT_EQ("dlns_inherit_0800", dlns_set_namespace_separated("dlns_inherit_0800_B", true), EOK);
224 EXPECT_EQ("dlns_inherit_0800", dlns_set_namespace_separated("dlns_inherit_0800_C", true), EOK);
225
226 // A继承B, B继承C
227 EXPECT_EQ("dlns_inherit_0800", dlns_inherit(&dlnsB, &dlnsC, NULL), EOK);
228 EXPECT_EQ("dlns_inherit_0800", dlns_inherit(&dlnsA, &dlnsB, NULL), EOK);
229
230 void* handle = dlopen_ns(&dlnsB, dllName_inh_008, RTLD_LAZY);
231 EXPECT_TRUE("dlns_inherit_0800", handle);
232 dlclose(handle);
233
234 handle = dlopen_ns(&dlnsA, dllName_inh_008, RTLD_LAZY);
235 EXPECT_FALSE("dlns_inherit_0800", handle);
236 }
237
238 /**
239 * @tc.name : dlns_inherit_0900
240 * @tc.desc : Inherit multiple namespaces
241 * @tc.level : Level 2
242 */
dlns_inherit_0900(void)243 void dlns_inherit_0900(void)
244 {
245 Dl_namespace dlnsA, dlnsB, dlnsC, dlnsD;
246 dlns_init(&dlnsA, "dlns_inherit_0900_A");
247 dlns_init(&dlnsB, "dlns_inherit_0900_B");
248 dlns_init(&dlnsC, "dlns_inherit_0900_C");
249 dlns_init(&dlnsD, "dlns_inherit_0900_D");
250
251 EXPECT_EQ("dlns_inherit_0900", dlns_create(&dlnsA, NULL), EOK);
252 EXPECT_EQ("dlns_inherit_0900", dlns_create(&dlnsB, pathB), EOK);
253 EXPECT_EQ("dlns_inherit_0900", dlns_create(&dlnsC, pathC), EOK);
254 EXPECT_EQ("dlns_inherit_0900", dlns_create(&dlnsD, pathD), EOK);
255
256 EXPECT_EQ("dlns_inherit_0900", dlns_set_namespace_separated("dlns_inherit_0900_A", true), EOK);
257 EXPECT_EQ("dlns_inherit_0900", dlns_set_namespace_separated("dlns_inherit_0900_B", true), EOK);
258 EXPECT_EQ("dlns_inherit_0900", dlns_set_namespace_separated("dlns_inherit_0900_C", true), EOK);
259 EXPECT_EQ("dlns_inherit_0900", dlns_set_namespace_separated("dlns_inherit_0900_D", true), EOK);
260
261 EXPECT_EQ("dlns_inherit_0900", dlns_inherit(&dlnsA, &dlnsB, NULL), EOK);
262 EXPECT_EQ("dlns_inherit_0900", dlns_inherit(&dlnsA, &dlnsC, NULL), EOK);
263 EXPECT_EQ("dlns_inherit_0900", dlns_inherit(&dlnsA, &dlnsD, NULL), EOK);
264
265 void* handle1 = dlopen_ns(&dlnsA, libB, RTLD_LAZY);
266 EXPECT_TRUE("dlns_inherit_0900", handle1);
267 dlclose(handle1);
268
269 void* handle2 = dlopen_ns(&dlnsA, libC, RTLD_LAZY);
270 EXPECT_TRUE("dlns_inherit_0900", handle2);
271 dlclose(handle2);
272
273 void* handle3 = dlopen_ns(&dlnsA, libD, RTLD_LAZY);
274 EXPECT_TRUE("dlns_inherit_0900", handle3);
275 dlclose(handle3);
276 }
277
278 /**
279 * @tc.name : dlns_inherit_1000
280 * @tc.desc : Inherited by multiple namespaces
281 * @tc.level : Level 2
282 */
dlns_inherit_1000(void)283 void dlns_inherit_1000(void)
284 {
285 Dl_namespace dlnsA, dlnsB, dlnsC, dlnsD;
286 dlns_init(&dlnsA, "dlns_inherit_1000_A");
287 dlns_init(&dlnsB, "dlns_inherit_1000_B");
288 dlns_init(&dlnsC, "dlns_inherit_1000_C");
289 dlns_init(&dlnsD, "dlns_inherit_1000_D");
290
291 EXPECT_EQ("dlns_inherit_1000", dlns_create(&dlnsA, path), EOK);
292 EXPECT_EQ("dlns_inherit_1000", dlns_create(&dlnsB, NULL), EOK);
293 EXPECT_EQ("dlns_inherit_1000", dlns_create(&dlnsC, NULL), EOK);
294 EXPECT_EQ("dlns_inherit_1000", dlns_create(&dlnsD, NULL), EOK);
295
296 EXPECT_EQ("dlns_inherit_1000", dlns_set_namespace_separated("dlns_inherit_1000_A", true), EOK);
297 EXPECT_EQ("dlns_inherit_1000", dlns_set_namespace_separated("dlns_inherit_1000_B", true), EOK);
298 EXPECT_EQ("dlns_inherit_1000", dlns_set_namespace_separated("dlns_inherit_1000_C", true), EOK);
299 EXPECT_EQ("dlns_inherit_1000", dlns_set_namespace_separated("dlns_inherit_1000_D", true), EOK);
300
301 EXPECT_EQ("dlns_inherit_1000", dlns_inherit(&dlnsB, &dlnsA, NULL), EOK);
302 EXPECT_EQ("dlns_inherit_1000", dlns_inherit(&dlnsC, &dlnsA, NULL), EOK);
303 EXPECT_EQ("dlns_inherit_1000", dlns_inherit(&dlnsD, &dlnsA, NULL), EOK);
304
305 void* handle1 = dlopen_ns(&dlnsB, dllName, RTLD_LAZY);
306 EXPECT_TRUE("dlns_inherit_1000", handle1);
307 dlclose(handle1);
308
309 void* handle2 = dlopen_ns(&dlnsC, dllName, RTLD_LAZY);
310 EXPECT_TRUE("dlns_inherit_1000", handle2);
311 dlclose(handle2);
312
313 void* handle3 = dlopen_ns(&dlnsD, dllName, RTLD_LAZY);
314 EXPECT_TRUE("dlns_inherit_1000", handle3);
315 dlclose(handle3);
316 }
317
318 /**
319 * @tc.name : dlns_inherit_1100
320 * @tc.desc : When repeating inheritance, replace the original inheritance relationship
321 * @tc.level : Level 2
322 */
dlns_inherit_1100(void)323 void dlns_inherit_1100(void)
324 {
325 Dl_namespace dlnsA, dlnsB;
326 dlns_init(&dlnsA, "dlns_inherit_1100_A");
327 dlns_init(&dlnsB, "dlns_inherit_1100_B");
328
329 EXPECT_EQ("dlns_inherit_1100", dlns_create(&dlnsA, NULL), EOK);
330 EXPECT_EQ("dlns_inherit_1100", dlns_create(&dlnsB, NULL), EOK);
331
332 EXPECT_EQ("dlns_inherit_1100", dlns_set_namespace_separated("dlns_inherit_1100_A", true), EOK);
333 EXPECT_EQ("dlns_inherit_1100", dlns_set_namespace_separated("dlns_inherit_1100_B", true), EOK);
334
335 EXPECT_EQ("dlns_inherit_1100", dlns_inherit(&dlnsA, &dlnsB, NULL), EOK);
336 void* handle = dlopen_ns(&dlnsA, dllName_inh_011, RTLD_LAZY);
337 EXPECT_FALSE("dlns_inherit_1100", handle);
338
339 EXPECT_EQ("dlns_inherit_1100", dlns_set_namespace_allowed_libs("dlns_inherit_1100_B", dllName_inh_011), EOK);
340 EXPECT_EQ("dlns_inherit_1100", dlns_set_namespace_permitted_paths("dlns_inherit_1100_B", dllNamePath), EOK);
341 EXPECT_EQ("dlns_inherit_1100", dlns_set_namespace_lib_path("dlns_inherit_1100_B", path), EOK);
342
343 // AGIN
344 EXPECT_EQ("dlns_inherit_1100", dlns_inherit(&dlnsA, &dlnsB, NULL), EOK);
345
346 handle = dlopen_ns(&dlnsA, dllName_inh_011, RTLD_LAZY);
347 EXPECT_TRUE("dlns_inherit_1100", handle);
348 dlclose(handle);
349 }
350
351 TEST_FUN G_Fun_Array[] = {
352 dlns_inherit_0100,
353 dlns_inherit_0200,
354 dlns_inherit_0300,
355 dlns_inherit_0400,
356 dlns_inherit_0500,
357 dlns_inherit_0600,
358 dlns_inherit_0700,
359 dlns_inherit_0800,
360 dlns_inherit_0900,
361 dlns_inherit_1000,
362 dlns_inherit_1100,
363 };
main(void)364 int main(void)
365 {
366 int num = sizeof(G_Fun_Array)/sizeof(TEST_FUN);
367 for (int pos = 0; pos < num; ++pos) {
368 G_Fun_Array[pos]();
369 }
370
371 return t_status;
372 }