[tip:timers/urgent] kselftests: timers: Add adjtimex SETOFFSET validity tests

2016-01-26 Thread tip-bot for John Stultz
Commit-ID:  e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Gitweb: http://git.kernel.org/tip/e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Author: John Stultz 
AuthorDate: Thu, 21 Jan 2016 15:03:35 -0800
Committer:  Thomas Gleixner 
CommitDate: Tue, 26 Jan 2016 16:26:06 +0100

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 
Acked-by: Shuah Khan 
Cc: Sasha Levin 
Cc: Richard Cochran 
Cc: Prarit Bhargava 
Cc: Harald Hoyer 
Cc: Kay Sievers 
Cc: David Herrmann 
Link: 
http://lkml.kernel.org/r/1453417415-19110-3-git-send-email-john.stu...@linaro.org
Signed-off-by: Thomas Gleixner 
---
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++-
 1 file changed, 138 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/timers/valid-adjtimex.c 
b/tools/testing/selftests/timers/valid-adjtimex.c
index e86d937..60fe3c5 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)
 }
 #endif
 
-#define NSEC_PER_SEC 10L
+#define NSEC_PER_SEC 10LL
+#define USEC_PER_SEC 100LL
+
+#define ADJ_SETOFFSET 0x0100
+
+#include 
+static int clock_adjtime(clockid_t id, struct timex *tx)
+{
+   return syscall(__NR_clock_adjtime, id, tx);
+}
+
 
 /* clear NTP time_status & time_state */
 int clear_time_state(void)
@@ -193,10 +203,137 @@ out:
 }
 
 
+int set_offset(long long offset, int use_nano)
+{
+   struct timex tmx = {};
+   int ret;
+
+   tmx.modes = ADJ_SETOFFSET;
+   if (use_nano) {
+   tmx.modes |= ADJ_NANO;
+
+   tmx.time.tv_sec = offset / NSEC_PER_SEC;
+   tmx.time.tv_usec = offset % NSEC_PER_SEC;
+
+   if (offset < 0 && tmx.time.tv_usec) {
+   tmx.time.tv_sec -= 1;
+   tmx.time.tv_usec += NSEC_PER_SEC;
+   }
+   } else {
+   tmx.time.tv_sec = offset / USEC_PER_SEC;
+   tmx.time.tv_usec = offset % USEC_PER_SEC;
+
+   if (offset < 0 && tmx.time.tv_usec) {
+   tmx.time.tv_sec -= 1;
+   tmx.time.tv_usec += USEC_PER_SEC;
+   }
+   }
+
+   ret = clock_adjtime(CLOCK_REALTIME, );
+   if (ret < 0) {
+   printf("(sec: %ld  usec: %ld) ", tmx.time.tv_sec, 
tmx.time.tv_usec);
+   printf("[FAIL]\n");
+   return -1;
+   }
+   return 0;
+}
+
+int set_bad_offset(long sec, long usec, int use_nano)
+{
+   struct timex tmx = {};
+   int ret;
+
+   tmx.modes = ADJ_SETOFFSET;
+   if (use_nano)
+   tmx.modes |= ADJ_NANO;
+
+   tmx.time.tv_sec = sec;
+   tmx.time.tv_usec = usec;
+   ret = clock_adjtime(CLOCK_REALTIME, );
+   if (ret >= 0) {
+   printf("Invalid (sec: %ld  usec: %ld) did not fail! ", 
tmx.time.tv_sec, tmx.time.tv_usec);
+   printf("[FAIL]\n");
+   return -1;
+   }
+   return 0;
+}
+
+int validate_set_offset(void)
+{
+   printf("Testing ADJ_SETOFFSET... ");
+
+   /* Test valid values */
+   if (set_offset(NSEC_PER_SEC - 1, 1))
+   return -1;
+
+   if (set_offset(-NSEC_PER_SEC + 1, 1))
+   return -1;
+
+   if (set_offset(-NSEC_PER_SEC - 1, 1))
+   return -1;
+
+   if (set_offset(5 * NSEC_PER_SEC, 1))
+   return -1;
+
+   if (set_offset(-5 * NSEC_PER_SEC, 1))
+   return -1;
+
+   if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+   return -1;
+
+   if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+   return -1;
+
+   if (set_offset(USEC_PER_SEC - 1, 0))
+   return -1;
+
+   if (set_offset(-USEC_PER_SEC + 1, 0))
+   return -1;
+
+   if (set_offset(-USEC_PER_SEC - 1, 0))
+   return -1;
+
+   if (set_offset(5 * USEC_PER_SEC, 0))
+   return -1;
+
+   if (set_offset(-5 * USEC_PER_SEC, 0))
+   return -1;
+
+   if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
+   return -1;
+
+   if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
+   return -1;
+
+   /* Test invalid values */
+   if (set_bad_offset(0, -1, 1))
+   return -1;
+   if (set_bad_offset(0, -1, 0))
+   return -1;
+   if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
+   return -1;
+   if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
+   return -1;
+   if (set_bad_offset(0, NSEC_PER_SEC, 1))
+   return -1;
+   if (set_bad_offset(0, USEC_PER_SEC, 0))
+   return -1;
+   if (set_bad_offset(0, -NSEC_PER_SEC, 1))
+   return -1;
+   if (set_bad_offset(0, -USEC_PER_SEC, 0))
+  

[tip:timers/urgent] kselftests: timers: Add adjtimex SETOFFSET validity tests

2016-01-26 Thread tip-bot for John Stultz
Commit-ID:  e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Gitweb: http://git.kernel.org/tip/e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Author: John Stultz 
AuthorDate: Thu, 21 Jan 2016 15:03:35 -0800
Committer:  Thomas Gleixner 
CommitDate: Tue, 26 Jan 2016 16:26:06 +0100

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 
Acked-by: Shuah Khan 
Cc: Sasha Levin 
Cc: Richard Cochran 
Cc: Prarit Bhargava 
Cc: Harald Hoyer 
Cc: Kay Sievers 
Cc: David Herrmann 
Link: 
http://lkml.kernel.org/r/1453417415-19110-3-git-send-email-john.stu...@linaro.org
Signed-off-by: Thomas Gleixner 
---
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++-
 1 file changed, 138 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/timers/valid-adjtimex.c 
b/tools/testing/selftests/timers/valid-adjtimex.c
index e86d937..60fe3c5 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)
 }
 #endif
 
