]> git.proxmox.com Git - systemd.git/blame - src/libsystemd/sd-bus/test-bus-error.c
Merge tag 'upstream/229'
[systemd.git] / src / libsystemd / sd-bus / test-bus-error.c
CommitLineData
60f067b4
JS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include "sd-bus.h"
db2df898
MP
21
22#include "bus-common-errors.h"
60f067b4
JS
23#include "bus-error.h"
24#include "bus-util.h"
f47781d8 25#include "errno-list.h"
60f067b4 26
f47781d8 27static void test_error(void) {
4c89c718 28 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL, second = SD_BUS_ERROR_NULL;
60f067b4
JS
29 const sd_bus_error const_error = SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "const error");
30 const sd_bus_error temporarily_const_error = {
31 .name = SD_BUS_ERROR_ACCESS_DENIED,
32 .message = "oh! no",
33 ._need_free = -1
34 };
35
36 assert_se(!sd_bus_error_is_set(&error));
e3bff60a 37 assert_se(sd_bus_error_set(&error, SD_BUS_ERROR_NOT_SUPPORTED, "xxx") == -EOPNOTSUPP);
60f067b4
JS
38 assert_se(streq(error.name, SD_BUS_ERROR_NOT_SUPPORTED));
39 assert_se(streq(error.message, "xxx"));
40 assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_NOT_SUPPORTED));
e3bff60a 41 assert_se(sd_bus_error_get_errno(&error) == EOPNOTSUPP);
60f067b4
JS
42 assert_se(sd_bus_error_is_set(&error));
43 sd_bus_error_free(&error);
44
4c89c718
MP
45 /* Check with no error */
46 assert_se(!sd_bus_error_is_set(&error));
47 assert_se(sd_bus_error_setf(&error, NULL, "yyy %i", -1) == 0);
48 assert_se(error.name == NULL);
49 assert_se(error.message == NULL);
50 assert_se(!sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
51 assert_se(sd_bus_error_get_errno(&error) == 0);
60f067b4 52 assert_se(!sd_bus_error_is_set(&error));
4c89c718 53
60f067b4
JS
54 assert_se(sd_bus_error_setf(&error, SD_BUS_ERROR_FILE_NOT_FOUND, "yyy %i", -1) == -ENOENT);
55 assert_se(streq(error.name, SD_BUS_ERROR_FILE_NOT_FOUND));
56 assert_se(streq(error.message, "yyy -1"));
57 assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
58 assert_se(sd_bus_error_get_errno(&error) == ENOENT);
59 assert_se(sd_bus_error_is_set(&error));
60
61 assert_se(!sd_bus_error_is_set(&second));
62 assert_se(second._need_free == 0);
63 assert_se(error._need_free > 0);
64 assert_se(sd_bus_error_copy(&second, &error) == -ENOENT);
65 assert_se(second._need_free > 0);
66 assert_se(streq(error.name, second.name));
67 assert_se(streq(error.message, second.message));
68 assert_se(sd_bus_error_get_errno(&second) == ENOENT);
69 assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_NOT_FOUND));
70 assert_se(sd_bus_error_is_set(&second));
71
72 sd_bus_error_free(&error);
73 sd_bus_error_free(&second);
74
75 assert_se(!sd_bus_error_is_set(&second));
76 assert_se(const_error._need_free == 0);
77 assert_se(sd_bus_error_copy(&second, &const_error) == -EEXIST);
78 assert_se(second._need_free == 0);
79 assert_se(streq(const_error.name, second.name));
80 assert_se(streq(const_error.message, second.message));
81 assert_se(sd_bus_error_get_errno(&second) == EEXIST);
82 assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_EXISTS));
83 assert_se(sd_bus_error_is_set(&second));
84 sd_bus_error_free(&second);
85
86 assert_se(!sd_bus_error_is_set(&second));
87 assert_se(temporarily_const_error._need_free < 0);
88 assert_se(sd_bus_error_copy(&second, &temporarily_const_error) == -EACCES);
89 assert_se(second._need_free > 0);
90 assert_se(streq(temporarily_const_error.name, second.name));
91 assert_se(streq(temporarily_const_error.message, second.message));
92 assert_se(sd_bus_error_get_errno(&second) == EACCES);
93 assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_ACCESS_DENIED));
94 assert_se(sd_bus_error_is_set(&second));
95
96 assert_se(!sd_bus_error_is_set(&error));
97 assert_se(sd_bus_error_set_const(&error, "System.Error.EUCLEAN", "Hallo") == -EUCLEAN);
98 assert_se(streq(error.name, "System.Error.EUCLEAN"));
99 assert_se(streq(error.message, "Hallo"));
100 assert_se(sd_bus_error_has_name(&error, "System.Error.EUCLEAN"));
101 assert_se(sd_bus_error_get_errno(&error) == EUCLEAN);
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_errno(&error, EBUSY) == -EBUSY);
107 assert_se(streq(error.name, "System.Error.EBUSY"));
108 assert_se(streq(error.message, strerror(EBUSY)));
109 assert_se(sd_bus_error_has_name(&error, "System.Error.EBUSY"));
110 assert_se(sd_bus_error_get_errno(&error) == EBUSY);
111 assert_se(sd_bus_error_is_set(&error));
112 sd_bus_error_free(&error);
113
114 assert_se(!sd_bus_error_is_set(&error));
115 assert_se(sd_bus_error_set_errnof(&error, EIO, "Waldi %c", 'X') == -EIO);
116 assert_se(streq(error.name, SD_BUS_ERROR_IO_ERROR));
117 assert_se(streq(error.message, "Waldi X"));
118 assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR));
119 assert_se(sd_bus_error_get_errno(&error) == EIO);
120 assert_se(sd_bus_error_is_set(&error));
4c89c718
MP
121 sd_bus_error_free(&error);
122
123 /* Check with no error */
124 assert_se(!sd_bus_error_is_set(&error));
125 assert_se(sd_bus_error_set_errnof(&error, 0, "Waldi %c", 'X') == 0);
126 assert_se(error.name == NULL);
127 assert_se(error.message == NULL);
128 assert_se(!sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR));
129 assert_se(sd_bus_error_get_errno(&error) == 0);
130 assert_se(!sd_bus_error_is_set(&error));
f47781d8
MP
131}
132
133extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
134extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
135
136static void dump_mapping_table(void) {
137 const sd_bus_error_map *m;
138
139 printf("----- errno mappings ------\n");
140 m = __start_BUS_ERROR_MAP;
141 while (m < __stop_BUS_ERROR_MAP) {
142
143 if (m->code == BUS_ERROR_MAP_END_MARKER) {
144 m = ALIGN8_PTR(m+1);
145 continue;
146 }
147
148 printf("%s -> %i/%s\n", strna(m->name), m->code, strna(errno_to_name(m->code)));
149 m ++;
150 }
151 printf("---------------------------\n");
152}
153
154static void test_errno_mapping_standard(void) {
155 assert_se(sd_bus_error_set(NULL, "System.Error.EUCLEAN", NULL) == -EUCLEAN);
156 assert_se(sd_bus_error_set(NULL, "System.Error.EBUSY", NULL) == -EBUSY);
157 assert_se(sd_bus_error_set(NULL, "System.Error.EINVAL", NULL) == -EINVAL);
158 assert_se(sd_bus_error_set(NULL, "System.Error.WHATSIT", NULL) == -EIO);
159}
160
161BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors[] = {
162 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error", 5),
163 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-2", 52),
164 SD_BUS_ERROR_MAP_END
165};
166
167BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors2[] = {
168 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-3", 33),
169 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-4", 44),
170 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-33", 333),
171 SD_BUS_ERROR_MAP_END
172};
173
174static const sd_bus_error_map test_errors3[] = {
175 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-88", 888),
176 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-99", 999),
177 SD_BUS_ERROR_MAP_END
178};
179
180static const sd_bus_error_map test_errors4[] = {
181 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-77", 777),
182 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-78", 778),
183 SD_BUS_ERROR_MAP_END
184};
185
4c89c718
MP
186static const sd_bus_error_map test_errors_bad1[] = {
187 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-1", 0),
188 SD_BUS_ERROR_MAP_END
189};
190
191static const sd_bus_error_map test_errors_bad2[] = {
192 SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-1", -1),
193 SD_BUS_ERROR_MAP_END
194};
195
f47781d8
MP
196static void test_errno_mapping_custom(void) {
197 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error", NULL) == -5);
198 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52);
199 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-x", NULL) == -EIO);
200 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-33", NULL) == -333);
201
202 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -EIO);
203 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -EIO);
204 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -EIO);
205
206 assert_se(sd_bus_error_add_map(test_errors3) > 0);
207 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -888);
208 assert_se(sd_bus_error_add_map(test_errors4) > 0);
209 assert_se(sd_bus_error_add_map(test_errors4) == 0);
210 assert_se(sd_bus_error_add_map(test_errors3) == 0);
211
212 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -999);
213 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -777);
214 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-78", NULL) == -778);
215 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52);
216 assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-y", NULL) == -EIO);
217
218 assert_se(sd_bus_error_set(NULL, BUS_ERROR_NO_SUCH_UNIT, NULL) == -ENOENT);
4c89c718
MP
219
220 assert_se(sd_bus_error_add_map(test_errors_bad1) == -EINVAL);
221 assert_se(sd_bus_error_add_map(test_errors_bad2) == -EINVAL);
f47781d8
MP
222}
223
224int main(int argc, char *argv[]) {
225 dump_mapping_table();
226
227 test_error();
228 test_errno_mapping_standard();
229 test_errno_mapping_custom();
60f067b4
JS
230
231 return 0;
232}