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 // Let's use libEGL_<chipset>.so as a SP-HAL in test
find_sphal_lib()25 static std::string find_sphal_lib() {
26     const char* path =
27 #if defined(__LP64__)
28         "/vendor/lib64/egl";
29 #else
30         "/vendor/lib/egl";
31 #endif
32     std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path), closedir);
33 
34     dirent* dp;
35     while ((dp = readdir(dir.get())) != nullptr) {
36         std::string name = dp->d_name;
37         if (android::base::StartsWith(name, "libEGL_")) {
38             return std::string(path) + "/" + name;
39         }
40     }
41     return "";
42 }
43 
TEST(linker,load_existing_lib)44 TEST(linker, load_existing_lib) {
45     std::string name = find_sphal_lib();
46     ASSERT_NE("", name);
47     void* handle = android_load_sphal_library(name.c_str(), RTLD_NOW | RTLD_LOCAL);
48     ASSERT_NE(nullptr, handle);
49     android_unload_sphal_library(handle);
50 }
51 
TEST(linker,load_nonexisting_lib)52 TEST(linker, load_nonexisting_lib) {
53     void* handle = android_load_sphal_library("libNeverUseThisName.so", RTLD_NOW | RTLD_LOCAL);
54     ASSERT_EQ(nullptr, handle);
55 }
56