| 1 | // Copyright 2011-2012 Renato Tegon Forti |
| 2 | // Copyright Antony Polukhin, 2015-2024 |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // (See accompanying file LICENSE_1_0.txt |
| 6 | // or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | // For more information, see http://www.boost.org |
| 9 | |
| 10 | // MinGW related workaround |
| 11 | #define BOOST_DLL_FORCE_ALIAS_INSTANTIATION |
| 12 | #include "../example/b2_workarounds.hpp" |
| 13 | |
| 14 | #include <boost/dll/library_info.hpp> |
| 15 | #include <boost/core/lightweight_test.hpp> |
| 16 | #include "../example/tutorial4/static_plugin.hpp" |
| 17 | |
| 18 | // Unit Tests |
| 19 | |
| 20 | #include <iterator> |
| 21 | |
| 22 | int main(int argc, char* argv[]) |
| 23 | { |
| 24 | boost::dll::fs::path shared_library_path = b2_workarounds::first_lib_from_argv(argc, argv); |
| 25 | BOOST_TEST(shared_library_path.string().find("test_library" ) != std::string::npos); |
| 26 | |
| 27 | boost::dll::library_info lib_info(shared_library_path); |
| 28 | std::vector<std::string> sec = lib_info.sections(); |
| 29 | std::copy(first: sec.begin(), last: sec.end(), result: std::ostream_iterator<std::string>(std::cout, ", " )); |
| 30 | BOOST_TEST(std::find(sec.begin(), sec.end(), "boostdll" ) != sec.end()); |
| 31 | |
| 32 | |
| 33 | std::cout << "\n\n\n" ; |
| 34 | std::vector<std::string> symb = lib_info.symbols(); |
| 35 | std::copy(first: symb.begin(), last: symb.end(), result: std::ostream_iterator<std::string>(std::cout, "\n" )); |
| 36 | BOOST_TEST(std::find(symb.begin(), symb.end(), "const_integer_g" ) != symb.end()); |
| 37 | BOOST_TEST(std::find(symb.begin(), symb.end(), "say_hello" ) != symb.end()); |
| 38 | |
| 39 | #if defined(__GNUC__) && __GNUC__ >= 4 && defined(__ELF__) |
| 40 | BOOST_TEST(std::find(symb.begin(), symb.end(), "protected_function" ) != symb.end()); |
| 41 | #endif |
| 42 | |
| 43 | symb = lib_info.symbols(section_name: "boostdll" ); |
| 44 | std::copy(first: symb.begin(), last: symb.end(), result: std::ostream_iterator<std::string>(std::cout, "\n" )); |
| 45 | BOOST_TEST(std::find(symb.begin(), symb.end(), "const_integer_g_alias" ) != symb.end()); |
| 46 | BOOST_TEST(std::find(symb.begin(), symb.end(), "foo_variable" ) != symb.end()); |
| 47 | BOOST_TEST(std::find(symb.begin(), symb.end(), "const_integer_g" ) == symb.end()); |
| 48 | BOOST_TEST(std::find(symb.begin(), symb.end(), "say_hello" ) == symb.end()); |
| 49 | BOOST_TEST(lib_info.symbols(std::string("boostdll" )) == symb); |
| 50 | |
| 51 | std::vector<std::string> empty = lib_info.symbols(section_name: "empty" ); |
| 52 | BOOST_TEST(empty.empty() == true); |
| 53 | |
| 54 | BOOST_TEST(lib_info.symbols("section_that_does_not_exist" ).empty()); |
| 55 | |
| 56 | // Self testing |
| 57 | std::cout << "Self: " << argv[0]; |
| 58 | boost::dll::library_info self_info(argv[0]); |
| 59 | |
| 60 | sec = self_info.sections(); |
| 61 | //std::copy(sec.begin(), sec.end(), std::ostream_iterator<std::string>(std::cout, ", ")); |
| 62 | BOOST_TEST(std::find(sec.begin(), sec.end(), "boostdll" ) != sec.end()); |
| 63 | |
| 64 | symb = self_info.symbols(section_name: "boostdll" ); |
| 65 | std::copy(first: symb.begin(), last: symb.end(), result: std::ostream_iterator<std::string>(std::cout, "\n" )); |
| 66 | BOOST_TEST(std::find(symb.begin(), symb.end(), "create_plugin" ) != symb.end()); |
| 67 | |
| 68 | boost::dll::fs::path sections_stripped_path = "/lib/x86_64-linux-gnu/libaio.so.1" ; |
| 69 | if (exists(p: sections_stripped_path)) { |
| 70 | boost::dll::library_info stripped_lib(sections_stripped_path); |
| 71 | std::cout << "\n\n\n" ; |
| 72 | symb = stripped_lib.symbols(); |
| 73 | std::copy(first: symb.begin(), last: symb.end(), result: std::ostream_iterator<std::string>(std::cout, "\n" )); |
| 74 | BOOST_TEST(!symb.empty()); |
| 75 | } |
| 76 | |
| 77 | return boost::report_errors(); |
| 78 | } |
| 79 | |