You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Date and time self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <time.h>
  32. #include <ipxe/test.h>
  33. /** A mktime() test */
  34. struct mktime_test {
  35. /** Broken-down time */
  36. struct tm tm;
  37. /** Day of the week */
  38. int wday;
  39. /** Day of the year */
  40. int yday;
  41. /** Seconds since the Epoch */
  42. time_t time;
  43. };
  44. /**
  45. * Define a mktime() test
  46. *
  47. * @v name Test name
  48. * @v SEC Seconds [0,60]
  49. * @v MIN Minutes [0,59]
  50. * @v HOUR Hour [0,23]
  51. * @v MDAY Day of month [1,31]
  52. * @v MON Month of year [0,11]
  53. * @v YEAR Years since 1900
  54. * @v WDAY Day of week [0,6] (Sunday=0)
  55. * @v YDAY Day of year [0,365]
  56. * @v ISDST Daylight savings flag (ignored)
  57. * @v TIME Seconds since the Epoch
  58. * @ret test mktime() test
  59. *
  60. * This macro is designed to make it easy to generate test vectors in
  61. * Perl using
  62. *
  63. * print join ", ", gmtime ( $time ), $time."ULL";
  64. *
  65. */
  66. #define MKTIME_TEST( name, SEC, MIN, HOUR, MDAY, MON, YEAR, WDAY, \
  67. YDAY, ISDST, TIME ) \
  68. static struct mktime_test name = { \
  69. .tm = { \
  70. .tm_sec = SEC, \
  71. .tm_min = MIN, \
  72. .tm_hour = HOUR, \
  73. .tm_mday = MDAY, \
  74. .tm_mon = MON, \
  75. .tm_year = YEAR, \
  76. .tm_isdst = ISDST, \
  77. }, \
  78. .wday = WDAY, \
  79. .yday = YDAY, \
  80. .time = TIME, \
  81. }
  82. /**
  83. * Report mktime() test result
  84. *
  85. * @v test mktime() test
  86. */
  87. #define mktime_ok( test ) do { \
  88. time_t time = mktime ( &(test)->tm ); \
  89. ok ( time == (test)->time ); \
  90. ok ( (test)->tm.tm_wday == (test)->wday ); \
  91. ok ( (test)->tm.tm_yday == (test)->yday ); \
  92. } while ( 0 )
  93. /* Start of the Epoch */
  94. MKTIME_TEST ( mktime_epoch, 00, 00, 00, 01, 00, 70, 4, 0, 0, 0 );
  95. /* Birth of iPXE as a new project */
  96. MKTIME_TEST ( mktime_ipxe, 01, 15, 20, 19, 03, 110, 1, 108, 0, 1271708101ULL );
  97. /* Random test vectors generated using Perl's gmtime() */
  98. MKTIME_TEST ( mktime_0, 4, 17, 20, 1, 0, 150, 6, 0, 0, 2524681024ULL );
  99. MKTIME_TEST ( mktime_1, 22, 47, 21, 27, 11, 77, 2, 360, 0, 252107242ULL );
  100. MKTIME_TEST ( mktime_2, 26, 10, 0, 7, 2, 196, 3, 66, 0, 3981917426ULL );
  101. MKTIME_TEST ( mktime_3, 44, 44, 23, 15, 9, 261, 4, 287, 0, 6052319084ULL );
  102. MKTIME_TEST ( mktime_4, 3, 22, 18, 8, 9, 296, 6, 281, 0, 7156232523ULL );
  103. MKTIME_TEST ( mktime_5, 27, 26, 16, 18, 11, 338, 2, 351, 0, 8487649587ULL );
  104. MKTIME_TEST ( mktime_6, 31, 36, 22, 3, 3, 293, 3, 92, 0, 7045310191ULL );
  105. MKTIME_TEST ( mktime_7, 2, 0, 6, 25, 5, 289, 4, 175, 0, 6926191202ULL );
  106. MKTIME_TEST ( mktime_8, 43, 50, 1, 8, 0, 210, 3, 7, 0, 4418589043ULL );
  107. MKTIME_TEST ( mktime_9, 48, 14, 20, 23, 3, 86, 3, 112, 0, 514671288ULL );
  108. MKTIME_TEST ( mktime_10, 4, 43, 5, 29, 11, 173, 5, 362, 0, 3281751784ULL );
  109. MKTIME_TEST ( mktime_11, 47, 26, 21, 12, 7, 177, 4, 223, 0, 3396029207ULL );
  110. MKTIME_TEST ( mktime_12, 18, 55, 20, 26, 11, 88, 1, 360, 0, 599172918ULL );
  111. MKTIME_TEST ( mktime_13, 8, 32, 13, 15, 7, 314, 1, 226, 0, 7719456728ULL );
  112. MKTIME_TEST ( mktime_14, 0, 16, 11, 20, 6, 138, 2, 200, 0, 2163237360ULL );
  113. MKTIME_TEST ( mktime_15, 48, 0, 9, 31, 2, 202, 5, 89, 0, 4173238848ULL );
  114. MKTIME_TEST ( mktime_16, 51, 55, 0, 15, 1, 323, 6, 45, 0, 7987769751ULL );
  115. MKTIME_TEST ( mktime_17, 36, 10, 7, 11, 5, 301, 4, 161, 0, 7303590636ULL );
  116. MKTIME_TEST ( mktime_18, 22, 39, 11, 21, 9, 233, 3, 293, 0, 5169181162ULL );
  117. MKTIME_TEST ( mktime_19, 48, 29, 8, 31, 7, 207, 3, 242, 0, 4344222588ULL );
  118. MKTIME_TEST ( mktime_20, 4, 53, 22, 8, 8, 165, 2, 250, 0, 3019675984ULL );
  119. MKTIME_TEST ( mktime_21, 14, 16, 8, 10, 5, 298, 0, 160, 0, 7208900174ULL );
  120. MKTIME_TEST ( mktime_22, 10, 35, 3, 12, 3, 188, 1, 102, 0, 3732579310ULL );
  121. MKTIME_TEST ( mktime_23, 47, 12, 18, 22, 2, 103, 6, 80, 0, 1048356767ULL );
  122. MKTIME_TEST ( mktime_24, 23, 29, 17, 23, 10, 201, 3, 326, 0, 4162210163ULL );
  123. MKTIME_TEST ( mktime_25, 58, 35, 23, 24, 3, 111, 0, 113, 0, 1303688158ULL );
  124. MKTIME_TEST ( mktime_26, 34, 56, 15, 24, 11, 154, 4, 357, 0, 2681740594ULL );
  125. MKTIME_TEST ( mktime_27, 7, 11, 22, 28, 1, 243, 4, 58, 0, 5464447867ULL );
  126. MKTIME_TEST ( mktime_28, 25, 45, 23, 29, 11, 90, 6, 362, 0, 662514325ULL );
  127. MKTIME_TEST ( mktime_29, 31, 20, 12, 24, 1, 146, 6, 54, 0, 2403087631ULL );
  128. MKTIME_TEST ( mktime_30, 49, 7, 18, 16, 10, 271, 6, 319, 0, 6370596469ULL );
  129. MKTIME_TEST ( mktime_31, 31, 55, 2, 25, 5, 141, 2, 175, 0, 2255741731ULL );
  130. /**
  131. * Perform date and time self-tests
  132. *
  133. */
  134. static void time_test_exec ( void ) {
  135. mktime_ok ( &mktime_epoch );
  136. mktime_ok ( &mktime_ipxe );
  137. mktime_ok ( &mktime_0 );
  138. mktime_ok ( &mktime_1 );
  139. mktime_ok ( &mktime_2 );
  140. mktime_ok ( &mktime_3 );
  141. mktime_ok ( &mktime_4 );
  142. mktime_ok ( &mktime_5 );
  143. mktime_ok ( &mktime_6 );
  144. mktime_ok ( &mktime_7 );
  145. mktime_ok ( &mktime_8 );
  146. mktime_ok ( &mktime_9 );
  147. mktime_ok ( &mktime_10 );
  148. mktime_ok ( &mktime_11 );
  149. mktime_ok ( &mktime_12 );
  150. mktime_ok ( &mktime_13 );
  151. mktime_ok ( &mktime_14 );
  152. mktime_ok ( &mktime_15 );
  153. mktime_ok ( &mktime_16 );
  154. mktime_ok ( &mktime_17 );
  155. mktime_ok ( &mktime_18 );
  156. mktime_ok ( &mktime_19 );
  157. mktime_ok ( &mktime_20 );
  158. mktime_ok ( &mktime_21 );
  159. mktime_ok ( &mktime_22 );
  160. mktime_ok ( &mktime_23 );
  161. mktime_ok ( &mktime_24 );
  162. mktime_ok ( &mktime_25 );
  163. mktime_ok ( &mktime_26 );
  164. mktime_ok ( &mktime_27 );
  165. mktime_ok ( &mktime_28 );
  166. mktime_ok ( &mktime_29 );
  167. mktime_ok ( &mktime_30 );
  168. mktime_ok ( &mktime_31 );
  169. }
  170. /** Date and time self-test */
  171. struct self_test time_test __self_test = {
  172. .name = "time",
  173. .exec = time_test_exec,
  174. };