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_01
25 * @Brief: dlclose可以关闭不存在依赖的动态库
26 *********************************************************************/
27
28 #include "common.h" /* PLEASE DON'T MODIFY THIS LINE */
29
30 // Func:本用例执行测试前进行环境初始化
31 // Args:同用例参数
tc_pre_test(int argc,char ** argv)32 int tc_pre_test(int argc, char **argv)
33 {
34 // @PRE-1: 测试so存在
35 char *files[] = {
36 "/data/tests/libc-test/ola_test/liba.so",
37 "/data/tests/libc-test/ola_test/libb.so"
38 };
39 int num_files = sizeof(files) /sizeof(files[0]);
40 if (check_so_exist(num_files, files) != 0) {
41 rt_err("check so failed\n");
42 }
43 return RT_EOK;
44 }
45
46 // Func: 用例执行测试活动,重要:本函数必须定义且函数名不可修改
47 // Args: 同用例参数
tc_do_test(int argc,char ** argv)48 int tc_do_test(int argc, char **argv)
49 {
50 // @STEP-1: dlopen加载liba.so, libb.so
51 // @EXPECT-1: 2个so都加载成功
52 int ret = 0;
53 char *direct_libs[] = {
54 "/data/tests/libc-test/ola_test/liba.so",
55 "/data/tests/libc-test/ola_test/libb.so"
56 };
57 void *direct_hdls[] = { NULL, NULL };
58
59 direct_hdls[0] = dlopen(direct_libs[0], RTLD_NOW | RTLD_GLOBAL);
60 if (!direct_hdls[0]) {
61 rt_err("dlopen %s failed: %s \n", direct_libs[0], dlerror());
62 }
63 direct_hdls[1] = dlopen(direct_libs[1], RTLD_NOW | RTLD_GLOBAL);
64 if (!direct_hdls[1]) {
65 rt_err("dlopen %s failed: %s \n", direct_libs[1], dlerror());
66 }
67 if (!check_loaded(direct_libs[0]) || !check_loaded(direct_libs[1])) {
68 rt_err("dso %s or %s should exist in /proc/self/maps\n", direct_libs[0], direct_libs[1]);
69 }
70
71 // @STEP-2: dlclose卸载liba.so, libb.so
72 // @EXPECT-2: 2个so均被卸载
73 ret = dlclose(direct_hdls[0]);
74 if (ret != 0) {
75 rt_err("dlclose %s failed: %s \n", direct_libs[0], dlerror());
76 }
77
78 ret = dlclose(direct_hdls[1]);
79 if (ret != 0) {
80 rt_err("dlclose %s failed: %s \n", direct_libs[1], dlerror());
81 }
82 if (check_loaded(direct_libs[0]) || check_loaded(direct_libs[1])) {
83 rt_err("dso %s or %s shouldn't exist in /proc/self/maps\n", direct_libs[0], direct_libs[1]);
84 }
85
86 return RT_EOK;
87 }
88
89 // Func: 本用例执行完测试活动后进行用例清理操作
90 // Args: 同用例参数
tc_post_test(int argc,char ** argv)91 int tc_post_test(int argc, char **argv)
92 {
93 return RT_EOK;
94 }
95
96 // Func: 本用例执行失败后需要进行的日志收集动作
tc_get_debuginfo(int argc,char ** argv)97 int tc_get_debuginfo(int argc, char **argv)
98 {
99 return RT_EOK;
100 }
101
main()102 int main() {
103 if(tc_pre_test(0, NULL)) {
104 rt_err("tc_pre_test failed:\n");
105 return -1;
106 }
107 if(tc_do_test(0, NULL)) {
108 rt_err("tc_do_test failed:\n");
109 return -1;
110 }
111 if(tc_post_test(0, NULL)) {
112 rt_err("tc_post_test failed:\n");
113 return -1;
114 }
115 if(tc_get_debuginfo(0, NULL)) {
116 rt_err("tc_get_debuginfo failed:\n");
117 return -1;
118 }
119 return 0;
120 }