valid-adjtimex.c 6.4 KB

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