valid-adjtimex.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include "../kselftest.h"
  33. #define NSEC_PER_SEC 1000000000LL
  34. #define USEC_PER_SEC 1000000LL
  35. #define ADJ_SETOFFSET 0x0100
  36. #include <sys/syscall.h>
  37. int clock_adjtime(clockid_t id, struct timex *tx)
  38. {
  39. return syscall(__NR_clock_adjtime, id, tx);
  40. }
  41. /* clear NTP time_status & time_state */
  42. int clear_time_state(void)
  43. {
  44. struct timex tx;
  45. int ret;
  46. tx.modes = ADJ_STATUS;
  47. tx.status = 0;
  48. ret = adjtimex(&tx);
  49. return ret;
  50. }
  51. #define NUM_FREQ_VALID 32
  52. #define NUM_FREQ_OUTOFRANGE 4
  53. #define NUM_FREQ_INVALID 2
  54. #define SHIFTED_PPM (1 << 16)
  55. long valid_freq[NUM_FREQ_VALID] = {
  56. -499 * SHIFTED_PPM,
  57. -450 * SHIFTED_PPM,
  58. -400 * SHIFTED_PPM,
  59. -350 * SHIFTED_PPM,
  60. -300 * SHIFTED_PPM,
  61. -250 * SHIFTED_PPM,
  62. -200 * SHIFTED_PPM,
  63. -150 * SHIFTED_PPM,
  64. -100 * SHIFTED_PPM,
  65. -75 * SHIFTED_PPM,
  66. -50 * SHIFTED_PPM,
  67. -25 * SHIFTED_PPM,
  68. -10 * SHIFTED_PPM,
  69. -5 * SHIFTED_PPM,
  70. -1 * SHIFTED_PPM,
  71. -1000,
  72. 1 * SHIFTED_PPM,
  73. 5 * SHIFTED_PPM,
  74. 10 * SHIFTED_PPM,
  75. 25 * SHIFTED_PPM,
  76. 50 * SHIFTED_PPM,
  77. 75 * SHIFTED_PPM,
  78. 100 * SHIFTED_PPM,
  79. 150 * SHIFTED_PPM,
  80. 200 * SHIFTED_PPM,
  81. 250 * SHIFTED_PPM,
  82. 300 * SHIFTED_PPM,
  83. 350 * SHIFTED_PPM,
  84. 400 * SHIFTED_PPM,
  85. 450 * SHIFTED_PPM,
  86. 499 * SHIFTED_PPM,
  87. };
  88. long outofrange_freq[NUM_FREQ_OUTOFRANGE] = {
  89. -1000 * SHIFTED_PPM,
  90. -550 * SHIFTED_PPM,
  91. 550 * SHIFTED_PPM,
  92. 1000 * SHIFTED_PPM,
  93. };
  94. #define LONG_MAX (~0UL>>1)
  95. #define LONG_MIN (-LONG_MAX - 1)
  96. long invalid_freq[NUM_FREQ_INVALID] = {
  97. LONG_MAX,
  98. LONG_MIN,
  99. };
  100. int validate_freq(void)
  101. {
  102. struct timex tx;
  103. int ret, pass = 0;
  104. int i;
  105. clear_time_state();
  106. memset(&tx, 0, sizeof(struct timex));
  107. /* Set the leap second insert flag */
  108. printf("Testing ADJ_FREQ... ");
  109. fflush(stdout);
  110. for (i = 0; i < NUM_FREQ_VALID; i++) {
  111. tx.modes = ADJ_FREQUENCY;
  112. tx.freq = valid_freq[i];
  113. ret = adjtimex(&tx);
  114. if (ret < 0) {
  115. printf("[FAIL]\n");
  116. printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
  117. valid_freq[i], valid_freq[i]>>16);
  118. pass = -1;
  119. goto out;
  120. }
  121. tx.modes = 0;
  122. ret = adjtimex(&tx);
  123. if (tx.freq != valid_freq[i]) {
  124. printf("Warning: freq value %ld not what we set it (%ld)!\n",
  125. tx.freq, valid_freq[i]);
  126. }
  127. }
  128. for (i = 0; i < NUM_FREQ_OUTOFRANGE; i++) {
  129. tx.modes = ADJ_FREQUENCY;
  130. tx.freq = outofrange_freq[i];
  131. ret = adjtimex(&tx);
  132. if (ret < 0) {
  133. printf("[FAIL]\n");
  134. printf("Error: adjtimex(ADJ_FREQ, %ld - %ld ppm\n",
  135. outofrange_freq[i], outofrange_freq[i]>>16);
  136. pass = -1;
  137. goto out;
  138. }
  139. tx.modes = 0;
  140. ret = adjtimex(&tx);
  141. if (tx.freq == outofrange_freq[i]) {
  142. printf("[FAIL]\n");
  143. printf("ERROR: out of range value %ld actually set!\n",
  144. tx.freq);
  145. pass = -1;
  146. goto out;
  147. }
  148. }
  149. if (sizeof(long) == 8) { /* this case only applies to 64bit systems */
  150. for (i = 0; i < NUM_FREQ_INVALID; i++) {
  151. tx.modes = ADJ_FREQUENCY;
  152. tx.freq = invalid_freq[i];
  153. ret = adjtimex(&tx);
  154. if (ret >= 0) {
  155. printf("[FAIL]\n");
  156. printf("Error: No failure on invalid ADJ_FREQUENCY %ld\n",
  157. invalid_freq[i]);
  158. pass = -1;
  159. goto out;
  160. }
  161. }
  162. }
  163. printf("[OK]\n");
  164. out:
  165. /* reset freq to zero */
  166. tx.modes = ADJ_FREQUENCY;
  167. tx.freq = 0;
  168. ret = adjtimex(&tx);
  169. return pass;
  170. }
  171. int set_offset(long long offset, int use_nano)
  172. {
  173. struct timex tmx = {};
  174. int ret;
  175. tmx.modes = ADJ_SETOFFSET;
  176. if (use_nano) {
  177. tmx.modes |= ADJ_NANO;
  178. tmx.time.tv_sec = offset / NSEC_PER_SEC;
  179. tmx.time.tv_usec = offset % NSEC_PER_SEC;
  180. if (offset < 0 && tmx.time.tv_usec) {
  181. tmx.time.tv_sec -= 1;
  182. tmx.time.tv_usec += NSEC_PER_SEC;
  183. }
  184. } else {
  185. tmx.time.tv_sec = offset / USEC_PER_SEC;
  186. tmx.time.tv_usec = offset % USEC_PER_SEC;
  187. if (offset < 0 && tmx.time.tv_usec) {
  188. tmx.time.tv_sec -= 1;
  189. tmx.time.tv_usec += USEC_PER_SEC;
  190. }
  191. }
  192. ret = clock_adjtime(CLOCK_REALTIME, &tmx);
  193. if (ret < 0) {
  194. printf("(sec: %ld usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec);
  195. printf("[FAIL]\n");
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. int set_bad_offset(long sec, long usec, int use_nano)
  201. {
  202. struct timex tmx = {};
  203. int ret;
  204. tmx.modes = ADJ_SETOFFSET;
  205. if (use_nano)
  206. tmx.modes |= ADJ_NANO;
  207. tmx.time.tv_sec = sec;
  208. tmx.time.tv_usec = usec;
  209. ret = clock_adjtime(CLOCK_REALTIME, &tmx);
  210. if (ret >= 0) {
  211. printf("Invalid (sec: %ld usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec);
  212. printf("[FAIL]\n");
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. int validate_set_offset(void)
  218. {
  219. printf("Testing ADJ_SETOFFSET... ");
  220. fflush(stdout);
  221. /* Test valid values */
  222. if (set_offset(NSEC_PER_SEC - 1, 1))
  223. return -1;
  224. if (set_offset(-NSEC_PER_SEC + 1, 1))
  225. return -1;
  226. if (set_offset(-NSEC_PER_SEC - 1, 1))
  227. return -1;
  228. if (set_offset(5 * NSEC_PER_SEC, 1))
  229. return -1;
  230. if (set_offset(-5 * NSEC_PER_SEC, 1))
  231. return -1;
  232. if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
  233. return -1;
  234. if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
  235. return -1;
  236. if (set_offset(USEC_PER_SEC - 1, 0))
  237. return -1;
  238. if (set_offset(-USEC_PER_SEC + 1, 0))
  239. return -1;
  240. if (set_offset(-USEC_PER_SEC - 1, 0))
  241. return -1;
  242. if (set_offset(5 * USEC_PER_SEC, 0))
  243. return -1;
  244. if (set_offset(-5 * USEC_PER_SEC, 0))
  245. return -1;
  246. if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
  247. return -1;
  248. if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
  249. return -1;
  250. /* Test invalid values */
  251. if (set_bad_offset(0, -1, 1))
  252. return -1;
  253. if (set_bad_offset(0, -1, 0))
  254. return -1;
  255. if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
  256. return -1;
  257. if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
  258. return -1;
  259. if (set_bad_offset(0, NSEC_PER_SEC, 1))
  260. return -1;
  261. if (set_bad_offset(0, USEC_PER_SEC, 0))
  262. return -1;
  263. if (set_bad_offset(0, -NSEC_PER_SEC, 1))
  264. return -1;
  265. if (set_bad_offset(0, -USEC_PER_SEC, 0))
  266. return -1;
  267. printf("[OK]\n");
  268. return 0;
  269. }
  270. int main(int argc, char **argv)
  271. {
  272. if (validate_freq())
  273. ksft_exit_fail();
  274. if (validate_set_offset())
  275. ksft_exit_fail();
  276. ksft_exit_pass();
  277. }