1 // Copyright 2011-2012 Renato Tegon Forti.
2 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
3 // Copyright 2015-2020 Antony Polukhin.
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 // For more information, see http://www.boost.org
10 
11 #include "../example/b2_workarounds.hpp"
12 #include <boost/dll/shared_library.hpp>
13 #include <boost/dll/library_info.hpp>
14 #include <boost/core/lightweight_test.hpp>
15 #include <boost/function.hpp>
16 
17 
18 // Unit Tests
main(int argc,char * argv[])19 int main(int argc, char* argv[]) {
20     using namespace boost::dll;
21 
22     boost::dll::fs::path shared_library_path = b2_workarounds::first_lib_from_argv(argc, argv);
23     BOOST_TEST(shared_library_path.string().find("test_library") != std::string::npos);
24     BOOST_TEST(b2_workarounds::is_shared_library(shared_library_path));
25     boost::dll::fs::path bad_path = shared_library_path / "directory_that_does_not_exist";
26 
27     try {
28         shared_library lib(bad_path);
29         BOOST_TEST(false);
30     } catch (const boost::dll::fs::system_error& e) {
31         std::cout << e.what() << '\n';
32     }
33 
34     try {
35         shared_library lib;
36         lib.get<int>("variable_or_function_that_does_not_exist");
37         BOOST_TEST(false);
38     } catch (const boost::dll::fs::system_error& e) {
39         std::cout << e.what() << '\n';
40     }
41 
42     try {
43         shared_library lib("");
44         BOOST_TEST(false);
45     } catch (const boost::dll::fs::system_error& e) {
46         std::cout << e.what() << '\n';
47     }
48 
49     try {
50         shared_library lib("\0\0");
51         BOOST_TEST(false);
52     } catch (const boost::dll::fs::system_error& e) {
53         std::cout << e.what() << '\n';
54     }
55 
56     try {
57         shared_library lib;
58         lib.location();
59         BOOST_TEST(false);
60     } catch (const boost::dll::fs::system_error& e) {
61         std::cout << e.what() << '\n';
62     }
63 
64     try {
65         shared_library lib;
66         lib.load("\0\0", load_mode::rtld_global);
67         BOOST_TEST(false);
68     } catch (const boost::dll::fs::system_error& e) {
69         std::cout << e.what() << '\n';
70     }
71 
72     shared_library sl(shared_library_path);
73     try {
74         sl.get<int>("variable_or_function_that_does_not_exist");
75         BOOST_TEST(false);
76     } catch (const boost::dll::fs::system_error& e) {
77         std::cout << e.what() << '\n';
78     }
79 
80     try {
81         library_info lib("\0");
82         BOOST_TEST(false);
83     } catch (const std::exception& e) {
84         std::cout << e.what() << '\n';
85     }
86 
87     try {
88         std::string not_a_binary(argv[1]);
89         not_a_binary += "/not_a_binary";
90         std::ofstream ofs(not_a_binary.c_str());
91         ofs << "This is not a binary file, so library_info must report 'Unsupported binary format'";
92         ofs.close();
93         library_info lib(not_a_binary);
94         BOOST_TEST(false);
95     } catch (const std::exception& e) {
96         std::cout << e.what() << '\n';
97     }
98     return boost::report_errors();
99 }
100 
101