os: if inet_ntop() is available, use it for IPv4 addresses as well
[xserver.git] / test / signal-logging.c
blobdb8efa852305e1ec6e4db4d52c5ec129bf6fc2e7
1 /**
2 * Copyright © 2012 Canonical, Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /* Test relies on assert() */
25 #undef NDEBUG
27 #include <dix-config.h>
29 #include <stdint.h>
30 #include <unistd.h>
32 #include "os/fmt.h"
34 #include "assert.h"
35 #include "misc.h"
37 #include "tests-common.h"
39 struct number_format_test {
40 uint64_t number;
41 char string[21];
42 char hex_string[17];
45 struct signed_number_format_test {
46 int64_t number;
47 char string[21];
50 struct float_number_format_test {
51 double number;
52 char string[21];
55 static Bool
56 check_signed_number_format_test(long int number)
58 char string[21];
59 char expected[21];
61 sprintf(expected, "%ld", number);
62 FormatInt64(number, string);
63 if(strncmp(string, expected, 21) != 0) {
64 fprintf(stderr, "Failed to convert %jd to decimal string (expected %s but got %s)\n",
65 (intmax_t) number, expected, string);
66 return FALSE;
69 return TRUE;
72 static Bool
73 check_float_format_test(double number)
75 char string[21];
76 char expected[21];
78 /* we currently always print float as .2f */
79 sprintf(expected, "%.2f", number);
81 FormatDouble(number, string);
82 if(strncmp(string, expected, 21) != 0) {
83 fprintf(stderr, "Failed to convert %f to string (%s vs %s)\n",
84 number, expected, string);
85 return FALSE;
88 return TRUE;
91 static Bool
92 check_number_format_test(long unsigned int number)
94 char string[21];
95 char expected[21];
97 sprintf(expected, "%lu", number);
99 FormatUInt64(number, string);
100 if(strncmp(string, expected, 21) != 0) {
101 fprintf(stderr, "Failed to convert %ju to decimal string (%s vs %s)\n",
102 (intmax_t) number, expected, string);
103 return FALSE;
106 sprintf(expected, "%lx", number);
107 FormatUInt64Hex(number, string);
108 if(strncmp(string, expected, 17) != 0) {
109 fprintf(stderr, "Failed to convert %ju to hexadecimal string (%s vs %s)\n",
110 (intmax_t) number, expected, string);
111 return FALSE;
114 return TRUE;
117 /* FIXME: max range stuff */
118 double float_tests[] = { 0, 5, 0.1, 0.01, 5.2342, 10.2301,
119 -1, -2.00, -0.6023, -1203.30
122 #pragma GCC diagnostic push
123 #pragma GCC diagnostic ignored "-Woverflow"
125 static void
126 number_formatting(void)
128 int i;
129 long unsigned int unsigned_tests[] = { 0,/* Zero */
130 5, /* Single digit number */
131 12, /* Two digit decimal number */
132 37, /* Two digit hex number */
133 0xC90B2, /* Large < 32 bit number */
134 0x15D027BF211B37A, /* Large > 32 bit number */
135 0xFFFFFFFFFFFFFFFF, /* Maximum 64-bit number */
138 long int signed_tests[] = { 0,/* Zero */
139 5, /* Single digit number */
140 12, /* Two digit decimal number */
141 37, /* Two digit hex number */
142 0xC90B2, /* Large < 32 bit number */
143 0x15D027BF211B37A, /* Large > 32 bit number */
144 0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
145 -1, /* Single digit number */
146 -12, /* Two digit decimal number */
147 -0xC90B2, /* Large < 32 bit number */
148 -0x15D027BF211B37A, /* Large > 32 bit number */
149 -0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
152 LogSetParameter(XLOG_VERBOSITY, -1);
154 for (i = 0; i < ARRAY_SIZE(unsigned_tests); i++)
155 assert(check_number_format_test(unsigned_tests[i]));
157 for (i = 0; i < ARRAY_SIZE(signed_tests); i++)
158 assert(check_signed_number_format_test(signed_tests[i]));
160 for (i = 0; i < ARRAY_SIZE(float_tests); i++)
161 assert(check_float_format_test(float_tests[i]));
163 #pragma GCC diagnostic pop
165 #pragma GCC diagnostic push
166 #pragma GCC diagnostic ignored "-Wformat-security"
167 #pragma GCC diagnostic ignored "-Wformat"
168 #pragma GCC diagnostic ignored "-Wformat-extra-args"
169 static void logging_format(void)
171 const char *log_file_path = "/tmp/Xorg-logging-test.log";
172 const char *str = "%s %d %u %% %p %i";
173 char buf[1024];
174 int i;
175 unsigned int ui;
176 long li;
177 unsigned long lui;
178 FILE *f;
179 char read_buf[2048];
180 char *logmsg;
181 uintptr_t ptr;
182 char *fname = NULL;
184 LogSetParameter(XLOG_VERBOSITY, -1);
186 /* set up buf to contain ".....end" */
187 memset(buf, '.', sizeof(buf));
188 strcpy(&buf[sizeof(buf) - 4], "end");
190 fname = (char*)LogInit(log_file_path, NULL);
191 assert(fname != NULL);
192 assert((f = fopen(log_file_path, "r")));
193 free(fname);
195 #define read_log_msg(msg) do { \
196 msg = fgets(read_buf, sizeof(read_buf), f); \
197 assert(msg != NULL); \
198 msg = strchr(read_buf, ']'); \
199 assert(msg != NULL); \
200 assert(strlen(msg) > 2); \
201 msg = msg + 2; /* advance past [time.stamp] */ \
202 } while (0)
204 /* boring test message */
205 LogMessageVerbSigSafe(X_ERROR, 1, "test message\n");
206 read_log_msg(logmsg);
207 assert(strcmp(logmsg, "(EE) test message\n") == 0);
209 /* long buf is truncated to "....en\n" */
210 LogMessageVerbSigSafe(X_ERROR, 1, buf);
211 read_log_msg(logmsg);
212 assert(strcmp(&logmsg[strlen(logmsg) - 3], "en\n") == 0);
214 /* same thing, this time as string substitution */
215 LogMessageVerbSigSafe(X_ERROR, 1, "%s", buf);
216 read_log_msg(logmsg);
217 assert(strcmp(&logmsg[strlen(logmsg) - 3], "en\n") == 0);
219 /* strings containing placeholders should just work */
220 LogMessageVerbSigSafe(X_ERROR, 1, "%s\n", str);
221 read_log_msg(logmsg);
222 assert(strcmp(logmsg, "(EE) %s %d %u %% %p %i\n") == 0);
224 /* literal % */
225 LogMessageVerbSigSafe(X_ERROR, 1, "test %%\n");
226 read_log_msg(logmsg);
227 assert(strcmp(logmsg, "(EE) test %\n") == 0);
229 /* character */
230 LogMessageVerbSigSafe(X_ERROR, 1, "test %c\n", 'a');
231 read_log_msg(logmsg);
232 assert(strcmp(logmsg, "(EE) test a\n") == 0);
234 /* something unsupported % */
235 LogMessageVerbSigSafe(X_ERROR, 1, "test %Q\n");
236 read_log_msg(logmsg);
237 assert(strstr(logmsg, "BUG") != NULL);
238 LogMessageVerbSigSafe(X_ERROR, 1, "\n");
239 fseek(f, 0, SEEK_END);
241 /* string substitution */
242 LogMessageVerbSigSafe(X_ERROR, 1, "%s\n", "substituted string");
243 read_log_msg(logmsg);
244 assert(strcmp(logmsg, "(EE) substituted string\n") == 0);
246 /* Invalid format */
247 LogMessageVerbSigSafe(X_ERROR, 1, "%4", 4);
248 read_log_msg(logmsg);
249 assert(strcmp(logmsg, "(EE) ") == 0);
250 LogMessageVerbSigSafe(X_ERROR, 1, "\n");
251 fseek(f, 0, SEEK_END);
253 /* %hld is bogus */
254 LogMessageVerbSigSafe(X_ERROR, 1, "%hld\n", 4);
255 read_log_msg(logmsg);
256 assert(strstr(logmsg, "BUG") != NULL);
257 LogMessageVerbSigSafe(X_ERROR, 1, "\n");
258 fseek(f, 0, SEEK_END);
260 /* number substitution */
261 ui = 0;
262 do {
263 char expected[30];
264 sprintf(expected, "(EE) %u\n", ui);
265 LogMessageVerbSigSafe(X_ERROR, 1, "%u\n", ui);
266 read_log_msg(logmsg);
267 assert(strcmp(logmsg, expected) == 0);
269 sprintf(expected, "(EE) %x\n", ui);
270 LogMessageVerbSigSafe(X_ERROR, 1, "%x\n", ui);
271 read_log_msg(logmsg);
272 assert(strcmp(logmsg, expected) == 0);
274 if (ui == 0)
275 ui = 1;
276 else
277 ui <<= 1;
278 } while(ui);
280 lui = 0;
281 do {
282 char expected[30];
283 sprintf(expected, "(EE) %lu\n", lui);
284 LogMessageVerbSigSafe(X_ERROR, 1, "%lu\n", lui);
285 read_log_msg(logmsg);
287 sprintf(expected, "(EE) %lld\n", (unsigned long long)ui);
288 LogMessageVerbSigSafe(X_ERROR, 1, "%lld\n", (unsigned long long)ui);
289 read_log_msg(logmsg);
290 assert(strcmp(logmsg, expected) == 0);
292 sprintf(expected, "(EE) %lx\n", lui);
293 LogMessageVerbSigSafe(X_ERROR, 1, "%lx\n", lui);
294 read_log_msg(logmsg);
295 assert(strcmp(logmsg, expected) == 0);
297 sprintf(expected, "(EE) %llx\n", (unsigned long long)ui);
298 LogMessageVerbSigSafe(X_ERROR, 1, "%llx\n", (unsigned long long)ui);
299 read_log_msg(logmsg);
300 assert(strcmp(logmsg, expected) == 0);
302 if (lui == 0)
303 lui = 1;
304 else
305 lui <<= 1;
306 } while(lui);
308 /* signed number substitution */
309 i = 0;
310 do {
311 char expected[30];
312 sprintf(expected, "(EE) %d\n", i);
313 LogMessageVerbSigSafe(X_ERROR, 1, "%d\n", i);
314 read_log_msg(logmsg);
315 assert(strcmp(logmsg, expected) == 0);
317 sprintf(expected, "(EE) %d\n", i | INT_MIN);
318 LogMessageVerbSigSafe(X_ERROR, 1, "%d\n", i | INT_MIN);
319 read_log_msg(logmsg);
320 assert(strcmp(logmsg, expected) == 0);
322 if (i == 0)
323 i = 1;
324 else
325 i <<= 1;
326 } while(i > INT_MIN);
328 li = 0;
329 do {
330 char expected[30];
331 sprintf(expected, "(EE) %ld\n", li);
332 LogMessageVerbSigSafe(X_ERROR, 1, "%ld\n", li);
333 read_log_msg(logmsg);
334 assert(strcmp(logmsg, expected) == 0);
336 sprintf(expected, "(EE) %ld\n", li | LONG_MIN);
337 LogMessageVerbSigSafe(X_ERROR, 1, "%ld\n", li | LONG_MIN);
338 read_log_msg(logmsg);
339 assert(strcmp(logmsg, expected) == 0);
341 sprintf(expected, "(EE) %lld\n", (long long)li);
342 LogMessageVerbSigSafe(X_ERROR, 1, "%lld\n", (long long)li);
343 read_log_msg(logmsg);
344 assert(strcmp(logmsg, expected) == 0);
346 sprintf(expected, "(EE) %lld\n", (long long)(li | LONG_MIN));
347 LogMessageVerbSigSafe(X_ERROR, 1, "%lld\n", (long long)(li | LONG_MIN));
348 read_log_msg(logmsg);
349 assert(strcmp(logmsg, expected) == 0);
351 if (li == 0)
352 li = 1;
353 else
354 li <<= 1;
355 } while(li > LONG_MIN);
358 /* pointer substitution */
359 /* we print a null-pointer differently to printf */
360 LogMessageVerbSigSafe(X_ERROR, 1, "%p\n", NULL);
361 read_log_msg(logmsg);
362 assert(strcmp(logmsg, "(EE) 0x0\n") == 0);
364 ptr = 1;
365 do {
366 char expected[30];
367 #ifdef __sun /* Solaris doesn't autoadd "0x" to %p format */
368 sprintf(expected, "(EE) 0x%p\n", (void*)ptr);
369 #else
370 sprintf(expected, "(EE) %p\n", (void*)ptr);
371 #endif
372 LogMessageVerbSigSafe(X_ERROR, 1, "%p\n", (void*)ptr);
373 read_log_msg(logmsg);
374 assert(strcmp(logmsg, expected) == 0);
375 ptr <<= 1;
376 } while(ptr);
379 for (i = 0; i < ARRAY_SIZE(float_tests); i++) {
380 double d = float_tests[i];
381 char expected[30];
382 sprintf(expected, "(EE) %.2f\n", d);
383 LogMessageVerbSigSafe(X_ERROR, 1, "%f\n", d);
384 read_log_msg(logmsg);
385 assert(strcmp(logmsg, expected) == 0);
387 /* test for length modifiers, we just ignore them atm */
388 LogMessageVerbSigSafe(X_ERROR, 1, "%.3f\n", d);
389 read_log_msg(logmsg);
390 assert(strcmp(logmsg, expected) == 0);
392 LogMessageVerbSigSafe(X_ERROR, 1, "%3f\n", d);
393 read_log_msg(logmsg);
394 assert(strcmp(logmsg, expected) == 0);
396 LogMessageVerbSigSafe(X_ERROR, 1, "%.0f\n", d);
397 read_log_msg(logmsg);
398 assert(strcmp(logmsg, expected) == 0);
402 LogClose(EXIT_NO_ERROR);
403 unlink(log_file_path);
405 #undef read_log_msg
407 #pragma GCC diagnostic pop /* "-Wformat-security" */
409 const testfunc_t*
410 signal_logging_test(void)
412 static const testfunc_t testfuncs[] = {
413 number_formatting,
414 logging_format,
415 NULL,
417 return testfuncs;