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