• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
17 #define LOG_TAG "library_tests"
18 #include <utils/Log.h>
19 
20 #include <mediautils/Library.h>
21 
22 #include <android-base/file.h>
23 #include <gtest/gtest.h>
24 
25 using namespace android::mediautils;
26 
27 namespace {
28 
29 [[maybe_unused]] static int32_t here = 0;  // accessed on same thread.
30 
31 #if __android__
TEST(library_tests,basic)32 TEST(library_tests, basic) {
33     std::string path = android::base::GetExecutableDirectory() + "/libsharedtest.so";
34     // The flags to loadLibrary should not include  RTLD_GLOBAL or RTLD_NODELETE
35     // which prevent unloading.
36     std::shared_ptr<void> library = loadLibrary(path.c_str(), RTLD_LAZY);
37     ASSERT_TRUE(library);
38     ASSERT_EQ(1, library.use_count());
39 
40     std::shared_ptr<int32_t*> ptr = getObjectFromLibrary<int32_t*>("gPtr", library);
41     ASSERT_TRUE(ptr);
42     ASSERT_EQ(2, library.use_count());
43 
44     ASSERT_EQ(nullptr, *ptr); // original contents are nullptr.
45 
46     // There is a static object destructor in libsharedtest.so that will set the
47     // contents of the integer pointer (if non-null) to 1 when called.
48     // This is used to detect that the library is unloaded.
49     *ptr = &here;
50 
51     ptr.reset();  // Note: this shared pointer uses library's refcount.
52     ASSERT_EQ(1, library.use_count());  // Verify library's refcount goes down by 1.
53     ASSERT_EQ(0, here);  // the shared library's object destructor hasn't been called.
54 
55     // use weak_ptr to investigate whether the library is gone.
56     std::weak_ptr<void> wlibrary = library;
57     ASSERT_EQ(1, wlibrary.use_count());
58     library.reset();
59 
60     // we should have released the last reference.
61     ASSERT_EQ(0, wlibrary.use_count());
62 
63     // The library should unload and the global object destroyed.
64     // Note on Android, specifying RTLD_GLOBAL or RTLD_NODELETE in the flags
65     // will prevent unloading libraries.
66     ASSERT_EQ(1, here);
67 }
68 #endif
69 
TEST(library_tests,sad_library)70 TEST(library_tests, sad_library) {
71     std::string path = android::base::GetExecutableDirectory()
72             + "/something_random_library_that_doesn't_exit.so";
73 
74     std::shared_ptr<void> library = loadLibrary(path.c_str(), RTLD_LAZY);
75     // We shouldn't crash on an invalid library path, just return an empty shared pointer.
76     // Check the logcat for any error details.
77     ASSERT_FALSE(library);
78 }
79 
80 } // namespace
81