c946cca63c7178c4a892859a403e7d8f280a8176
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / emugl / common / shared_library_unittest.cpp
1 // Copyright (C) 2014 The Android Open Source Project
2 //
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 "emugl/common/shared_library.h"
16
17 #include <gtest/gtest.h>
18
19 #include <string>
20
21 #include <limits.h>
22 #include <string.h>
23
24 // Hack to get the current executable's full path.
25 namespace testing {
26 namespace internal {
27
28 extern std::string g_executable_path;
29
30 }  // namespace internal
31 }  // namespace testing
32
33 namespace emugl {
34
35 namespace {
36
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() {
42 #ifdef __x86_64__
43     static const char kLibraryPrefix[] = "lib64";
44     static const char kSubDir[] = "lib64/";
45 #else
46     static const char kLibraryPrefix[] = "lib";
47     static const char kSubDir[] = "lib/";
48 #endif
49     static const char kTestLibrarySuffix[] = "emugl_test_shared_library";
50
51     const char* exec_path = testing::internal::g_executable_path.c_str();
52
53 #ifdef _WIN32
54     const char* p = strrchr(exec_path, '/');
55     const char* p2 = strrchr(exec_path, '\\');
56     if (p2) {
57         if (!p || p2 > p) {
58             p = p2;
59         }
60     }
61 #else
62     const char* p = strrchr(exec_path, '/');
63 #endif
64
65     std::string path;
66
67     if (!p) {
68         path = "./";
69     } else {
70         path = std::string(exec_path, p - exec_path + 1U);
71     }
72     path += kSubDir;
73     path += kLibraryPrefix;
74     path += kTestLibrarySuffix;
75     printf("Library path: %s\n", path.c_str());
76     return path;
77 }
78
79 class SharedLibraryTest : public testing::Test {
80 public:
81     SharedLibraryTest() {
82         // Locate the shared library
83         mLibraryPath = GetTestLibraryName();
84     }
85
86     ~SharedLibraryTest() {}
87
88     const char* library_path() const { return mLibraryPath.c_str(); }
89
90 private:
91     std::string mLibraryPath;
92 };
93
94 class ScopedSharedLibrary {
95 public:
96     explicit ScopedSharedLibrary(const SharedLibrary* lib) : mLib(lib) {}
97     ~ScopedSharedLibrary() {
98         delete mLib;
99     }
100     const SharedLibrary* get() const { return mLib; }
101
102     const SharedLibrary* operator->() { return mLib; }
103
104     void release() {
105         delete mLib;
106         mLib = NULL;
107     }
108
109 private:
110     const SharedLibrary* mLib;
111 };
112
113 }  // namespace
114
115 TEST_F(SharedLibraryTest, Open) {
116     ScopedSharedLibrary lib(SharedLibrary::open(library_path()));
117     EXPECT_TRUE(lib.get());
118 }
119
120 TEST_F(SharedLibraryTest, OpenFailureWithError) {
121     char error[256];
122     error[0] = '\0';
123     SharedLibrary* lib = SharedLibrary::open("/tmp/does/not/exists",
124                                              error,
125                                              sizeof(error));
126     EXPECT_FALSE(lib);
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);
130 }
131
132 TEST_F(SharedLibraryTest, OpenLibraryWithExtension) {
133     std::string path = library_path();
134
135     // test extension append
136     ScopedSharedLibrary libNoExtension(SharedLibrary::open(path.c_str()));
137     EXPECT_TRUE(libNoExtension.get());
138     libNoExtension.release();
139
140 #ifdef _WIN32
141     path += ".dll";
142 #elif defined(__APPLE__)
143     // try to open the library without an extension
144
145     path += ".dylib";
146 #else
147     path += ".so";
148 #endif
149
150     // test open with prepended extension
151     ScopedSharedLibrary lib(SharedLibrary::open(path.c_str()));
152     EXPECT_TRUE(lib.get());
153 }
154
155 #ifdef __APPLE__
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());
160 }
161 #endif
162
163 TEST_F(SharedLibraryTest, FindSymbol) {
164     ScopedSharedLibrary lib(SharedLibrary::open(library_path()));
165     EXPECT_TRUE(lib.get());
166
167     if (lib.get()) {
168         typedef int (*FooFunction)(void);
169
170         FooFunction foo_func = reinterpret_cast<FooFunction>(
171                 lib->findSymbol("foo_function"));
172         EXPECT_TRUE(foo_func);
173         EXPECT_EQ(42, (*foo_func)());
174     }
175 }
176
177 }  // namespace emugl