1 // Copyright (C) 2014 The Android Open Source Project
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "emugl/common/shared_library.h"
17 #include <gtest/gtest.h>
24 // Hack to get the current executable's full path.
28 extern std::string g_executable_path;
30 } // namespace internal
31 } // namespace testing
37 // Return the name/path of the test shared library to load.
38 // Note that this doesn't include a platform-specific extension.
39 // This assumes that the test shared library is under the lib/ sub-directory
40 // of the current executable's path!
41 std::string GetTestLibraryName() {
43 static const char kLibraryPrefix[] = "lib64";
44 static const char kSubDir[] = "lib64/";
46 static const char kLibraryPrefix[] = "lib";
47 static const char kSubDir[] = "lib/";
49 static const char kTestLibrarySuffix[] = "emugl_test_shared_library";
51 const char* exec_path = testing::internal::g_executable_path.c_str();
54 const char* p = strrchr(exec_path, '/');
55 const char* p2 = strrchr(exec_path, '\\');
62 const char* p = strrchr(exec_path, '/');
70 path = std::string(exec_path, p - exec_path + 1U);
73 path += kLibraryPrefix;
74 path += kTestLibrarySuffix;
75 printf("Library path: %s\n", path.c_str());
79 class SharedLibraryTest : public testing::Test {
82 // Locate the shared library
83 mLibraryPath = GetTestLibraryName();
86 ~SharedLibraryTest() {}
88 const char* library_path() const { return mLibraryPath.c_str(); }
91 std::string mLibraryPath;
94 class ScopedSharedLibrary {
96 explicit ScopedSharedLibrary(const SharedLibrary* lib) : mLib(lib) {}
97 ~ScopedSharedLibrary() {
100 const SharedLibrary* get() const { return mLib; }
102 const SharedLibrary* operator->() { return mLib; }
110 const SharedLibrary* mLib;
115 TEST_F(SharedLibraryTest, Open) {
116 ScopedSharedLibrary lib(SharedLibrary::open(library_path()));
117 EXPECT_TRUE(lib.get());
120 TEST_F(SharedLibraryTest, OpenFailureWithError) {
123 SharedLibrary* lib = SharedLibrary::open("/tmp/does/not/exists",
127 EXPECT_TRUE(error[0])
128 << "Could not get error string when failing to load library";
129 printf("Expected library load failure: [%s]\n", error);
132 TEST_F(SharedLibraryTest, OpenLibraryWithExtension) {
133 std::string path = library_path();
135 // test extension append
136 ScopedSharedLibrary libNoExtension(SharedLibrary::open(path.c_str()));
137 EXPECT_TRUE(libNoExtension.get());
138 libNoExtension.release();
142 #elif defined(__APPLE__)
143 // try to open the library without an extension
150 // test open with prepended extension
151 ScopedSharedLibrary lib(SharedLibrary::open(path.c_str()));
152 EXPECT_TRUE(lib.get());
156 TEST_F(SharedLibraryTest, OpenLibraryWithoutExtension) {
157 const char* library = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
158 ScopedSharedLibrary lib(SharedLibrary::open(library));
159 EXPECT_TRUE(lib.get());
163 TEST_F(SharedLibraryTest, FindSymbol) {
164 ScopedSharedLibrary lib(SharedLibrary::open(library_path()));
165 EXPECT_TRUE(lib.get());
168 typedef int (*FooFunction)(void);
170 FooFunction foo_func = reinterpret_cast<FooFunction>(
171 lib->findSymbol("foo_function"));
172 EXPECT_TRUE(foo_func);
173 EXPECT_EQ(42, (*foo_func)());