signal-logging.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include <stdint.h>
  27. #include <unistd.h>
  28. #include "assert.h"
  29. #include "misc.h"
  30. struct number_format_test {
  31. uint64_t number;
  32. char string[21];
  33. char hex_string[17];
  34. };
  35. struct signed_number_format_test {
  36. int64_t number;
  37. char string[21];
  38. };
  39. struct float_number_format_test {
  40. double number;
  41. char string[21];
  42. };
  43. static Bool
  44. check_signed_number_format_test(long int number)
  45. {
  46. char string[21];
  47. char expected[21];
  48. sprintf(expected, "%ld", number);
  49. FormatInt64(number, string);
  50. if(strncmp(string, expected, 21) != 0) {
  51. fprintf(stderr, "Failed to convert %jd to decimal string (expected %s but got %s)\n",
  52. (intmax_t) number, expected, string);
  53. return FALSE;
  54. }
  55. return TRUE;
  56. }
  57. static Bool
  58. check_float_format_test(double number)
  59. {
  60. char string[21];
  61. char expected[21];
  62. /* we currently always print float as .2f */
  63. sprintf(expected, "%.2f", number);
  64. FormatDouble(number, string);
  65. if(strncmp(string, expected, 21) != 0) {
  66. fprintf(stderr, "Failed to convert %f to string (%s vs %s)\n",
  67. number, expected, string);
  68. return FALSE;
  69. }
  70. return TRUE;
  71. }
  72. static Bool
  73. check_number_format_test(long unsigned int number)
  74. {
  75. char string[21];
  76. char expected[21];
  77. sprintf(expected, "%lu", number);
  78. FormatUInt64(number, string);
  79. if(strncmp(string, expected, 21) != 0) {
  80. fprintf(stderr, "Failed to convert %ju to decimal string (%s vs %s)\n",
  81. (intmax_t) number, expected, string);
  82. return FALSE;
  83. }
  84. sprintf(expected, "%lx", number);
  85. FormatUInt64Hex(number, string);
  86. if(strncmp(string, expected, 17) != 0) {
  87. fprintf(stderr, "Failed to convert %ju to hexadecimal string (%s vs %s)\n",
  88. (intmax_t) number, expected, string);
  89. return FALSE;
  90. }
  91. return TRUE;
  92. }
  93. /* FIXME: max range stuff */
  94. double float_tests[] = { 0, 5, 0.1, 0.01, 5.2342, 10.2301,
  95. -1, -2.00, -0.6023, -1203.30
  96. };
  97. static void
  98. number_formatting(void)
  99. {
  100. int i;
  101. #pragma GCC diagnostic push
  102. #pragma GCC diagnostic ignored "-Woverflow"
  103. long unsigned int unsigned_tests[] = { 0,/* Zero */
  104. 5, /* Single digit number */
  105. 12, /* Two digit decimal number */
  106. 37, /* Two digit hex number */
  107. 0xC90B2, /* Large < 32 bit number */
  108. 0x15D027BF211B37A, /* Large > 32 bit number */
  109. 0xFFFFFFFFFFFFFFFF, /* Maximum 64-bit number */
  110. };
  111. long int signed_tests[] = { 0,/* Zero */
  112. 5, /* Single digit number */
  113. 12, /* Two digit decimal number */
  114. 37, /* Two digit hex number */
  115. 0xC90B2, /* Large < 32 bit number */
  116. 0x15D027BF211B37A, /* Large > 32 bit number */
  117. 0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
  118. -1, /* Single digit number */
  119. -12, /* Two digit decimal number */
  120. -0xC90B2, /* Large < 32 bit number */
  121. -0x15D027BF211B37A, /* Large > 32 bit number */
  122. -0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
  123. } ;
  124. #pragma GCC diagnostic pop
  125. for (i = 0; i < sizeof(unsigned_tests) / sizeof(unsigned_tests[0]); i++)
  126. assert(check_number_format_test(unsigned_tests[i]));
  127. for (i = 0; i < sizeof(unsigned_tests) / sizeof(signed_tests[0]); i++)
  128. assert(check_signed_number_format_test(signed_tests[i]));
  129. for (i = 0; i < sizeof(float_tests) / sizeof(float_tests[0]); i++)
  130. assert(check_float_format_test(float_tests[i]));
  131. }
  132. #pragma GCC diagnostic push
  133. #pragma GCC diagnostic ignored "-Wformat-security"
  134. #pragma GCC diagnostic ignored "-Wformat"
  135. #pragma GCC diagnostic ignored "-Wformat-extra-args"
  136. static void logging_format(void)
  137. {
  138. const char *log_file_path = "/tmp/Xorg-logging-test.log";
  139. const char *str = "%s %d %u %% %p %i";
  140. char buf[1024];
  141. int i;
  142. unsigned int ui;
  143. long li;
  144. unsigned long lui;
  145. FILE *f;
  146. char read_buf[2048];
  147. char *logmsg;
  148. uintptr_t ptr;
  149. /* set up buf to contain ".....end" */
  150. memset(buf, '.', sizeof(buf));
  151. strcpy(&buf[sizeof(buf) - 4], "end");
  152. LogInit(log_file_path, NULL);
  153. assert(f = fopen(log_file_path, "r"));
  154. #define read_log_msg(msg) do { \
  155. msg = fgets(read_buf, sizeof(read_buf), f); \
  156. assert(msg != NULL); \
  157. msg = strchr(read_buf, ']'); \
  158. assert(msg != NULL); \
  159. assert(strlen(msg) > 2); \
  160. msg = msg + 2; /* advance past [time.stamp] */ \
  161. } while (0)
  162. /* boring test message */
  163. LogMessageVerbSigSafe(X_ERROR, -1, "test message\n");
  164. read_log_msg(logmsg);
  165. assert(strcmp(logmsg, "(EE) test message\n") == 0);
  166. /* long buf is truncated to "....en\n" */
  167. LogMessageVerbSigSafe(X_ERROR, -1, buf);
  168. read_log_msg(logmsg);
  169. assert(strcmp(&logmsg[strlen(logmsg) - 3], "en\n") == 0);
  170. /* same thing, this time as string substitution */
  171. LogMessageVerbSigSafe(X_ERROR, -1, "%s", buf);
  172. read_log_msg(logmsg);
  173. assert(strcmp(&logmsg[strlen(logmsg) - 3], "en\n") == 0);
  174. /* strings containing placeholders should just work */
  175. LogMessageVerbSigSafe(X_ERROR, -1, "%s\n", str);
  176. read_log_msg(logmsg);
  177. assert(strcmp(logmsg, "(EE) %s %d %u %% %p %i\n") == 0);
  178. /* literal % */
  179. LogMessageVerbSigSafe(X_ERROR, -1, "test %%\n");
  180. read_log_msg(logmsg);
  181. assert(strcmp(logmsg, "(EE) test %\n") == 0);
  182. /* character */
  183. LogMessageVerbSigSafe(X_ERROR, -1, "test %c\n", 'a');
  184. read_log_msg(logmsg);
  185. assert(strcmp(logmsg, "(EE) test a\n") == 0);
  186. /* something unsupported % */
  187. LogMessageVerbSigSafe(X_ERROR, -1, "test %Q\n");
  188. read_log_msg(logmsg);
  189. assert(strstr(logmsg, "BUG") != NULL);
  190. LogMessageVerbSigSafe(X_ERROR, -1, "\n");
  191. fseek(f, 0, SEEK_END);
  192. /* string substitution */
  193. LogMessageVerbSigSafe(X_ERROR, -1, "%s\n", "substituted string");
  194. read_log_msg(logmsg);
  195. assert(strcmp(logmsg, "(EE) substituted string\n") == 0);
  196. /* Invalid format */
  197. LogMessageVerbSigSafe(X_ERROR, -1, "%4", 4);
  198. read_log_msg(logmsg);
  199. assert(strcmp(logmsg, "(EE) ") == 0);
  200. LogMessageVerbSigSafe(X_ERROR, -1, "\n");
  201. fseek(f, 0, SEEK_END);
  202. /* %hld is bogus */
  203. LogMessageVerbSigSafe(X_ERROR, -1, "%hld\n", 4);
  204. read_log_msg(logmsg);
  205. assert(strstr(logmsg, "BUG") != NULL);
  206. LogMessageVerbSigSafe(X_ERROR, -1, "\n");
  207. fseek(f, 0, SEEK_END);
  208. /* number substitution */
  209. ui = 0;
  210. do {
  211. char expected[30];
  212. sprintf(expected, "(EE) %u\n", ui);
  213. LogMessageVerbSigSafe(X_ERROR, -1, "%u\n", ui);
  214. read_log_msg(logmsg);
  215. assert(strcmp(logmsg, expected) == 0);
  216. sprintf(expected, "(EE) %x\n", ui);
  217. LogMessageVerbSigSafe(X_ERROR, -1, "%x\n", ui);
  218. read_log_msg(logmsg);
  219. assert(strcmp(logmsg, expected) == 0);
  220. if (ui == 0)
  221. ui = 1;
  222. else
  223. ui <<= 1;
  224. } while(ui);
  225. lui = 0;
  226. do {
  227. char expected[30];
  228. sprintf(expected, "(EE) %lu\n", lui);
  229. LogMessageVerbSigSafe(X_ERROR, -1, "%lu\n", lui);
  230. read_log_msg(logmsg);
  231. sprintf(expected, "(EE) %lld\n", (unsigned long long)ui);
  232. LogMessageVerbSigSafe(X_ERROR, -1, "%lld\n", (unsigned long long)ui);
  233. read_log_msg(logmsg);
  234. assert(strcmp(logmsg, expected) == 0);
  235. sprintf(expected, "(EE) %lx\n", lui);
  236. printf("%s\n", expected);
  237. LogMessageVerbSigSafe(X_ERROR, -1, "%lx\n", lui);
  238. read_log_msg(logmsg);
  239. assert(strcmp(logmsg, expected) == 0);
  240. sprintf(expected, "(EE) %llx\n", (unsigned long long)ui);
  241. LogMessageVerbSigSafe(X_ERROR, -1, "%llx\n", (unsigned long long)ui);
  242. read_log_msg(logmsg);
  243. assert(strcmp(logmsg, expected) == 0);
  244. if (lui == 0)
  245. lui = 1;
  246. else
  247. lui <<= 1;
  248. } while(lui);
  249. /* signed number substitution */
  250. i = 0;
  251. do {
  252. char expected[30];
  253. sprintf(expected, "(EE) %d\n", i);
  254. LogMessageVerbSigSafe(X_ERROR, -1, "%d\n", i);
  255. read_log_msg(logmsg);
  256. assert(strcmp(logmsg, expected) == 0);
  257. sprintf(expected, "(EE) %d\n", i | INT_MIN);
  258. LogMessageVerbSigSafe(X_ERROR, -1, "%d\n", i | INT_MIN);
  259. read_log_msg(logmsg);
  260. assert(strcmp(logmsg, expected) == 0);
  261. if (i == 0)
  262. i = 1;
  263. else
  264. i <<= 1;
  265. } while(i > INT_MIN);
  266. li = 0;
  267. do {
  268. char expected[30];
  269. sprintf(expected, "(EE) %ld\n", li);
  270. LogMessageVerbSigSafe(X_ERROR, -1, "%ld\n", li);
  271. read_log_msg(logmsg);
  272. assert(strcmp(logmsg, expected) == 0);
  273. sprintf(expected, "(EE) %ld\n", li | LONG_MIN);
  274. LogMessageVerbSigSafe(X_ERROR, -1, "%ld\n", li | LONG_MIN);
  275. read_log_msg(logmsg);
  276. assert(strcmp(logmsg, expected) == 0);
  277. sprintf(expected, "(EE) %lld\n", (long long)li);
  278. LogMessageVerbSigSafe(X_ERROR, -1, "%lld\n", (long long)li);
  279. read_log_msg(logmsg);
  280. assert(strcmp(logmsg, expected) == 0);
  281. sprintf(expected, "(EE) %lld\n", (long long)(li | LONG_MIN));
  282. LogMessageVerbSigSafe(X_ERROR, -1, "%lld\n", (long long)(li | LONG_MIN));
  283. read_log_msg(logmsg);
  284. assert(strcmp(logmsg, expected) == 0);
  285. if (li == 0)
  286. li = 1;
  287. else
  288. li <<= 1;
  289. } while(li > LONG_MIN);
  290. /* pointer substitution */
  291. /* we print a null-pointer differently to printf */
  292. LogMessageVerbSigSafe(X_ERROR, -1, "%p\n", NULL);
  293. read_log_msg(logmsg);
  294. assert(strcmp(logmsg, "(EE) 0x0\n") == 0);
  295. ptr = 1;
  296. do {
  297. char expected[30];
  298. #ifdef __sun /* Solaris doesn't autoadd "0x" to %p format */
  299. sprintf(expected, "(EE) 0x%p\n", (void*)ptr);
  300. #else
  301. sprintf(expected, "(EE) %p\n", (void*)ptr);
  302. #endif
  303. LogMessageVerbSigSafe(X_ERROR, -1, "%p\n", (void*)ptr);
  304. read_log_msg(logmsg);
  305. assert(strcmp(logmsg, expected) == 0);
  306. ptr <<= 1;
  307. } while(ptr);
  308. for (i = 0; i < sizeof(float_tests)/sizeof(float_tests[0]); i++) {
  309. double d = float_tests[i];
  310. char expected[30];
  311. sprintf(expected, "(EE) %.2f\n", d);
  312. LogMessageVerbSigSafe(X_ERROR, -1, "%f\n", d);
  313. read_log_msg(logmsg);
  314. assert(strcmp(logmsg, expected) == 0);
  315. /* test for length modifiers, we just ignore them atm */
  316. LogMessageVerbSigSafe(X_ERROR, -1, "%.3f\n", d);
  317. read_log_msg(logmsg);
  318. assert(strcmp(logmsg, expected) == 0);
  319. LogMessageVerbSigSafe(X_ERROR, -1, "%3f\n", d);
  320. read_log_msg(logmsg);
  321. assert(strcmp(logmsg, expected) == 0);
  322. LogMessageVerbSigSafe(X_ERROR, -1, "%.0f\n", d);
  323. read_log_msg(logmsg);
  324. assert(strcmp(logmsg, expected) == 0);
  325. }
  326. LogClose(EXIT_NO_ERROR);
  327. unlink(log_file_path);
  328. #undef read_log_msg
  329. }
  330. #pragma GCC diagnostic pop /* "-Wformat-security" */
  331. int
  332. main(int argc, char **argv)
  333. {
  334. number_formatting();
  335. logging_format();
  336. return 0;
  337. }