1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/*
* Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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 "gtest/gtest.h"
#include "test_sample1.h"
#include "test_sample2.h"
#include "test_sample4.h"
#include "symbol_searcher.h"
#include <dlfcn.h>
#include <linux/limits.h>
#include <unistd.h>
#include <cstdlib>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
fs::path exePath()
{
char result[PATH_MAX] = {0};
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
return fs::path(result).parent_path();
}
namespace backstage
{
struct SymbolSearcher : public ::testing::Test
{
};
TEST_F(SymbolSearcher, should_find_symbol_in_linked_library)
{
ASSERT_TRUE((void *)funcDefinedOnlyInTestSample4 == findSymbol("funcDefinedOnlyInTestSample4"));
}
TEST_F(SymbolSearcher, should_find_symbol_in_library_which_have_been_loaded_in_runtime)
{
fs::path pathToTestLib = exePath() / "libtest_sample2.so";
void *handle = dlopen(pathToTestLib.c_str(), RTLD_NOW);
ASSERT_TRUE(handle);
ASSERT_TRUE(dlsym(handle, "funcDefinedOnlyInTestSample2") ==
findSymbol("funcDefinedOnlyInTestSample2"));
dlclose(handle);
}
TEST_F(SymbolSearcher,
should_ignore_symbols_found_in_current_translation_unit_if_there_is_another_alternative)
{
fs::path pathToTestSample2 = exePath() / "libtest_sample2.so";
void *test_sample2_handle = dlopen(pathToTestSample2.c_str(), RTLD_NOW);
void *func_addr_in_test_sample2 =
dlsym(test_sample2_handle, "funcWhichCallFuncDefinedInTestSample3");
ASSERT_TRUE(test_sample2_handle);
ASSERT_TRUE((void *)funcDefinedInTestSample3_ButWrappedInTestSample1 !=
reinterpret_cast<void *(*)()>(func_addr_in_test_sample2)());
dlclose(test_sample2_handle);
}
} // namespace backstage
|