Back to home page

OSCL-LXR

 
 

    


0001 /* Time bounds setting test
0002  *      by: john stultz (johnstul@us.ibm.com)
0003  *      (C) Copyright IBM 2012
0004  *      Licensed under the GPLv2
0005  *
0006  *  NOTE: This is a meta-test which sets the time to edge cases then
0007  *  uses other tests to detect problems. Thus this test requires that
0008  *  the inconsistency-check and nanosleep tests be present in the same
0009  *  directory it is run from.
0010  *
0011  *  To build:
0012  *  $ gcc set-2038.c -o set-2038 -lrt
0013  *
0014  *   This program is free software: you can redistribute it and/or modify
0015  *   it under the terms of the GNU General Public License as published by
0016  *   the Free Software Foundation, either version 2 of the License, or
0017  *   (at your option) any later version.
0018  *
0019  *   This program is distributed in the hope that it will be useful,
0020  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0021  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0022  *   GNU General Public License for more details.
0023  */
0024 
0025 #include <stdio.h>
0026 #include <stdlib.h>
0027 #include <unistd.h>
0028 #include <time.h>
0029 #include <sys/time.h>
0030 #include "../kselftest.h"
0031 
0032 #define NSEC_PER_SEC 1000000000LL
0033 
0034 #define KTIME_MAX   ((long long)~((unsigned long long)1 << 63))
0035 #define KTIME_SEC_MAX   (KTIME_MAX / NSEC_PER_SEC)
0036 
0037 #define YEAR_1901 (-0x7fffffffL)
0038 #define YEAR_1970 1
0039 #define YEAR_2038 0x7fffffffL           /*overflows 32bit time_t */
0040 #define YEAR_2262 KTIME_SEC_MAX         /*overflows 64bit ktime_t */
0041 #define YEAR_MAX  ((long long)((1ULL<<63)-1))   /*overflows 64bit time_t */
0042 
0043 int is32bits(void)
0044 {
0045     return (sizeof(long) == 4);
0046 }
0047 
0048 int settime(long long time)
0049 {
0050     struct timeval now;
0051     int ret;
0052 
0053     now.tv_sec = (time_t)time;
0054     now.tv_usec  = 0;
0055 
0056     ret = settimeofday(&now, NULL);
0057 
0058     printf("Setting time to 0x%lx: %d\n", (long)time, ret);
0059     return ret;
0060 }
0061 
0062 int do_tests(void)
0063 {
0064     int ret;
0065 
0066     ret = system("date");
0067     ret = system("./inconsistency-check -c 0 -t 20");
0068     ret |= system("./nanosleep");
0069     ret |= system("./nsleep-lat");
0070     return ret;
0071 
0072 }
0073 
0074 int main(int argc, char *argv[])
0075 {
0076     int ret = 0;
0077     int opt, dangerous = 0;
0078     time_t start;
0079 
0080     /* Process arguments */
0081     while ((opt = getopt(argc, argv, "d")) != -1) {
0082         switch (opt) {
0083         case 'd':
0084             dangerous = 1;
0085         }
0086     }
0087 
0088     start = time(0);
0089 
0090     /* First test that crazy values don't work */
0091     if (!settime(YEAR_1901)) {
0092         ret = -1;
0093         goto out;
0094     }
0095     if (!settime(YEAR_MAX)) {
0096         ret = -1;
0097         goto out;
0098     }
0099     if (!is32bits() && !settime(YEAR_2262)) {
0100         ret = -1;
0101         goto out;
0102     }
0103 
0104     /* Now test behavior near edges */
0105     settime(YEAR_1970);
0106     ret = do_tests();
0107     if (ret)
0108         goto out;
0109 
0110     settime(YEAR_2038 - 600);
0111     ret = do_tests();
0112     if (ret)
0113         goto out;
0114 
0115     /* The rest of the tests can blowup on 32bit systems */
0116     if (is32bits() && !dangerous)
0117         goto out;
0118     /* Test rollover behavior 32bit edge */
0119     settime(YEAR_2038 - 10);
0120     ret = do_tests();
0121     if (ret)
0122         goto out;
0123 
0124     settime(YEAR_2262 - 600);
0125     ret = do_tests();
0126 
0127 out:
0128     /* restore clock */
0129     settime(start);
0130     if (ret)
0131         return ksft_exit_fail();
0132     return ksft_exit_pass();
0133 }