0
Files
android_webview
apps
ash
base
allocator
android
apple
containers
debug
files
fuchsia
functional
hash
i18n
ios
json
logging
mac
macros
memory
message_loop
metrics
nix
numerics
posix
power_monitor
process
profiler
ranges
sampling_heap_profiler
strings
substring_set_matcher
synchronization
system
task
test
third_party
threading
time
timer
trace_event
tracing
types
version_info
win
BUILD.gn
DEPS
DIR_METADATA
OWNERS
PRESUBMIT.py
README.md
SECURITY_OWNERS
at_exit.cc
at_exit.h
at_exit_unittest.cc
atomic_ref_count.h
atomic_sequence_num.h
atomicops.h
atomicops_internals_atomicword_compat.h
atomicops_internals_portable.h
atomicops_unittest.cc
auto_reset.h
auto_reset_unittest.cc
barrier_callback.h
barrier_callback_unittest.cc
barrier_closure.cc
barrier_closure.h
barrier_closure_unittest.cc
base64.cc
base64.h
base64_decode_fuzzer.cc
base64_encode_fuzzer.cc
base64_unittest.cc
base64url.cc
base64url.h
base64url_fuzzer.cc
base64url_unittest.cc
base_export.h
base_paths.cc
base_paths.h
base_paths_android.cc
base_paths_android.h
base_paths_apple.cc
base_paths_apple.h
base_paths_fuchsia.cc
base_paths_ios.h
base_paths_ios.mm
base_paths_mac.h
base_paths_mac.mm
base_paths_posix.cc
base_paths_posix.h
base_paths_win.cc
base_paths_win.h
base_switches.cc
base_switches.h
big_endian.h
big_endian_perftest.cc
bit_cast.h
bit_cast_unittest.cc
bits.h
bits_unittest.cc
build_time.h
build_time_unittest.cc
callback_list.cc
callback_list.h
callback_list_nocompile.nc
callback_list_unittest.cc
cancelable_callback.h
cancelable_callback_unittest.cc
check.cc
check.h
check_deref.h
check_example.cc
check_is_test.cc
check_is_test.h
check_is_test_unittest.cc
check_op.cc
check_op.h
check_unittest.cc
check_version_internal.h.in
command_line.cc
command_line.h
command_line_fuzzer.cc
command_line_unittest.cc
compiler_specific.h
component_export.h
component_export_unittest.cc
cpu.cc
cpu.h
cpu_reduction_experiment.cc
cpu_reduction_experiment.h
cpu_unittest.cc
critical_closure.h
critical_closure_internal_ios.mm
dcheck_is_on.h
enterprise_util.cc
enterprise_util.h
enterprise_util_mac.mm
enterprise_util_mac_unittest.mm
enterprise_util_win.cc
environment.cc
environment.h
environment_unittest.cc
export_template.h
feature_list.cc
feature_list.h
feature_list_unittest.cc
feature_visitor.h
features.cc
features.h
file_descriptor_posix.cc
file_descriptor_posix.h
file_descriptor_store.cc
file_descriptor_store.h
file_version_info.h
file_version_info_apple.h
file_version_info_apple.mm
file_version_info_win.cc
file_version_info_win.h
file_version_info_win_unittest.cc
format_macros.h
gmock_unittest.cc
gtest_prod_util.h
immediate_crash.h
immediate_crash_unittest.cc
lazy_instance.h
lazy_instance_helpers.cc
lazy_instance_helpers.h
lazy_instance_unittest.cc
libcpp_hardening_test.cc
linux_util.cc
linux_util.h
linux_util_unittest.cc
location.cc
location.h
location_unittest.cc
logging.cc
logging.h
logging_chromeos.cc
logging_nocompile.nc
logging_unittest.cc
logging_win.cc
logging_win.h
moving_window.h
moving_window_unittest.cc
native_library.cc
native_library.h
native_library_apple.mm
native_library_fuchsia.cc
native_library_posix.cc
native_library_unittest.cc
native_library_win.cc
no_destructor.h
no_destructor_nocompile.nc
no_destructor_unittest.cc
not_fatal_until.h
notimplemented.h
notreached.h
observer_list.h
observer_list_internal.cc
observer_list_internal.h
observer_list_nocompile.nc
observer_list_perftest.cc
observer_list_threadsafe.cc
observer_list_threadsafe.h
observer_list_threadsafe_unittest.cc
observer_list_types.cc
observer_list_types.h
observer_list_unittest.cc
one_shot_event.cc
one_shot_event.h
one_shot_event_unittest.cc
os_compat_android.cc
os_compat_android.h
os_compat_nacl.cc
os_compat_nacl.h
parameter_pack.h
parameter_pack_unittest.cc
path_service.cc
path_service.h
path_service_unittest.cc
pending_task.cc
pending_task.h
pickle.cc
pickle.h
pickle_fuzzer.cc
pickle_unittest.cc
rand_util.cc
rand_util.h
rand_util_fuchsia.cc
rand_util_nacl.cc
rand_util_perftest.cc
rand_util_posix.cc
rand_util_unittest.cc
rand_util_win.cc
run_loop.cc
run_loop.h
run_loop_nocompile.nc
run_loop_unittest.cc
safe_numerics_unittest.cc
scoped_add_feature_flags.cc
scoped_add_feature_flags.h
scoped_add_feature_flags_unittest.cc
scoped_clear_last_error.h
scoped_clear_last_error_unittest.cc
scoped_clear_last_error_win.cc
scoped_environment_variable_override.cc
scoped_environment_variable_override.h
scoped_generic.h
scoped_generic_unittest.cc
scoped_multi_source_observation.h
scoped_multi_source_observation_unittest.cc
scoped_native_library.cc
scoped_native_library.h
scoped_native_library_unittest.cc
scoped_observation.h
scoped_observation_traits.h
scoped_observation_unittest.cc
security_unittest.cc
sequence_checker.cc
sequence_checker.h
sequence_checker_impl.cc
sequence_checker_impl.h
sequence_checker_nocompile.nc
sequence_checker_unittest.cc
sequence_token.cc
sequence_token.h
sequence_token_unittest.cc
stack_canary_linux.cc
stack_canary_linux.h
stack_canary_linux_unittest.cc
state_transitions.h
state_transitions_unittest.cc
std_clamp_unittest.cc
stl_util.h
stl_util_unittest.cc
supports_user_data.cc
supports_user_data.h
supports_user_data_unittest.cc
sync_socket.cc
sync_socket.h
sync_socket_nacl.cc
sync_socket_posix.cc
sync_socket_unittest.cc
sync_socket_win.cc
sys_byteorder.h
sys_byteorder_unittest.cc
syslog_logging.cc
syslog_logging.h
thread_annotations.h
thread_annotations_nocompile.nc
thread_annotations_unittest.cc
token.cc
token.h
token_unittest.cc
tools_sanity_unittest.cc
traits_bag.h
traits_bag_nocompile.nc
traits_bag_unittest.cc
tuple.h
tuple_unittest.cc
unguessable_token.cc
unguessable_token.h
unguessable_token_unittest.cc
unsafe_buffers_nocompile.nc
unsafe_buffers_unittest.cc
uuid.cc
uuid.h
uuid_unittest.cc
value_iterators.cc
value_iterators.h
value_iterators_unittest.cc
values.cc
values.h
values_nocompile.nc
values_unittest.cc
version.cc
version.h
version_unittest.cc
vlog.cc
vlog.h
vlog_unittest.cc
write_build_date_header.py
build
build_overrides
buildtools
cc
chrome
chromecast
chromeos
clank
codelabs
components
content
crypto
dbus
device
docs
extensions
fuchsia_web
gin
google_apis
gpu
headless
infra
internal
ios
ios_internal
ipc
media
mojo
native_client
native_client_sdk
net
pdf
ppapi
printing
remoting
rlz
sandbox
services
signing_keys
skia
sql
storage
styleguide
testing
third_party
tools
ui
url
v8
webkit
.clang-format
.clang-tidy
.clangd
.git-blame-ignore-revs
.gitallowed
.gitattributes
.gitignore
.gitmodules
.gn
.mailmap
.rustfmt.toml
.vpython3
.yapfignore
ATL_OWNERS
AUTHORS
BUILD.gn
CODE_OF_CONDUCT.md
CPPLINT.cfg
CRYPTO_OWNERS
DEPS
DIR_METADATA
LICENSE
LICENSE.chromium_os
OWNERS
PRESUBMIT.py
PRESUBMIT_test.py
PRESUBMIT_test_mocks.py
README.md
WATCHLISTS
codereview.settings
src/base/native_library_unittest.cc
Rohit Rao 3477316f10 [ios] Implements support for NativeLibrary.
Bug: 356608952
Change-Id: Ic0286a8a811551422162cbe28d86e6a91a9355d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5838523
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1351837}
2024-09-06 02:32:17 +00:00

