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 #ifdef HAVE_DIX_CONFIG_H
25 #include <dix-config.h>
33 struct number_format_test
{
39 struct signed_number_format_test
{
44 struct float_number_format_test
{
50 check_signed_number_format_test(long int number
)
55 sprintf(expected
, "%ld", number
);
56 FormatInt64(number
, string
);
57 if(strncmp(string
, expected
, 21) != 0) {
58 fprintf(stderr
, "Failed to convert %jd to decimal string (expected %s but got %s)\n",
59 number
, expected
, string
);
67 check_float_format_test(double number
)
72 /* we currently always print float as .2f */
73 sprintf(expected
, "%.2f", number
);
75 FormatDouble(number
, string
);
76 if(strncmp(string
, expected
, 21) != 0) {
77 fprintf(stderr
, "Failed to convert %f to string (%s vs %s)\n",
78 number
, expected
, string
);
86 check_number_format_test(long unsigned int number
)
91 sprintf(expected
, "%lu", number
);
93 FormatUInt64(number
, string
);
94 if(strncmp(string
, expected
, 21) != 0) {
95 fprintf(stderr
, "Failed to convert %ju to decimal string (%s vs %s)\n",
96 number
, expected
, string
);
100 sprintf(expected
, "%lx", number
);
101 FormatUInt64Hex(number
, string
);
102 if(strncmp(string
, expected
, 17) != 0) {
103 fprintf(stderr
, "Failed to convert %ju to hexadecimal string (%s vs %s)\n",
104 number
, expected
, string
);
111 /* FIXME: max range stuff */
112 double float_tests
[] = { 0, 5, 0.1, 0.01, 5.2342, 10.2301,
113 -1, -2.00, -0.6023, -1203.30
117 number_formatting(void)
120 long unsigned int unsigned_tests
[] = { 0,/* Zero */
121 5, /* Single digit number */
122 12, /* Two digit decimal number */
123 37, /* Two digit hex number */
124 0xC90B2, /* Large < 32 bit number */
125 0x15D027BF211B37A, /* Large > 32 bit number */
126 0xFFFFFFFFFFFFFFFF, /* Maximum 64-bit number */
129 long int signed_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 0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
136 -1, /* Single digit number */
137 -12, /* Two digit decimal number */
138 -0xC90B2, /* Large < 32 bit number */
139 -0x15D027BF211B37A, /* Large > 32 bit number */
140 -0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
143 for (i
= 0; i
< sizeof(unsigned_tests
) / sizeof(unsigned_tests
[0]); i
++)
144 assert(check_number_format_test(unsigned_tests
[i
]));
146 for (i
= 0; i
< sizeof(unsigned_tests
) / sizeof(signed_tests
[0]); i
++)
147 assert(check_signed_number_format_test(signed_tests
[i
]));
149 for (i
= 0; i
< sizeof(float_tests
) / sizeof(float_tests
[0]); i
++)
150 assert(check_float_format_test(float_tests
[i
]));
153 #pragma GCC diagnostic push
154 #pragma GCC diagnostic ignored "-Wformat-security"
155 static void logging_format(void)
157 const char *log_file_path
= "/tmp/Xorg-logging-test.log";
158 const char *str
= "%s %d %u %% %p %i";
169 /* set up buf to contain ".....end" */
170 memset(buf
, '.', sizeof(buf
));
171 strcpy(&buf
[sizeof(buf
) - 4], "end");
173 LogInit(log_file_path
, NULL
);
174 assert(f
= fopen(log_file_path
, "r"));
176 #define read_log_msg(msg) \
177 fgets(read_buf, sizeof(read_buf), f); \
178 msg = strchr(read_buf, ']') + 2; /* advance past [time.stamp] */
180 /* boring test message */
181 LogMessageVerbSigSafe(X_ERROR
, -1, "test message\n");
182 read_log_msg(logmsg
);
183 assert(strcmp(logmsg
, "(EE) test message\n") == 0);
185 /* long buf is truncated to "....en\n" */
186 LogMessageVerbSigSafe(X_ERROR
, -1, buf
);
187 read_log_msg(logmsg
);
188 assert(strcmp(&logmsg
[strlen(logmsg
) - 3], "en\n") == 0);
190 /* same thing, this time as string substitution */
191 LogMessageVerbSigSafe(X_ERROR
, -1, "%s", buf
);
192 read_log_msg(logmsg
);
193 assert(strcmp(&logmsg
[strlen(logmsg
) - 3], "en\n") == 0);
195 /* strings containing placeholders should just work */
196 LogMessageVerbSigSafe(X_ERROR
, -1, "%s\n", str
);
197 read_log_msg(logmsg
);
198 assert(strcmp(logmsg
, "(EE) %s %d %u %% %p %i\n") == 0);
201 LogMessageVerbSigSafe(X_ERROR
, -1, "test %%\n");
202 read_log_msg(logmsg
);
203 assert(strcmp(logmsg
, "(EE) test %\n") == 0);
206 LogMessageVerbSigSafe(X_ERROR
, -1, "test %c\n", 'a');
207 read_log_msg(logmsg
);
208 assert(strcmp(logmsg
, "(EE) test a\n") == 0);
210 /* something unsupported % */
211 LogMessageVerbSigSafe(X_ERROR
, -1, "test %Q\n");
212 read_log_msg(logmsg
);
213 assert(strstr(logmsg
, "BUG") != NULL
);
214 LogMessageVerbSigSafe(X_ERROR
, -1, "\n");
215 fseek(f
, 0, SEEK_END
);
217 /* string substitution */
218 LogMessageVerbSigSafe(X_ERROR
, -1, "%s\n", "substituted string");
219 read_log_msg(logmsg
);
220 assert(strcmp(logmsg
, "(EE) substituted string\n") == 0);
223 #warning Ignore compiler warning below "lacks type at end of format". This is intentional.
224 LogMessageVerbSigSafe(X_ERROR
, -1, "%4", 4);
225 read_log_msg(logmsg
);
226 assert(strcmp(logmsg
, "(EE) ") == 0);
227 LogMessageVerbSigSafe(X_ERROR
, -1, "\n");
228 fseek(f
, 0, SEEK_END
);
230 #warning Ignore compiler warning below "unknown conversion type character". This is intentional.
232 LogMessageVerbSigSafe(X_ERROR
, -1, "%hld\n", 4);
233 read_log_msg(logmsg
);
234 assert(strstr(logmsg
, "BUG") != NULL
);
235 LogMessageVerbSigSafe(X_ERROR
, -1, "\n");
236 fseek(f
, 0, SEEK_END
);
238 /* number substitution */
242 sprintf(expected
, "(EE) %u\n", ui
);
243 LogMessageVerbSigSafe(X_ERROR
, -1, "%u\n", ui
);
244 read_log_msg(logmsg
);
245 assert(strcmp(logmsg
, expected
) == 0);
247 sprintf(expected
, "(EE) %x\n", ui
);
248 LogMessageVerbSigSafe(X_ERROR
, -1, "%x\n", ui
);
249 read_log_msg(logmsg
);
250 assert(strcmp(logmsg
, expected
) == 0);
261 sprintf(expected
, "(EE) %lu\n", lui
);
262 LogMessageVerbSigSafe(X_ERROR
, -1, "%lu\n", lui
);
263 read_log_msg(logmsg
);
265 sprintf(expected
, "(EE) %lld\n", (unsigned long long)ui
);
266 LogMessageVerbSigSafe(X_ERROR
, -1, "%lld\n", (unsigned long long)ui
);
267 read_log_msg(logmsg
);
268 assert(strcmp(logmsg
, expected
) == 0);
270 sprintf(expected
, "(EE) %lx\n", lui
);
271 printf("%s\n", expected
);
272 LogMessageVerbSigSafe(X_ERROR
, -1, "%lx\n", lui
);
273 read_log_msg(logmsg
);
274 assert(strcmp(logmsg
, expected
) == 0);
276 sprintf(expected
, "(EE) %llx\n", (unsigned long long)ui
);
277 LogMessageVerbSigSafe(X_ERROR
, -1, "%llx\n", (unsigned long long)ui
);
278 read_log_msg(logmsg
);
279 assert(strcmp(logmsg
, expected
) == 0);
287 /* signed number substitution */
291 sprintf(expected
, "(EE) %d\n", i
);
292 LogMessageVerbSigSafe(X_ERROR
, -1, "%d\n", i
);
293 read_log_msg(logmsg
);
294 assert(strcmp(logmsg
, expected
) == 0);
296 sprintf(expected
, "(EE) %d\n", i
| INT_MIN
);
297 LogMessageVerbSigSafe(X_ERROR
, -1, "%d\n", i
| INT_MIN
);
298 read_log_msg(logmsg
);
299 assert(strcmp(logmsg
, expected
) == 0);
305 } while(i
> INT_MIN
);
310 sprintf(expected
, "(EE) %ld\n", li
);
311 LogMessageVerbSigSafe(X_ERROR
, -1, "%ld\n", li
);
312 read_log_msg(logmsg
);
313 assert(strcmp(logmsg
, expected
) == 0);
315 sprintf(expected
, "(EE) %ld\n", li
| LONG_MIN
);
316 LogMessageVerbSigSafe(X_ERROR
, -1, "%ld\n", li
| LONG_MIN
);
317 read_log_msg(logmsg
);
318 assert(strcmp(logmsg
, expected
) == 0);
320 sprintf(expected
, "(EE) %lld\n", (long long)li
);
321 LogMessageVerbSigSafe(X_ERROR
, -1, "%lld\n", (long long)li
);
322 read_log_msg(logmsg
);
323 assert(strcmp(logmsg
, expected
) == 0);
325 sprintf(expected
, "(EE) %lld\n", (long long)(li
| LONG_MIN
));
326 LogMessageVerbSigSafe(X_ERROR
, -1, "%lld\n", (long long)(li
| LONG_MIN
));
327 read_log_msg(logmsg
);
328 assert(strcmp(logmsg
, expected
) == 0);
334 } while(li
> LONG_MIN
);
337 /* pointer substitution */
338 /* we print a null-pointer differently to printf */
339 LogMessageVerbSigSafe(X_ERROR
, -1, "%p\n", NULL
);
340 read_log_msg(logmsg
);
341 assert(strcmp(logmsg
, "(EE) 0x0\n") == 0);
346 sprintf(expected
, "(EE) %p\n", (void*)ptr
);
347 LogMessageVerbSigSafe(X_ERROR
, -1, "%p\n", (void*)ptr
);
348 read_log_msg(logmsg
);
349 assert(strcmp(logmsg
, expected
) == 0);
354 for (i
= 0; i
< sizeof(float_tests
)/sizeof(float_tests
[0]); i
++) {
355 double d
= float_tests
[i
];
357 sprintf(expected
, "(EE) %.2f\n", d
);
358 LogMessageVerbSigSafe(X_ERROR
, -1, "%f\n", d
);
359 read_log_msg(logmsg
);
360 assert(strcmp(logmsg
, expected
) == 0);
362 /* test for length modifiers, we just ignore them atm */
363 LogMessageVerbSigSafe(X_ERROR
, -1, "%.3f\n", d
);
364 read_log_msg(logmsg
);
365 assert(strcmp(logmsg
, expected
) == 0);
367 LogMessageVerbSigSafe(X_ERROR
, -1, "%3f\n", d
);
368 read_log_msg(logmsg
);
369 assert(strcmp(logmsg
, expected
) == 0);
371 LogMessageVerbSigSafe(X_ERROR
, -1, "%.0f\n", d
);
372 read_log_msg(logmsg
);
373 assert(strcmp(logmsg
, expected
) == 0);
377 LogClose(EXIT_NO_ERROR
);
378 unlink(log_file_path
);
382 #pragma GCC diagnostic pop /* "-Wformat-security" */
385 main(int argc
, char **argv
)