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
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() */
27 #include <dix-config.h>
37 #include "tests-common.h"
39 struct number_format_test
{
45 struct signed_number_format_test
{
50 struct float_number_format_test
{
56 check_signed_number_format_test(long int number
)
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
);
73 check_float_format_test(double number
)
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
);
92 check_number_format_test(long unsigned int number
)
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
);
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
);
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"
126 number_formatting(void)
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";
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")));
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] */ \
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);
225 LogMessageVerbSigSafe(X_ERROR
, 1, "test %%\n");
226 read_log_msg(logmsg
);
227 assert(strcmp(logmsg
, "(EE) test %\n") == 0);
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);
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
);
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 */
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);
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);
308 /* signed number substitution */
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);
326 } while(i
> INT_MIN
);
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);
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);
367 #ifdef __sun /* Solaris doesn't autoadd "0x" to %p format */
368 sprintf(expected
, "(EE) 0x%p\n", (void*)ptr
);
370 sprintf(expected
, "(EE) %p\n", (void*)ptr
);
372 LogMessageVerbSigSafe(X_ERROR
, 1, "%p\n", (void*)ptr
);
373 read_log_msg(logmsg
);
374 assert(strcmp(logmsg
, expected
) == 0);
379 for (i
= 0; i
< ARRAY_SIZE(float_tests
); i
++) {
380 double d
= float_tests
[i
];
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
);
407 #pragma GCC diagnostic pop /* "-Wformat-security" */
410 signal_logging_test(void)
412 static const testfunc_t testfuncs
[] = {