valid-adjtimex.c 6.5 KB

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