aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Stultz <john.stultz@linaro.org>2016-06-01 14:53:27 -0400
committerShuah Khan <shuahkh@osg.samsung.com>2016-06-02 18:39:56 -0400
commit1a77e2bd8c89a37703dc9de18a7b552ad76e1c12 (patch)
treef83938868f1a3e7b037ba90acc5063532be7737d
parent1a695a905c18548062509178b98bc91e67510864 (diff)
kselftests: timers: Add set-tz test case
Mika Westerberg reported a erroneous change in the error checking of settimeofday, so I wanted to add a test to ensure we don't trip over this again. Cc: Mika Westerberg <mika.westerberg@linux.intel.com> Cc: Baolin Wang <baolin.wang@linaro.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Shuah Khan <shuahkh@osg.samsung.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Ingo Molnar <mingo@kernel.org> Cc: Richard Cochran <richardcochran@gmail.com> Cc: Prarit Bhargava <prarit@redhat.com> Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
-rw-r--r--tools/testing/selftests/timers/Makefile3
-rw-r--r--tools/testing/selftests/timers/set-tz.c119
2 files changed, 121 insertions, 1 deletions
diff --git a/tools/testing/selftests/timers/Makefile b/tools/testing/selftests/timers/Makefile
index 4a1be1b75a7f..1d5556869137 100644
--- a/tools/testing/selftests/timers/Makefile
+++ b/tools/testing/selftests/timers/Makefile
@@ -10,7 +10,7 @@ TEST_PROGS = posix_timers nanosleep nsleep-lat set-timer-lat mqueue-lat \
10 10
11TEST_PROGS_EXTENDED = alarmtimer-suspend valid-adjtimex adjtick change_skew \ 11TEST_PROGS_EXTENDED = alarmtimer-suspend valid-adjtimex adjtick change_skew \
12 skew_consistency clocksource-switch leap-a-day \ 12 skew_consistency clocksource-switch leap-a-day \
13 leapcrash set-tai set-2038 13 leapcrash set-tai set-2038 set-tz
14 14
15bins = $(TEST_PROGS) $(TEST_PROGS_EXTENDED) 15bins = $(TEST_PROGS) $(TEST_PROGS_EXTENDED)
16 16
@@ -30,6 +30,7 @@ run_destructive_tests: run_tests
30 ./clocksource-switch 30 ./clocksource-switch
31 ./leap-a-day -s -i 10 31 ./leap-a-day -s -i 10
32 ./leapcrash 32 ./leapcrash
33 ./set-tz
33 ./set-tai 34 ./set-tai
34 ./set-2038 35 ./set-2038
35 36
diff --git a/tools/testing/selftests/timers/set-tz.c b/tools/testing/selftests/timers/set-tz.c
new file mode 100644
index 000000000000..f4184928b16b
--- /dev/null
+++ b/tools/testing/selftests/timers/set-tz.c
@@ -0,0 +1,119 @@
1/* Set tz value
2 * by: John Stultz <john.stultz@linaro.org>
3 * (C) Copyright Linaro 2016
4 * Licensed under the GPLv2
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <time.h>
21#include <sys/time.h>
22#include <sys/timex.h>
23#include <string.h>
24#include <signal.h>
25#include <unistd.h>
26#ifdef KTEST
27#include "../kselftest.h"
28#else
29static inline int ksft_exit_pass(void)
30{
31 exit(0);
32}
33static inline int ksft_exit_fail(void)
34{
35 exit(1);
36}
37#endif
38
39int set_tz(int min, int dst)
40{
41 struct timezone tz;
42
43 tz.tz_minuteswest = min;
44 tz.tz_dsttime = dst;
45
46 return settimeofday(0, &tz);
47}
48
49int get_tz_min(void)
50{
51 struct timezone tz;
52 struct timeval tv;
53
54 memset(&tz, 0, sizeof(tz));
55 gettimeofday(&tv, &tz);
56 return tz.tz_minuteswest;
57}
58
59int get_tz_dst(void)
60{
61 struct timezone tz;
62 struct timeval tv;
63
64 memset(&tz, 0, sizeof(tz));
65 gettimeofday(&tv, &tz);
66 return tz.tz_dsttime;
67}
68
69int main(int argc, char **argv)
70{
71 int i, ret;
72 int min, dst;
73
74 min = get_tz_min();
75 dst = get_tz_dst();
76 printf("tz_minuteswest started at %i, dst at %i\n", min, dst);
77
78 printf("Checking tz_minuteswest can be properly set: ");
79 for (i = -15*60; i < 15*60; i += 30) {
80 ret = set_tz(i, dst);
81 ret = get_tz_min();
82 if (ret != i) {
83 printf("[FAILED] expected: %i got %i\n", i, ret);
84 goto err;
85 }
86 }
87 printf("[OK]\n");
88
89 printf("Checking invalid tz_minuteswest values are caught: ");
90
91 if (!set_tz(-15*60-1, dst)) {
92 printf("[FAILED] %i didn't return failure!\n", -15*60-1);
93 goto err;
94 }
95
96 if (!set_tz(15*60+1, dst)) {
97 printf("[FAILED] %i didn't return failure!\n", 15*60+1);
98 goto err;
99 }
100
101 if (!set_tz(-24*60, dst)) {
102 printf("[FAILED] %i didn't return failure!\n", -24*60);
103 goto err;
104 }
105
106 if (!set_tz(24*60, dst)) {
107 printf("[FAILED] %i didn't return failure!\n", 24*60);
108 goto err;
109 }
110
111 printf("[OK]\n");
112
113 set_tz(min, dst);
114 return ksft_exit_pass();
115
116err:
117 set_tz(min, dst);
118 return ksft_exit_fail();
119}