set-2038.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Time bounds setting test
  2. * by: john stultz (johnstul@us.ibm.com)
  3. * (C) Copyright IBM 2012
  4. * Licensed under the GPLv2
  5. *
  6. * NOTE: This is a meta-test which sets the time to edge cases then
  7. * uses other tests to detect problems. Thus this test requires that
  8. * the inconsistency-check and nanosleep tests be present in the same
  9. * directory it is run from.
  10. *
  11. * To build:
  12. * $ gcc set-2038.c -o set-2038 -lrt
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <time.h>
  28. #include <sys/time.h>
  29. #ifdef KTEST
  30. #include "../kselftest.h"
  31. #else
  32. static inline int ksft_exit_pass(void)
  33. {
  34. exit(0);
  35. }
  36. static inline int ksft_exit_fail(void)
  37. {
  38. exit(1);
  39. }
  40. #endif
  41. #define NSEC_PER_SEC 1000000000LL
  42. #define KTIME_MAX ((long long)~((unsigned long long)1 << 63))
  43. #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
  44. #define YEAR_1901 (-0x7fffffffL)
  45. #define YEAR_1970 1
  46. #define YEAR_2038 0x7fffffffL /*overflows 32bit time_t */
  47. #define YEAR_2262 KTIME_SEC_MAX /*overflows 64bit ktime_t */
  48. #define YEAR_MAX ((long long)((1ULL<<63)-1)) /*overflows 64bit time_t */
  49. int is32bits(void)
  50. {
  51. return (sizeof(long) == 4);
  52. }
  53. int settime(long long time)
  54. {
  55. struct timeval now;
  56. int ret;
  57. now.tv_sec = (time_t)time;
  58. now.tv_usec = 0;
  59. ret = settimeofday(&now, NULL);
  60. printf("Setting time to 0x%lx: %d\n", (long)time, ret);
  61. return ret;
  62. }
  63. int do_tests(void)
  64. {
  65. int ret;
  66. ret = system("date");
  67. ret = system("./inconsistency-check -c 0 -t 20");
  68. ret |= system("./nanosleep");
  69. ret |= system("./nsleep-lat");
  70. return ret;
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. int ret = 0;
  75. int opt, dangerous = 0;
  76. time_t start;
  77. /* Process arguments */
  78. while ((opt = getopt(argc, argv, "d")) != -1) {
  79. switch (opt) {
  80. case 'd':
  81. dangerous = 1;
  82. }
  83. }
  84. start = time(0);
  85. /* First test that crazy values don't work */
  86. if (!settime(YEAR_1901)) {
  87. ret = -1;
  88. goto out;
  89. }
  90. if (!settime(YEAR_MAX)) {
  91. ret = -1;
  92. goto out;
  93. }
  94. if (!is32bits() && !settime(YEAR_2262)) {
  95. ret = -1;
  96. goto out;
  97. }
  98. /* Now test behavior near edges */
  99. settime(YEAR_1970);
  100. ret = do_tests();
  101. if (ret)
  102. goto out;
  103. settime(YEAR_2038 - 600);
  104. ret = do_tests();
  105. if (ret)
  106. goto out;
  107. /* The rest of the tests can blowup on 32bit systems */
  108. if (is32bits() && !dangerous)
  109. goto out;
  110. /* Test rollover behavior 32bit edge */
  111. settime(YEAR_2038 - 10);
  112. ret = do_tests();
  113. if (ret)
  114. goto out;
  115. settime(YEAR_2262 - 600);
  116. ret = do_tests();
  117. out:
  118. /* restore clock */
  119. settime(start);
  120. if (ret)
  121. return ksft_exit_fail();
  122. return ksft_exit_pass();
  123. }