shared_library_errors.cpp 2.9 KB

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