1/**
2 * Copyright (c) 2022 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 <dlfcn.h>
17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20
21#include "dlns_test.h"
22#include "functionalext.h"
23
24
25/**
26 * @tc.name      : separated_0100
27 * @tc.desc      : When the separated property is false, under non-strict isolation, so can be opened by short name
28 * @tc.level     : Level 1
29 */
30void separated_0100(void)
31{
32    Dl_namespace dlns;
33    dlns_init(&dlns, "ns_separated_flase");
34
35    void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
36    EXPECT_TRUE("separated_0100", handle);
37    dlclose(handle);
38}
39
40/**
41 * @tc.name      : separated_0200
42 * @tc.desc      : When the separated property is false, under non-strict isolation, the wrong path cannot open so
43 * @tc.level     : Level 2
44 */
45void separated_0200(void)
46{
47    Dl_namespace dlns;
48    dlns_init(&dlns, "ns_separated1");
49
50    // 错误短名称
51    void* handle = dlopen_ns(&dlns, dllName2, RTLD_LAZY);
52    EXPECT_FALSE("separated_0200", handle);
53}
54
55/**
56 * @tc.name      : separated_0300
57 * @tc.desc      : When the separated property is true, under strict isolation, the name of the incoming
58 *     library is under the allowed.libs configuration item, and lib_paths and permitted.paths are configured correctly
59 * @tc.level     : Level 1
60 */
61void separated_0300(void)
62{
63    Dl_namespace dlns;
64    dlns_init(&dlns, "ns_normal");
65
66    // 长路径
67    void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
68    EXPECT_TRUE("separated_0300", handle);
69    dlclose(handle);
70}
71
72/**
73 * @tc.name      : separated_0400
74 * @tc.desc      : When the separated property is true, under strict isolation, the name of the incoming
75 *                  library is under the allowed.libs configuration item, and the lib_paths and permitted.paths
76 *                    configurations do not match.
77 * @tc.level     : Level 2
78 */
79void separated_0400(void)
80{
81    Dl_namespace dlns;
82    dlns_init(&dlns, "ns_wrong_lib_path");
83
84    void* handle = dlopen_ns(&dlns, errdllNamePath, RTLD_LAZY);
85    EXPECT_FALSE("separated_0400", handle);
86}
87
88/**
89 * @tc.name      : separated_0500
90 * @tc.desc      : When the separated property is true, under strict isolation, the lib_paths, permitted.paths
91 *                  configuration matches,The name of the incoming library is not in the allowed.libs configuration item
92 * @tc.level     : Level 2
93 */
94void separated_0500(void)
95{
96    Dl_namespace dlns;
97    dlns_init(&dlns, "ns_normal");
98
99    void* handle = dlopen_ns(&dlns, dllNamePath2, RTLD_LAZY);
100    EXPECT_FALSE("separated_0500", handle);
101}
102
103/**
104 * @tc.name      : separated_0600
105 * @tc.desc      : When the separated attribute is false, under non-strict isolation,
106 *                  calling dlopen_ns can open so by short name
107 * @tc.level     : Level 2
108 */
109void separated_0600(void)
110{
111    Dl_namespace dlns;
112    dlns_init(&dlns, "separated_0600");
113
114    EXPECT_EQ("separated_0600", dlns_create(&dlns, path), EOK);
115    EXPECT_EQ("separated_0600", dlns_set_namespace_separated("separated_0600", false), EOK);
116
117    void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
118    EXPECT_TRUE("separated_0600", handle);
119    dlclose(handle);
120}
121
122/**
123 * @tc.name      : separated_0700
124 * @tc.desc      : When the separated property is false, under non-strict isolation,
125 *                  calling dlopen_ns error short name cannot open so
126 */
127void separated_0700(void)
128{
129    Dl_namespace dlns;
130    dlns_init(&dlns, "separated_0700");
131
132    EXPECT_EQ("separated_0700", dlns_create(&dlns, path), EOK);
133    EXPECT_EQ("separated_0700", dlns_set_namespace_separated("separated_0700", false), EOK);
134
135    void* handle = dlopen_ns(&dlns, errPath_ns, RTLD_LAZY);
136    EXPECT_FALSE("separated_0700", handle);
137}
138
139/**
140 * @tc.name      : separated_0800
141 * @tc.desc      : When the separated property is true, under strict isolation, the name of the incoming library is in
142 *                  the allowed.libs configuration item, and the lib_paths and permitted.paths configurations match.
143 *                    Calling dlopen_ns can open so through the full path.
144 * @tc.level     : Level 1
145 */
146void separated_0800(void)
147{
148    Dl_namespace dlns;
149    dlns_init(&dlns, "separated_0800");
150
151    EXPECT_EQ("separated_0800", dlns_create(&dlns, path), EOK);
152    EXPECT_EQ("separated_0800", dlns_set_namespace_separated("separated_0800", true), EOK);
153    EXPECT_EQ("separated_0800", dlns_set_namespace_permitted_paths("separated_0800", dllNamePath), EOK);
154    EXPECT_EQ("separated_0800", dlns_set_namespace_lib_path("separated_0800", path), EOK);
155    EXPECT_EQ("separated_0800", dlns_set_namespace_allowed_libs("separated_0800", dllName), EOK);
156
157    void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
158    EXPECT_TRUE("separated_0800", handle);
159    dlclose(handle);
160}
161
162/**
163 * @tc.name      : separated_0900
164 * @tc.desc      : When the separated property is true, under strict isolation, the name of the incoming library is
165 *                  in the allowed.libs configuration item, and the lib_paths, permitted.paths configuration does not
166 *                      match, and calling dlopen_ns cannot open so through the full path
167 * @tc.level     : Level 2
168 */
169void separated_0900(void)
170{
171    Dl_namespace dlns;
172    dlns_init(&dlns, "separated_0900");
173
174    EXPECT_EQ("separated_0900", dlns_create(&dlns, path), EOK);
175    EXPECT_EQ("separated_0900", dlns_set_namespace_separated("separated_0900", true), EOK);
176    EXPECT_EQ("separated_0900", dlns_set_namespace_permitted_paths("separated_0900", errdllNamePath), EOK);
177    EXPECT_EQ("separated_0900", dlns_set_namespace_lib_path("separated_0900", errPath_ns), EOK);
178    EXPECT_EQ("separated_0900", dlns_set_namespace_allowed_libs("separated_0900", dllName_sep_009), EOK);
179
180    void* handle = dlopen_ns(&dlns, dllName_sep_009, RTLD_LAZY);
181    EXPECT_FALSE("separated_0900", handle);
182}
183
184/**
185 * @tc.name      : separated_1000
186 * @tc.desc      : When the separated property is true, under strict isolation, the lib_paths, permitted.paths
187 *                  configuration matches,The name of the incoming library is not in the allowed.libs configuration
188 *                      item, calling dlopen_ns cannot open so through the full path
189 * @tc.level     : Level 2
190 */
191void separated_1000(void)
192{
193    Dl_namespace dlns;
194    dlns_init(&dlns, "separated_1000");
195
196    EXPECT_EQ("separated_1000", dlns_create(&dlns, path), EOK);
197    EXPECT_EQ("separated_1000", dlns_set_namespace_separated("separated_1000", true), EOK);
198    EXPECT_EQ("separated_1000", dlns_set_namespace_permitted_paths("separated_1000", dllNamePath), EOK);
199    EXPECT_EQ("separated_1000", dlns_set_namespace_lib_path("separated_1000", path), EOK);
200    EXPECT_EQ("separated_1000", dlns_set_namespace_allowed_libs("separated_1000", dllName2), EOK);
201
202    void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
203    EXPECT_FALSE("separated_1000", handle);
204}
205
206/**
207 * @tc.name      : separated_1100
208 * @tc.desc      : The asan.lib.paths path will not be used when the Asan function is not enabled
209 * @tc.level     : Level 2
210 */
211void separated_1100(void)
212{
213    Dl_namespace dlns;
214    dlns_init(&dlns, "ns_asan_lib_path");
215
216    void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
217    EXPECT_TRUE("separated_1100", handle);
218    dlclose(handle);
219
220    void* handle1 = dlopen_ns(&dlns, libB, RTLD_LAZY);
221    EXPECT_FALSE("separated_1100", handle1);
222}
223
224/**
225 * @tc.name      : separated_1200
226 * @tc.desc      : asan.permitted.paths paths will not be used when the Asan function is not enabled
227 * @tc.level     : Level 2
228 */
229void separated_1200(void)
230{
231    Dl_namespace dlns;
232    dlns_init(&dlns, "ns_asan_permit_path");
233
234    char *libcc = "/data/tests/libc-test/src/C/libC.so";
235
236    void* handle = dlopen_ns(&dlns, libcc, RTLD_LAZY);
237    EXPECT_TRUE("separated_1200", handle);
238    dlclose(handle);
239
240    char *libbb = "/data/tests/libc-test/src/B/libB.so";
241
242    void* handle1 = dlopen_ns(&dlns, libbb, RTLD_LAZY);
243    EXPECT_FALSE("separated_1200", handle1);
244}
245
246TEST_FUN G_Fun_Array[] = {
247    separated_0100,
248    separated_0200,
249    separated_0300,
250    separated_0400,
251    separated_0500,
252    separated_0600,
253    separated_0700,
254    separated_0800,
255    separated_0900,
256    separated_1000,
257    separated_1100,
258    separated_1200,
259};
260
261int main(void)
262{
263    int num = sizeof(G_Fun_Array)/sizeof(TEST_FUN);
264    for (int pos = 0; pos < num; ++pos) {
265        G_Fun_Array[pos]();
266    }
267    return t_status;
268}