4 #include "fileHandlingTest.h"
12 const char * Version = "stub unit test Version string";
16 sockaddr_u CreateSockaddr4(const char* address) {
18 s.sa4.sin_family = AF_INET;
19 s.sa4.sin_addr.s_addr = inet_addr(address);
25 struct addrinfo CreateAddrinfo( sockaddr_u* sock) {
27 a.ai_family = sock->sa.sa_family;
28 a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family);
29 a.ai_addr = &sock->sa;
34 bool outputFileOpened;
37 //debugUtilitiesTest() : outputFileOpened(false) {}
39 void 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_TRUE(outputFile != NULL);
44 outputFileOpened = true;
47 // Closes outputFile, and compare contents.
48 void FinishDebugTest(const char * expected,
49 const char * actual) {
53 FILE * e = fopen(expected,"rb");
54 FILE * a = fopen(actual,"rb");
55 TEST_ASSERT_TRUE(e != NULL);
56 TEST_ASSERT_TRUE(a != NULL);
58 CompareFileContent(e, a);
63 * These tests are essentially a copy of the tests for socktoa()
64 * in libntp. If sntp switches to using that functions, these
65 * tests can be removed.
68 void test_IPv4Address() {
69 const char* ADDR = "192.0.2.10";
71 sockaddr_u input = CreateSockaddr4(ADDR);
72 struct addrinfo inputA = CreateAddrinfo(&input);
74 TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input));
75 TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA));
78 void test_IPv6Address() {
79 const struct in6_addr address = {
80 0x20, 0x01, 0x0d, 0xb8,
81 0x85, 0xa3, 0x08, 0xd3,
82 0x13, 0x19, 0x8a, 0x2e,
83 0x03, 0x70, 0x73, 0x34
85 const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334";
87 struct addrinfo inputA;
89 memset(&input, 0, sizeof(input));
90 input.sa6.sin6_family = AF_INET6;
91 input.sa6.sin6_addr = address;
92 TEST_ASSERT_EQUAL_STRING(expected, ss_to_str(&input));
94 inputA = CreateAddrinfo(&input);
95 TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA));
98 void test_SetLiVnMode1() {
100 expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
105 set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION,
108 TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
111 void test_SetLiVnMode2() {
113 expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
118 set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION,
121 TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode);
124 /* Debug utilities tests */
126 void test_PktOutput() {
127 char * filename = "debug-output-pkt";//CreatePath("debug-output-pkt", OUTPUT_DIR);
128 InitDebugTest(filename);
131 memset(&testpkt, 0, sizeof(struct pkt));
132 testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
138 test.l_uf = 2147483647; // Lots of ones.
139 HTONL_FP(&test, &testpkt.xmt);
141 pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile);
143 FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename);
146 void test_LfpOutputBinaryFormat() {
147 char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR);
148 InitDebugTest(filename);
151 test.l_ui = 63; // 00000000 00000000 00000000 00111111
152 test.l_uf = 127; // 00000000 00000000 00000000 01111111
155 HTONL_FP(&test, &network);
157 l_fp_output_bin(&network, outputFile);
159 FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename);
162 void test_LfpOutputDecimalFormat() {
163 char * filename = "debug-output-lfp-dec"; //CreatePath("debug-output-lfp-dec", OUTPUT_DIR);
164 InitDebugTest(filename);
167 test.l_ui = 6310; // 0x000018A6
168 test.l_uf = 308502; // 0x00004B516
171 HTONL_FP(&test, &network);
173 l_fp_output_dec(&network, outputFile);
175 FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename);