1 /**
2  * Copyright (c) 2023 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 "functionalext.h"
17 
18 #define LIB_PATH_1 "/data/tests/libc-test/src/libldso_debug_test_lib_1.so"
19 #define LIB_PATH_2 "/data/tests/libc-test/src/libldso_debug_test_lib_2.so"
20 #define LIB_PATH_3 "/data/tests/libc-test/src/libldso_debug_test_lib_3.so"
21 #define LIB_PATH_4 "/data/tests/libc-test/src/libldso_debug_test_lib_4.so"
22 #define LIB_PATH_5 "/data/tests/libc-test/src/libldso_debug_test_lib_5.so"
23 #define LIB_PATH_6 "/data/tests/libc-test/src/libldso_debug_test_lib_6.so"
24 #define LIB_PATH_BAK "/data/tests/libc-test/src/libldso_debug_test_lib_bak.so"
25 
26 #define TEST_LIB_LOADING_TIME 20
27 
28 typedef void(*TEST_FUN)(void);
29 
30 /**
31  * @tc.name      : ldso_debug_test_0001
32  * @tc.desc      : Loading three dsos and unload them not in order.
33  * @tc.level     : Level 0
34  */
ldso_debug_test_0001(void)35 void ldso_debug_test_0001(void)
36 {
37     printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
38 
39     void* handle_lib1 = dlopen(LIB_PATH_1, RTLD_NOW);
40     EXPECT_PTRNE("ldso_debug_test_0001", handle_lib1, NULL);
41     void* handle_lib2 = dlopen(LIB_PATH_2, RTLD_NOW);
42     EXPECT_PTRNE("ldso_debug_test_0001", handle_lib2, NULL);
43     void* handle_lib3 = dlopen(LIB_PATH_3, RTLD_NOW);
44     EXPECT_PTRNE("ldso_debug_test_0001", handle_lib3, NULL);
45 
46     EXPECT_EQ("ldso_debug_test_0001", dlclose(handle_lib1), 0);
47     EXPECT_EQ("ldso_debug_test_0001", dlclose(handle_lib3), 0);
48     EXPECT_EQ("ldso_debug_test_0001", dlclose(handle_lib2), 0);
49 
50     printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
51 }
52 
53 /**
54  * @tc.name      : ldso_debug_test_0002
55  * @tc.desc      : Loading one dso and reload it for 20 times.
56  * @tc.level     : Level 0
57  */
ldso_debug_test_0002(void)58 void ldso_debug_test_0002(void)
59 {
60     printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
61 
62     void* handle_lib1 = dlopen(LIB_PATH_1, RTLD_NOW);
63     EXPECT_PTRNE("ldso_debug_test_0002", handle_lib1, NULL);
64 
65     for (int i = 0; i < TEST_LIB_LOADING_TIME; i++) {
66         handle_lib1 = dlopen(LIB_PATH_1, RTLD_NOW);
67         EXPECT_PTRNE("ldso_debug_test_0002", handle_lib1, NULL);
68     }
69 
70     EXPECT_EQ("ldso_debug_test_0002", dlclose(handle_lib1), 0);
71 
72     printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
73 }
74 
75 /**
76  * @tc.name      : ldso_debug_test_0003
77  * @tc.desc      : Loading a dso which depends on non-existing dso.
78  * @tc.level     : Level 0
79  */
ldso_debug_test_0003(void)80 void ldso_debug_test_0003(void)
81 {
82     printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
83 
84     EXPECT_EQ("ldso_debug_test_0003", rename(LIB_PATH_5, LIB_PATH_BAK), 0);
85 
86     void* handle_lib4 = dlopen(LIB_PATH_4, RTLD_NOW);
87     EXPECT_PTREQ("ldso_debug_test_0003", handle_lib4, NULL);
88 
89     EXPECT_EQ("ldso_debug_test_0003", rename(LIB_PATH_BAK, LIB_PATH_5), 0);
90 
91     printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
92 }
93 
94 /**
95  * @tc.name      : ldso_debug_test_0004
96  * @tc.desc      : Loading a dso which is not exist.
97  * @tc.level     : Level 0
98  */
ldso_debug_test_0004(void)99 void ldso_debug_test_0004(void)
100 {
101     printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
102 
103     void* handle_lib6 = dlopen(LIB_PATH_6, RTLD_NOW);
104     EXPECT_PTREQ("ldso_debug_test_0004", handle_lib6, NULL);
105 
106     printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
107 }
108 
109 TEST_FUN G_Fun_Array[] = {
110     ldso_debug_test_0001,
111     ldso_debug_test_0002,
112     ldso_debug_test_0003,
113     ldso_debug_test_0004,
114 };
115 
main(void)116 int main(void)
117 {
118     printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
119     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
120     for (int pos = 0; pos < num; ++pos) {
121         G_Fun_Array[pos]();
122     }
123     printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
124     return 0;
125 }