aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorJohn Stultz <john.stultz@linaro.org>2016-01-21 18:03:35 -0500
committerThomas Gleixner <tglx@linutronix.de>2016-01-26 10:26:06 -0500
commite03a58c320e1103ebe97bda8ebdfcc5c9829c53f (patch)
treee6b738bb9b4d9358fe85718cd882ebf675168031 /tools
parentdd4e17ab704269bce71402285f5e8b9ac24b1eff (diff)
kselftests: timers: Add adjtimex SETOFFSET validity tests
Add some simple tests to check both valid and invalid offsets when using adjtimex's ADJ_SETOFFSET method. Signed-off-by: John Stultz <john.stultz@linaro.org> Acked-by: Shuah Khan <shuahkh@osg.samsung.com> Cc: Sasha Levin <sasha.levin@oracle.com> Cc: Richard Cochran <richardcochran@gmail.com> Cc: Prarit Bhargava <prarit@redhat.com> Cc: Harald Hoyer <harald@redhat.com> Cc: Kay Sievers <kay@vrfy.org> Cc: David Herrmann <dh.herrmann@gmail.com> Link: http://lkml.kernel.org/r/1453417415-19110-3-git-send-email-john.stultz@linaro.org Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'tools')
-rw-r--r--tools/testing/selftests/timers/valid-adjtimex.c139
1 files changed, 138 insertions, 1 deletions
diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
index e86d937cc22c..60fe3c569bd9 100644
--- a/tools/testing/selftests/timers/valid-adjtimex.c
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -45,7 +45,17 @@ static inline int ksft_exit_fail(void)
45} 45}
46#endif 46#endif
47 47
48#define NSEC_PER_SEC 1000000000L 48#define NSEC_PER_SEC 1000000000LL
49#define USEC_PER_SEC 1000000LL
50
51#define ADJ_SETOFFSET 0x0100
52
53#include <sys/syscall.h>
54static int clock_adjtime(clockid_t id, struct timex *tx)
55{
56 return syscall(__NR_clock_adjtime, id, tx);
57}
58
49 59
50/* clear NTP time_status & time_state */ 60/* clear NTP time_status & time_state */
51int clear_time_state(void) 61int clear_time_state(void)
@@ -193,10 +203,137 @@ out:
193} 203}
194 204
195 205
206int set_offset(long long offset, int use_nano)
207{
208 struct timex tmx = {};
209 int ret;
210
211 tmx.modes = ADJ_SETOFFSET;
212 if (use_nano) {
213 tmx.modes |= ADJ_NANO;
214
215 tmx.time.tv_sec = offset / NSEC_PER_SEC;
216 tmx.time.tv_usec = offset % NSEC_PER_SEC;
217
218 if (offset < 0 && tmx.time.tv_usec) {
219 tmx.time.tv_sec -= 1;
220 tmx.time.tv_usec += NSEC_PER_SEC;
221 }
222 } else {
223 tmx.time.tv_sec = offset / USEC_PER_SEC;
224 tmx.time.tv_usec = offset % USEC_PER_SEC;
225
226 if (offset < 0 && tmx.time.tv_usec) {
227 tmx.time.tv_sec -= 1;
228 tmx.time.tv_usec += USEC_PER_SEC;
229 }
230 }
231
232 ret = clock_adjtime(CLOCK_REALTIME, &tmx);
233 if (ret < 0) {
234 printf("(sec: %ld usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec);
235 printf("[FAIL]\n");
236 return -1;
237 }
238 return 0;
239}
240
241int set_bad_offset(long sec, long usec, int use_nano)
242{
243 struct timex tmx = {};
244 int ret;
245
246 tmx.modes = ADJ_SETOFFSET;
247 if (use_nano)
248 tmx.modes |= ADJ_NANO;
249
250 tmx.time.tv_sec = sec;
251 tmx.time.tv_usec = usec;
252 ret = clock_adjtime(CLOCK_REALTIME, &tmx);
253 if (ret >= 0) {
254 printf("Invalid (sec: %ld usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec);
255 printf("[FAIL]\n");
256 return -1;
257 }
258 return 0;
259}
260
261int validate_set_offset(void)
262{
263 printf("Testing ADJ_SETOFFSET... ");
264
265 /* Test valid values */
266 if (set_offset(NSEC_PER_SEC - 1, 1))
267 return -1;
268
269 if (set_offset(-NSEC_PER_SEC + 1, 1))
270 return -1;
271
272 if (set_offset(-NSEC_PER_SEC - 1, 1))
273 return -1;
274
275 if (set_offset(5 * NSEC_PER_SEC, 1))
276 return -1;
277
278 if (set_offset(-5 * NSEC_PER_SEC, 1))
279 return -1;
280
281 if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
282 return -1;
283
284 if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
285 return -1;
286
287 if (set_offset(USEC_PER_SEC - 1, 0))
288 return -1;
289
290 if (set_offset(-USEC_PER_SEC + 1, 0))
291 return -1;
292
293 if (set_offset(-USEC_PER_SEC - 1, 0))
294 return -1;
295
296 if (set_offset(5 * USEC_PER_SEC, 0))
297 return -1;
298
299 if (set_offset(-5 * USEC_PER_SEC, 0))
300 return -1;
301
302 if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
303 return -1;
304
305 if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
306 return -1;
307
308 /* Test invalid values */
309 if (set_bad_offset(0, -1, 1))
310 return -1;
311 if (set_bad_offset(0, -1, 0))
312 return -1;
313 if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
314 return -1;
315 if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
316 return -1;
317 if (set_bad_offset(0, NSEC_PER_SEC, 1))
318 return -1;
319 if (set_bad_offset(0, USEC_PER_SEC, 0))
320 return -1;
321 if (set_bad_offset(0, -NSEC_PER_SEC, 1))
322 return -1;
323 if (set_bad_offset(0, -USEC_PER_SEC, 0))
324 return -1;
325
326 printf("[OK]\n");
327 return 0;
328}
329
196int main(int argc, char **argv) 330int main(int argc, char **argv)
197{ 331{
198 if (validate_freq()) 332 if (validate_freq())
199 return ksft_exit_fail(); 333 return ksft_exit_fail();
200 334
335 if (validate_set_offset())
336 return ksft_exit_fail();
337
201 return ksft_exit_pass(); 338 return ksft_exit_pass();
202} 339}