• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2025 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 #include "adlt_common.h"
16 
17 const char *libname = "libadlt_base_rpath_a.so";
18 
19 class DynLib
20 {
21 public:
DynLib()22     DynLib(): lib(nullptr), testPrintMessagefromA(nullptr)
23     {
24         lib = dlopen(libname, RTLD_LAZY);
25         printf("try to dlopen lib %s\n", libname);
26         testPrintMessagefromA = nullptr;
27         if (lib) {
28             testPrintMessagefromA = reinterpret_cast<void(*)()>(dlsym(lib, "testPrintMessagefromA"));
29         }
30     }
~DynLib()31     ~DynLib()
32     {
33         if (lib) {
34             dlclose(lib);
35         }
36     }
37     void *lib;
38     void (*testPrintMessagefromA)();
39 };
40 
TestRpath_test1(int testno)41 static void TestRpath_test1(int testno) {
42     DynLib *dynLib = new DynLib();
43     switch (testno) {
44         case 0:
45             // without -rpath, without LD_LIBRARY_PATH
46             printf("without -rpath, without LD_LIBRARY_PATH - lib is unaccessible\n");
47             EXPECT_PTREQ(__func__, dynLib->lib, nullptr);
48             break;
49         case 1:
50             // without -rpath, with LD_LIBRARY_PATH
51             printf("without -rpath, with LD_LIBRARY_PATH - check that lib is loaded,\n");
52             EXPECT_PTRNE(__func__, dynLib->lib, nullptr);
53             printf("load and call testPrintMessagefromA():\n");
54             EXPECT_PTRNE(__func__, dynLib->testPrintMessagefromA, nullptr);
55             dynLib->testPrintMessagefromA();
56             break;
57         case 2:
58             // with -rpath, without LD_LIBRARY_PATH
59             printf("with -rpath, without LD_LIBRARY_PATH - check that lib is loaded,\n");
60             EXPECT_PTRNE(__func__, dynLib->lib, nullptr);
61             printf("load and call testPrintMessagefromA():\n");
62             EXPECT_PTRNE(__func__, dynLib->testPrintMessagefromA, nullptr);
63             dynLib->testPrintMessagefromA();
64             break;
65         default:
66             EXPECT_TRUE("unknown test case", 0);
67         }
68     delete dynLib;
69 }
70 
main(int argc,char ** argv)71 int main(int argc, char **argv) {
72     if (argc > 1) {
73         int testno = atoi(argv[1]);
74         TestRpath_test1(testno);
75         return t_status;
76     }
77 
78     int ret = 0;
79 #ifdef WITHOUT_RPATH
80     ret = ret || run_self_command("0");
81     ret = ret || run_self_command("1", "LD_LIBRARY_PATH=/data/local/tmp/libc-test-lib");
82 #else
83     ret = run_self_command("2");
84 #endif
85 
86     return ret;
87 }
88