aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/timers
diff options
context:
space:
mode:
authorJohn Stultz <john.stultz@linaro.org>2015-03-11 20:40:06 -0400
committerShuah Khan <shuahkh@osg.samsung.com>2015-03-12 15:22:14 -0400
commitde52133f1892ed2c385655473df90f3d2797fff9 (patch)
tree9c3a9d9b14137880f3f03967e9e9b0678b819701 /tools/testing/selftests/timers
parent274d631e481504e347b491ddb5e18487b855aba5 (diff)
selftests/timers: Add adjtimex validation test from timetest suite
This adds a adjtimex validation test which checks the behavior for a set of valida and invalid inputs. So far this only tests ADJ_FREQUENCY, but hopefully will grow. Cc: Shuah Khan <shuahkh@osg.samsung.com> Cc: Prarit Bhargava <prarit@redhat.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Richard Cochran <richardcochran@gmail.com> Signed-off-by: John Stultz <john.stultz@linaro.org> Tested-by: Prarit Bhargava <prarit@redhat.com> Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
Diffstat (limited to 'tools/testing/selftests/timers')
-rw-r--r--tools/testing/selftests/timers/Makefile11
-rw-r--r--tools/testing/selftests/timers/valid-adjtimex.c202
2 files changed, 212 insertions, 1 deletions
diff --git a/tools/testing/selftests/timers/Makefile b/tools/testing/selftests/timers/Makefile
index d0bfb2d007cf..01b5a2ed343a 100644
--- a/tools/testing/selftests/timers/Makefile
+++ b/tools/testing/selftests/timers/Makefile
@@ -3,10 +3,12 @@ BUILD_FLAGS = -DKTEST
3CFLAGS += -O3 -Wl,-no-as-needed -Wall $(BUILD_FLAGS) 3CFLAGS += -O3 -Wl,-no-as-needed -Wall $(BUILD_FLAGS)
4LDFLAGS += -lrt -lpthread 4LDFLAGS += -lrt -lpthread
5bins = posix_timers nanosleep inconsistency-check nsleep-lat raw_skew \ 5bins = posix_timers nanosleep inconsistency-check nsleep-lat raw_skew \
6 set-timer-lat threadtest mqueue-lat 6 set-timer-lat threadtest mqueue-lat valid-adjtimex
7 7
8all: ${bins} 8all: ${bins}
9 9
10# these are all "safe" tests that don't modify
11# system time or require escalated privledges
10run_tests: all 12run_tests: all
11 ./posix_timers 13 ./posix_timers
12 ./nanosleep 14 ./nanosleep
@@ -16,5 +18,12 @@ run_tests: all
16 ./inconsistency-check 18 ./inconsistency-check
17 ./raw_skew 19 ./raw_skew
18 ./threadtest -t 30 -n 8 20 ./threadtest -t 30 -n 8
21
22# these tests require escalated privledges
23# and may modify the system time or trigger
24# other behavior like suspend
25run_destructive_tests: all
26 ./valid-adjtimex
27
19clean: 28clean:
20 rm -f ${bins} 29 rm -f ${bins}
diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
new file mode 100644
index 000000000000..e86d937cc22c
--- /dev/null
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -0,0 +1,202 @@
1/* valid adjtimex test
2 * by: John Stultz <john.stultz@linaro.org>
3 * (C) Copyright Linaro 2015
4 * Licensed under the GPLv2
5 *
6 * This test validates adjtimex interface with valid
7 * and invalid test data.
8 *
9 * Usage: valid-adjtimex
10 *
11 * To build:
12 * $ gcc valid-adjtimex.c -o valid-adjtimex -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
25
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <time.h>
30#include <sys/time.h>
31#include <sys/timex.h>
32#include <string.h>
33#include <signal.h>
34#include <unistd.h>
35#ifdef KTEST
36#include "../kselftest.h"
37#else
38static inline int ksft_exit_pass(void)
39{
40 exit(0);
41}
42static inline int ksft_exit_fail(void)
43{
44 exit(1);
45}
46#endif
47
48#define NSEC_PER_SEC 1000000000L
49
50/* clear NTP time_status & time_state */
51int clear_time_state(void)
52{
53 struct timex tx;
54 int ret;
55
56 tx.modes = ADJ_STATUS;
57 tx.status = 0;
58 ret = adjtimex(&tx);
59 return ret;
60}
61
62#define NUM_FREQ_VALID 32
63#define NUM_FREQ_OUTOFRANGE 4
64#define NUM_FREQ_INVALID 2
65
66long valid_freq[NUM_FREQ_VALID] = {
67 -499<<16,
68 -450<<16,
69 -400<<16,
70 -350<<16,
71 -300<<16,
72 -250<<16,
73 -200<<16,
74 -150<<16,
75 -100<<16,
76 -75<<16,
77 -50<<16,
78 -25<<16,
79 -10<<16,
80 -5<<16,
81 -1<<16,
82 -1000,
83 1<<16,
84 5<<16,
85 10<<16,
86 25<<16,
87 50<<16,
88 75<<16,
89 100<<16,
90 150<<16,
91 200<<16,
92 250<<16,
93 300<<16,
94 350<<16,
95 400<<16,
96 450<<16,
97 499<<16,
98};
99
100long outofrange_freq[NUM_FREQ_OUTOFRANGE] = {
101 -1000<<16,
102 -550<<16,
103 550<<16,
104 1000<<16,
105};
106
107#define LONG_MAX (~0UL>>1)
108#define LONG_MIN (-LONG_MAX - 1)
109
110long invalid_freq[NUM_FREQ_INVALID] = {
111 LONG_MAX,
112 LONG_MIN,
113};
114
115int validate_freq(void)
116{
117 struct timex tx;
118 int ret, pass = 0;
119 int i;
120
121 clear_time_state();
122
123 memset(&tx, 0, sizeof(struct timex));
124 /* Set the leap second insert flag */
125
126 printf("Testing ADJ_FREQ... ");
127 for (i = 0; i < NUM_FREQ_VALID; i++) {
128 tx.modes = ADJ_FREQUENCY;
129 tx.freq = valid_freq[i];
130
131 ret = adjtimex(&tx);
132 if (ret < 0) {
133 printf("[FAIL]\n");
134 printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
135 valid_freq[i], valid_freq[i]>>16);
136 pass = -1;
137 goto out;
138 }
139 tx.modes = 0;
140 ret = adjtimex(&tx);
141 if (tx.freq != valid_freq[i]) {
142 printf("Warning: freq value %ld not what we set it (%ld)!\n",
143 tx.freq, valid_freq[i]);
144 }
145 }
146 for (i = 0; i < NUM_FREQ_OUTOFRANGE; i++) {
147 tx.modes = ADJ_FREQUENCY;
148 tx.freq = outofrange_freq[i];
149
150 ret = adjtimex(&tx);
151 if (ret < 0) {
152 printf("[FAIL]\n");
153 printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
154 outofrange_freq[i], outofrange_freq[i]>>16);
155 pass = -1;
156 goto out;
157 }
158 tx.modes = 0;
159 ret = adjtimex(&tx);
160 if (tx.freq == outofrange_freq[i]) {
161 printf("[FAIL]\n");
162 printf("ERROR: out of range value %ld actually set!\n",
163 tx.freq);
164 pass = -1;
165 goto out;
166 }
167 }
168
169
170 if (sizeof(long) == 8) { /* this case only applies to 64bit systems */
171 for (i = 0; i < NUM_FREQ_INVALID; i++) {
172 tx.modes = ADJ_FREQUENCY;
173 tx.freq = invalid_freq[i];
174 ret = adjtimex(&tx);
175 if (ret >= 0) {
176 printf("[FAIL]\n");
177 printf("Error: No failure on invalid ADJ_FREQUENCY %ld\n",
178 invalid_freq[i]);
179 pass = -1;
180 goto out;
181 }
182 }
183 }
184
185 printf("[OK]\n");
186out:
187 /* reset freq to zero */
188 tx.modes = ADJ_FREQUENCY;
189 tx.freq = 0;
190 ret = adjtimex(&tx);
191
192 return pass;
193}
194
195
196int main(int argc, char **argv)
197{
198 if (validate_freq())
199 return ksft_exit_fail();
200
201 return ksft_exit_pass();
202}