• 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 #include <errno.h>
16 #include "test.h"
17 #include "dlfcn.h"
18 #include "dlfcn_ext.h"
19 #include "namespace.h"
20 #include "ns_config.h"
21 #include "dynlink_rand.h"
22 #include "functionalext.h"
23 #include "ld_log.h"
24 #include "dynlink.h"
25 #include "link.h"
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 
29 #include <fcntl.h>
30 #include <libc.h>
31 
32 typedef void (*TEST_FUN)(void);
33 int handle_asan_path_open(int fd, const char *name, ns_t *namespace, char *buf, size_t buf_size);
34 
35 /**
36  * @tc.name      : dynlink_test_0010
37  * @tc.desc      : Test dlns_set_namespace_lib_path with invalid input
38  * @tc.level     : Level 2
39  */
dynlink_test_0010(void)40 static void dynlink_test_0010(void)
41 {
42     int ret = dlns_set_namespace_lib_path(NULL, NULL);
43     EXPECT_EQ(__FUNCTION__, ret, EINVAL);
44 }
45 
46 /**
47  * @tc.name      : dynlink_test_0020
48  * @tc.desc      : Test dlns_set_namespace_lib_path with invalid input
49  * @tc.level     : Level 2
50  */
dynlink_test_0020(void)51 static void dynlink_test_0020(void)
52 {
53     int ret = dlns_set_namespace_lib_path("abc", "abc");
54     EXPECT_EQ(__FUNCTION__, ret, ENOKEY);
55 }
56 
57 /**
58  * @tc.name      : dynlink_test_0030
59  * @tc.desc      : Set path for default lib use dlns_set_namespace_lib_path
60  * @tc.level     : Level 1
61  */
dynlink_test_0030(void)62 static void dynlink_test_0030(void)
63 {
64     int ret = dlns_set_namespace_lib_path("default", "/data");
65     EXPECT_EQ(__FUNCTION__, ret, 0);
66 }
67 
68 /**
69  * @tc.name      : dynlink_test_0040
70  * @tc.desc      : Test dlns_set_namespace_separated with invalid input
71  * @tc.level     : Level 2
72  */
dynlink_test_0040(void)73 static void dynlink_test_0040(void)
74 {
75     int ret = dlns_set_namespace_separated(NULL, true);
76     EXPECT_EQ(__FUNCTION__, ret, EINVAL);
77 }
78 
79 /**
80  * @tc.name      : dynlink_test_0050
81  * @tc.desc      : Test dlns_set_namespace_separated with invalid input
82  * @tc.level     : Level 2
83  */
dynlink_test_0050(void)84 static void dynlink_test_0050(void)
85 {
86     int ret = dlns_set_namespace_separated("abc", true);
87     EXPECT_EQ(__FUNCTION__, ret, ENOKEY);
88 }
89 
90 /**
91  * @tc.name      : dynlink_test_0060
92  * @tc.desc      : Set separated for default lib use dlns_set_namespace_separated
93  * @tc.level     : Level 0
94  */
dynlink_test_0060(void)95 static void dynlink_test_0060(void)
96 {
97     int ret = dlns_set_namespace_separated("default", true);
98     EXPECT_EQ(__FUNCTION__, ret, 0);
99 }
100 
101 /**
102  * @tc.name      : dynlink_test_0070
103  * @tc.desc      : Test dlns_set_namespace_permitted_paths with invalid input
104  * @tc.level     : Level 2
105  */
dynlink_test_0070(void)106 static void dynlink_test_0070(void)
107 {
108     int ret = dlns_set_namespace_permitted_paths(NULL, NULL);
109     EXPECT_EQ(__FUNCTION__, ret, EINVAL);
110 }
111 
112 /**
113  * @tc.name      : dynlink_test_0080
114  * @tc.desc      : Test dlns_set_namespace_permitted_paths with invalid input
115  * @tc.level     : Level 2
116  */
dynlink_test_0080(void)117 static void dynlink_test_0080(void)
118 {
119     int ret = dlns_set_namespace_permitted_paths("abc", "abc");
120     EXPECT_EQ(__FUNCTION__, ret, ENOKEY);
121 }
122 
123 /**
124  * @tc.name      : dynlink_test_0090
125  * @tc.desc      : Set path for default lib use dlns_set_namespace_permitted_paths
126  * @tc.level     : Level 0
127  */
dynlink_test_0090(void)128 static void dynlink_test_0090(void)
129 {
130     int ret = dlns_set_namespace_permitted_paths("default", "/data");
131     EXPECT_EQ(__FUNCTION__, ret, 0);
132 }
133 
134 /**
135  * @tc.name      : dynlink_test_0100
136  * @tc.desc      : Test dlns_set_namespace_allowed_libs with invalid input
137  * @tc.level     : Level 2
138  */
dynlink_test_0100(void)139 static void dynlink_test_0100(void)
140 {
141     int ret = dlns_set_namespace_allowed_libs(NULL, NULL);
142     EXPECT_EQ(__FUNCTION__, ret, EINVAL);
143 }
144 
145 /**
146  * @tc.name      : dynlink_test_0110
147  * @tc.desc      : Test dlns_set_namespace_allowed_libs with invalid input
148  * @tc.level     : Level 2
149  */
dynlink_test_0110(void)150 static void dynlink_test_0110(void)
151 {
152     int ret = dlns_set_namespace_allowed_libs("abc", "abc");
153     EXPECT_EQ(__FUNCTION__, ret, ENOKEY);
154 }
155 
156 /**
157  * @tc.name      : dynlink_test_0120
158  * @tc.desc      : Set allowed libs for default lib use dlns_set_namespace_allowed_libs
159  * @tc.level     : Level 0
160  */
dynlink_test_0120(void)161 static void dynlink_test_0120(void)
162 {
163     int ret = dlns_set_namespace_allowed_libs("default", "/data");
164     EXPECT_EQ(__FUNCTION__, ret, 0);
165 }
166 
167 /**
168  * @tc.name      : dynlink_test_0002
169  * @tc.desc      : Test handle_asan_path_open with invalid input
170  * @tc.level     : Level 2
171  */
dynlink_test_0002(void)172 static void dynlink_test_0002(void)
173 {
174     int ret = handle_asan_path_open(2, NULL, NULL, NULL, 0);
175     EXPECT_EQ(__FUNCTION__, ret, 2);
176 }
177 
178 
179 /**
180  * @tc.name      : dynlink_test_0003
181  * @tc.desc      : Set asan lib path use handle_asan_path_open
182  * @tc.level     : Level 0
183  */
dynlink_test_0003(void)184 static void dynlink_test_0003(void)
185 {
186     ns_t ns;
187     ns.asan_lib_paths = "/lib:/system/lib";
188     ns.lib_paths = NULL;
189     char buf[512];
190     int ret = handle_asan_path_open(-1, "libz.so", &ns, buf, 512);
191     EXPECT_GT(__FUNCTION__, ret, 0);
192     close(ret);
193 }
194 
195 /**
196  * @tc.name      : dynlink_test_0004
197  * @tc.desc      : Set lib path use handle_asan_path_open
198  * @tc.level     : Level 0
199  */
dynlink_test_0004(void)200 static void dynlink_test_0004(void)
201 {
202     ns_t ns;
203     ns.asan_lib_paths = NULL;
204     ns.lib_paths = "/lib:/system/lib";
205     char buf[512];
206     int ret = handle_asan_path_open(-1, "libz.so", &ns, buf, 512);
207     EXPECT_GT(__FUNCTION__, ret, 0);
208     close(ret);
209 }
210 
211 /**
212  * @tc.name      : dynlink_test_0005
213  * @tc.desc      : Set both asan lib path and lib path use handle_asan_path_open
214  * @tc.level     : Level 1
215  */
dynlink_test_0005(void)216 static void dynlink_test_0005(void)
217 {
218     ns_t ns;
219     ns.asan_lib_paths = "/lib:/system";
220     ns.lib_paths = "/system/lib:/lib";
221     char buf[512];
222     int ret = handle_asan_path_open(-1, "libz.so", &ns, buf, 512);
223     EXPECT_GT(__FUNCTION__, ret, 0);
224     close(ret);
225 }
226 
227 // Because the function logic branch in dynlink.c is very complex, some interfaces are covered by white-box unit testing.
228 #ifdef UNIT_TEST_STATIC
229 struct reserved_address_params {
230     void* start_addr;
231     size_t reserved_size;
232     bool must_use_reserved;
233     bool reserved_address_recursive;
234 #ifdef LOAD_ORDER_RANDOMIZATION
235     struct dso *target;
236 #endif
237 };
238 
239 void set_ns_attrs(ns_t *ns, ns_configor *conf);
240 void set_ns_inherits(ns_t *ns, ns_configor *conf);
241 void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off);
242 void fill_random_data(void *buf, size_t buflen);
243 void *map_library(int fd, struct dso *dso, struct reserved_address_params *reserved_params);
244 void unmap_library(struct dso *dso);
245 int fixup_rpath(struct dso *p, char *buf, size_t buf_size);
246 void __InstallSignalHandler();
247 bool get_random(void *buf, size_t buflen);
248 void assign_tls(struct dso *p);
249 size_t count_syms(struct dso *p);
250 int check_vna_hash(Verdef *def, int16_t vsym, uint32_t vna_hash);
251 int check_verinfo(Verdef *def, int16_t *versym, uint32_t index, struct verinfo *verinfo, char *strings);
252 struct dso *find_library_by_fstat(const struct stat *st, const ns_t *ns, bool check_inherited, uint64_t file_offset);
253 void load_preload(char *s, ns_t *ns, struct loadtasks *tasks);
254 bool get_ld_debug_dlclose_value();
255 
256 /**
257  * @tc.name      : dynlink_test_0130
258  * @tc.desc      : Test set_ns_attrs with invalid input
259  * @tc.level     : Level 2
260  */
dynlink_test_0130(void)261 static void dynlink_test_0130(void)
262 {
263     set_ns_attrs(NULL, NULL);
264     EXPECT_EQ(__FUNCTION__, 0, 0);
265 }
266 
267 /**
268  * @tc.name      : dynlink_test_0140
269  * @tc.desc      : Test set_ns_attrs with default ns
270  * @tc.level     : Level 1
271  */
dynlink_test_0140(void)272 static void dynlink_test_0140(void)
273 {
274     ns_configor *conf = configor_init();
275     ns_t *d_ns = get_default_ns();
276     set_ns_attrs(d_ns, conf);
277     EXPECT_EQ(__FUNCTION__, 0, 0);
278 }
279 
280 /**
281  * @tc.name      : dynlink_test_0150
282  * @tc.desc      : Test set_ns_inherits with invalid input
283  * @tc.level     : Level 2
284  */
dynlink_test_0150(void)285 static void dynlink_test_0150(void)
286 {
287     set_ns_inherits(NULL, NULL);
288     EXPECT_EQ(__FUNCTION__, 0, 0);
289 }
290 
291 /**
292  * @tc.name      : dynlink_test_0160
293  * @tc.desc      : Test set_ns_attrs with default ns
294  * @tc.level     : Level 1
295  */
dynlink_test_0160(void)296 static void dynlink_test_0160(void)
297 {
298     ns_configor *conf = configor_init();
299     ns_t *d_ns = get_default_ns();
300     set_ns_inherits(d_ns, conf);
301     EXPECT_EQ(__FUNCTION__, 0, 0);
302 }
303 
304 /**
305  * @tc.name      : dynlink_test_0200
306  * @tc.desc      : Test mmap_fixed with invalid input
307  * @tc.level     : Level 2
308  */
dynlink_test_0200(void)309 static void dynlink_test_0200(void)
310 {
311     void *p = mmap_fixed(0, -1, PROT_NONE, MAP_FIXED, -1, 0);
312     EXPECT_EQ(__FUNCTION__, p, MAP_FAILED);
313 }
314 
315 /**
316  * @tc.name      : dynlink_test_0210
317  * @tc.desc      : mmap_fixed test
318  * @tc.level     : Level 1
319  */
dynlink_test_0210(void)320 static void dynlink_test_0210(void)
321 {
322     char buf[128];
323     void *p = mmap_fixed(buf, 128, PROT_NONE, MAP_ANONYMOUS, -1, 0);
324     EXPECT_PTRNE(dynlink_test_0200, p, NULL);
325 }
326 
327 /**
328  * @tc.name      : dynlink_test_0220
329  * @tc.desc      : Test mmap_fixed with normal input
330  * @tc.level     : Level 1
331  */
dynlink_test_0220(void)332 static void dynlink_test_0220(void)
333 {
334     int fd = -1;
335     char *path = "/dev/random";
336     fd = open(path, O_RDWR);
337 
338     if (fd == -1){
339         EXPECT_TRUE(__FUNCTION__, false);
340         return;
341     }
342 
343     char buf[128];
344     void *p = mmap_fixed(buf, 128, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
345     EXPECT_TRUE(__FUNCTION__, (p != MAP_FAILED));
346     if (fd != -1){
347         close(fd);
348     }
349 }
350 
351 /**
352  * @tc.name      : dynlink_test_0230
353  * @tc.desc      : Test fill_random_data with invalid input
354  * @tc.level     : Level 2
355  */
dynlink_test_0230(void)356 static void dynlink_test_0230(void)
357 {
358     fill_random_data((void *)0, 0);
359     EXPECT_EQ(__FUNCTION__, 0, 0);
360 }
361 
362 /**
363  * @tc.name      : dynlink_test_0240
364  * @tc.desc      : Test get_random with invalid input
365  * @tc.level     : Level 2
366  */
dynlink_test_0240(void)367 static void dynlink_test_0240(void)
368 {
369     bool ret = get_random("adb", 1);
370     EXPECT_FALSE(__FUNCTION__, ret);
371 }
372 
373 /**
374  * @tc.name      : dynlink_test_0250
375  * @tc.desc      : Test unmap_library when loadmap is not null
376  * @tc.level     : Level 1
377  */
dynlink_test_0250(void)378 static void dynlink_test_0250(void)
379 {
380     struct dso temp_dso ;
381 
382     temp_dso.loadmap = (struct fdpic_loadmap *)calloc(1, sizeof(struct fdpic_loadmap) + 1 * sizeof(struct fdpic_loadseg));
383     if (temp_dso.loadmap == NULL){
384     	EXPECT_TRUE(__FUNCTION__, true);
385 	return;
386     }
387     temp_dso.loadmap->nsegs = 1;
388 
389     char *path = "/dev/zero";
390     int fd = open(path, O_RDWR);
391     if (fd == -1){
392         EXPECT_TRUE(__FUNCTION__, false);
393         return;
394     }
395 
396     void *p = mmap(NULL, sizeof(uintptr_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
397     if (p == MAP_FAILED){
398         EXPECT_TRUE(__FUNCTION__, false);
399         return;
400     }
401     temp_dso.loadmap->segs[0].addr = (uintptr_t)p;
402     temp_dso.loadmap->segs[0].p_memsz = 1;
403 
404     unmap_library(&temp_dso);
405     EXPECT_EQ(__FUNCTION__, 0, 0);
406 
407     if (get_ld_debug_dlclose_value()){
408     	munmap(p, sizeof(uintptr_t));
409     }
410 
411     if (fd != -1){
412         close(fd);
413     }
414 }
415 
416 /**
417  * @tc.name      : dynlink_test_0260
418  * @tc.desc      : Test map_library with normal input
419  * @tc.level     : Level 1
420  */
dynlink_test_0260(void)421 static void dynlink_test_0260(void)
422 {
423     struct dso temp_dso = {0};
424     int fd = open("/system/lib/libutils.z.so", O_RDONLY|O_CLOEXEC);
425     unsigned char *map = map_library(fd, &temp_dso, NULL);
426     close(fd);
427     EXPECT_PTRNE(__FUNCTION__, map, NULL);
428 }
429 
430 /**
431  * @tc.name      : dynlink_test_0270
432  * @tc.desc      : Test assign_tls with normal input
433  * @tc.level     : Level 1
434  */
dynlink_test_0270(void)435 static void dynlink_test_0270(void)
436 {
437     struct tls_module tls = { .image = "1", .len = 1, .size = 1, .align = 0, .offset = 0};
438     struct dso temp_dso = {.tls = tls};
439     assign_tls(&temp_dso);
440     EXPECT_TRUE(__FUNCTION__,true);
441 }
442 
443 /**
444  * @tc.name      : dynlink_test_0280
445  * @tc.desc      : Test assign_tls with normal input
446  * @tc.level     : Level 1
447  */
dynlink_test_0280(void)448 static void dynlink_test_0280(void)
449 {
450     struct tls_module tls_next2 = { .image = "3", .len = 1, .size = 1, .align = 0, .offset = 0};
451     struct tls_module tls_next = { .image = "2", .len = 1, .size = 1, .align = 0, .offset = 0, .next = &tls_next2};
452     struct tls_module tls = { .image = "1", .len = 1, .size = 1, .align = 0, .offset = 0, .next = &tls_next};
453 
454     struct dso temp_dso = {.tls = tls};
455     assign_tls(&temp_dso);
456     EXPECT_TRUE(__FUNCTION__,true);
457 }
458 
459 /**
460  * @tc.name      : dynlink_test_0290
461  * @tc.desc      : Test dlns_inherit with invalid input
462  * @tc.level     : Level 2
463  */
dynlink_test_0290(void)464 static void dynlink_test_0290(void)
465 {
466     int ret = dlns_inherit(NULL, NULL, "");
467     EXPECT_EQ(__FUNCTION__, ret, EINVAL);
468 }
469 
470 
471 /**
472  * @tc.name      : dynlink_test_0300
473  * @tc.desc      : Test dlns_inherit when input name is space string
474  * @tc.level     : Level 1
475  */
dynlink_test_0300(void)476 static void dynlink_test_0300(void)
477 {
478     Dl_namespace dlns = {.name = ""};
479     Dl_namespace inherited = {.name = ""};
480     int ret = dlns_inherit(&dlns, &inherited, "");
481     EXPECT_EQ(__FUNCTION__, ret, ENOKEY);
482 }
483 
484 /**
485  * @tc.name      : dynlink_test_0310
486  * @tc.desc      : Test dlns_create2 when input name is space string
487  * @tc.level     : Level 1
488  */
dynlink_test_0310(void)489 static void dynlink_test_0310(void)
490 {
491     Dl_namespace dlns = {.name = ""};
492     int ret = dlns_create2(&dlns, "", CREATE_INHERIT_CURRENT);
493     EXPECT_EQ(__FUNCTION__, ret, 0);
494 }
495 
496 /**
497  * @tc.name      : dynlink_test_0320
498  * @tc.desc      : Test dlns_init with invalid input
499  * @tc.level     : Level 2
500  */
dynlink_test_0320(void)501 static void dynlink_test_0320(void)
502 {
503     dlns_init(NULL, NULL);
504     EXPECT_EQ(__FUNCTION__, 0, 0);
505 }
506 
507 /**
508  * @tc.name      : dynlink_test_0330
509  * @tc.desc      : Test dlns_init when input name is space string
510  * @tc.level     : Level 1
511  */
dynlink_test_0330(void)512 static void dynlink_test_0330(void)
513 {
514     Dl_namespace dlns = {.name = ""};
515     dlns_init(&dlns, NULL);
516     EXPECT_EQ(__FUNCTION__, 0, 0);
517 }
518 
519 /**
520  * @tc.name      : dynlink_test_0340
521  * @tc.desc      : Test find_library_by_fstat with normal input
522  * @tc.level     : Level 1
523  */
dynlink_test_0340(void)524 static void dynlink_test_0340(void)
525 {
526     struct stat st = {};
527     struct _ns_inherit_list_  ns_inherit_list= {};
528     struct _dso_list_ dso_list= {.num = 0};
529     ns_t ns = {.ns_inherits = &ns_inherit_list, .ns_dsos = &dso_list, .ns_name = "abc"};
530 
531     find_library_by_fstat(&st, &ns, true, 0);
532     EXPECT_EQ(__FUNCTION__, 0, 0);
533 }
534 
535 /**
536  * @tc.name      : dynlink_test_0350
537  * @tc.desc      : Test find_library_by_fstat with normal input
538  * @tc.level     : Level 1
539  */
dynlink_test_0350(void)540 static void dynlink_test_0350(void)
541 {
542     struct stat st = {};
543     struct _dso_list_ dso_list= {.num = 0};
544     ns_t second_ns = {.ns_dsos = &dso_list, .ns_name = "ns_2"};
545     struct _namespace_inherit_ inherit = {.inherited_ns = &second_ns, .shared_libs = NULL};
546     struct _namespace_inherit_ *inherits[] = {&inherit};
547     struct _ns_inherit_list_  ns_inherit_list= {.inherits = inherits, .num = 1};
548     ns_t ns = {.ns_inherits = &ns_inherit_list, .ns_dsos = &dso_list, .ns_name = "ns_1"};
549 
550     find_library_by_fstat(&st, &ns, true, 0);
551     EXPECT_EQ(__FUNCTION__, 0, 0);
552 }
553 
554 // use by case dynlink_test_0360
dummy_config_get_inherits(const char * ns_name)555 static strlist *dummy_config_get_inherits(const char *ns_name)
556 {
557     strlist *list = (strlist *)calloc(1, sizeof(strlist));
558     list->num = 1;
559     list->size = 2;
560     list->strs = (char **)calloc(2, sizeof *list->strs);
561     char name[] = "default";
562     list->strs[0] = malloc(strlen(name) + 1);
563     memcpy(list->strs[0], name, strlen(name) + 1);
564     return list;
565 }
566 
567 /**
568  * @tc.name      : dynlink_test_0360
569  * @tc.desc      : Test set_ns_inherits with normal input
570  * @tc.level     : Level 1
571  */
dynlink_test_0360(void)572 static void dynlink_test_0360(void)
573 {
574     ns_configor *conf = configor_init();
575     ns_t *d_ns = get_default_ns();
576     ns_set_name(d_ns, "default");
577     conf->get_inherits = dummy_config_get_inherits;
578     set_ns_inherits(d_ns, conf);
579     EXPECT_EQ(__FUNCTION__, 0, 0);
580 }
581 
582 /**
583  * @tc.name      : dynlink_test_0370
584  * @tc.desc      : Test fill_random_data with normal input
585  * @tc.level     : Level 1
586  */
dynlink_test_0370(void)587 static void dynlink_test_0370(void)
588 {
589     char buf[1024];
590     fill_random_data((void*)buf, 1024);
591     EXPECT_EQ(__FUNCTION__, 0, 0);
592 }
593 
594 /**
595  * @tc.name      : dynlink_test_2000
596  * @tc.desc      : Test fixup_rpath with normal input
597  * @tc.level     : Level 0
598  */
dynlink_test_2000(void)599 static void dynlink_test_2000(void)
600 {
601     struct dso dso = {.rpath = NULL, .rpath_orig = "$ARIGIN"};
602     char buf[128] = "";
603     int ret = fixup_rpath(&dso, buf, 128);
604     EXPECT_EQ(__FUNCTION__, ret, 0);
605 }
606 
607 /**
608  * @tc.name      : dynlink_test_2001
609  * @tc.desc      : Test fixup_rpath when kernel_mapped is 0
610  * @tc.level     : Level 1
611  */
dynlink_test_2001(void)612 static void dynlink_test_2001(void)
613 {
614     struct dso dso = {.rpath = NULL, .rpath_orig = "$ORIGIN", .kernel_mapped = 0, .name="abc/abc"};
615     char buf[128] = "";
616     int ret = fixup_rpath(&dso, buf, 128);
617     EXPECT_EQ(__FUNCTION__, ret, 0);
618 }
619 
620 /**
621  * @tc.name      : dynlink_test_2002
622  * @tc.desc      : Test fixup_rpath when kernel_mapped is 1
623  * @tc.level     : Level 1
624  */
dynlink_test_2002(void)625 static void dynlink_test_2002(void)
626 {
627     struct dso dso = {.rpath = NULL, .rpath_orig = "$ORIGIN", .kernel_mapped = 1, .name = "abc/abc"};
628     char buf[128] = "";
629     int ret = fixup_rpath(&dso, buf, 128);
630     EXPECT_EQ(__FUNCTION__, ret, 0);
631 }
632 
633 /**
634  * @tc.name      : dynlink_test_2003
635  * @tc.desc      : __InstallSignalHandler coverage test case
636  * @tc.level     : Level 0
637  */
dynlink_test_2003(void)638 static void dynlink_test_2003(void)
639 {
640     __InstallSignalHandler();
641     EXPECT_EQ(__FUNCTION__, 0, 0);
642 }
643 
644 /**
645  * @tc.name      : dynlink_test_2004
646  * @tc.desc      : Test check_vna_hash with normal input
647  * @tc.level     : Level 0
648  */
dynlink_test_2004(void)649 static void dynlink_test_2004(void)
650 {
651     Verdef def;
652     def.vd_ndx = 0xf;
653     def.vd_hash = 0x123;
654     int ret = check_vna_hash(&def, 0xf, 0x123);
655     EXPECT_EQ(__FUNCTION__, ret, 1);
656 }
657 
658 /**
659  * @tc.name      : dynlink_test_2005
660  * @tc.desc      : Test check_vna_hash with normal input
661  * @tc.level     : Level 1
662  */
dynlink_test_2005(void)663 static void dynlink_test_2005(void)
664 {
665     Verdef def;
666     def.vd_ndx = 0x0;
667     def.vd_hash = 0x123;
668     def.vd_next = 0;
669     int ret = check_vna_hash(&def, 0xf, 0x123);
670     EXPECT_EQ(__FUNCTION__, ret, 0);
671 }
672 
673 /**
674  * @tc.name      : dynlink_test_2006
675  * @tc.desc      : Test check_verinfo with normal input
676  * @tc.level     : Level 0
677  */
dynlink_test_2006(void)678 static void dynlink_test_2006(void)
679 {
680     Verdef def;
681     int16_t versym[10];
682     versym[0] = 0xf;
683     struct verinfo info;
684     info.use_vna_hash = true;
685     info.vna_hash = 0x123;
686     def.vd_ndx = 0xf;
687     def.vd_hash = 0x123;
688     def.vd_next = 0;
689     int ret = check_verinfo(&def, versym, 0, &info, "abc");
690     EXPECT_EQ(__FUNCTION__, ret, 1);
691 }
692 
693 /**
694  * @tc.name      : dynlink_test_2007
695  * @tc.desc      : Test check_verinfo with normal input
696  * @tc.level     : Level 1
697  */
dynlink_test_2007(void)698 static void dynlink_test_2007(void)
699 {
700     Verdef def;
701     int16_t versym[10];
702     versym[0] = 0;
703     struct verinfo info;
704     info.use_vna_hash = true;
705     info.vna_hash = 0x123;
706     info.v = "";
707     def.vd_ndx = 0xf;
708     def.vd_hash = 0x123;
709     def.vd_next = 0;
710     int ret = check_verinfo(&def, versym, 0, &info, "abc");
711     EXPECT_EQ(__FUNCTION__, ret, 1);
712 }
713 
714 /**
715  * @tc.name      : dynlink_test_2008
716  * @tc.desc      : Test check_verinfo with normal input
717  * @tc.level     : Level 1
718  */
dynlink_test_2008(void)719 static void dynlink_test_2008(void)
720 {
721     Verdef def;
722     int16_t versym[10];
723     versym[0] = 0;
724     struct verinfo info;
725     info.use_vna_hash = true;
726     info.vna_hash = 0x123;
727     info.v = "123";
728     def.vd_ndx = 0;
729     def.vd_hash = 0x123;
730     def.vd_next = 0;
731     def.vd_flags = 0x1;
732     int ret = check_verinfo(&def, versym, 0, &info, "abc");
733     EXPECT_EQ(__FUNCTION__, ret, 0);
734 }
735 
736 /**
737  * @tc.name      : dynlink_test_2009
738  * @tc.desc      : Test count_syms with normal input
739  * @tc.level     : Level 0
740  */
dynlink_test_2009(void)741 static void dynlink_test_2009(void)
742 {
743     struct dso dso;
744     uint32_t buf[128] = {0};
745     buf[0] = 1;
746     buf[2] = 1;
747     int i = 4 + (buf[2]*sizeof(size_t)/4);
748     buf[i] = 1;
749     int j = i + buf[0] + (buf[0] - buf[1]);
750     buf[j] = 1;
751     dso.ghashtab = buf;
752     size_t ret = count_syms(&dso);
753     EXPECT_EQ(__FUNCTION__, ret, 2);
754 }
755 
756 /**
757  * @tc.name      : dynlink_test_2010
758  * @tc.desc      : Test free_loadtasks with normal input
759  * @tc.level     : Level 0
760  */
dynlink_test_2010(void)761 static void dynlink_test_2010(void)
762 {
763     struct loadtasks *tasks = malloc(sizeof(struct loadtasks));
764     if (tasks) {
765         tasks->array = NULL;
766         tasks->capacity = 0;
767         tasks->length = 0;
768     }
769 
770     char name[] = "libz.so";
771     load_preload(name, get_default_ns(), tasks);
772     EXPECT_TRUE(__FUNCTION__, true);
773     if (tasks){
774         free_loadtasks(tasks);
775     }
776 }
777 #endif
778 
779 TEST_FUN G_Fun_Array[] = {
780     dynlink_test_0002,
781     dynlink_test_0003,
782     dynlink_test_0004,
783     dynlink_test_0005,
784     dynlink_test_0010,
785     dynlink_test_0020,
786     dynlink_test_0030,
787     dynlink_test_0040,
788     dynlink_test_0050,
789     dynlink_test_0060,
790     dynlink_test_0070,
791     dynlink_test_0080,
792     dynlink_test_0090,
793     dynlink_test_0100,
794     dynlink_test_0110,
795     dynlink_test_0120,
796 #ifdef UNIT_TEST_STATIC
797     dynlink_test_0130,
798     dynlink_test_0140,
799     dynlink_test_0150,
800     dynlink_test_0160,
801     dynlink_test_0200,
802     dynlink_test_0210,
803     dynlink_test_0220,
804     dynlink_test_0230,
805     dynlink_test_0240,
806     dynlink_test_0250,
807     dynlink_test_0260,
808     dynlink_test_0270,
809     dynlink_test_0280,
810     dynlink_test_0290,
811     dynlink_test_0300,
812     dynlink_test_0310,
813     dynlink_test_0320,
814     dynlink_test_0330,
815     dynlink_test_0340,
816     dynlink_test_0350,
817     dynlink_test_0360,
818     dynlink_test_0370,
819     dynlink_test_2000,
820     dynlink_test_2001,
821     dynlink_test_2002,
822     dynlink_test_2003,
823     dynlink_test_2004,
824     dynlink_test_2005,
825     dynlink_test_2006,
826     dynlink_test_2007,
827     dynlink_test_2008,
828     dynlink_test_2009,
829     dynlink_test_2010,
830 #endif
831 };
832 
main(void)833 int main(void)
834 {
835     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
836     for (int pos = 0; pos < num; ++pos) {
837         G_Fun_Array[pos]();
838     }
839 
840     return t_status;
841 }