torture_su_time.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005,2006 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@ingroup su_time
  25. *
  26. * @IFILE torture_su_time.c
  27. *
  28. * Tests for su_time functions.
  29. *
  30. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  31. * @author Kai Vehmanen <first.surname@nokia.com>
  32. *
  33. * @date Created: Fri May 10 16:08:18 2002 ppessi
  34. */
  35. #include "config.h"
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stddef.h>
  39. #include <limits.h>
  40. #include <stdlib.h>
  41. #include <sofia-sip/su_time.h>
  42. #define TSTFLAGS flags
  43. #include <sofia-sip/tstdef.h>
  44. char const *name = "torture_su_time.c";
  45. int expensive_tests = 0;
  46. static int test1(int flags);
  47. static int test2(int flags);
  48. static int test3(int flags);
  49. su_time_t tv0;
  50. void su_time0(su_time_t *tv)
  51. {
  52. *tv = tv0;
  53. }
  54. uint64_t nanotime;
  55. uint64_t su_nanotime0(uint64_t *retval)
  56. {
  57. if (nanotime)
  58. return *retval = nanotime;
  59. else
  60. return *retval = (uint64_t)tv0.tv_sec * 1000000000U + tv0.tv_usec * 1000U;
  61. }
  62. #define E9 (1000000000U)
  63. int test1(int flags)
  64. {
  65. uint32_t ntp_hi, ntp_lo, ntp_mw;
  66. su_time_t now;
  67. su_ntp_t ntp, ntp0, ntp1;
  68. uint64_t increment = 3019;
  69. BEGIN();
  70. ntp_hi = 2208988800UL;
  71. ntp_lo = ((su_ntp_t)500000 << 32) / 1000000;
  72. ntp_mw = (ntp_hi << 16) | (ntp_lo >> 16);
  73. tv0.tv_sec = ntp_hi;
  74. tv0.tv_usec = 500000UL;
  75. ntp0 = (((su_ntp_t)2208988800UL) << 32) + ntp_lo;
  76. ntp1 = (((su_ntp_t)2208988800UL) << 32);
  77. now = su_now();
  78. TEST(now.tv_sec, 2208988800UL);
  79. TEST(now.tv_usec, 500000);
  80. ntp = su_ntp_now();
  81. TEST64(ntp, ntp0);
  82. TEST64(su_ntp_hi(ntp), ntp_hi);
  83. TEST64(su_ntp_lo(ntp), ntp_lo);
  84. TEST64(su_ntp_mw(ntp), ntp_mw);
  85. TEST(su_ntp_fraq(tv0), ntp_mw);
  86. tv0.tv_usec = 0;
  87. ntp = su_ntp_now();
  88. TEST64(ntp, ntp1);
  89. TEST64(su_ntp_hi(ntp), ntp_hi);
  90. TEST64(su_ntp_lo(ntp), 0);
  91. TEST64(su_ntp_mw(ntp), (ntp_hi & 0xffff) << 16);
  92. TEST(su_ntp_fraq(tv0), su_ntp_mw(ntp));
  93. if (expensive_tests)
  94. increment = 1;
  95. for (nanotime = 1; nanotime <= 2 * E9; nanotime += increment) {
  96. ntp = su_ntp_now();
  97. ntp0 = ((nanotime / E9) << 32) | ((((nanotime % E9) << 32) + E9/2) / E9);
  98. TEST(ntp, ntp0);
  99. }
  100. nanotime = 0;
  101. END();
  102. }
  103. #include <sofia-sip/su_uniqueid.h>
  104. #include <sofia-sip/su.h> /* htonl() and guys */
  105. int test2(int flags)
  106. {
  107. char buf[64];
  108. su_guid_t g1[1], g2[2];
  109. uint64_t tl;
  110. uint16_t seq1, seq2;
  111. const uint64_t granularity = 10000000U;
  112. const uint64_t ntp_epoch =
  113. (uint64_t)(141427) * (24 * 60 * 60L) * granularity;
  114. isize_t i;
  115. BEGIN();
  116. nanotime = 0;
  117. tv0.tv_sec = 268435455;
  118. tv0.tv_usec = 98765;
  119. tl = tv0.tv_sec * granularity + tv0.tv_usec * (granularity / 1000000);
  120. tl += ntp_epoch;
  121. su_guid_generate(g1);
  122. seq1 = ((g1->s.clock_seq_hi_and_reserved & 0x3f) << 8) + g1->s.clock_seq_low;
  123. TEST(g1->s.clock_seq_hi_and_reserved & 0xc0, 0x80);
  124. TEST(g1->s.time_high_and_version, htons(((tl >> 48) & 0xfffU) | 0x1000));
  125. TEST(ntohs(g1->s.time_mid), (tl >> 32) & 0xffffU);
  126. //TEST(ntohl(g1->s.time_low), tl & 0xffffFFFFU);
  127. TEST(i = su_guid_sprintf(buf, sizeof(buf), g1), su_guid_strlen);
  128. TEST_SIZE(strlen(buf), i);
  129. tv0.tv_usec++;
  130. su_guid_generate(g1);
  131. seq2 = ((g1->s.clock_seq_hi_and_reserved & 0x3f) << 8) + g1->s.clock_seq_low;
  132. TEST(seq1, seq2);
  133. su_guid_generate(g2);
  134. seq2 = ((g2->s.clock_seq_hi_and_reserved & 0x3f) << 8) + g2->s.clock_seq_low;
  135. TEST(g2->s.time_low, g1->s.time_low);
  136. TEST(g2->s.time_mid, g1->s.time_mid);
  137. TEST(g2->s.time_high_and_version, g1->s.time_high_and_version);
  138. TEST((seq1 + 1) % 16384, seq2);
  139. for (i = 0; i < 32000; i++) {
  140. su_guid_generate(g2);
  141. seq2 = ((g2->s.clock_seq_hi_and_reserved & 0x3f) << 8)
  142. + g2->s.clock_seq_low;
  143. TEST((seq1 + i + 2) % 16384, seq2);
  144. }
  145. END();
  146. }
  147. int test3(int flags)
  148. {
  149. su_time_t a = { ULONG_MAX , ULONG_MAX };
  150. su_time_t b = { 0 , 0 };
  151. su_time_t c = { ULONG_MAX , ULONG_MAX - 1 };
  152. BEGIN();
  153. TEST_1(su_time_cmp(a, b) > 0);
  154. TEST_1(su_time_cmp(a, a) == 0);
  155. TEST_1(su_time_cmp(b, a) < 0);
  156. TEST_1(su_time_cmp(a, c) > 0);
  157. TEST_1(su_time_cmp(c, c) == 0);
  158. TEST_1(su_time_cmp(c, a) < 0);
  159. TEST_1(SU_TIME_CMP(a, b) > 0);
  160. TEST_1(SU_TIME_CMP(a, a) == 0);
  161. TEST_1(SU_TIME_CMP(b, a) < 0);
  162. TEST_1(SU_TIME_CMP(a, c) > 0);
  163. TEST_1(SU_TIME_CMP(c, c) == 0);
  164. TEST_1(SU_TIME_CMP(c, a) < 0);
  165. END();
  166. }
  167. void su_time0(su_time_t *tv);
  168. uint64_t su_nanotime0(uint64_t *);
  169. extern void (*_su_time)(su_time_t *tv);
  170. extern uint64_t (*_su_nanotime)(uint64_t *);
  171. void usage(int exitcode)
  172. {
  173. fprintf(stderr,
  174. "usage: %s [-v] [-a]\n",
  175. name);
  176. exit(exitcode);
  177. }
  178. char *lastpart(char *path)
  179. {
  180. if (strchr(path, '/'))
  181. return strrchr(path, '/') + 1;
  182. else
  183. return path;
  184. }
  185. int main(int argc, char *argv[])
  186. {
  187. int flags = 0;
  188. int retval = 0;
  189. int i;
  190. name = lastpart(argv[0]); /* Set our name */
  191. for (i = 1; argv[i]; i++) {
  192. if (strcmp(argv[i], "-v") == 0)
  193. flags |= tst_verbatim;
  194. else if (strcmp(argv[i], "-x") == 0)
  195. expensive_tests = 1;
  196. else if (strcmp(argv[i], "-a") == 0)
  197. flags |= tst_abort;
  198. else
  199. usage(1);
  200. }
  201. if (getenv("EXPENSIVE_CHECKS"))
  202. expensive_tests = 1;
  203. _su_time = su_time0, _su_nanotime = su_nanotime0;
  204. retval |= test1(flags); fflush(stdout);
  205. retval |= test2(flags); fflush(stdout);
  206. retval |= test3(flags); fflush(stdout);
  207. return retval;
  208. }