-#define NSEC_PER_SEC 10L
+#define NSEC_PER_SEC 10LL
+#define USEC_PER_SEC 100LL
+
+#define ADJ_SETOFFSET 0x0100
+
+#include 
+static int clock_adjtime(clockid_t id, struct timex *tx)
+{
+   return syscall(__NR_clock_adjtime, id, tx);
+}
+
 
 /* clear NTP time_status & time_state */
 int clear_time_state(void)
@@ -193,10 +203,137 @@ out:
 }
 
 
+int set_offset(long long offset, int use_nano)
+{
+   struct timex tmx = {};
+   int ret;
+
+   tmx.modes = ADJ_SETOFFSET;
+   if (use_nano) {
+   tmx.modes |= ADJ_NANO;
+
+   tmx.time.tv_sec = offset / NSEC_PER_SEC;
+   tmx.time.tv_usec = offset % NSEC_PER_SEC;
+
+   if (offset < 0 && tmx.time.tv_usec) {
+   tmx.time.tv_sec -= 1;
+   tmx.time.tv_usec += NSEC_PER_SEC;
+   }
+   } else {
+   tmx.time.tv_sec = offset / USEC_PER_SEC;
+   tmx.time.tv_usec = offset % USEC_PER_SEC;
+
+   if (offset < 0 && tmx.time.tv_usec) {
+   tmx.time.tv_sec -= 1;
+   tmx.time.tv_usec += USEC_PER_SEC;
+   }
+   }
+
+   ret = clock_adjtime(CLOCK_REALTIME, );
+   if (ret < 0) {
+   printf("(sec: %ld  usec: %ld) ", tmx.time.tv_sec, 
tmx.time.tv_usec);
+   printf("[FAIL]\n");
+   return -1;
+   }
+   return 0;
+}
+
+int set_bad_offset(long sec, long usec, int use_nano)
+{
+   struct timex tmx = {};
+   int ret;
+
+   tmx.modes = ADJ_SETOFFSET;
+   if (use_nano)
+   tmx.modes |= ADJ_NANO;
+
+   tmx.time.tv_sec = sec;
+   tmx.time.tv_usec = usec;
+   ret = clock_adjtime(CLOCK_REALTIME, );
+   if (ret >= 0) {
+   printf("Invalid (sec: %ld  usec: %ld) did not fail! ", 
tmx.time.tv_sec, tmx.time.tv_usec);
+   printf("[FAIL]\n");
+   return -1;
+   }
+   return 0;
+}
+
+int validate_set_offset(void)
+{
+   printf("Testing ADJ_SETOFFSET... ");
+
+   /* Test valid values */
+   if (set_offset(NSEC_PER_SEC - 1, 1))
+   return -1;
+
+   if (set_offset(-NSEC_PER_SEC + 1, 1))
+   return -1;
+
+   if (set_offset(-NSEC_PER_SEC - 1, 1))
+   return -1;
+
+   if (set_offset(5 * NSEC_PER_SEC, 1))
+   return -1;
+
+   if (set_offset(-5 * NSEC_PER_SEC, 1))
+   return -1;
+
+   if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+   return -1;
+
+   if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+   return -1;
+
+   if (set_offset(USEC_PER_SEC - 1, 0))
+   return -1;
+
+   if (set_offset(-USEC_PER_SEC + 1, 0))
+   return -1;
+
+   if (set_offset(-USEC_PER_SEC - 1, 0))
+   return -1;
+
+   if (set_offset(5 * USEC_PER_SEC, 0))
+   return -1;
+
+   if (set_offset(-5 * USEC_PER_SEC, 0))
+   return -1;
+
+   if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
+   return -1;
+
+   if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
+   return -1;
+
+   /* Test invalid values */
+   if (set_bad_offset(0, -1, 1))
+   return -1;
+   if (set_bad_offset(0, -1, 0))
+   return -1;
+   if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
+   return -1;
+   if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
+   return -1;
+   if (set_bad_offset(0,