diff options
author | Dimitry Ivanov <dimitry@google.com> | 2016-09-19 10:50:28 -0700 |
---|---|---|
committer | Dimitry Ivanov <dimitry@google.com> | 2016-10-10 04:41:56 -0700 |
commit | 708589f5e8244a17d690848eed5f0fbfcce48a26 (patch) | |
tree | c89c96b86a7248a9e398a00128cd3c4459503a3c /tests/dlfcn_symlink_support.cpp | |
parent | 1c45e6fa47a2ab9b0b9dc8ab05f75023a8ebbd2d (diff) | |
download | bionic-708589f5e8244a17d690848eed5f0fbfcce48a26.tar.gz |
bionic-unit-tests: Create symlinks in runtime
Instead of creating symlinks in build time and then relying
on usage of adb sync (adb push follows symlinks) - create
them in test run time.
Test: mm && run bionic-unit-tests --gtest_filter=dl*:Dl*
Bug: http://b/22182538
Change-Id: I9d2a873e3ba303533c14ba5e3b779e4a67925462
Diffstat (limited to 'tests/dlfcn_symlink_support.cpp')
-rw-r--r-- | tests/dlfcn_symlink_support.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/dlfcn_symlink_support.cpp b/tests/dlfcn_symlink_support.cpp new file mode 100644 index 000000000..be1839ecf --- /dev/null +++ b/tests/dlfcn_symlink_support.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dlfcn_symlink_support.h" + +#include <gtest/gtest.h> + +#include <dlfcn.h> +#include <libgen.h> +#include <link.h> +#include <unistd.h> + +#include <android-base/strings.h> + +#include <algorithm> +#include <string> +#include <vector> + +static const constexpr char* source_file_name = "libdlext_test.so"; +static const constexpr char* symlink_name_prefix = "libdlext_test_"; + +static int dl_callback(struct dl_phdr_info *info, size_t /* size */, void *data) { + // The case when path is not absolute and is equal to source_file_name + // is disregarded intentionally since in bionic dlpi_name should always + // be realpath to a shared object. + const std::string suffix = std::string("/") + source_file_name; + + // TODO (dimitry): remove this check once fake libdl.so is gone + if (info->dlpi_name == nullptr) { + // This is linker imposing as libdl.so - skip it + return 0; + } + + if (android::base::EndsWith(info->dlpi_name, suffix.c_str())) { + std::string* path = reinterpret_cast<std::string*>(data); + *path = info->dlpi_name; + return 1; // found + } + + return 0; +} + +void create_dlfcn_test_symlink(const char* suffix, std::string* result) { + void* handle = dlopen(source_file_name, RTLD_NOW); + std::string source_file_path; + + ASSERT_TRUE(handle != nullptr) << dlerror(); + ASSERT_TRUE(dl_iterate_phdr(dl_callback, &source_file_path) == 1) + << "dl_phdr_info for \"" << source_file_name << "\" was not found."; + + dlclose(handle); + std::vector<char> buf; + std::copy(source_file_path.begin(), source_file_path.end(), std::back_inserter(buf)); + buf.push_back('\0'); + + std::string path_dir = dirname(&buf[0]); + std::string link_path = path_dir + "/" + symlink_name_prefix + suffix + ".so"; + + ASSERT_TRUE(symlink(source_file_path.c_str(), link_path.c_str()) == 0) << strerror(errno); + *result = link_path; +} + +void remove_dlfcn_test_symlink(const std::string& path) { + ASSERT_TRUE(unlink(path.c_str()) == 0) << strerror(errno); +} |