From fc2e491f74ef151ac121fee2db00e33073255677 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 7 Feb 2019 19:21:26 -0500 Subject: Add test for exception handling There have been issues reported where exceptions are not thrown properly across shared library/DLL boundaries, so add a test specifically to ensure that exceptions are caught as thrown. --- qpdf/qtest/qpdf.test | 10 ++++++++++ qpdf/qtest/qpdf/exceptions.out | 5 +++++ qpdf/test_driver.cc | 44 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 qpdf/qtest/qpdf/exceptions.out diff --git a/qpdf/qtest/qpdf.test b/qpdf/qtest/qpdf.test index af103201..b6b33977 100644 --- a/qpdf/qtest/qpdf.test +++ b/qpdf/qtest/qpdf.test @@ -178,6 +178,16 @@ $td->runtest("check final version", {$td->STRING => "test 54 done\n", $td->EXIT_STATUS => 0}, $td->NORMALIZE_NEWLINES); +show_ntests(); +# ---------- +$td->notify("--- Exceptions ---"); +$n_tests += 1; + +$td->runtest("check exception handling", + {$td->COMMAND => "test_driver 61 -"}, + {$td->FILE => "exceptions.out", $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); + show_ntests(); # ---------- $td->notify("--- Dangling Refs ---"); diff --git a/qpdf/qtest/qpdf/exceptions.out b/qpdf/qtest/qpdf/exceptions.out new file mode 100644 index 00000000..ab69668e --- /dev/null +++ b/qpdf/qtest/qpdf/exceptions.out @@ -0,0 +1,5 @@ +Caught QPDFExc as expected +Caught QPDFSystemError as expected +Caught logic_error as expected +Caught runtime_error as expected +test 61 done diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index e83f84aa..462a8a23 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -269,6 +270,10 @@ void runtest(int n, char const* filename1, char const* arg2) pdf.processMemoryFile((std::string(filename1) + ".pdf").c_str(), p, size); } + else if (n == 61) + { + // Ignore filename argument entirely + } else if (n % 2 == 0) { if (n % 4 == 0) @@ -2046,6 +2051,45 @@ void runtest(int n, char const* filename1, char const* arg2) w.setStaticID(true); w.write(); } + else if (n == 61) + { + // Test to make sure exceptions can be caught properly across + // shared library boundaries. + pdf.setAttemptRecovery(false); + pdf.setSuppressWarnings(true); + try + { + pdf.processMemoryFile("empty", "", 0); + } + catch (QPDFExc& e) + { + std::cout << "Caught QPDFExc as expected" << std::endl; + } + try + { + QUtil::safe_fopen("/does/not/exist", "r"); + } + catch (QPDFSystemError& e) + { + std::cout << "Caught QPDFSystemError as expected" << std::endl; + } + try + { + QUtil::int_to_string_base(0, 12); + } + catch (std::logic_error& e) + { + std::cout << "Caught logic_error as expected" << std::endl; + } + try + { + QUtil::toUTF8(0xffffffff); + } + catch (std::runtime_error& e) + { + std::cout << "Caught runtime_error as expected" << std::endl; + } + } else { throw std::runtime_error(std::string("invalid test ") + -- cgit v1.2.3-54-g00ecf