test_locale.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * Tilghman Lesher <tlesher AT digium DOT com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Locale Test
  21. *
  22. * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <sys/types.h>
  33. #include <dirent.h>
  34. #ifndef __USE_GNU
  35. #define __USE_GNU 1
  36. #endif
  37. #include <locale.h>
  38. #include "asterisk/cli.h"
  39. #include "asterisk/linkedlists.h"
  40. #include "asterisk/localtime.h"
  41. #include "asterisk/utils.h"
  42. #include "asterisk/module.h"
  43. static char *handle_cli_test_locales(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  44. {
  45. DIR *localedir;
  46. struct dirent *dent;
  47. struct ast_tm atm;
  48. struct timeval tv;
  49. const char *orig_locale;
  50. char origlocalformat[200] = "", localformat[200] = "";
  51. struct test_locales {
  52. AST_LIST_ENTRY(test_locales) list;
  53. char *localformat;
  54. char name[0];
  55. } *tl = NULL;
  56. AST_LIST_HEAD_NOLOCK(locales, test_locales) locales;
  57. int varies = 0, all_successful = 1, count = 0, count_fail = 0;
  58. switch (cmd) {
  59. case CLI_INIT:
  60. e->command = "test locale";
  61. e->usage = ""
  62. "Usage: test locale\n"
  63. " Test thread safety of locale functions.\n";
  64. return NULL;
  65. case CLI_GENERATE:
  66. return NULL;
  67. }
  68. if (a->argc != e->args) {
  69. return CLI_SHOWUSAGE;
  70. }
  71. /* First we run a set of tests with the global locale, which isn't thread-safe. */
  72. if (!(localedir = opendir(
  73. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__)
  74. "/usr/share/locale"
  75. #else /* Linux */
  76. "/usr/lib/locale"
  77. #endif
  78. ))) {
  79. ast_cli(a->fd, "No locales seem to exist on this platform.\n");
  80. return CLI_SUCCESS;
  81. }
  82. tv = ast_tvnow();
  83. ast_localtime(&tv, &atm, NULL);
  84. orig_locale = setlocale(LC_ALL, NULL);
  85. AST_LIST_HEAD_SET_NOLOCK(&locales, NULL);
  86. /* Get something different, to compare against. */
  87. ast_strftime(origlocalformat, sizeof(origlocalformat), "%c", &atm);
  88. while ((dent = readdir(localedir))) {
  89. size_t namelen;
  90. if (dent->d_name[0] == '.') {
  91. continue;
  92. }
  93. setlocale(LC_ALL, dent->d_name);
  94. ast_strftime(localformat, sizeof(localformat), "%c", &atm);
  95. /* Store values */
  96. if (!(tl = ast_calloc(1, sizeof(*tl) + strlen(localformat) + (namelen = strlen(dent->d_name)) + 2))) {
  97. continue;
  98. }
  99. strcpy(tl->name, dent->d_name); /* SAFE */
  100. tl->localformat = tl->name + namelen + 1;
  101. strcpy(tl->localformat, localformat); /* SAFE */
  102. AST_LIST_INSERT_TAIL(&locales, tl, list);
  103. /* Ensure that at least two entries differ, otherwise this test doesn't mean much. */
  104. if (!varies && strcmp(AST_LIST_FIRST(&locales)->localformat, localformat)) {
  105. varies = 1;
  106. }
  107. }
  108. setlocale(LC_ALL, orig_locale);
  109. closedir(localedir);
  110. if (!varies) {
  111. if (!strcmp(origlocalformat, localformat)) {
  112. ast_cli(a->fd, "WARNING: the locales on your system don't differ. Install more locales if you want this test to mean something.\n");
  113. }
  114. }
  115. orig_locale = ast_setlocale(AST_LIST_FIRST(&locales)->name);
  116. while ((tl = AST_LIST_REMOVE_HEAD(&locales, list))) {
  117. ast_setlocale(tl->name);
  118. ast_strftime(localformat, sizeof(localformat), "%c", &atm);
  119. if (strcmp(localformat, tl->localformat)) {
  120. ast_cli(a->fd, "WARNING: locale test fails for locale %s\n", tl->name);
  121. all_successful = 0;
  122. count_fail++;
  123. }
  124. ast_free(tl);
  125. count++;
  126. }
  127. ast_setlocale(orig_locale);
  128. if (all_successful) {
  129. ast_cli(a->fd, "All %d locale tests successful\n", count);
  130. } else if (count_fail == count && count > 0) {
  131. ast_cli(a->fd, "No locale tests successful out of %d tries\n", count);
  132. } else if (count > 0) {
  133. ast_cli(a->fd, "Partial failure (%d/%d) for a %.0f%% failure rate\n", count_fail, count, count_fail * 100.0 / count);
  134. } else {
  135. ast_cli(a->fd, "No locales tested. Install more locales.\n");
  136. }
  137. return CLI_SUCCESS;
  138. }
  139. static struct ast_cli_entry cli_locales[] = {
  140. AST_CLI_DEFINE(handle_cli_test_locales, "Test locales for thread-safety"),
  141. };
  142. static int unload_module(void)
  143. {
  144. ast_cli_unregister_multiple(cli_locales, ARRAY_LEN(cli_locales));
  145. return 0;
  146. }
  147. static int load_module(void)
  148. {
  149. ast_cli_register_multiple(cli_locales, ARRAY_LEN(cli_locales));
  150. return AST_MODULE_LOAD_SUCCESS;
  151. }
  152. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Locale tests");