| 1 | /** |
| 2 | * Copyright © 2012 Canonical, Ltd. |
| 3 | * |
| 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: |
| 10 | * |
| 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. |
| 14 | * |
| 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. |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_DIX_CONFIG_H |
| 25 | #include <dix-config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | #include <unistd.h> |
| 30 | #include "assert.h" |
| 31 | #include "misc.h" |
| 32 | |
| 33 | struct number_format_test { |
| 34 | uint64_t number; |
| 35 | char string[21]; |
| 36 | char hex_string[17]; |
| 37 | }; |
| 38 | |
| 39 | struct signed_number_format_test { |
| 40 | int64_t number; |
| 41 | char string[21]; |
| 42 | }; |
| 43 | |
| 44 | struct float_number_format_test { |
| 45 | double number; |
| 46 | char string[21]; |
| 47 | }; |
| 48 | |
| 49 | static Bool |
| 50 | check_signed_number_format_test(long int number) |
| 51 | { |
| 52 | char string[21]; |
| 53 | char expected[21]; |
| 54 | |
| 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); |
| 60 | return FALSE; |
| 61 | } |
| 62 | |
| 63 | return TRUE; |
| 64 | } |
| 65 | |
| 66 | static Bool |
| 67 | check_float_format_test(double number) |
| 68 | { |
| 69 | char string[21]; |
| 70 | char expected[21]; |
| 71 | |
| 72 | /* we currently always print float as .2f */ |
| 73 | sprintf(expected, "%.2f", number); |
| 74 | |
| 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); |
| 79 | return FALSE; |
| 80 | } |
| 81 | |
| 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | static Bool |
| 86 | check_number_format_test(long unsigned int number) |
| 87 | { |
| 88 | char string[21]; |
| 89 | char expected[21]; |
| 90 | |
| 91 | sprintf(expected, "%lu", number); |
| 92 | |
| 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); |
| 97 | return FALSE; |
| 98 | } |
| 99 | |
| 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); |
| 105 | return FALSE; |
| 106 | } |
| 107 | |
| 108 | return TRUE; |
| 109 | } |
| 110 | |
| 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 |
| 114 | }; |
| 115 | |
| 116 | static void |
| 117 | number_formatting(void) |
| 118 | { |
| 119 | int i; |
| 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 */ |
| 127 | }; |
| 128 | |
| 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 */ |
| 141 | } ; |
| 142 | |
| 143 | for (i = 0; i < sizeof(unsigned_tests) / sizeof(unsigned_tests[0]); i++) |
| 144 | assert(check_number_format_test(unsigned_tests[i])); |
| 145 | |
| 146 | for (i = 0; i < sizeof(unsigned_tests) / sizeof(signed_tests[0]); i++) |
| 147 | assert(check_signed_number_format_test(signed_tests[i])); |
| 148 | |
| 149 | for (i = 0; i < sizeof(float_tests) / sizeof(float_tests[0]); i++) |
| 150 | assert(check_float_format_test(float_tests[i])); |
| 151 | } |
| 152 | |
| 153 | #pragma GCC diagnostic push |
| 154 | #pragma GCC diagnostic ignored "-Wformat-security" |
| 155 | static void logging_format(void) |
| 156 | { |
| 157 | const char *log_file_path = "/tmp/Xorg-logging-test.log"; |
| 158 | const char *str = "%s %d %u %% %p %i"; |
| 159 | char buf[1024]; |
| 160 | int i; |
| 161 | unsigned int ui; |
| 162 | long li; |
| 163 | unsigned long lui; |
| 164 | FILE *f; |
| 165 | char read_buf[2048]; |
| 166 | char *logmsg; |
| 167 | uintptr_t ptr; |
| 168 | |
| 169 | /* set up buf to contain ".....end" */ |
| 170 | memset(buf, '.', sizeof(buf)); |
| 171 | strcpy(&buf[sizeof(buf) - 4], "end"); |
| 172 | |
| 173 | LogInit(log_file_path, NULL); |
| 174 | assert(f = fopen(log_file_path, "r")); |
| 175 | |
| 176 | #define read_log_msg(msg) \ |
| 177 | fgets(read_buf, sizeof(read_buf), f); \ |
| 178 | msg = strchr(read_buf, ']') + 2; /* advance past [time.stamp] */ |
| 179 | |
| 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); |
| 184 | |
| 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); |
| 189 | |
| 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); |
| 194 | |
| 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); |
| 199 | |
| 200 | /* literal % */ |
| 201 | LogMessageVerbSigSafe(X_ERROR, -1, "test %%\n"); |
| 202 | read_log_msg(logmsg); |
| 203 | assert(strcmp(logmsg, "(EE) test %\n") == 0); |
| 204 | |
| 205 | /* character */ |
| 206 | LogMessageVerbSigSafe(X_ERROR, -1, "test %c\n", 'a'); |
| 207 | read_log_msg(logmsg); |
| 208 | assert(strcmp(logmsg, "(EE) test a\n") == 0); |
| 209 | |
| 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); |
| 216 | |
| 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); |
| 221 | |
| 222 | /* Invalid format */ |
| 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); |
| 229 | |
| 230 | #warning Ignore compiler warning below "unknown conversion type character". This is intentional. |
| 231 | /* %hld is bogus */ |
| 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); |
| 237 | |
| 238 | /* number substitution */ |
| 239 | ui = 0; |
| 240 | do { |
| 241 | char expected[30]; |
| 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); |
| 246 | |
| 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); |
| 251 | |
| 252 | if (ui == 0) |
| 253 | ui = 1; |
| 254 | else |
| 255 | ui <<= 1; |
| 256 | } while(ui); |
| 257 | |
| 258 | lui = 0; |
| 259 | do { |
| 260 | char expected[30]; |
| 261 | sprintf(expected, "(EE) %lu\n", lui); |
| 262 | LogMessageVerbSigSafe(X_ERROR, -1, "%lu\n", lui); |
| 263 | read_log_msg(logmsg); |
| 264 | |
| 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); |
| 269 | |
| 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); |
| 275 | |
| 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); |
| 280 | |
| 281 | if (lui == 0) |
| 282 | lui = 1; |
| 283 | else |
| 284 | lui <<= 1; |
| 285 | } while(lui); |
| 286 | |
| 287 | /* signed number substitution */ |
| 288 | i = 0; |
| 289 | do { |
| 290 | char expected[30]; |
| 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); |
| 295 | |
| 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); |
| 300 | |
| 301 | if (i == 0) |
| 302 | i = 1; |
| 303 | else |
| 304 | i <<= 1; |
| 305 | } while(i > INT_MIN); |
| 306 | |
| 307 | li = 0; |
| 308 | do { |
| 309 | char expected[30]; |
| 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); |
| 314 | |
| 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); |
| 319 | |
| 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); |
| 324 | |
| 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); |
| 329 | |
| 330 | if (li == 0) |
| 331 | li = 1; |
| 332 | else |
| 333 | li <<= 1; |
| 334 | } while(li > LONG_MIN); |
| 335 | |
| 336 | |
| 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); |
| 342 | |
| 343 | ptr = 1; |
| 344 | do { |
| 345 | char expected[30]; |
| 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); |
| 350 | ptr <<= 1; |
| 351 | } while(ptr); |
| 352 | |
| 353 | |
| 354 | for (i = 0; i < sizeof(float_tests)/sizeof(float_tests[0]); i++) { |
| 355 | double d = float_tests[i]; |
| 356 | char expected[30]; |
| 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); |
| 361 | |
| 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); |
| 366 | |
| 367 | LogMessageVerbSigSafe(X_ERROR, -1, "%3f\n", d); |
| 368 | read_log_msg(logmsg); |
| 369 | assert(strcmp(logmsg, expected) == 0); |
| 370 | |
| 371 | LogMessageVerbSigSafe(X_ERROR, -1, "%.0f\n", d); |
| 372 | read_log_msg(logmsg); |
| 373 | assert(strcmp(logmsg, expected) == 0); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | LogClose(EXIT_NO_ERROR); |
| 378 | unlink(log_file_path); |
| 379 | |
| 380 | #undef read_log_msg |
| 381 | } |
| 382 | #pragma GCC diagnostic pop /* "-Wformat-security" */ |
| 383 | |
| 384 | int |
| 385 | main(int argc, char **argv) |
| 386 | { |
| 387 | number_formatting(); |
| 388 | logging_format(); |
| 389 | |
| 390 | return 0; |
| 391 | } |