]>
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" | |
cf0fbc49 TA |
23 | |
24 | #include "bus-common-errors.h" | |
057171ef | 25 | #include "bus-error.h" |
780896a4 | 26 | #include "bus-util.h" |
5f86c1f4 | 27 | #include "errno-list.h" |
780896a4 | 28 | |
4a0a7417 | 29 | static void test_error(void) { |
4afd3348 | 30 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL, second = SD_BUS_ERROR_NULL; |
79f8d3d2 LP |
31 | const sd_bus_error const_error = SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "const error"); |
32 | const sd_bus_error temporarily_const_error = { | |
33 | .name = SD_BUS_ERROR_ACCESS_DENIED, | |
34 | .message = "oh! no", | |
35 | ._need_free = -1 | |
36 | }; | |
780896a4 LP |
37 | |
38 | assert_se(!sd_bus_error_is_set(&error)); | |
15411c0c | 39 | assert_se(sd_bus_error_set(&error, SD_BUS_ERROR_NOT_SUPPORTED, "xxx") == -EOPNOTSUPP); |
780896a4 LP |
40 | assert_se(streq(error.name, SD_BUS_ERROR_NOT_SUPPORTED)); |
41 | assert_se(streq(error.message, "xxx")); | |
42 | assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_NOT_SUPPORTED)); | |
15411c0c | 43 | assert_se(sd_bus_error_get_errno(&error) == EOPNOTSUPP); |
780896a4 LP |
44 | assert_se(sd_bus_error_is_set(&error)); |
45 | sd_bus_error_free(&error); | |
46 | ||
47 | assert_se(!sd_bus_error_is_set(&error)); | |
48 | assert_se(sd_bus_error_setf(&error, SD_BUS_ERROR_FILE_NOT_FOUND, "yyy %i", -1) == -ENOENT); | |
49 | assert_se(streq(error.name, SD_BUS_ERROR_FILE_NOT_FOUND)); | |
50 | assert_se(streq(error.message, "yyy -1")); | |
51 | assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND)); | |
52 | assert_se(sd_bus_error_get_errno(&error) == ENOENT); | |
53 | assert_se(sd_bus_error_is_set(&error)); | |
54 | ||
55 | assert_se(!sd_bus_error_is_set(&second)); | |
79f8d3d2 LP |
56 | assert_se(second._need_free == 0); |
57 | assert_se(error._need_free > 0); | |
780896a4 | 58 | assert_se(sd_bus_error_copy(&second, &error) == -ENOENT); |
79f8d3d2 | 59 | assert_se(second._need_free > 0); |
780896a4 LP |
60 | assert_se(streq(error.name, second.name)); |
61 | assert_se(streq(error.message, second.message)); | |
62 | assert_se(sd_bus_error_get_errno(&second) == ENOENT); | |
63 | assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_NOT_FOUND)); | |
64 | assert_se(sd_bus_error_is_set(&second)); | |
65 | ||
66 | sd_bus_error_free(&error); | |
79f8d3d2 LP |
67 | sd_bus_error_free(&second); |
68 | ||
69 | assert_se(!sd_bus_error_is_set(&second)); | |
70 | assert_se(const_error._need_free == 0); | |
71 | assert_se(sd_bus_error_copy(&second, &const_error) == -EEXIST); | |
72 | assert_se(second._need_free == 0); | |
73 | assert_se(streq(const_error.name, second.name)); | |
74 | assert_se(streq(const_error.message, second.message)); | |
75 | assert_se(sd_bus_error_get_errno(&second) == EEXIST); | |
76 | assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_EXISTS)); | |
77 | assert_se(sd_bus_error_is_set(&second)); | |
78 | sd_bus_error_free(&second); | |
79 | ||
80 | assert_se(!sd_bus_error_is_set(&second)); | |
81 | assert_se(temporarily_const_error._need_free < 0); | |
82 | assert_se(sd_bus_error_copy(&second, &temporarily_const_error) == -EACCES); | |
83 | assert_se(second._need_free > 0); | |
84 | assert_se(streq(temporarily_const_error.name, second.name)); | |
85 | assert_se(streq(temporarily_const_error.message, second.message)); | |
86 | assert_se(sd_bus_error_get_errno(&second) == EACCES); | |
87 | assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_ACCESS_DENIED)); | |
88 | assert_se(sd_bus_error_is_set(&second)); | |
780896a4 LP |
89 | |
90 | assert_se(!sd_bus_error_is_set(&error)); | |
763e20e6 LP |
91 | assert_se(sd_bus_error_set_const(&error, "System.Error.EUCLEAN", "Hallo") == -EUCLEAN); |
92 | assert_se(streq(error.name, "System.Error.EUCLEAN")); | |
780896a4 | 93 | assert_se(streq(error.message, "Hallo")); |
763e20e6 | 94 | assert_se(sd_bus_error_has_name(&error, "System.Error.EUCLEAN")); |
780896a4 LP |
95 | assert_se(sd_bus_error_get_errno(&error) == EUCLEAN); |
96 | assert_se(sd_bus_error_is_set(&error)); | |
97 | sd_bus_error_free(&error); | |
98 | ||
99 | assert_se(!sd_bus_error_is_set(&error)); | |
100 | assert_se(sd_bus_error_set_errno(&error, EBUSY) == -EBUSY); | |
763e20e6 | 101 | assert_se(streq(error.name, "System.Error.EBUSY")); |
780896a4 | 102 | assert_se(streq(error.message, strerror(EBUSY))); |
763e20e6 | 103 | assert_se(sd_bus_error_has_name(&error, "System.Error.EBUSY")); |
780896a4 LP |
104 | assert_se(sd_bus_error_get_errno(&error) == EBUSY); |
105 | assert_se(sd_bus_error_is_set(&error)); | |
106 | sd_bus_error_free(&error); | |
107 | ||
108 | assert_se(!sd_bus_error_is_set(&error)); | |
109 | assert_se(sd_bus_error_set_errnof(&error, EIO, "Waldi %c", 'X') == -EIO); | |
110 | assert_se(streq(error.name, SD_BUS_ERROR_IO_ERROR)); | |
111 | assert_se(streq(error.message, "Waldi X")); | |
112 | assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR)); | |
113 | assert_se(sd_bus_error_get_errno(&error) == EIO); | |
114 | assert_se(sd_bus_error_is_set(&error)); | |
4a0a7417 ZJS |
115 | } |
116 | ||
5f86c1f4 LP |
117 | extern const sd_bus_error_map __start_BUS_ERROR_MAP[]; |
118 | extern const sd_bus_error_map __stop_BUS_ERROR_MAP[]; | |
5e071f20 ZJS |
119 | |
120 | static void dump_mapping_table(void) { | |
5f86c1f4 | 121 | const sd_bus_error_map *m; |
5e071f20 ZJS |
122 | |
123 | printf("----- errno mappings ------\n"); | |
5f86c1f4 LP |
124 | m = __start_BUS_ERROR_MAP; |
125 | while (m < __stop_BUS_ERROR_MAP) { | |
126 | ||
127 | if (m->code == BUS_ERROR_MAP_END_MARKER) { | |
128 | m = ALIGN8_PTR(m+1); | |
129 | continue; | |
130 | } | |
131 | ||
132 | printf("%s -> %i/%s\n", strna(m->name), m->code, strna(errno_to_name(m->code))); | |
133 | m ++; | |
134 | } | |
5e071f20 ZJS |
135 | printf("---------------------------\n"); |
136 | } | |
137 | ||
4a0a7417 ZJS |
138 | static void test_errno_mapping_standard(void) { |
139 | assert_se(sd_bus_error_set(NULL, "System.Error.EUCLEAN", NULL) == -EUCLEAN); | |
140 | assert_se(sd_bus_error_set(NULL, "System.Error.EBUSY", NULL) == -EBUSY); | |
141 | assert_se(sd_bus_error_set(NULL, "System.Error.EINVAL", NULL) == -EINVAL); | |
142 | assert_se(sd_bus_error_set(NULL, "System.Error.WHATSIT", NULL) == -EIO); | |
143 | } | |
144 | ||
5f86c1f4 LP |
145 | BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors[] = { |
146 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error", 5), | |
147 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-2", 52), | |
148 | SD_BUS_ERROR_MAP_END | |
149 | }; | |
150 | ||
151 | BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors2[] = { | |
152 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-3", 33), | |
153 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-4", 44), | |
154 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-33", 333), | |
155 | SD_BUS_ERROR_MAP_END | |
156 | }; | |
157 | ||
158 | static const sd_bus_error_map test_errors3[] = { | |
159 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-88", 888), | |
160 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-99", 999), | |
161 | SD_BUS_ERROR_MAP_END | |
162 | }; | |
163 | ||
164 | static const sd_bus_error_map test_errors4[] = { | |
165 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-77", 777), | |
166 | SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-78", 778), | |
167 | SD_BUS_ERROR_MAP_END | |
4a0a7417 ZJS |
168 | }; |
169 | ||
170 | static void test_errno_mapping_custom(void) { | |
171 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error", NULL) == -5); | |
172 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52); | |
173 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-x", NULL) == -EIO); | |
5f86c1f4 LP |
174 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-33", NULL) == -333); |
175 | ||
176 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -EIO); | |
177 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -EIO); | |
178 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -EIO); | |
179 | ||
180 | assert_se(sd_bus_error_add_map(test_errors3) > 0); | |
181 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -888); | |
182 | assert_se(sd_bus_error_add_map(test_errors4) > 0); | |
183 | assert_se(sd_bus_error_add_map(test_errors4) == 0); | |
184 | assert_se(sd_bus_error_add_map(test_errors3) == 0); | |
185 | ||
186 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -999); | |
187 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -777); | |
188 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-78", NULL) == -778); | |
189 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52); | |
190 | assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-y", NULL) == -EIO); | |
191 | ||
192 | assert_se(sd_bus_error_set(NULL, BUS_ERROR_NO_SUCH_UNIT, NULL) == -ENOENT); | |
4a0a7417 ZJS |
193 | } |
194 | ||
195 | int main(int argc, char *argv[]) { | |
5e071f20 | 196 | dump_mapping_table(); |
4a0a7417 ZJS |
197 | |
198 | test_error(); | |
199 | test_errno_mapping_standard(); | |
200 | test_errno_mapping_custom(); | |
780896a4 LP |
201 | |
202 | return 0; | |
203 | } |