• 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 /**
26  * @tc.name      : dlopen_0100
27  * @tc.desc      : Enter a valid path, dlopen compatibility test
28  * @tc.level     : Level 0
29  */
dlopen_0100(void)30 void dlopen_0100(void)
31 {
32     void* handle = dlopen(dllNamePath, RTLD_LAZY);
33     EXPECT_TRUE("dlopen_0100", handle);
34     dlclose(handle);
35 }
36 
37 
38 /**
39  * @tc.name      : dlopen_0200
40  * @tc.desc      : Enter invalid path, dlopen compatibility test
41  * @tc.level     : Level 2
42  */
dlopen_0200(void)43 void dlopen_0200(void)
44 {
45     void* handle = dlopen(errPath_ns, RTLD_LAZY);
46     EXPECT_FALSE("dlopen_0200", handle);
47 }
48 
49 /**
50  * @tc.name      : dlns_init_0100
51  * @tc.desc      : Use a string less than 255 bytes as the dlns_init parameter
52  * @tc.level     : Level 1
53  */
dlns_init_0100(void)54 void dlns_init_0100(void)
55 {
56     Dl_namespace dlns;
57     char n[] = "dlns_init_0100";
58     dlns_init(&dlns, n);
59     EXPECT_EQ("dlns_init_0100", strcmp(dlns.name, n), 0);
60 }
61 
62 /**
63  * @tc.name      : dlns_init_0200
64  * @tc.desc      : Use very long strings as dlns_init parameters
65  * @tc.level     : Level 2
66  */
dlns_init_0200(void)67 void dlns_init_0200(void)
68 {
69     Dl_namespace dlns;
70     int size = 256;
71     char n[] = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 \
72                 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 \
73                 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 \
74                 012345678901234567890123456789";
75     int len = strlen(n);
76     dlns_init(&dlns, n);
77     EXPECT_TRUE("dlns_init_0200", size < len);
78 
79     int dlnslen = strlen(dlns.name);
80     EXPECT_TRUE("dlns_init_0200", size > dlnslen);
81 }
82 
83 /**
84  * @tc.name      : dlns_create_0100
85  * @tc.desc      : Using dlns_create, create a new namespace
86  * @tc.level     : Level 1
87  */
dlns_create_0100(void)88 void dlns_create_0100(void)
89 {
90     Dl_namespace dlns;
91     dlns_init(&dlns, "dlns_create_0100");
92 
93     EXPECT_EQ("dlns_create_0100", dlns_create(&dlns, path), EOK);
94 }
95 
96 /**
97  * @tc.name      : dlns_create_0200
98  * @tc.desc      : Using dlns_create, create a new namespace
99  * @tc.level     : Level 2
100  */
dlns_create_0200(void)101 void dlns_create_0200(void)
102 {
103     // ns_no_allowed_libs already exists
104     Dl_namespace dlns;
105     dlns_init(&dlns, "ns_no_allowed_libs");
106 
107     EXPECT_EQ("dlns_create_0200", dlns_create(&dlns, NULL), EEXIST);
108 }
109 
110 /**
111  * @tc.name      : dlns_create_0300
112  * @tc.desc      : After creating a new namespace, check lib_path validity
113  * @tc.level     : Level 1
114  */
dlns_create_0300(void)115 void dlns_create_0300(void)
116 {
117     // ns_no_allowed_libs already exists
118     Dl_namespace dlns;
119     dlns_init(&dlns, "dlns_create_0300");
120 
121     EXPECT_EQ("dlns_create_0300", dlns_create(&dlns, path), EOK);
122 
123     void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
124     EXPECT_TRUE("dlns_create_0300", handle);
125     dlclose(handle);
126 }
127 
128 /**
129  * @tc.name      : dlns_create_0400
130  * @tc.desc      : Namespace creation when dlns=NULL
131  * @tc.level     : Level 2
132  */
dlns_create_0400(void)133 void dlns_create_0400(void)
134 {
135     EXPECT_EQ("dlns_create_0400", dlns_create(NULL, path), EINVAL);
136 }
137 
138 /**
139  * @tc.name      : dlns_create_0500
140  * @tc.desc      : Namespace creation when lib_path=NULL
141  * @tc.level     : Level 2
142  */
dlns_create_0500(void)143 void dlns_create_0500(void)
144 {
145     Dl_namespace dlns;
146     dlns_init(&dlns, "dlns_create_0500");
147 
148     EXPECT_EQ("dlns_create_0500", dlns_create(&dlns, NULL), EOK);
149 }
150 
151 
152 /**
153  * @tc.name      : dlopen_ns_0100
154  * @tc.desc      : dlopen_ns valid full path test
155  * @tc.level     : Level 1
156  */
dlopen_ns_0100(void)157 void dlopen_ns_0100(void)
158 {
159     Dl_namespace dlns;
160     dlns_init(&dlns, "ns_no_allowed_libs");
161 
162     void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
163     EXPECT_TRUE("dlopen_ns_0100", handle);
164     dlclose(handle);
165 }
166 
167 /**
168  * @tc.name      : dlopen_ns_0200
169  * @tc.desc      : dlopen_ns invalid full path test
170  * @tc.level     : Level 2
171  */
dlopen_ns_0200(void)172 void dlopen_ns_0200(void)
173 {
174     Dl_namespace dlns;
175     dlns_init(&dlns, "ns_no_allowed_libs");
176 
177     void* handle = dlopen_ns(&dlns, errdllNamePath, RTLD_LAZY);
178     EXPECT_FALSE("dlopen_ns_0200", handle);
179 }
180 
181 /**
182  * @tc.name      : dlopen_ns_0300
183  * @tc.desc      : dlopen_ns valid short name test
184  * @tc.level     : Level 1
185  */
dlopen_ns_0300(void)186 void dlopen_ns_0300(void)
187 {
188     Dl_namespace dlns;
189     dlns_init(&dlns, "ns_no_allowed_libs");
190 
191     void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
192     EXPECT_TRUE("dlopen_ns_0300", handle);
193     dlclose(handle);
194 }
195 
196 /**
197  * @tc.name      : dlopen_ns_0400
198  * @tc.desc      : When dlns=NULL, call dlopen_ns to load the library
199  * @tc.level     : Level 1
200  */
dlopen_ns_0400(void)201 void dlopen_ns_0400(void)
202 {
203     void* handle = dlopen_ns(NULL, dllNamePath, RTLD_LAZY);
204     EXPECT_TRUE("dlopen_ns_0400", handle);
205     dlclose(handle);
206 }
207 
208 /**
209  * @tc.name      : dlopen_ns_0500
210  * @tc.desc      : When file=NULL, call dlopen_ns to load the library
211  * @tc.level     : Level 2
212  */
dlopen_ns_0500(void)213 void dlopen_ns_0500(void)
214 {
215     Dl_namespace dlns;
216     dlns_init(&dlns, "ns_no_allowed_libs");
217 
218     void *handle = dlopen_ns(&dlns, NULL, RTLD_LAZY);
219     EXPECT_TRUE("dlopen_ns_0500", handle);
220     dlclose(handle);
221 }
222 
223 /**
224  * @tc.name      : dlns_get_0100
225  * @tc.desc      : Gets an existing namespace handle.
226  * @tc.level     : Level 1
227  */
dlns_get_0100(void)228 void dlns_get_0100(void)
229 {
230     Dl_namespace dlns;
231     dlns_init(&dlns, "dlns_get_0100");
232     dlns_create(&dlns, NULL);
233     dlns_get("dlns_get_0100", &dlns);
234     EXPECT_EQ("dlns_get_0100", strcmp(dlns.name, "dlns_get_0100"), 0);
235 }
236 
237 /**
238  * @tc.name      : dlns_get_0200
239  * @tc.desc      : Gets the current namespace handle when name=NULL.
240  * @tc.level     : Level 1
241  */
dlns_get_0200(void)242 void dlns_get_0200(void)
243 {
244     Dl_namespace dlns;
245     dlns_get(NULL, &dlns);
246     EXPECT_EQ("dlns_get_0200", strcmp(dlns.name, "default"), 0);
247 }
248 
249 /**
250  * @tc.name      : dlns_get_0300
251  * @tc.desc      : Call dlns_get when dlns=NULL.
252  * @tc.level     : Level 2
253  */
dlns_get_0300(void)254 void dlns_get_0300(void)
255 {
256     dlns_get(NULL, NULL);
257     EXPECT_EQ("dlns_get_0300", dlns_get(NULL, NULL), EINVAL);
258 }
259 
260 /**
261  * @tc.name      : dlns_get_0400
262  * @tc.desc      : Gets a namespace handle that does not exist.
263  * @tc.level     : Level 2
264  */
dlns_get_0400(void)265 void dlns_get_0400(void)
266 {
267     Dl_namespace dlns;
268     dlns_init(&dlns, "dlns_get_0400");
269     EXPECT_EQ("dlns_get_0400", dlns_get("dlns_get_0400", &dlns), ENOKEY);
270 }
271 
272 /**
273  * @tc.name      : dlopen_add_reloc
274  * @tc.desc      : Test add_reloc_can_search_dso: not enough memory.
275  *                 Dlopen lib whth more than 32 dependent libs.(libace.z.so)
276  * @tc.level     : Level 1
277  */
dlopen_add_reloc(void)278 void dlopen_add_reloc(void)
279 {
280     void* handle = dlopen(dllAcePath, RTLD_LAZY);
281     EXPECT_TRUE("dlopen_add_reloc", handle);
282 }
283 
284 /**
285  * @tc.name      : dlopen_relocation
286  * @tc.desc      : Test dlopen lib relocation type is REL_DTPMOD/REL_DTPOFF.(libstd.dylib.so)
287  * @tc.level     : Level 1
288  */
dlopen_relocation(void)289 void dlopen_relocation(void)
290 {
291     void* handle = dlopen(dllDylibPath, RTLD_LAZY);
292     EXPECT_TRUE("dlopen_relocation", handle);
293     if(handle){
294         dlclose(handle);
295     }
296 }
297 
298 /**
299  * @tc.name      : dlopen_hash_type
300  * @tc.desc      : Test dlopen lib hash type is sysv.(libdlopen_hash_sysv.so)
301  * @tc.level     : Level 1
302  */
dlopen_hash_type(void)303 void dlopen_hash_type(void)
304 {
305     void* handle = dlopen(dllHashsysvPath, RTLD_LAZY);
306     EXPECT_TRUE("dlopen_hash_type", handle);
307     if(handle){
308         dlclose(handle);
309     }
310 }
311 
312 /**
313  * @tc.name      : dlopen_set_rpath
314  * @tc.desc      : Test dlopen lib by set rpath.(libdlopen_hash_sysv.so)
315  * @tc.level     : Level 1
316  */
dlopen_set_rpath(void)317 void dlopen_set_rpath(void)
318 {
319     void* handle = dlopen(dllHashsysv, RTLD_LAZY);
320     EXPECT_TRUE("dlopen_set_rpath", handle);
321     if(handle){
322         dlclose(handle);
323     }
324 }
325 
326 /**
327  * @tc.name      : dlopen_fill_random
328  * @tc.desc      : Test dlopen lib libdlopen_fill_random.so to coverage fill_random_data.
329  * @tc.level     : Level 1
330  */
dlopen_fill_random(void)331 void dlopen_fill_random(void)
332 {
333     void* handle = dlopen(dllFillRandom, RTLD_LAZY);
334     EXPECT_TRUE("dlopen_fill_random", handle);
335     if(handle){
336         dlclose(handle);
337     }
338 }
339 
340 TEST_FUN G_Fun_Array[] = {
341     dlopen_0100,
342     dlopen_0200,
343     dlns_init_0100,
344     dlns_init_0200,
345     dlns_create_0100,
346     dlns_create_0200,
347     dlns_create_0300,
348     dlns_create_0400,
349     dlns_create_0500,
350     dlopen_ns_0100,
351     dlopen_ns_0200,
352     dlopen_ns_0300,
353     dlopen_ns_0400,
354     dlopen_ns_0500,
355     dlns_get_0100,
356     dlns_get_0200,
357     dlns_get_0300,
358     dlns_get_0400,
359     dlopen_relocation,
360     dlopen_hash_type,
361     dlopen_set_rpath,
362     dlopen_fill_random,
363     dlopen_add_reloc,
364 };
365 
main(void)366 int main(void)
367 {
368     int num = sizeof(G_Fun_Array)/sizeof(TEST_FUN);
369     for (int pos = 0; pos < num; ++pos) {
370         G_Fun_Array[pos]();
371     }
372 
373     return t_status;
374 }