]>
Commit | Line | Data |
---|---|---|
780896a4 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include "sd-bus.h" | |
23 | #include "bus-error.h" | |
24 | #include "bus-util.h" | |
25 | ||
4a0a7417 | 26 | static void test_error(void) { |
780896a4 | 27 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL, second = SD_BUS_ERROR_NULL; |
79f8d3d2 LP |
28 | const sd_bus_error const_error = SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "const error"); |
29 | const sd_bus_error temporarily_const_error = { | |
30 | .name = SD_BUS_ERROR_ACCESS_DENIED, | |
31 | .message = "oh! no", | |
32 | ._need_free = -1 | |
33 | }; | |
780896a4 LP |
34 | |
35 | assert_se(!sd_bus_error_is_set(&error)); | |
36 | assert_se(sd_bus_error_set(&error, SD_BUS_ERROR_NOT_SUPPORTED, "xxx") == -ENOTSUP); | |
37 | assert_se(streq(error.name, SD_BUS_ERROR_NOT_SUPPORTED)); | |
38 | assert_se(streq(error.message, "xxx")); | |
39 | assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_NOT_SUPPORTED)); | |
40 | assert_se(sd_bus_error_get_errno(&error) == ENOTSUP); | |
41 | assert_se(sd_bus_error_is_set(&error)); | |
42 | sd_bus_error_free(&error); | |
43 | ||
44 | assert_se(!sd_bus_error_is_set(&error)); | |
45 | assert_se(sd_bus_error_setf(&error, SD_BUS_ERROR_FILE_NOT_FOUND, "yyy %i", -1) == -ENOENT); | |
46 | assert_se(streq(error.name, SD_BUS_ERROR_FILE_NOT_FOUND)); | |
47 | assert_se(streq(error.message, "yyy -1")); | |
48 | assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND)); | |
49 | assert_se(sd_bus_error_get_errno(&error) == ENOENT); | |
50 | assert_se(sd_bus_error_is_set(&error)); | |
51 | ||
52 | assert_se(!sd_bus_error_is_set(&second)); | |
79f8d3d2 LP |
53 | assert_se(second._need_free == 0); |
54 | assert_se(error._need_free > 0); | |
780896a4 | 55 | assert_se(sd_bus_error_copy(&second, &error) == -ENOENT); |
79f8d3d2 | 56 | assert_se(second._need_free > 0); |
780896a4 LP |
57 | assert_se(streq(error.name, second.name)); |
58 | assert_se(streq(error.message, second.message)); | |
59 | assert_se(sd_bus_error_get_errno(&second) == ENOENT); | |
60 | assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_NOT_FOUND)); | |
61 | assert_se(sd_bus_error_is_set(&second)); | |
62 | ||
63 | sd_bus_error_free(&error); | |
79f8d3d2 LP |
64 | sd_bus_error_free(&second); |
65 | ||
66 | assert_se(!sd_bus_error_is_set(&second)); | |
67 | assert_se(const_error._need_free == 0); | |
68 | assert_se(sd_bus_error_copy(&second, &const_error) == -EEXIST); | |
69 | assert_se(second._need_free == 0); | |
70 | assert_se(streq(const_error.name, second.name)); | |
71 | assert_se(streq(const_error.message, second.message)); | |
72 | assert_se(sd_bus_error_get_errno(&second) == EEXIST); | |
73 | assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_EXISTS)); | |
74 | assert_se(sd_bus_error_is_set(&second)); | |
75 | sd_bus_error_free(&second); | |
76 | ||
77 | assert_se(!sd_bus_error_is_set(&second)); | |
78 | assert_se(temporarily_const_error._need_free < 0); | |
79 | assert_se(sd_bus_error_copy(&second, &temporarily_const_error) == -EACCES); | |
80 | assert_se(second._need_free > 0); | |
81 | assert_se(streq(temporarily_const_error.name, second.name)); | |
82 | assert_se(streq(temporarily_const_error.message, second.message)); | |
83 | assert_se(sd_bus_error_get_errno(&second) == EACCES); | |
84 | assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_ACCESS_DENIED)); | |
85 | assert_se(sd_bus_error_is_set(&second)); | |
780896a4 LP |
86 | |
87 | assert_se(!sd_bus_error_is_set(&error)); | |
763e20e6 LP |
88 | assert_se(sd_bus_error_set_const(&error, "System.Error.EUCLEAN", "Hallo") == -EUCLEAN); |
89 | assert_se(streq(error.name, "System.Error.EUCLEAN")); | |
780896a4 | 90 | assert_se(streq(error.message, "Hallo")); |
763e20e6 | 91 | assert_se(sd_bus_error_has_name(&error, "System.Error.EUCLEAN")); |
780896a4 LP |
92 | assert_se(sd_bus_error_get_errno(&error) == EUCLEAN); |
93 | assert_se(sd_bus_error_is_set(&error)); | |
94 | sd_bus_error_free(&error); | |
95 | ||
96 | assert_se(!sd_bus_error_is_set(&error)); | |
97 | assert_se(sd_bus_error_set_errno(&error, EBUSY) == -EBUSY); | |
763e20e6 | 98 | assert_se(streq(error.name, "System.Error.EBUSY")); |
780896a4 | 99 | assert_se(streq(error.message, strerror(EBUSY))); |
763e20e6 | 100 | assert_se(sd_bus_error_has_name(&error, "System.Error.EBUSY")); |
780896a4 LP |
101 | assert_se(sd_bus_error_get_errno(&error) == EBUSY); |
102 | assert_se(sd_bus_error_is_set(&error)); | |
103 | sd_bus_error_free(&error); | |
104 | ||
105 | assert_se(!sd_bus_error_is_set(&error)); | |
106 | assert_se(sd_bus_error_set_errnof(&error, EIO, "Waldi %c", 'X') == -EIO); | |
107 | assert_se(streq(error.name, SD_BUS_ERROR_IO_ERROR)); | |
108 | assert_se(streq(error.message, "Waldi X")); | |
109 | assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR)); | |
110 | assert_se(sd_bus_error_get_errno(&error) == EIO); | |
111 | assert_se(sd_bus_error_is_set(&error)); | |
4a0a7417 ZJS |
112 | } |
113 | ||
5e071f20 ZJS |
114 | extern const sd_bus_name_error_mapping __start_sd_bus_errnomap[]; |
115 | extern const sd_bus_name_error_mapping __stop_sd_bus_errnomap[]; | |
116 | ||
117 | static void dump_mapping_table(void) { | |
118 | const sd_bus_name_error_mapping *m; | |
119 | ||
120 | printf("----- errno mappings ------\n"); | |
121 | for (m = __start_sd_bus_errnomap; m < __stop_sd_bus_errnomap; m++) | |
122 | printf("%s -> %d\n", m->name, m->code); | |
123 | printf("---------------------------\n"); | |
124 | } | |
125 | ||
4a0a7417 ZJS |
126 | static void test_errno_mapping_standard(void) { |
127 | assert_se(sd_bus_error_set(NULL, "System.Error.EUCLEAN", NULL) == -EUCLEAN); | |
128 | assert_se(sd_bus_error_set(NULL, "System.Error.EBUSY", NULL) == -EBUSY); | |
129 | assert_se(sd_bus_error_set(NULL, "System.Error.EINVAL", NULL) == -EINVAL); | |
130 | assert_se(sd_bus_error_set(NULL, "System.Error.WHATSIT", NULL) == -EIO); | |
131 | } | |
132 | ||
fedfcdee | 133 | SD_BUS_ERROR_MAPPING(test) = { |
4a0a7417 ZJS |
134 | {"org.freedesktop.custom-dbus-error", 5}, |
135 | {"org.freedesktop.custom-dbus-error-2", 52}, | |
136 | }; | |
137 | ||
138 | static void test_errno_mapping_custom(void) { | |
139 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error", NULL) == -5); | |
140 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52); | |
141 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-x", NULL) == -EIO); | |
142 | } | |
143 | ||
144 | int main(int argc, char *argv[]) { | |
5e071f20 | 145 | dump_mapping_table(); |
4a0a7417 ZJS |
146 | |
147 | test_error(); | |
148 | test_errno_mapping_standard(); | |
149 | test_errno_mapping_custom(); | |
780896a4 LP |
150 | |
151 | return 0; | |
152 | } |