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 * @tc.name      : dlopen_ns_special_0100
26 * @tc.desc      : Loading the same library multiple times from the same namespace.
27 * @tc.level     : Level 2
28 */
29void dlopen_ns_special_0100(void)
30{
31    Dl_namespace dlns_default;
32    dlns_get(NULL, &dlns_default);
33    Dl_namespace dlns;
34    dlns_init(&dlns, "ns_no_allowed_libs");
35    dlns_inherit(&dlns, &dlns_default, "libc++.so");
36
37    void* handle1 = dlopen_ns(&dlns, dllName, RTLD_LAZY);
38    EXPECT_TRUE("dlopen_ns_special_0100", handle1);
39
40    void* handle2 = dlopen_ns(&dlns, dllName, RTLD_LAZY);
41    EXPECT_TRUE("dlopen_ns_special_0100", handle2);
42
43    void* handle3 = dlopen_ns(&dlns, dllName, RTLD_LAZY);
44    EXPECT_TRUE("dlopen_ns_special_0100", handle3);
45
46    dlclose(handle1);
47    dlclose(handle2);
48    dlclose(handle3);
49}
50
51/**
52 * @tc.name      : dlopen_ns_special_0200
53 * @tc.desc      : Loading the same library multiple times in different namespaces.
54 * @tc.level     : Level 2
55 */
56void dlopen_ns_special_0200(void)
57{
58    Dl_namespace dlns_default;
59    dlns_get(NULL, &dlns_default);
60    Dl_namespace dlns_no_allowed_libs, dlns_normal, dlns_wrong_lib_path;
61    dlns_init(&dlns_no_allowed_libs, "ns_no_allowed_libs");
62    dlns_init(&dlns_normal, "ns_normal");
63    dlns_init(&dlns_wrong_lib_path, "inherited_class");
64    dlns_inherit(&dlns_no_allowed_libs, &dlns_default, "libc++.so");
65    dlns_inherit(&dlns_normal, &dlns_default, "libc++.so");
66    dlns_inherit(&dlns_wrong_lib_path, &dlns_default, "libc++.so");
67
68    void* handle1 = dlopen_ns(&dlns_no_allowed_libs, dllName, RTLD_LAZY);
69    EXPECT_TRUE("dlopen_ns_special_0200", handle1);
70
71    void* handle2 = dlopen_ns(&dlns_normal, dllName, RTLD_LAZY);
72    EXPECT_TRUE("dlopen_ns_special_0200", handle2);
73
74    void* handle3 = dlopen_ns(&dlns_wrong_lib_path, dllName, RTLD_LAZY);
75    EXPECT_TRUE("dlopen_ns_special_0200", handle3);
76
77    dlclose(handle1);
78    dlclose(handle2);
79    dlclose(handle3);
80}
81
82/**
83 * @tc.name      : dlopen_ns_sys_path_0100
84 * @tc.desc      : arm platform, lib_paths in the default namespace is the system path
85 *                  read in the ld-musl-namespace-arm.ini file
86 * @tc.level     : Level 2
87 */
88void dlopen_ns_sys_path_0100(void)
89{
90    Dl_namespace dlns;
91    dlns_init(&dlns, "default");
92    void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
93    EXPECT_TRUE("dlopen_ns_sys_path_0100", handle);
94    dlclose(handle);
95}
96
97int main(void)
98{
99    dlopen_ns_special_0100();
100    dlopen_ns_special_0200();
101    dlopen_ns_sys_path_0100();
102
103    return t_status;
104}