• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
17 #include <stdlib.h>
18 #include <dlfcn.h>
19 
20 #include "rpath_test.h"
21 #include "functionalext.h"
22 
23 /**
24  * RapthAbsolute - Test dynamic library loading with absolute rpath
25  *
26  * Tests loading a library with an absolute rpath specified as '/data/tests/libc-test/src/rpath_lib/rpath_support_A'.
27  * Verifies if the library 'g_absoluteLib' can be successfully loaded with this rpath configuration.
28  */
RapthAbsolute(void)29 static void RapthAbsolute(void)
30 {
31     void *handle = dlopen(g_absoluteLib, RTLD_LAZY);
32     EXPECT_TRUE("RapthAbsolute", handle);
33     if (handle) {
34         dlclose(handle);
35     }
36 }
37 
38 /**
39  * RapthRelative - Test dynamic library loading with relative rpath
40  *
41  * Tests the ability to load a library with a relative rpath specified as '\$ORIGIN/rpath_supportB/../rpath_support_A'.
42  * Checks the successful loading of 'g_originLib' using this relative rpath setting.
43  */
RapthRelative(void)44 static void RapthRelative(void)
45 {
46     void *handle = dlopen(g_originLib, RTLD_LAZY);
47     EXPECT_TRUE("RapthRelative", handle);
48     if (handle) {
49         dlclose(handle);
50     }
51 }
52 
53 /**
54  * RapthMultiple - Test dynamic library loading with multiple rpath entries
55  *
56  * Assesses the ability to load a library with multiple rpath entries,
57  * specified as '\$ORIGIN/rpath_supportA:/data/tests/libc-test/src/rpath_lib/rpath_supportB'.
58  * Checks if 'g_multipleLib' can be loaded successfully using these combined rpath settings.
59  */
RapthMultiple(void)60 static void RapthMultiple(void)
61 {
62     void *handle = dlopen(g_multipleLib, RTLD_LAZY);
63     EXPECT_TRUE("RapthMultiple", handle);
64     if (handle) {
65         dlclose(handle);
66     }
67 }
68 
69 /**
70  * RpathNsOrigin - Test dynamic library loading with relative paths using $ORIGIN
71  *
72  * This function tests the dynamic library loading mechanism with relative paths based on $ORIGIN.
73  * It checks two scenarios:
74  * 1. With 'permitted_path' set to '/data/tests/libc-test/src/lib_path/rpath_support_B',
75  *    it expects the library load to fail as the actual library is located at
76  *    '$ORIGIN/rpath_support_B/../rpath_support_A'.
77  * 2. With 'permitted_path' set to '/data/tests/libc-test/src/lib_path', it expects the library load to succeed
78  *    since includes the correct path to the library.
79  */
RpathNsOrigin(void)80 static void RpathNsOrigin(void)
81 {
82     Dl_namespace dlns;
83     dlns_init(&dlns, g_dlName);
84     dlns_create2(&dlns, g_dlpath, 0);
85     dlns_set_namespace_separated(g_dlName, true);
86     dlns_set_namespace_permitted_paths(g_dlName, g_errPath);
87     void *handleErr = dlopen_ns(&dlns, g_originLib, RTLD_LAZY);
88     EXPECT_FALSE("RpathNsOrigin_err", handleErr);
89     if (handleErr) {
90         dlclose(handleErr);
91     }
92     dlns_set_namespace_permitted_paths(g_dlName, g_path);
93     void *handle = dlopen_ns(&dlns, g_originLib, RTLD_LAZY);
94     EXPECT_TRUE("RpathNsOrigin_correct", handle);
95     if (handle) {
96         dlclose(handle);
97     }
98 }
99 
100 /**
101  * RpathNsMultiple - Test dynamic library loading with dependencies on multiple .so files
102  *
103  * The test paths are specified as '/data/tests/libc-test/src/rpath_lib/rpath_support_A:$ORIGIN/../rpath_support_C'.
104  * The function sets 'permitted_path' to '/data/tests/libc-test/src/lib_path' and assesses whether the target library
105  * can be successfully loaded, given that it has dependencies on both libraries located at the specified paths.
106  * This test is critical to verify the handling of multiple rpath entries and their resolution for dependent libraries.
107  */
RpathNsMultiple(void)108 static void RpathNsMultiple(void)
109 {
110     Dl_namespace dlns;
111     dlns_init(&dlns, g_dlName);
112     dlns_create2(&dlns, g_dlpath, 0);
113     dlns_set_namespace_separated(g_dlName, true);
114     dlns_set_namespace_permitted_paths(g_dlName, g_path);
115     void *handle = dlopen_ns(&dlns, g_multipleLib, RTLD_LAZY);
116     EXPECT_TRUE("RpathNsMultiple", handle);
117     if (handle) {
118         dlclose(handle);
119     }
120 }
121 
122 TEST_FUN g_gFunArray[] = {
123     RapthAbsolute,
124     RapthRelative,
125     RapthMultiple,
126     RpathNsOrigin,
127     RpathNsMultiple
128 };
129 
main(void)130 int main(void)
131 {
132     int num = sizeof(g_gFunArray) / sizeof(TEST_FUN);
133     for (int pos = 0; pos < num; ++pos) {
134         g_gFunArray[pos]();
135     }
136 
137     return t_status;
138 }
139