• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "../kselftest.h"
36 
37 #define NSEC_PER_SEC 1000000000LL
38 #define USEC_PER_SEC 1000000LL
39 
40 #define ADJ_SETOFFSET 0x0100
41 
42 #ifndef __ANDROID__
43 #include <sys/syscall.h>
clock_adjtime(clockid_t id,struct timex * tx)44 static int clock_adjtime(clockid_t id, struct timex *tx)
45 {
46     return syscall(__NR_clock_adjtime, id, tx);
47 }
48 #endif
49 
50 /* clear NTP time_status & time_state */
clear_time_state(void)51 int 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 
66 long 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 
100 long 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 
110 long invalid_freq[NUM_FREQ_INVALID] = {
111 	LONG_MAX,
112 	LONG_MIN,
113 };
114 
validate_freq(void)115 int 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 	fflush(stdout);
128 	for (i = 0; i < NUM_FREQ_VALID; i++) {
129 		tx.modes = ADJ_FREQUENCY;
130 		tx.freq = valid_freq[i];
131 
132 		ret = adjtimex(&tx);
133 		if (ret < 0) {
134 			printf("[FAIL]\n");
135 			printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
136 				valid_freq[i], valid_freq[i]>>16);
137 			pass = -1;
138 			goto out;
139 		}
140 		tx.modes = 0;
141 		ret = adjtimex(&tx);
142 		if (tx.freq != valid_freq[i]) {
143 			printf("Warning: freq value %ld not what we set it (%ld)!\n",
144 					tx.freq, valid_freq[i]);
145 		}
146 	}
147 	for (i = 0; i < NUM_FREQ_OUTOFRANGE; i++) {
148 		tx.modes = ADJ_FREQUENCY;
149 		tx.freq = outofrange_freq[i];
150 
151 		ret = adjtimex(&tx);
152 		if (ret < 0) {
153 			printf("[FAIL]\n");
154 			printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
155 				outofrange_freq[i], outofrange_freq[i]>>16);
156 			pass = -1;
157 			goto out;
158 		}
159 		tx.modes = 0;
160 		ret = adjtimex(&tx);
161 		if (tx.freq == outofrange_freq[i]) {
162 			printf("[FAIL]\n");
163 			printf("ERROR: out of range value %ld actually set!\n",
164 					tx.freq);
165 			pass = -1;
166 			goto out;
167 		}
168 	}
169 
170 
171 	if (sizeof(long) == 8) { /* this case only applies to 64bit systems */
172 		for (i = 0; i < NUM_FREQ_INVALID; i++) {
173 			tx.modes = ADJ_FREQUENCY;
174 			tx.freq = invalid_freq[i];
175 			ret = adjtimex(&tx);
176 			if (ret >= 0) {
177 				printf("[FAIL]\n");
178 				printf("Error: No failure on invalid ADJ_FREQUENCY %ld\n",
179 					invalid_freq[i]);
180 				pass = -1;
181 				goto out;
182 			}
183 		}
184 	}
185 
186 	printf("[OK]\n");
187 out:
188 	/* reset freq to zero */
189 	tx.modes = ADJ_FREQUENCY;
190 	tx.freq = 0;
191 	ret = adjtimex(&tx);
192 
193 	return pass;
194 }
195 
196 
set_offset(long long offset,int use_nano)197 int set_offset(long long offset, int use_nano)
198 {
199 	struct timex tmx = {};
200 	int ret;
201 
202 	tmx.modes = ADJ_SETOFFSET;
203 	if (use_nano) {
204 		tmx.modes |= ADJ_NANO;
205 
206 		tmx.time.tv_sec = offset / NSEC_PER_SEC;
207 		tmx.time.tv_usec = offset % NSEC_PER_SEC;
208 
209 		if (offset < 0 && tmx.time.tv_usec) {
210 			tmx.time.tv_sec -= 1;
211 			tmx.time.tv_usec += NSEC_PER_SEC;
212 		}
213 	} else {
214 		tmx.time.tv_sec = offset / USEC_PER_SEC;
215 		tmx.time.tv_usec = offset % USEC_PER_SEC;
216 
217 		if (offset < 0 && tmx.time.tv_usec) {
218 			tmx.time.tv_sec -= 1;
219 			tmx.time.tv_usec += USEC_PER_SEC;
220 		}
221 	}
222 
223 	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
224 	if (ret < 0) {
225 		printf("(sec: %ld  usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec);
226 		printf("[FAIL]\n");
227 		return -1;
228 	}
229 	return 0;
230 }
231 
set_bad_offset(long sec,long usec,int use_nano)232 int set_bad_offset(long sec, long usec, int use_nano)
233 {
234 	struct timex tmx = {};
235 	int ret;
236 
237 	tmx.modes = ADJ_SETOFFSET;
238 	if (use_nano)
239 		tmx.modes |= ADJ_NANO;
240 
241 	tmx.time.tv_sec = sec;
242 	tmx.time.tv_usec = usec;
243 	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
244 	if (ret >= 0) {
245 		printf("Invalid (sec: %ld  usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec);
246 		printf("[FAIL]\n");
247 		return -1;
248 	}
249 	return 0;
250 }
251 
validate_set_offset(void)252 int validate_set_offset(void)
253 {
254 	printf("Testing ADJ_SETOFFSET... ");
255 	fflush(stdout);
256 
257 	/* Test valid values */
258 	if (set_offset(NSEC_PER_SEC - 1, 1))
259 		return -1;
260 
261 	if (set_offset(-NSEC_PER_SEC + 1, 1))
262 		return -1;
263 
264 	if (set_offset(-NSEC_PER_SEC - 1, 1))
265 		return -1;
266 
267 	if (set_offset(5 * NSEC_PER_SEC, 1))
268 		return -1;
269 
270 	if (set_offset(-5 * NSEC_PER_SEC, 1))
271 		return -1;
272 
273 	if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
274 		return -1;
275 
276 	if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
277 		return -1;
278 
279 	if (set_offset(USEC_PER_SEC - 1, 0))
280 		return -1;
281 
282 	if (set_offset(-USEC_PER_SEC + 1, 0))
283 		return -1;
284 
285 	if (set_offset(-USEC_PER_SEC - 1, 0))
286 		return -1;
287 
288 	if (set_offset(5 * USEC_PER_SEC, 0))
289 		return -1;
290 
291 	if (set_offset(-5 * USEC_PER_SEC, 0))
292 		return -1;
293 
294 	if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
295 		return -1;
296 
297 	if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
298 		return -1;
299 
300 	/* Test invalid values */
301 	if (set_bad_offset(0, -1, 1))
302 		return -1;
303 	if (set_bad_offset(0, -1, 0))
304 		return -1;
305 	if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
306 		return -1;
307 	if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
308 		return -1;
309 	if (set_bad_offset(0, NSEC_PER_SEC, 1))
310 		return -1;
311 	if (set_bad_offset(0, USEC_PER_SEC, 0))
312 		return -1;
313 	if (set_bad_offset(0, -NSEC_PER_SEC, 1))
314 		return -1;
315 	if (set_bad_offset(0, -USEC_PER_SEC, 0))
316 		return -1;
317 
318 	printf("[OK]\n");
319 	return 0;
320 }
321 
main(int argc,char ** argv)322 int main(int argc, char **argv)
323 {
324 	if (validate_freq())
325 		return ksft_exit_fail();
326 
327 	if (validate_set_offset())
328 		return ksft_exit_fail();
329 
330 	return ksft_exit_pass();
331 }
332