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_03
25 * @Brief: test dlcose in multi level n-to-1 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/libd.so",
37 "/data/tests/libc-test/ola_test/libe.so",
38 "/data/tests/libc-test/ola_test/libf.so",
39 "/data/tests/libc-test/ola_test/libg.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 libf.so
53 // @EXPECT-1: load libd.so, libe.so success
54 int ret = 0;
55 char *direct_libs[] = {
56 "/data/tests/libc-test/ola_test/libf.so",
57 "/data/tests/libc-test/ola_test/libg.so",
58 };
59 char *depend_libs[] = {
60 "/data/tests/libc-test/ola_test/libd.so",
61 "/data/tests/libc-test/ola_test/libe.so",
62 };
63 void *direct_hdls[] = { NULL, NULL };
64 direct_hdls[0] = dlopen(direct_libs[0], RTLD_NOW | RTLD_GLOBAL);
65 if (!direct_hdls[0]) {
66 rt_err("dlopen %s failed: %s\n", direct_libs[0], dlerror());
67 }
68
69 if (!check_loaded(depend_libs[0]) || !check_loaded(depend_libs[1])) {
70 rt_err("dso %s or %s should exist in /proc/self/maps\n", depend_libs[0], depend_libs[1]);
71 }
72
73 // @SETP-2: dlopen libg.so
74 // @EXPECT-2: load libf.so success
75 direct_hdls[1] = dlopen(direct_libs[1], RTLD_NOW | RTLD_GLOBAL);
76 if (!direct_hdls[1]) {
77 rt_err("dlopen %s failed: %s\n", direct_libs[1], dlerror());
78 }
79
80 // @STEP-3: dlclose libf.so
81 // @EXPECT-3: libd.so libe.so exist
82 ret = dlclose(direct_hdls[0]);
83 if (ret != 0) {
84 rt_err("dlclose %s failed: %s\n", direct_libs[0], dlerror());
85 }
86
87 if (!check_loaded(depend_libs[0]) || !check_loaded(depend_libs[1])) {
88 rt_err("dso %s or %s should exist in /proc/self/maps\n", depend_libs[0], depend_libs[1]);
89 }
90
91 // @STEP-4: dlclose libg.so
92 // @EXPECT-4: libd.so libe.so unloaded
93 ret = dlclose(direct_hdls[1]);
94 if (ret != 0) {
95 rt_err("dlclose %s failed: %s\n", direct_libs[1], dlerror());
96 }
97
98 if (check_loaded(depend_libs[0]) || check_loaded(depend_libs[1])) {
99 rt_err("dso %s or %s should exist in /proc/self/maps\n", depend_libs[0], depend_libs[1]);
100 }
101
102 return RT_EOK;
103 }
104
105 // Func: post test
106 // Args: same as testcase
tc_post_test(int argc,char ** argv)107 int tc_post_test(int argc, char **argv)
108 {
109 return RT_EOK;
110 }
111
112 // Func: collect log when testcase failed
tc_get_debuginfo(int argc,char ** argv)113 int tc_get_debuginfo(int argc, char **argv)
114 {
115 return RT_EOK;
116 }
117
main()118 int main()
119 {
120 if (tc_pre_test(0, NULL)) {
121 rt_err("tc_pre_test failed:\n");
122 return -1;
123 }
124 if (tc_do_test(0, NULL)) {
125 rt_err("tc_do_test failed:\n");
126 return -1;
127 }
128 if (tc_post_test(0, NULL)) {
129 rt_err("tc_post_test failed:\n");
130 return -1;
131 }
132 if (tc_get_debuginfo(0, NULL)) {
133 rt_err("tc_get_debug_info failed:\n");
134 return -1;
135 }
136 return 0;
137 }