1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <dlfcn.h>
17570af302Sopenharmony_ci#include <errno.h>
18570af302Sopenharmony_ci#include <stdio.h>
19570af302Sopenharmony_ci#include <string.h>
20570af302Sopenharmony_ci
21570af302Sopenharmony_ci#include "dlns_test.h"
22570af302Sopenharmony_ci#include "functionalext.h"
23570af302Sopenharmony_ci
24570af302Sopenharmony_ciconst char *LIB_NAME_A = "libdlns_dlsym_dep_a.so";
25570af302Sopenharmony_ciconst char *LIB_NAME_B = "libdlns_dlsym_dep_b.so";
26570af302Sopenharmony_ciconst char *LIB_NAME_C = "libdlns_dlsym_dep_c.so";
27570af302Sopenharmony_ci
28570af302Sopenharmony_cistatic inline void dlns_dlsym_0100_init_ns(Dl_namespace *dlnsA, Dl_namespace *dlnsB, Dl_namespace *dlnsC)
29570af302Sopenharmony_ci{
30570af302Sopenharmony_ci    dlns_init(dlnsA, "dlns_dlsym_0100_A");
31570af302Sopenharmony_ci    dlns_init(dlnsB, "dlns_dlsym_0100_B");
32570af302Sopenharmony_ci    dlns_init(dlnsC, "dlns_dlsym_0100_C");
33570af302Sopenharmony_ci    EXPECT_EQ(__FUNCTION__, dlns_create(dlnsA, pathA), EOK);
34570af302Sopenharmony_ci    EXPECT_EQ(__FUNCTION__, dlns_create(dlnsB, pathB), EOK);
35570af302Sopenharmony_ci    EXPECT_EQ(__FUNCTION__, dlns_create(dlnsC, pathC), EOK);
36570af302Sopenharmony_ci    EXPECT_EQ(__FUNCTION__, dlns_inherit(dlnsB, dlnsC, NULL), EOK);
37570af302Sopenharmony_ci    EXPECT_EQ(__FUNCTION__, dlns_inherit(dlnsA, dlnsB, NULL), EOK);
38570af302Sopenharmony_ci}
39570af302Sopenharmony_ci
40570af302Sopenharmony_cistatic inline void close_so(void* handle)
41570af302Sopenharmony_ci{
42570af302Sopenharmony_ci    if (handle) {
43570af302Sopenharmony_ci        dlclose(handle);
44570af302Sopenharmony_ci    }
45570af302Sopenharmony_ci}
46570af302Sopenharmony_ci
47570af302Sopenharmony_ci/**
48570af302Sopenharmony_ci * @tc.name      : dlns_dlsym_0100
49570af302Sopenharmony_ci * @tc.desc      : Call the dlsym interface to load libA,libB,libC dynamic library.
50570af302Sopenharmony_ci * @tc.level     : Level1
51570af302Sopenharmony_ci */
52570af302Sopenharmony_cistatic void dlns_dlsym_0100(void)
53570af302Sopenharmony_ci{
54570af302Sopenharmony_ci    Dl_namespace dlnsA, dlnsB, dlnsC;
55570af302Sopenharmony_ci    dlns_dlsym_0100_init_ns(&dlnsA, &dlnsB, &dlnsC);
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci    void* handle_c = dlopen_ns(&dlnsC, LIB_NAME_C, RTLD_NOW);
58570af302Sopenharmony_ci    EXPECT_PTRNE(__FUNCTION__, handle_c, NULL);
59570af302Sopenharmony_ci    if (handle_c == NULL) {
60570af302Sopenharmony_ci        return;
61570af302Sopenharmony_ci    }
62570af302Sopenharmony_ci
63570af302Sopenharmony_ci    void* handle_b = dlopen_ns(&dlnsB, LIB_NAME_B, RTLD_NOW);
64570af302Sopenharmony_ci    EXPECT_PTRNE(__FUNCTION__, handle_b, NULL);
65570af302Sopenharmony_ci    if (handle_b == NULL) {
66570af302Sopenharmony_ci        close_so(handle_c);
67570af302Sopenharmony_ci        return;
68570af302Sopenharmony_ci    }
69570af302Sopenharmony_ci
70570af302Sopenharmony_ci    void* handle_a = dlopen_ns(&dlnsA, LIB_NAME_A, RTLD_NOW);
71570af302Sopenharmony_ci    EXPECT_PTRNE(__FUNCTION__, handle_a, NULL);
72570af302Sopenharmony_ci    if (handle_a == NULL) {
73570af302Sopenharmony_ci        close_so(handle_b);
74570af302Sopenharmony_ci        close_so(handle_c);
75570af302Sopenharmony_ci        return;
76570af302Sopenharmony_ci    }
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci    int(* test_a)(void) = dlsym(handle_a, "test");
79570af302Sopenharmony_ci    EXPECT_PTRNE(__FUNCTION__, test_a, NULL);
80570af302Sopenharmony_ci    if (test_a) {
81570af302Sopenharmony_ci        EXPECT_EQ(__FUNCTION__, test_result_1, test_a());
82570af302Sopenharmony_ci    }
83570af302Sopenharmony_ci
84570af302Sopenharmony_ci    int(* test_b)(void) = dlsym(handle_a, "test_b");
85570af302Sopenharmony_ci    EXPECT_PTRNE(__FUNCTION__, test_b, NULL);
86570af302Sopenharmony_ci    if (test_b) {
87570af302Sopenharmony_ci        EXPECT_EQ(__FUNCTION__, test_result_2, test_b());
88570af302Sopenharmony_ci    }
89570af302Sopenharmony_ci
90570af302Sopenharmony_ci    int(* test_c)(void) = dlsym(handle_c, "test");
91570af302Sopenharmony_ci    EXPECT_PTRNE(__FUNCTION__, test_c, NULL);
92570af302Sopenharmony_ci    if (test_c) {
93570af302Sopenharmony_ci        EXPECT_EQ(__FUNCTION__, test_result_3, test_c());
94570af302Sopenharmony_ci    }
95570af302Sopenharmony_ci
96570af302Sopenharmony_ci    int(* test_d)(void) = dlsym(handle_c, "test_c");
97570af302Sopenharmony_ci    EXPECT_PTRNE(__FUNCTION__, test_d, NULL);
98570af302Sopenharmony_ci    if (test_d) {
99570af302Sopenharmony_ci        EXPECT_EQ(__FUNCTION__, test_result_4, test_d());
100570af302Sopenharmony_ci    }
101570af302Sopenharmony_ci
102570af302Sopenharmony_ci    int(* test_e)(void) = dlsym(handle_a, "test_c");
103570af302Sopenharmony_ci    EXPECT_PTREQ(__FUNCTION__, test_e, NULL);
104570af302Sopenharmony_ci
105570af302Sopenharmony_ci    close_so(handle_c);
106570af302Sopenharmony_ci    close_so(handle_b);
107570af302Sopenharmony_ci    close_so(handle_a);
108570af302Sopenharmony_ci}
109570af302Sopenharmony_ci
110570af302Sopenharmony_ciTEST_FUN G_Fun_Array[] = {
111570af302Sopenharmony_ci    dlns_dlsym_0100,
112570af302Sopenharmony_ci};
113570af302Sopenharmony_ci
114570af302Sopenharmony_ciint main(void)
115570af302Sopenharmony_ci{
116570af302Sopenharmony_ci    int num = sizeof(G_Fun_Array)/sizeof(TEST_FUN);
117570af302Sopenharmony_ci    for (int pos = 0; pos < num; ++pos) {
118570af302Sopenharmony_ci        G_Fun_Array[pos]();
119570af302Sopenharmony_ci    }
120570af302Sopenharmony_ci
121570af302Sopenharmony_ci    return t_status;
122570af302Sopenharmony_ci}