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