PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/ntp/sntp/tests/utilities.c

https://bitbucket.org/freebsd/freebsd-base
C | 205 lines | 142 code | 54 blank | 9 comment | 1 complexity | 9c9cbdd84de17de8fffafc89314ded17 MD5 | raw file
  1. #include "config.h"
  2. #include "sntptest.h"
  3. #include "fileHandlingTest.h"
  4. #include "main.h"
  5. #include "utilities.h"
  6. #include "unity.h"
  7. #include <math.h>
  8. sockaddr_u CreateSockaddr4(const char* address);
  9. struct addrinfo CreateAddrinfo(sockaddr_u* sock);
  10. void InitDebugTest(const char * filename);
  11. void FinishDebugTest(const char * expected,const char * actual);
  12. void test_IPv4Address(void);
  13. void test_IPv6Address(void);
  14. void test_SetLiVnMode1(void);
  15. void test_SetLiVnMode2(void);
  16. void test_PktOutput(void);
  17. void test_LfpOutputBinaryFormat(void);
  18. void test_LfpOutputDecimalFormat(void);
  19. const char * Version = "stub unit test Version string";
  20. sockaddr_u
  21. CreateSockaddr4(const char* address) {
  22. sockaddr_u s;
  23. s.sa4.sin_family = AF_INET;
  24. s.sa4.sin_addr.s_addr = inet_addr(address);
  25. SET_PORT(&s, 123);
  26. return s;
  27. }
  28. struct addrinfo
  29. CreateAddrinfo(sockaddr_u* sock) {
  30. struct addrinfo a;
  31. a.ai_family = sock->sa.sa_family;
  32. a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family);
  33. a.ai_addr = &sock->sa;
  34. return a;
  35. }
  36. bool outputFileOpened;
  37. FILE* outputFile;
  38. void
  39. InitDebugTest(const char * filename) {
  40. // Clear the contents of the current file.
  41. // Open the output file
  42. outputFile = fopen(filename, "w+");
  43. TEST_ASSERT_NOT_NULL(outputFile);
  44. outputFileOpened = true;
  45. }
  46. // Closes outputFile, and compare contents.
  47. void
  48. FinishDebugTest(const char * expected,
  49. const char * actual) {
  50. if (outputFileOpened)
  51. fclose(outputFile);
  52. FILE * e = fopen(expected,"rb");
  53. FILE * a = fopen(actual,"rb");
  54. TEST_ASSERT_NOT_NULL(e);
  55. TEST_ASSERT_NOT_NULL(a);
  56. CompareFileContent(e, a);
  57. }
  58. /*
  59. * These tests are essentially a copy of the tests for socktoa()
  60. * in libntp. If sntp switches to using that functions, these
  61. * tests can be removed.
  62. */
  63. void
  64. test_IPv4Address(void) {
  65. const char* ADDR = "192.0.2.10";
  66. sockaddr_u input = CreateSockaddr4(ADDR);
  67. struct addrinfo inputA = CreateAddrinfo(&input);
  68. TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input));
  69. TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA));
  70. }
  71. void
  72. test_IPv6Address(void) {
  73. const struct in6_addr address = { { {
  74. 0x20, 0x01, 0x0d, 0xb8,
  75. 0x85, 0xa3, 0x08, 0xd3,
  76. 0x13, 0x19, 0x8a, 0x2e,
  77. 0x03, 0x70, 0x73, 0x34
  78. } } };
  79. const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334";
  80. sockaddr_u input;
  81. struct addrinfo inputA;
  82. memset(&input, 0, sizeof(input));
  83. input.sa6.sin6_family = AF_INET6;
  84. input.sa6.sin6_addr = address;
  85. TEST_ASSERT_EQUAL_STRING(expected, ss_to_str(&input));
  86. inputA = CreateAddrinfo(&input);
  87. TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA));
  88. }
  89. void
  90. test_SetLiVnMode1(void) {
  91. struct pkt expected;
  92. expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
  93. NTP_VERSION,
  94. MODE_SERVER);
  95. struct pkt actual;
  96. set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION,
  97. MODE_SERVER);
  98. TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
  99. }
  100. void
  101. test_SetLiVnMode2(void) {
  102. struct pkt expected;
  103. expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
  104. NTP_OLDVERSION,
  105. MODE_BROADCAST);
  106. struct pkt actual;
  107. set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION,
  108. MODE_BROADCAST);
  109. TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
  110. }
  111. /* Debug utilities tests */
  112. void
  113. test_PktOutput(void) {
  114. char * filename = "debug-output-pkt";
  115. InitDebugTest(filename);
  116. struct pkt testpkt;
  117. memset(&testpkt, 0, sizeof(struct pkt));
  118. testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
  119. NTP_VERSION,
  120. MODE_SERVER);
  121. l_fp test;
  122. test.l_ui = 8;
  123. test.l_uf = 2147483647; // Lots of ones.
  124. HTONL_FP(&test, &testpkt.xmt);
  125. pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile);
  126. FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename);
  127. }
  128. void
  129. test_LfpOutputBinaryFormat(void) {
  130. char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR);
  131. InitDebugTest(filename);
  132. l_fp test;
  133. test.l_ui = 63; // 00000000 00000000 00000000 00111111
  134. test.l_uf = 127; // 00000000 00000000 00000000 01111111
  135. l_fp network;
  136. HTONL_FP(&test, &network);
  137. l_fp_output_bin(&network, outputFile);
  138. FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename);
  139. }
  140. void
  141. test_LfpOutputDecimalFormat(void) {
  142. char * filename = "debug-output-lfp-dec";
  143. InitDebugTest(filename);
  144. l_fp test;
  145. test.l_ui = 6310; // 0x000018A6
  146. test.l_uf = 308502; // 0x00004B516
  147. l_fp network;
  148. HTONL_FP(&test, &network);
  149. l_fp_output_dec(&network, outputFile);
  150. FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename);
  151. }