1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <gtest/gtest.h>
17 
18 #include <android-base/strings.h>
19 #include <dirent.h>
20 #include <dlfcn.h>
21 #include <vndksupport/linker.h>
22 #include <string>
23 
24 // Since the test executable will be in /data and ld.config.txt does not
25 // configure sphal namespace for executables in /data, the call to
26 // android_load_sphal_library will always fallback to the plain dlopen from the
27 // default namespace.
28 
29 // Let's use libEGL_<chipset>.so as a SP-HAL in test
find_sphal_lib()30 static std::string find_sphal_lib() {
31     const char* path =
32 #if defined(__LP64__)
33         "/vendor/lib64/egl";
34 #else
35         "/vendor/lib/egl";
36 #endif
37     std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path), closedir);
38 
39     dirent* dp;
40     while ((dp = readdir(dir.get())) != nullptr) {
41         std::string name = dp->d_name;
42         if (android::base::StartsWith(name, "libEGL_")) {
43             return std::string(path) + "/" + name;
44         }
45     }
46     return "";
47 }
48 
TEST(linker,load_existing_lib)49 TEST(linker, load_existing_lib) {
50     std::string name = find_sphal_lib();
51     ASSERT_NE("", name);
52     void* handle = android_load_sphal_library(name.c_str(), RTLD_NOW | RTLD_LOCAL);
53     ASSERT_NE(nullptr, handle);
54     android_unload_sphal_library(handle);
55 }
56 
TEST(linker,load_nonexisting_lib)57 TEST(linker, load_nonexisting_lib) {
58     void* handle = android_load_sphal_library("libNeverUseThisName.so", RTLD_NOW | RTLD_LOCAL);
59     ASSERT_EQ(nullptr, handle);
60 }
61