t-ehandle.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-ehandle.c - test error handling implementation
  4. *
  5. * Copyright © 2016 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <stdbool.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <dpkg/test.h>
  26. #include <dpkg/ehandle.h>
  27. jmp_buf global_handler_jump;
  28. static void
  29. printer_empty(const char *msg, const void *data)
  30. {
  31. }
  32. static void
  33. handler_func(void)
  34. {
  35. longjmp(global_handler_jump, 1);
  36. }
  37. static void
  38. test_error_handler_func(void)
  39. {
  40. bool pass;
  41. if (setjmp(global_handler_jump)) {
  42. pass = true;
  43. pop_error_context(ehflag_normaltidy);
  44. } else {
  45. pass = false;
  46. push_error_context_func(handler_func, printer_empty, "test func");
  47. ohshit("test func error");
  48. test_bail("ohshit() is not supposed to return");
  49. }
  50. test_pass(pass);
  51. }
  52. static void
  53. test_error_handler_jump(void)
  54. {
  55. jmp_buf handler_jump;
  56. bool pass;
  57. if (setjmp(handler_jump)) {
  58. pass = true;
  59. pop_error_context(ehflag_normaltidy);
  60. } else {
  61. pass = false;
  62. push_error_context_jump(&handler_jump, printer_empty, "test jump");
  63. ohshit("test jump error");
  64. test_bail("ohshit() is not supposed to return");
  65. }
  66. test_pass(pass);
  67. }
  68. static void
  69. cleanup_error(int argc, void **argv)
  70. {
  71. ohshit("cleanup error");
  72. }
  73. static void
  74. test_cleanup_error(void)
  75. {
  76. jmp_buf handler_jump;
  77. bool pass;
  78. if (setjmp(handler_jump)) {
  79. /* The ohshit() is not supposed to get us here, as it should
  80. * be caught by the internal recursive error context. */
  81. pass = false;
  82. pop_cleanup(ehflag_normaltidy);
  83. pop_error_context(ehflag_normaltidy);
  84. } else {
  85. /* Mark any error before this as not passing. */
  86. pass = false;
  87. push_error_context_jump(&handler_jump, printer_empty, "test cleanup");
  88. push_cleanup(cleanup_error, ~ehflag_normaltidy, NULL, 0, 0);
  89. pop_error_context(ehflag_bombout);
  90. /* We should have recovered from the cleanup handler failing,
  91. * and arrived here corerctly. */
  92. pass = true;
  93. }
  94. test_pass(pass);
  95. }
  96. TEST_ENTRY(test)
  97. {
  98. int fd;
  99. test_plan(3);
  100. /* XXX: Shut up stderr, we don't want the error output. */
  101. fd = open("/dev/null", O_RDWR);
  102. if (fd < 0)
  103. test_bail("cannot open /dev/null");
  104. dup2(fd, 2);
  105. test_error_handler_func();
  106. test_error_handler_jump();
  107. test_cleanup_error();
  108. }