187 lines
6.2 KiB
C++

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/native_library.h"
#include "base/check.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/test/native_library_test_utils.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
const FilePath::CharType kDummyLibraryPath[] =
FILE_PATH_LITERAL("dummy_library");
TEST(NativeLibraryTest, LoadFailure) {
NativeLibraryLoadError error;
EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error));
EXPECT_FALSE(error.ToString().empty());
}
// |error| is optional and can be null.
TEST(NativeLibraryTest, LoadFailureWithNullError) {
EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), nullptr));
}
#if BUILDFLAG(IS_FUCHSIA)
TEST(NativeLibraryTest, LoadAbsolutePath) {
EXPECT_TRUE(LoadNativeLibrary(FilePath("/pkg/lib/libtest_shared_library.so"),
nullptr));
}
TEST(NativeLibraryTest, LoadAbsolutePath_OutsideLibraryRoot) {
NativeLibraryLoadError error;
EXPECT_FALSE(LoadNativeLibrary(FilePath("/pkg/tmp/libtest_shared_library.so"),
&error));
std::string expected_error =
"Absolute library paths must begin with /pkg/lib";
EXPECT_EQ(error.ToString(), expected_error);
}
#endif
TEST(NativeLibraryTest, GetNativeLibraryName) {
const char kExpectedName[] =
#if BUILDFLAG(IS_WIN)
"mylib.dll";
#elif BUILDFLAG(IS_IOS)
"Frameworks/mylib.framework/mylib";
#elif BUILDFLAG(IS_MAC)
"libmylib.dylib";
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
"libmylib.so";
#endif
EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib"));
}
TEST(NativeLibraryTest, GetLoadableModuleName) {
const char kExpectedName[] =
#if BUILDFLAG(IS_WIN)
"mylib.dll";
#elif BUILDFLAG(IS_IOS)
"Frameworks/mylib.framework";
#elif BUILDFLAG(IS_MAC)
"mylib.so";
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
"libmylib.so";
#endif
EXPECT_EQ(kExpectedName, GetLoadableModuleName("mylib"));
}
// ASAN will complain about our intentional ODR violation because of
// |g_native_library_exported_value| being defined globally both here
// and in the shared library.
#if !defined(ADDRESS_SANITIZER)
const char kTestLibraryName[] =
#if BUILDFLAG(IS_WIN)
"test_shared_library.dll";
#elif BUILDFLAG(IS_IOS)
"Frameworks/test_shared_library_ios.framework/test_shared_library_ios";
#elif BUILDFLAG(IS_MAC)
"libtest_shared_library.dylib";
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
"libtest_shared_library.so";
#endif
class TestLibrary {
public:
TestLibrary() : TestLibrary(NativeLibraryOptions()) {}
explicit TestLibrary(const NativeLibraryOptions& options)
: library_(nullptr) {
base::FilePath exe_path;
#if !BUILDFLAG(IS_FUCHSIA)
// Libraries do not sit alongside the executable in Fuchsia. NativeLibrary
// is aware of this and is able to resolve library paths correctly.
CHECK(base::PathService::Get(base::DIR_EXE, &exe_path));
#endif
library_ = LoadNativeLibraryWithOptions(
exe_path.AppendASCII(kTestLibraryName), options, nullptr);
CHECK(library_);
}
TestLibrary(const TestLibrary&) = delete;
TestLibrary& operator=(const TestLibrary&) = delete;
~TestLibrary() { UnloadNativeLibrary(library_); }
template <typename ReturnType, typename... Args>
ReturnType Call(const char* function_name, Args... args) {
return reinterpret_cast<ReturnType (*)(Args...)>(
GetFunctionPointerFromNativeLibrary(library_, function_name))(args...);
}
private:
NativeLibrary library_;
};
// NativeLibraaryTest.LoadLibrary is failing on M tablets only.
// crbug/641309
#if !BUILDFLAG(IS_ANDROID)
// Verifies that we can load a native library and resolve its exported symbols.
TEST(NativeLibraryTest, LoadLibrary) {
TestLibrary library;
EXPECT_EQ(5, library.Call<int>("GetSimpleTestValue"));
}
#endif // !BUILDFLAG(IS_ANDROID)
// Android dlopen() requires further investigation, as it might vary across
// versions with respect to symbol resolution scope.
// TSan and MSan error out on RTLD_DEEPBIND, https://crbug.com/705255
#if !BUILDFLAG(IS_ANDROID) && !defined(THREAD_SANITIZER) && \
!defined(MEMORY_SANITIZER)
// Verifies that the |prefer_own_symbols| option satisfies its guarantee that
// a loaded library will always prefer local symbol resolution before
// considering global symbols.
TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) {
NativeLibraryOptions options;
options.prefer_own_symbols = true;
TestLibrary library(options);
// Verify that this binary and the DSO use different storage for
// |g_native_library_exported_value|.
g_native_library_exported_value = 1;
library.Call<void>("SetExportedValue", 2);
EXPECT_EQ(1, g_native_library_exported_value);
g_native_library_exported_value = 3;
EXPECT_EQ(2, library.Call<int>("GetExportedValue"));
// Both this binary and the library link against the
// native_library_test_utils source library, which in turn exports the
// NativeLibraryTestIncrement() function whose return value depends on some
// static internal state.
//
// The DSO's GetIncrementValue() forwards to that function inside the DSO.
//
// Here we verify that direct calls to NativeLibraryTestIncrement() in this
// binary return a sequence of values independent from the sequence returned
// by GetIncrementValue(), ensuring that the DSO is calling its own local
// definition of NativeLibraryTestIncrement().
// Ensure that the counter starts at the expected value (0).
library.Call<void>("NativeLibraryResetCounter");
NativeLibraryResetCounter();
EXPECT_EQ(1, library.Call<int>("GetIncrementValue"));
EXPECT_EQ(1, NativeLibraryTestIncrement());
EXPECT_EQ(2, library.Call<int>("GetIncrementValue"));
EXPECT_EQ(3, library.Call<int>("GetIncrementValue"));
EXPECT_EQ(4, library.Call<int>("NativeLibraryTestIncrement"));
EXPECT_EQ(2, NativeLibraryTestIncrement());
EXPECT_EQ(3, NativeLibraryTestIncrement());
}
#endif // !BUILDFLAG(IS_ANDROID) && !defined(THREAD_SANITIZER) && \
// !defined(MEMORY_SANITIZER)
#endif // !defined(ADDRESS_SANITIZER)
} // namespace base