• 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 const char *LIB_NAME_A = "libdlns_dlsym_dep_a.so";
25 const char *LIB_NAME_B = "libdlns_dlsym_dep_b.so";
26 const char *LIB_NAME_C = "libdlns_dlsym_dep_c.so";
27 
dlns_dlsym_0100_init_ns(Dl_namespace * dlnsA,Dl_namespace * dlnsB,Dl_namespace * dlnsC)28 static inline void dlns_dlsym_0100_init_ns(Dl_namespace *dlnsA, Dl_namespace *dlnsB, Dl_namespace *dlnsC)
29 {
30     dlns_init(dlnsA, "dlns_dlsym_0100_A");
31     dlns_init(dlnsB, "dlns_dlsym_0100_B");
32     dlns_init(dlnsC, "dlns_dlsym_0100_C");
33     EXPECT_EQ(__FUNCTION__, dlns_create(dlnsA, pathA), EOK);
34     EXPECT_EQ(__FUNCTION__, dlns_create(dlnsB, pathB), EOK);
35     EXPECT_EQ(__FUNCTION__, dlns_create(dlnsC, pathC), EOK);
36     EXPECT_EQ(__FUNCTION__, dlns_inherit(dlnsB, dlnsC, NULL), EOK);
37     EXPECT_EQ(__FUNCTION__, dlns_inherit(dlnsA, dlnsB, NULL), EOK);
38 }
39 
close_so(void * handle)40 static inline void close_so(void* handle)
41 {
42     if (handle) {
43         dlclose(handle);
44     }
45 }
46 
47 /**
48  * @tc.name      : dlns_dlsym_0100
49  * @tc.desc      : Call the dlsym interface to load libA,libB,libC dynamic library.
50  * @tc.level     : Level1
51  */
dlns_dlsym_0100(void)52 static void dlns_dlsym_0100(void)
53 {
54     Dl_namespace dlnsA, dlnsB, dlnsC;
55     dlns_dlsym_0100_init_ns(&dlnsA, &dlnsB, &dlnsC);
56 
57     void* handle_c = dlopen_ns(&dlnsC, LIB_NAME_C, RTLD_NOW);
58     EXPECT_PTRNE(__FUNCTION__, handle_c, NULL);
59     if (handle_c == NULL) {
60         return;
61     }
62 
63     void* handle_b = dlopen_ns(&dlnsB, LIB_NAME_B, RTLD_NOW);
64     EXPECT_PTRNE(__FUNCTION__, handle_b, NULL);
65     if (handle_b == NULL) {
66         close_so(handle_c);
67         return;
68     }
69 
70     void* handle_a = dlopen_ns(&dlnsA, LIB_NAME_A, RTLD_NOW);
71     EXPECT_PTRNE(__FUNCTION__, handle_a, NULL);
72     if (handle_a == NULL) {
73         close_so(handle_b);
74         close_so(handle_c);
75         return;
76     }
77 
78     int(* test_a)(void) = dlsym(handle_a, "test");
79     EXPECT_PTRNE(__FUNCTION__, test_a, NULL);
80     if (test_a) {
81         EXPECT_EQ(__FUNCTION__, test_result_1, test_a());
82     }
83 
84     int(* test_b)(void) = dlsym(handle_a, "test_b");
85     EXPECT_PTRNE(__FUNCTION__, test_b, NULL);
86     if (test_b) {
87         EXPECT_EQ(__FUNCTION__, test_result_2, test_b());
88     }
89 
90     int(* test_c)(void) = dlsym(handle_c, "test");
91     EXPECT_PTRNE(__FUNCTION__, test_c, NULL);
92     if (test_c) {
93         EXPECT_EQ(__FUNCTION__, test_result_3, test_c());
94     }
95 
96     int(* test_d)(void) = dlsym(handle_c, "test_c");
97     EXPECT_PTRNE(__FUNCTION__, test_d, NULL);
98     if (test_d) {
99         EXPECT_EQ(__FUNCTION__, test_result_4, test_d());
100     }
101 
102     int(* test_e)(void) = dlsym(handle_a, "test_c");
103     EXPECT_PTREQ(__FUNCTION__, test_e, NULL);
104 
105     close_so(handle_c);
106     close_so(handle_b);
107     close_so(handle_a);
108 }
109 
110 TEST_FUN G_Fun_Array[] = {
111     dlns_dlsym_0100,
112 };
113 
main(void)114 int main(void)
115 {
116     int num = sizeof(G_Fun_Array)/sizeof(TEST_FUN);
117     for (int pos = 0; pos < num; ++pos) {
118         G_Fun_Array[pos]();
119     }
120 
121     return t_status;
122 }