2 * by: John Stultz <john.stultz@linaro.org>
3 * (C) Copyright Linaro 2015
4 * Licensed under the GPLv2
6 * This test validates adjtimex interface with valid
7 * and invalid test data.
9 * Usage: valid-adjtimex
12 * $ gcc valid-adjtimex.c -o valid-adjtimex -lrt
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.
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.
31 #include <sys/timex.h>
35 #include "../kselftest.h"
37 #define NSEC_PER_SEC 1000000000LL
38 #define USEC_PER_SEC 1000000LL
40 #define ADJ_SETOFFSET 0x0100
42 #include <sys/syscall.h>
43 static int clock_adjtime(clockid_t id
, struct timex
*tx
)
45 return syscall(__NR_clock_adjtime
, id
, tx
);
49 /* clear NTP time_status & time_state */
50 int clear_time_state(void)
55 tx
.modes
= ADJ_STATUS
;
61 #define NUM_FREQ_VALID 32
62 #define NUM_FREQ_OUTOFRANGE 4
63 #define NUM_FREQ_INVALID 2
65 long valid_freq
[NUM_FREQ_VALID
] = {
99 long outofrange_freq
[NUM_FREQ_OUTOFRANGE
] = {
106 #define LONG_MAX (~0UL>>1)
107 #define LONG_MIN (-LONG_MAX - 1)
109 long invalid_freq
[NUM_FREQ_INVALID
] = {
114 int validate_freq(void)
122 memset(&tx
, 0, sizeof(struct timex
));
123 /* Set the leap second insert flag */
125 printf("Testing ADJ_FREQ... ");
127 for (i
= 0; i
< NUM_FREQ_VALID
; i
++) {
128 tx
.modes
= ADJ_FREQUENCY
;
129 tx
.freq
= valid_freq
[i
];
134 printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
135 valid_freq
[i
], valid_freq
[i
]>>16);
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
]);
146 for (i
= 0; i
< NUM_FREQ_OUTOFRANGE
; i
++) {
147 tx
.modes
= ADJ_FREQUENCY
;
148 tx
.freq
= outofrange_freq
[i
];
153 printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
154 outofrange_freq
[i
], outofrange_freq
[i
]>>16);
160 if (tx
.freq
== outofrange_freq
[i
]) {
162 printf("ERROR: out of range value %ld actually set!\n",
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
];
177 printf("Error: No failure on invalid ADJ_FREQUENCY %ld\n",
187 /* reset freq to zero */
188 tx
.modes
= ADJ_FREQUENCY
;
196 int set_offset(long long offset
, int use_nano
)
198 struct timex tmx
= {};
201 tmx
.modes
= ADJ_SETOFFSET
;
203 tmx
.modes
|= ADJ_NANO
;
205 tmx
.time
.tv_sec
= offset
/ NSEC_PER_SEC
;
206 tmx
.time
.tv_usec
= offset
% NSEC_PER_SEC
;
208 if (offset
< 0 && tmx
.time
.tv_usec
) {
209 tmx
.time
.tv_sec
-= 1;
210 tmx
.time
.tv_usec
+= NSEC_PER_SEC
;
213 tmx
.time
.tv_sec
= offset
/ USEC_PER_SEC
;
214 tmx
.time
.tv_usec
= offset
% USEC_PER_SEC
;
216 if (offset
< 0 && tmx
.time
.tv_usec
) {
217 tmx
.time
.tv_sec
-= 1;
218 tmx
.time
.tv_usec
+= USEC_PER_SEC
;
222 ret
= clock_adjtime(CLOCK_REALTIME
, &tmx
);
224 printf("(sec: %ld usec: %ld) ", tmx
.time
.tv_sec
, tmx
.time
.tv_usec
);
231 int set_bad_offset(long sec
, long usec
, int use_nano
)
233 struct timex tmx
= {};
236 tmx
.modes
= ADJ_SETOFFSET
;
238 tmx
.modes
|= ADJ_NANO
;
240 tmx
.time
.tv_sec
= sec
;
241 tmx
.time
.tv_usec
= usec
;
242 ret
= clock_adjtime(CLOCK_REALTIME
, &tmx
);
244 printf("Invalid (sec: %ld usec: %ld) did not fail! ", tmx
.time
.tv_sec
, tmx
.time
.tv_usec
);
251 int validate_set_offset(void)
253 printf("Testing ADJ_SETOFFSET... ");
256 /* Test valid values */
257 if (set_offset(NSEC_PER_SEC
- 1, 1))
260 if (set_offset(-NSEC_PER_SEC
+ 1, 1))
263 if (set_offset(-NSEC_PER_SEC
- 1, 1))
266 if (set_offset(5 * NSEC_PER_SEC
, 1))
269 if (set_offset(-5 * NSEC_PER_SEC
, 1))
272 if (set_offset(5 * NSEC_PER_SEC
+ NSEC_PER_SEC
/ 2, 1))
275 if (set_offset(-5 * NSEC_PER_SEC
- NSEC_PER_SEC
/ 2, 1))
278 if (set_offset(USEC_PER_SEC
- 1, 0))
281 if (set_offset(-USEC_PER_SEC
+ 1, 0))
284 if (set_offset(-USEC_PER_SEC
- 1, 0))
287 if (set_offset(5 * USEC_PER_SEC
, 0))
290 if (set_offset(-5 * USEC_PER_SEC
, 0))
293 if (set_offset(5 * USEC_PER_SEC
+ USEC_PER_SEC
/ 2, 0))
296 if (set_offset(-5 * USEC_PER_SEC
- USEC_PER_SEC
/ 2, 0))
299 /* Test invalid values */
300 if (set_bad_offset(0, -1, 1))
302 if (set_bad_offset(0, -1, 0))
304 if (set_bad_offset(0, 2 * NSEC_PER_SEC
, 1))
306 if (set_bad_offset(0, 2 * USEC_PER_SEC
, 0))
308 if (set_bad_offset(0, NSEC_PER_SEC
, 1))
310 if (set_bad_offset(0, USEC_PER_SEC
, 0))
312 if (set_bad_offset(0, -NSEC_PER_SEC
, 1))
314 if (set_bad_offset(0, -USEC_PER_SEC
, 0))
321 int main(int argc
, char **argv
)
324 return ksft_exit_fail();
326 if (validate_set_offset())
327 return ksft_exit_fail();
329 return ksft_exit_pass();