valid-adjtimex.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <time.h>
  27. #include <sys/time.h>
  28. #include <sys/timex.h>
  29. #include <string.h>
  30. #include <signal.h>
  31. #include <unistd.h>
  32. #ifdef KTEST
  33. #include "../kselftest.h"
  34. #else
  35. static inline int ksft_exit_pass(void)
  36. {
  37. exit(0);
  38. }
  39. static inline int ksft_exit_fail(void)
  40. {
  41. exit(1);
  42. }
  43. #endif
  44. #define NSEC_PER_SEC 1000000000L
  45. /* clear NTP time_status & time_state */
  46. int clear_time_state(void)
  47. {
  48. struct timex tx;
  49. int ret;
  50. tx.modes = ADJ_STATUS;
  51. tx.status = 0;
  52. ret = adjtimex(&tx);
  53. return ret;
  54. }
  55. #define NUM_FREQ_VALID 32
  56. #define NUM_FREQ_OUTOFRANGE 4
  57. #define NUM_FREQ_INVALID 2
  58. long valid_freq[NUM_FREQ_VALID] = {
  59. -499<<16,
  60. -450<<16,
  61. -400<<16,
  62. -350<<16,
  63. -300<<16,
  64. -250<<16,
  65. -200<<16,
  66. -150<<16,
  67. -100<<16,
  68. -75<<16,
  69. -50<<16,
  70. -25<<16,
  71. -10<<16,
  72. -5<<16,
  73. -1<<16,
  74. -1000,
  75. 1<<16,
  76. 5<<16,
  77. 10<<16,
  78. 25<<16,
  79. 50<<16,
  80. 75<<16,
  81. 100<<16,
  82. 150<<16,
  83. 200<<16,
  84. 250<<16,
  85. 300<<16,
  86. 350<<16,
  87. 400<<16,
  88. 450<<16,
  89. 499<<16,
  90. };
  91. long outofrange_freq[NUM_FREQ_OUTOFRANGE] = {
  92. -1000<<16,
  93. -550<<16,
  94. 550<<16,
  95. 1000<<16,
  96. };
  97. #define LONG_MAX (~0UL>>1)
  98. #define LONG_MIN (-LONG_MAX - 1)
  99. long invalid_freq[NUM_FREQ_INVALID] = {
  100. LONG_MAX,
  101. LONG_MIN,
  102. };
  103. int validate_freq(void)
  104. {
  105. struct timex tx;
  106. int ret, pass = 0;
  107. int i;
  108. clear_time_state();
  109. memset(&tx, 0, sizeof(struct timex));
  110. /* Set the leap second insert flag */
  111. printf("Testing ADJ_FREQ... ");
  112. for (i = 0; i < NUM_FREQ_VALID; i++) {
  113. tx.modes = ADJ_FREQUENCY;
  114. tx.freq = valid_freq[i];
  115. ret = adjtimex(&tx);
  116. if (ret < 0) {
  117. printf("[FAIL]\n");
  118. printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
  119. valid_freq[i], valid_freq[i]>>16);
  120. pass = -1;
  121. goto out;
  122. }
  123. tx.modes = 0;
  124. ret = adjtimex(&tx);
  125. if (tx.freq != valid_freq[i]) {
  126. printf("Warning: freq value %ld not what we set it (%ld)!\n",
  127. tx.freq, valid_freq[i]);
  128. }
  129. }
  130. for (i = 0; i < NUM_FREQ_OUTOFRANGE; i++) {
  131. tx.modes = ADJ_FREQUENCY;
  132. tx.freq = outofrange_freq[i];
  133. ret = adjtimex(&tx);
  134. if (ret < 0) {
  135. printf("[FAIL]\n");
  136. printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
  137. outofrange_freq[i], outofrange_freq[i]>>16);
  138. pass = -1;
  139. goto out;
  140. }
  141. tx.modes = 0;
  142. ret = adjtimex(&tx);
  143. if (tx.freq == outofrange_freq[i]) {
  144. printf("[FAIL]\n");
  145. printf("ERROR: out of range value %ld actually set!\n",
  146. tx.freq);
  147. pass = -1;
  148. goto out;
  149. }
  150. }
  151. if (sizeof(long) == 8) { /* this case only applies to 64bit systems */
  152. for (i = 0; i < NUM_FREQ_INVALID; i++) {
  153. tx.modes = ADJ_FREQUENCY;
  154. tx.freq = invalid_freq[i];
  155. ret = adjtimex(&tx);
  156. if (ret >= 0) {
  157. printf("[FAIL]\n");
  158. printf("Error: No failure on invalid ADJ_FREQUENCY %ld\n",
  159. invalid_freq[i]);
  160. pass = -1;
  161. goto out;
  162. }
  163. }
  164. }
  165. printf("[OK]\n");
  166. out:
  167. /* reset freq to zero */
  168. tx.modes = ADJ_FREQUENCY;
  169. tx.freq = 0;
  170. ret = adjtimex(&tx);
  171. return pass;
  172. }
  173. int main(int argc, char **argv)
  174. {
  175. if (validate_freq())
  176. return ksft_exit_fail();
  177. return ksft_exit_pass();
  178. }