1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22
23 /**************
24 * @Casename: dlclose_04
25 * @Brief: test dlcose in multi-so scenario
26 */
27
28 #include "common.h"
29
30 // Func: init environment
31 // Args: same as testcase
tc_pre_test(int argc,char ** argv)32 int tc_pre_test(int argc, char **argv)
33 {
34 // @PRE-1
35 char *files[] = {
36 "/data/tests/libc-test/ola_test/liba.so",
37 "/data/tests/libc-test/ola_test/libb.so",
38 "/data/tests/libc-test/ola_test/libcc.so",
39 "/data/tests/libc-test/ola_test/libi.so",
40 };
41 int num_files = sizeof(files) / sizeof(files[0]);
42 if (check_so_exist(num_files, files) != 0) {
43 rt_err("check so failed\n");
44 }
45 return RT_EOK;
46 }
47
48 // Func: do test
49 // Args: same as testcase
tc_do_test(int argc,char ** argv)50 int tc_do_test(int argc, char **argv)
51 {
52 // @STEP-1: dlopen libcc.so libi.so
53 // @EXPECT-1: all so load success
54 int ret = 0;
55 char *direct_libs[] = {
56 "/data/tests/libc-test/ola_test/libcc.so",
57 "/data/tests/libc-test/ola_test/libi.so",
58 };
59 void *direct_hdls[] = { NULL, NULL };
60 char *depend_libs[] = {
61 "/data/tests/libc-test/ola_test/liba.so",
62 "/data/tests/libc-test/ola_test/libb.so",
63 };
64 int num1 = sizeof(direct_libs) / sizeof(direct_libs[0]);
65 int num2 = sizeof(depend_libs) / sizeof(depend_libs[0]);
66
67 for (int i = 0; i < num1; i++) {
68 direct_hdls[i] = dlopen(direct_libs[i], RTLD_NOW | RTLD_GLOBAL);
69 if (!direct_hdls[i]) {
70 rt_err("dlopen %s failed: %s\n", direct_libs[i], dlerror());
71 }
72 if (!check_loaded(direct_libs[i])) {
73 rt_err("dso %s should exist in /proc/self/maps\n", direct_libs[i]);
74 }
75 }
76 for (int j = 0; j < num2; j++) {
77 if (!check_loaded(depend_libs[j])) {
78 rt_err("dso %s should exist in /proc/self/maps\n", depend_libs[j]);
79 }
80 }
81
82 // @SETP-2: dlclose libi.so
83 // @EXPECT-2: liba.so and libb.so exist
84 ret = dlclose(direct_hdls[0]);
85 if (ret != 0) {
86 rt_err("dlclose %s failed: %s\n", direct_libs[0], dlerror());
87 }
88 for (int j = 0; j < num2; j++) {
89 if (!check_loaded(depend_libs[j])) {
90 rt_err("dso %s should exist in /proc/self/maps\n", depend_libs[j]);
91 }
92 }
93
94 // @STEP-3: dlclose libi.so
95 // @EXPECT-3: liba.so libb.so unloaded
96 ret = dlclose(direct_hdls[1]);
97 if (ret != 0) {
98 rt_err("dlclose %s failed: %s\n", direct_libs[1], dlerror());
99 }
100 for (int j = 0; j < num2; j++) {
101 if (check_loaded(depend_libs[j])) {
102 rt_err("dso %s shouldn't exist in /proc/self/maps\n", direct_libs[j]);
103 }
104 }
105
106 return RT_EOK;
107 }
108
109 // Func: post test
110 // Args: same as testcase
tc_post_test(int argc,char ** argv)111 int tc_post_test(int argc, char **argv)
112 {
113 return RT_EOK;
114 }
115
116 // Func: collect log when testcase failed
tc_get_debuginfo(int argc,char ** argv)117 int tc_get_debuginfo(int argc, char **argv)
118 {
119 return RT_EOK;
120 }
121
main()122 int main()
123 {
124 if (tc_pre_test(0, NULL)) {
125 rt_err("tc_pre_test failed:\n");
126 return -1;
127 }
128 if (tc_do_test(0, NULL)) {
129 rt_err("tc_do_test failed:\n");
130 return -1;
131 }
132 if (tc_post_test(0, NULL)) {
133 rt_err("tc_post_test failed:\n");
134 return -1;
135 }
136 if (tc_get_debuginfo(0, NULL)) {
137 rt_err("tc_get_debug_info failed:\n");
138 return -1;
139 }
140 return 0;
141 }