Commit | Line | Data |
---|---|---|
1479465f GJ |
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 | ||
21 | #include <config.h> | |
22 | #include <compat.h> | |
23 | ||
24 | #include <stdbool.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | #include <dpkg/test.h> | |
29 | #include <dpkg/ehandle.h> | |
30 | ||
31 | jmp_buf global_handler_jump; | |
32 | ||
33 | static void | |
34 | printer_empty(const char *msg, const void *data) | |
35 | { | |
36 | } | |
37 | ||
38 | static void | |
39 | handler_func(void) | |
40 | { | |
41 | longjmp(global_handler_jump, 1); | |
42 | } | |
43 | ||
44 | static void | |
45 | test_error_handler_func(void) | |
46 | { | |
47 | bool pass; | |
48 | ||
49 | if (setjmp(global_handler_jump)) { | |
50 | pass = true; | |
51 | pop_error_context(ehflag_normaltidy); | |
52 | } else { | |
53 | pass = false; | |
54 | push_error_context_func(handler_func, printer_empty, "test func"); | |
55 | ohshit("test func error"); | |
56 | test_bail("ohshit() is not supposed to return"); | |
57 | } | |
58 | test_pass(pass); | |
59 | } | |
60 | ||
61 | static void | |
62 | test_error_handler_jump(void) | |
63 | { | |
64 | jmp_buf handler_jump; | |
65 | bool pass; | |
66 | ||
67 | if (setjmp(handler_jump)) { | |
68 | pass = true; | |
69 | pop_error_context(ehflag_normaltidy); | |
70 | } else { | |
71 | pass = false; | |
72 | push_error_context_jump(&handler_jump, printer_empty, "test jump"); | |
73 | ohshit("test jump error"); | |
74 | test_bail("ohshit() is not supposed to return"); | |
75 | } | |
76 | test_pass(pass); | |
77 | } | |
78 | ||
79 | static void | |
80 | cleanup_error(int argc, void **argv) | |
81 | { | |
82 | ohshit("cleanup error"); | |
83 | } | |
84 | ||
85 | static void | |
86 | test_cleanup_error(void) | |
87 | { | |
88 | jmp_buf handler_jump; | |
89 | bool pass; | |
90 | ||
91 | if (setjmp(handler_jump)) { | |
92 | /* The ohshit() is not supposed to get us here, as it should | |
93 | * be caught by the internal recursive error context. */ | |
94 | pass = false; | |
95 | ||
96 | pop_cleanup(ehflag_normaltidy); | |
97 | pop_error_context(ehflag_normaltidy); | |
98 | } else { | |
99 | /* Mark any error before this as not passing. */ | |
100 | pass = false; | |
101 | ||
102 | push_error_context_jump(&handler_jump, printer_empty, "test cleanup"); | |
103 | push_cleanup(cleanup_error, ~ehflag_normaltidy, NULL, 0, 0); | |
104 | pop_error_context(ehflag_bombout); | |
105 | ||
106 | /* We should have recovered from the cleanup handler failing, | |
107 | * and arrived here correctly. */ | |
108 | pass = true; | |
109 | } | |
110 | ||
111 | test_pass(pass); | |
112 | } | |
113 | ||
114 | TEST_ENTRY(test) | |
115 | { | |
116 | int fd; | |
117 | ||
118 | test_plan(3); | |
119 | ||
120 | /* XXX: Shut up stderr, we don't want the error output. */ | |
121 | fd = open("/dev/null", O_RDWR); | |
122 | if (fd < 0) | |
123 | test_bail("cannot open /dev/null"); | |
124 | dup2(fd, 2); | |
125 | ||
126 | test_error_handler_func(); | |
127 | test_error_handler_jump(); | |
128 | test_cleanup_error(); | |
129 | } |