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