• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @tc.name      : dlns_set_namespace_lib_path_0100
26  * @tc.desc      : When name is no namespace created, call dlns_set_namespace_lib_path
27  * @tc.level     : Level 2
28  */
dlns_set_namespace_lib_path_0100(void)29 void dlns_set_namespace_lib_path_0100(void)
30 {
31     EXPECT_EQ("dlns_set_namespace_lib_path_0100",
32         dlns_set_namespace_lib_path("dlns_set_namespace_lib_path_0100", path), ENOKEY);
33 }
34 
35 /**
36  * @tc.name      : dlns_set_namespace_lib_path_0200
37  * @tc.desc      : When name is a created namespace, call dlns_set_namespace_lib_path
38  * @tc.level     : Level 2
39  */
dlns_set_namespace_lib_path_0200(void)40 void dlns_set_namespace_lib_path_0200(void)
41 {
42     Dl_namespace dlns;
43     dlns_init(&dlns, "dlns_set_namespace_lib_path_0200");
44 
45     EXPECT_EQ("dlns_set_namespace_lib_path_0200", dlns_create(&dlns, NULL), EOK);
46 
47     void* handle = dlopen_ns(&dlns, dllName_set_002, RTLD_LAZY);
48     EXPECT_FALSE("dlns_set_namespace_lib_path_0200", handle);
49 
50     EXPECT_EQ("dlns_set_namespace_lib_path_0200",
51         dlns_set_namespace_lib_path("dlns_set_namespace_lib_path_0200", path), EOK);
52 
53     handle = dlopen_ns(&dlns, dllName_set_002, RTLD_LAZY);
54     EXPECT_TRUE("dlns_set_namespace_lib_path_0200", handle);
55     dlclose(handle);
56 }
57 
58 /**
59  * @tc.name      : dlns_set_namespace_lib_path_0300
60  * @tc.desc      : When name=NULL, call dlns_set_namespace_lib_path
61  * @tc.level     : Level 2
62  */
dlns_set_namespace_lib_path_0300(void)63 void dlns_set_namespace_lib_path_0300(void)
64 {
65     EXPECT_EQ("dlns_set_namespace_lib_path_0300", dlns_set_namespace_lib_path(NULL, path), EINVAL);
66 }
67 
68 /**
69  * @tc.name      : dlns_set_namespace_lib_path_0400
70  * @tc.desc      : When lib_path=NULL, call dlns_set_namespace_lib_path
71  * @tc.level     : Level 2
72  */
dlns_set_namespace_lib_path_0400(void)73 void dlns_set_namespace_lib_path_0400(void)
74 {
75     Dl_namespace dlns;
76     dlns_init(&dlns, "dlns_set_namespace_lib_path_0400");
77 
78     EXPECT_EQ("dlns_set_namespace_lib_path_0400", dlns_create(&dlns, NULL), EOK);
79 
80     EXPECT_EQ("dlns_set_namespace_lib_path_0400",
81         dlns_set_namespace_lib_path("dlns_set_namespace_lib_path_0400", NULL), EINVAL);
82 }
83 
84 /**
85  * @tc.name      : dlns_set_namespace_lib_path_0500
86  * @tc.desc      : When the lib_path parameter is multiple paths, call dlns_set_namespace_lib_path
87  * @tc.level     : Level 1
88  */
dlns_set_namespace_lib_path_0500(void)89 void dlns_set_namespace_lib_path_0500(void)
90 {
91     Dl_namespace dlns;
92     dlns_init(&dlns, "dlns_set_namespace_lib_path_0500");
93 
94     EXPECT_EQ("dlns_set_namespace_lib_path_0500", dlns_create(&dlns, NULL), EOK);
95 
96     char* newlibpath = "/data/tests/libc-test/src/functionalext/dlns:/data/tests/libc-test/src/functionalext/dlns/B";
97 
98     EXPECT_EQ("dlns_set_namespace_lib_path_0500",
99         dlns_set_namespace_lib_path("dlns_set_namespace_lib_path_0500", newlibpath), EOK);
100 
101     void *handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
102     EXPECT_TRUE("dlns_set_namespace_lib_path_0500", handle);
103     dlclose(handle);
104 
105     void *handle1 = dlopen_ns(&dlns, libB, RTLD_LAZY);
106     EXPECT_TRUE("dlns_set_namespace_lib_path_0500", handle1);
107     dlclose(handle1);
108 }
109 
110 /**
111  * @tc.name      : dlns_set_namespace_separated_0100
112  * @tc.desc      : When name is no namespace created, call dlns_set_namespace_separated
113  * @tc.level     : Level 2
114  */
dlns_set_namespace_separated_0100(void)115 void dlns_set_namespace_separated_0100(void)
116 {
117     EXPECT_EQ("dlns_set_namespace_separated_0100",
118         dlns_set_namespace_separated("dlns_set_namespace_permitted_paths_0100", true), ENOKEY);
119 }
120 
121 /**
122  * @tc.name      : dlns_set_namespace_separated_0200
123  * @tc.desc      : When the name is a created namespace, call dlns_set_namespace_separated
124  * @tc.level     : Level 1
125  */
dlns_set_namespace_separated_0200(void)126 void dlns_set_namespace_separated_0200(void)
127 {
128     Dl_namespace dlns;
129     dlns_init(&dlns, "dlns_set_namespace_separated_0200");
130 
131     EXPECT_EQ("dlns_set_namespace_separated_0200", dlns_create(&dlns, NULL), EOK);
132 
133     void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
134     EXPECT_TRUE("dlns_set_namespace_separated_0200", handle);
135     dlclose(handle);
136 
137     EXPECT_EQ("dlns_set_namespace_separated_0200",
138         dlns_set_namespace_separated("dlns_set_namespace_separated_0200", true), EOK);
139 
140     void* handle1 = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
141     EXPECT_FALSE("dlns_set_namespace_separated_0200", handle1);
142 }
143 
144 /**
145  * @tc.name      : dlns_set_namespace_separated_0300
146  * @tc.desc      : When name=NULL, call dlns_set_namespace_separated
147  * @tc.level     : Level 2
148  */
dlns_set_namespace_separated_0300(void)149 void dlns_set_namespace_separated_0300(void)
150 {
151     EXPECT_EQ("dlns_set_namespace_separated_0300", dlns_set_namespace_separated(NULL, true), EINVAL);
152 }
153 
154 /**
155  * @tc.name      : dlns_set_namespace_permitted_paths_0100
156  * @tc.desc      : When name is no namespace created, call dlns_set_namespace_permitted_paths
157  * @tc.level     : Level 2
158  */
dlns_set_namespace_permitted_paths_0100(void)159 void dlns_set_namespace_permitted_paths_0100(void)
160 {
161     EXPECT_EQ("dlns_set_namespace_permitted_paths_0100",
162         dlns_set_namespace_permitted_paths("dlns_set_namespace_permitted_paths_0100", dllNamePath), ENOKEY);
163 }
164 
165 /**
166  * @tc.name      : dlns_set_namespace_permitted_paths_0200
167  * @tc.desc      : name为已创建命名空间时,调用dlns_set_namespace_permitted_paths
168  * @tc.level     : Level 2
169  */
dlns_set_namespace_permitted_paths_0200(void)170 void dlns_set_namespace_permitted_paths_0200(void)
171 {
172     Dl_namespace dlns;
173     dlns_init(&dlns, "dlns_set_namespace_permitted_paths_0200");
174 
175     EXPECT_EQ("dlns_set_namespace_permitted_paths_0200", dlns_create(&dlns, NULL), EOK);
176 
177     EXPECT_EQ("dlns_set_namespace_permitted_paths_0200",
178         dlns_set_namespace_separated("dlns_set_namespace_permitted_paths_0200", true), EOK);
179 
180     void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
181     EXPECT_FALSE("dlns_set_namespace_permitted_paths_0200", handle);
182 
183     EXPECT_EQ("dlns_set_namespace_permitted_paths_0200",
184         dlns_set_namespace_permitted_paths("dlns_set_namespace_permitted_paths_0200", path), EOK);
185 
186     void* handle1 = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
187     EXPECT_TRUE("dlns_set_namespace_permitted_paths_0200", handle1);
188     dlclose(handle1);
189 }
190 
191 /**
192  * @tc.name      : dlns_set_namespace_permitted_paths_0300
193  * @tc.desc      : When the name is a created namespace, call dlns_set_namespace_permitted_paths
194  * @tc.level     : Level 1
195  */
dlns_set_namespace_permitted_paths_0300(void)196 void dlns_set_namespace_permitted_paths_0300(void)
197 {
198     Dl_namespace dlns;
199     dlns_init(&dlns, "dlns_set_namespace_permitted_paths_0300");
200 
201     char *newLibPath = "/data/tests/libc-test/src/functionalext/dlns/B/libB.so";
202 
203     EXPECT_EQ("dlns_set_namespace_permitted_paths_0300", dlns_create(&dlns, NULL), EOK);
204 
205     EXPECT_EQ("dlns_set_namespace_permitted_paths_0300",
206         dlns_set_namespace_separated("dlns_set_namespace_permitted_paths_0300", true), EOK);
207 
208     void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
209     EXPECT_FALSE("dlns_set_namespace_permitted_paths_0300", handle);
210 
211     EXPECT_EQ("dlns_set_namespace_permitted_paths_0300",
212         dlns_set_namespace_permitted_paths("dlns_set_namespace_permitted_paths_0300", path), EOK);
213 
214     void* handle1 = dlopen_ns(&dlns, newLibPath, RTLD_LAZY);
215     EXPECT_TRUE("dlns_set_namespace_permitted_paths_0300", handle1);
216     dlclose(handle1);
217 }
218 
219 /**
220  * @tc.name      : dlns_set_namespace_permitted_paths_0400
221  * @tc.desc      : When name=NULL, call dlns_set_namespace_permitted_paths
222  * @tc.level     : Level 2
223  */
dlns_set_namespace_permitted_paths_0400(void)224 void dlns_set_namespace_permitted_paths_0400(void)
225 {
226     EXPECT_EQ("dlns_set_namespace_permitted_paths_0400",
227         dlns_set_namespace_permitted_paths(NULL, dllNamePath), EINVAL);
228 }
229 
230 /**
231  * @tc.name      : dlns_set_namespace_permitted_paths_0500
232  * @tc.desc      : When the permitted_paths parameter is multiple paths, call dlns_set_namespace_permitted_paths
233  * @tc.level     : Level 1
234  */
dlns_set_namespace_permitted_paths_0500(void)235 void dlns_set_namespace_permitted_paths_0500(void)
236 {
237     Dl_namespace dlns;
238     dlns_init(&dlns, "dlns_set_namespace_permitted_paths_0500");
239 
240     char* newPermittPath = "/data/tests/libc-test/src/functionalext";
241     char* libBpath = "/data/tests/libc-test/src/functionalext/dlns/B/libB.so";
242 
243     EXPECT_EQ("dlns_set_namespace_permitted_paths_0500", dlns_create(&dlns, NULL), EOK);
244 
245     EXPECT_EQ("dlns_set_namespace_permitted_paths_0500",
246         dlns_set_namespace_separated("dlns_set_namespace_permitted_paths_0500", true), EOK);
247 
248     void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
249     EXPECT_FALSE("dlns_set_namespace_permitted_paths_0500", handle);
250 
251     void* handle1 = dlopen_ns(&dlns, libBpath, RTLD_LAZY);
252     EXPECT_FALSE("dlns_set_namespace_permitted_paths_0500", handle1);
253 
254     EXPECT_EQ("dlns_set_namespace_permitted_paths_0500",
255         dlns_set_namespace_permitted_paths("dlns_set_namespace_permitted_paths_0500", newPermittPath), EOK);
256 
257     void* handle2 = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
258     EXPECT_TRUE("dlns_set_namespace_permitted_paths_0500", handle2);
259     dlclose(handle2);
260 
261     void* handle3 = dlopen_ns(&dlns, libBpath, RTLD_LAZY);
262     EXPECT_TRUE("dlns_set_namespace_permitted_paths_0500", handle3);
263     dlclose(handle3);
264 }
265 
266 /**
267  * @tc.name      : dlns_set_namespace_allowed_libs_0100
268  * @tc.desc      : When name is no namespace created, call dlns_set_namespace_allowed_libs
269  * @tc.level     : Level 2
270  */
dlns_set_namespace_allowed_libs_0100(void)271 void dlns_set_namespace_allowed_libs_0100(void)
272 {
273     // 未创建的命名空间
274     EXPECT_EQ("dlns_set_namespace_allowed_libs_0100",
275         dlns_set_namespace_allowed_libs("dlns_set_namespace_allowed_libs_0100", path), ENOKEY);
276 }
277 
278 /**
279  * @tc.name      : dlns_set_namespace_allowed_libs_0200
280  * @tc.desc      : When the name is a created namespace, call dlns_set_namespace_allowed_libs
281  * @tc.level     : Level 1
282  */
dlns_set_namespace_allowed_libs_0200(void)283 void dlns_set_namespace_allowed_libs_0200(void)
284 {
285     Dl_namespace dlns;
286     dlns_init(&dlns, "ns_no_allowed_libs");
287 
288     void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
289     EXPECT_TRUE("dlns_set_namespace_allowed_libs_0200", handle);
290     dlclose(handle);
291 
292     EXPECT_EQ("dlns_set_namespace_allowed_libs_0200",
293         dlns_set_namespace_allowed_libs("ns_no_allowed_libs", dllName2), EOK);
294 
295     void* handle1 = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
296     EXPECT_FALSE("dlns_set_namespace_allowed_libs_0200", handle1);
297 
298     EXPECT_EQ("dlns_set_namespace_allowed_libs_0200",
299         dlns_set_namespace_allowed_libs("ns_no_allowed_libs", dllName), EOK);
300 }
301 
302 /**
303  * @tc.name      : dlns_set_namespace_allowed_libs_0300
304  * @tc.desc      : When name=NULL, call dlns_set_namespace_allowed_libs
305  * @tc.level     : Level 2
306  */
dlns_set_namespace_allowed_libs_0300(void)307 void dlns_set_namespace_allowed_libs_0300(void)
308 {
309     EXPECT_EQ("dlns_set_namespace_allowed_libs_0300", dlns_set_namespace_allowed_libs(NULL, path), EINVAL);
310 }
311 
312 /**
313  * @tc.name      : dlns_set_namespace_allowed_libs_0400
314  * @tc.desc      : When the allowed_libs parameter is multiple file names, call dlns_set_namespace_allowed_libs
315  * @tc.level     : Level 1
316  */
dlns_set_namespace_allowed_libs_0400(void)317 void dlns_set_namespace_allowed_libs_0400(void)
318 {
319     Dl_namespace dlns;
320     dlns_init(&dlns, "ns_no_allowed_libs");
321 
322     void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
323     EXPECT_TRUE("dlns_set_namespace_allowed_libs_0400", handle);
324     dlclose(handle);
325 
326     char* dll4 = "libdlopen_ns_dso.so:libdlopen_dso.so:sharedlibtest.so";
327 
328     EXPECT_EQ("dlns_set_namespace_allowed_libs_0400",
329         dlns_set_namespace_allowed_libs("ns_no_allowed_libs", dll4), EOK);
330 
331     void* handle1 = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
332     EXPECT_TRUE("dlns_set_namespace_allowed_libs_0400", handle1);
333     dlclose(handle1);
334 }
335 
336 TEST_FUN G_Fun_Array[] = {
337     dlns_set_namespace_lib_path_0100,
338     dlns_set_namespace_lib_path_0200,
339     dlns_set_namespace_lib_path_0300,
340     dlns_set_namespace_lib_path_0400,
341     dlns_set_namespace_lib_path_0500,
342     dlns_set_namespace_separated_0100,
343     dlns_set_namespace_separated_0200,
344     dlns_set_namespace_separated_0300,
345     dlns_set_namespace_permitted_paths_0100,
346     dlns_set_namespace_permitted_paths_0200,
347     dlns_set_namespace_permitted_paths_0300,
348     dlns_set_namespace_permitted_paths_0400,
349     dlns_set_namespace_permitted_paths_0500,
350     dlns_set_namespace_allowed_libs_0100,
351     dlns_set_namespace_allowed_libs_0200,
352     dlns_set_namespace_allowed_libs_0300,
353     dlns_set_namespace_allowed_libs_0400,
354 };
355 
main(void)356 int main(void)
357 {
358     int num = sizeof(G_Fun_Array)/sizeof(TEST_FUN);
359     for (int pos = 0; pos < num; ++pos) {
360         G_Fun_Array[pos]();
361     }
362 
363     return t_status;
364 }