Commit | Line | Data |
---|---|---|
1479465f GJ |
1 | /* |
2 | * libdpkg - Debian packaging suite library routines | |
3 | * t-error.c - test error message reporting | |
4 | * | |
5 | * Copyright © 2014 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 <https://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <compat.h> | |
23 | ||
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <stdio.h> | |
27 | ||
28 | #include <dpkg/test.h> | |
29 | #include <dpkg/error.h> | |
30 | ||
31 | static void | |
32 | test_dpkg_error_put(void) | |
33 | { | |
34 | struct dpkg_error err = DPKG_ERROR_INIT; | |
35 | char *errstr_ref = NULL; | |
36 | ||
37 | test_pass(err.type == DPKG_MSG_NONE); | |
38 | test_pass(err.str == NULL); | |
39 | ||
40 | test_pass(dpkg_put_warn(NULL, "void error") < 0); | |
41 | test_pass(dpkg_put_error(NULL, "void error") < 0); | |
42 | test_pass(dpkg_put_errno(NULL, "void error") < 0); | |
43 | ||
44 | test_pass(dpkg_put_warn(&err, "test warning %d", 10) < 0); | |
45 | test_str(err.str, ==, "test warning 10"); | |
46 | test_warn(err); | |
47 | ||
48 | test_pass(dpkg_put_error(&err, "test error %d", 20) < 0); | |
49 | test_str(err.str, ==, "test error 20"); | |
50 | test_error(err); | |
51 | ||
52 | errno = ENOENT; | |
53 | if (asprintf(&errstr_ref, "test errno 30 (%s)", strerror(errno)) < 0) | |
54 | test_bail("cannot allocate string"); | |
55 | test_pass(dpkg_put_errno(&err, "test errno %d", 30) < 0); | |
56 | test_str(err.str, ==, errstr_ref); | |
57 | test_error(err); | |
58 | free(errstr_ref); | |
59 | errno = 0; | |
60 | } | |
61 | ||
62 | static void | |
63 | test_dpkg_error_destroy(void) | |
64 | { | |
65 | struct dpkg_error err = DPKG_ERROR_INIT; | |
66 | ||
67 | test_pass(dpkg_put_error(&err, "test destroy") < 0); | |
68 | test_pass(err.type == DPKG_MSG_ERROR); | |
69 | test_pass(err.str != NULL); | |
70 | dpkg_error_destroy(&err); | |
71 | test_pass(err.type == DPKG_MSG_NONE); | |
72 | test_pass(err.str == NULL); | |
73 | } | |
74 | ||
75 | TEST_ENTRY(test) | |
76 | { | |
77 | test_plan(19); | |
78 | ||
79 | test_dpkg_error_put(); | |
80 | test_dpkg_error_destroy(); | |
81 | } |