test_time.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, 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. /*!
  19. * \file
  20. * \brief Timezone tests
  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 "asterisk/utils.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/test.h"
  36. #ifndef TZDIR
  37. #ifdef SOLARIS
  38. #define TZDIR "/usr/share/lib/zoneinfo"
  39. #else
  40. #define TZDIR "/usr/share/zoneinfo"
  41. #endif /* defined SOLARIS */
  42. #endif /* !defined TZDIR */
  43. AST_TEST_DEFINE(test_timezone_watch)
  44. {
  45. const char *zones[] = { "America/Chicago", "America/New_York" };
  46. int type, i, res = AST_TEST_PASS;
  47. struct timeval tv = ast_tvnow();
  48. struct ast_tm atm[ARRAY_LEN(zones)];
  49. char tmpdir[] = "/tmp/timezone.XXXXXX";
  50. char tzfile[50], syscmd[256];
  51. switch (cmd) {
  52. case TEST_INIT:
  53. info->name = "timezone_watch";
  54. info->category = "/main/stdtime/";
  55. info->summary = "Verify deleting timezone file purges cache";
  56. info->description =
  57. "Verifies that the caching engine properly destroys a timezone entry when its file is deleted.";
  58. return AST_TEST_NOT_RUN;
  59. case TEST_EXECUTE:
  60. break;
  61. }
  62. if (!mkdtemp(tmpdir)) {
  63. ast_test_status_update(test, "Unable to create working directory: %s\n", strerror(errno));
  64. return AST_TEST_NOT_RUN;
  65. }
  66. snprintf(tzfile, sizeof(tzfile), "%s/test", tmpdir);
  67. for (type = 0; type <
  68. #ifdef SOLARIS
  69. 1 /* Solaris doesn't use symlinks for timezones */
  70. #else
  71. 2
  72. #endif
  73. ; type++) {
  74. ast_test_status_update(test, "Executing %s test...\n", type == 0 ? "deletion" : "symlink");
  75. for (i = 0; i < ARRAY_LEN(zones); i++) {
  76. int system_res;
  77. snprintf(syscmd, sizeof(syscmd), "%s " TZDIR "/%s %s", type == 0 ? "cp" : "ln -sf", zones[i], tzfile);
  78. if ((system_res = ast_safe_system(syscmd))) {
  79. ast_log(LOG_WARNING, "system(%s) returned non-zero: %d\n", syscmd, system_res);
  80. }
  81. ast_localtime_wakeup_monitor(test);
  82. ast_test_status_update(test, "Querying timezone %s\n", tzfile);
  83. ast_localtime(&tv, &atm[i], tzfile);
  84. if (i != 0) {
  85. if (atm[i].tm_hour == atm[i - 1].tm_hour) {
  86. res = AST_TEST_FAIL;
  87. ast_test_status_update(test, "Failed %s test: %d(%s) = %d(%s)\n", type == 0 ? "deletion" : "symlink", atm[i].tm_hour, zones[i], atm[i-1].tm_hour, zones[i-1]);
  88. }
  89. }
  90. if (i + 1 != ARRAY_LEN(zones)) {
  91. /* stat(2) only has resolution to 1 second - must wait, or the mtime is the same */
  92. usleep(1100000);
  93. }
  94. }
  95. }
  96. snprintf(syscmd, sizeof(syscmd), "rm -rf %s", tmpdir);
  97. if (ast_safe_system(syscmd)) {
  98. ast_log(LOG_WARNING, "system(%s) returned non-zero.\n", syscmd);
  99. }
  100. return res;
  101. }
  102. static int unload_module(void)
  103. {
  104. AST_TEST_UNREGISTER(test_timezone_watch);
  105. return 0;
  106. }
  107. static int load_module(void)
  108. {
  109. AST_TEST_REGISTER(test_timezone_watch);
  110. return AST_MODULE_LOAD_SUCCESS;
  111. }
  112. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Time Tests");