packetHandling.c (285169) | packetHandling.c (289764) |
---|---|
1#include "config.h" 2#include "ntp_debug.h" 3#include "ntp_stdlib.h" 4#include "ntp_types.h" 5 6#include "sntptest.h" 7 8#include "kod_management.h" 9#include "main.h" 10#include "networking.h" 11#include "ntp.h" 12 13#include "unity.h" 14 | 1#include "config.h" 2#include "ntp_debug.h" 3#include "ntp_stdlib.h" 4#include "ntp_types.h" 5 6#include "sntptest.h" 7 8#include "kod_management.h" 9#include "main.h" 10#include "networking.h" 11#include "ntp.h" 12 13#include "unity.h" 14 |
15void setUp(void); 16int LfpEquality(const l_fp expected, const l_fp actual); 17void test_GenerateUnauthenticatedPacket(void); 18void test_GenerateAuthenticatedPacket(void); 19void test_OffsetCalculationPositiveOffset(void); 20void test_OffsetCalculationNegativeOffset(void); 21void test_HandleUnusableServer(void); 22void test_HandleUnusablePacket(void); 23void test_HandleServerAuthenticationFailure(void); 24void test_HandleKodDemobilize(void); 25void test_HandleKodRate(void); 26void test_HandleCorrectPacket(void); |
|
15 | 27 |
16int counter = 0; | |
17 | 28 |
18 19// old code from google test framework, moved to SetUp() for unity 20void setUp(void) 21{ | 29void 30setUp(void) { |
22 init_lib(); 23} 24 25 | 31 init_lib(); 32} 33 34 |
26 27int LfpEquality(const l_fp expected, const l_fp actual) { 28 if (L_ISEQU(&expected, &actual)) { 29 return TRUE; 30 } else { 31 return FALSE; 32 } | 35int 36LfpEquality(const l_fp expected, const l_fp actual) { 37 if (L_ISEQU(&expected, &actual)) 38 return TRUE; 39 else 40 return FALSE; |
33} 34 | 41} 42 |
35void test_GenerateUnauthenticatedPacket(void) { | 43 44void 45test_GenerateUnauthenticatedPacket(void) { |
36 struct pkt testpkt; 37 38 struct timeval xmt; 39 GETTIMEOFDAY(&xmt, NULL); 40 xmt.tv_sec += JAN_1970; 41 42 TEST_ASSERT_EQUAL(LEN_PKT_NOMAC, 43 generate_pkt(&testpkt, &xmt, 0, NULL)); --- 6 unchanged lines hidden (view full) --- 50 TEST_ASSERT_EQUAL(8, testpkt.ppoll); 51 52 l_fp expected_xmt, actual_xmt; 53 TVTOTS(&xmt, &expected_xmt); 54 NTOHL_FP(&testpkt.xmt, &actual_xmt); 55 TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt)); 56} 57 | 46 struct pkt testpkt; 47 48 struct timeval xmt; 49 GETTIMEOFDAY(&xmt, NULL); 50 xmt.tv_sec += JAN_1970; 51 52 TEST_ASSERT_EQUAL(LEN_PKT_NOMAC, 53 generate_pkt(&testpkt, &xmt, 0, NULL)); --- 6 unchanged lines hidden (view full) --- 60 TEST_ASSERT_EQUAL(8, testpkt.ppoll); 61 62 l_fp expected_xmt, actual_xmt; 63 TVTOTS(&xmt, &expected_xmt); 64 NTOHL_FP(&testpkt.xmt, &actual_xmt); 65 TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt)); 66} 67 |
58void test_GenerateAuthenticatedPacket(void) { | 68 69void 70test_GenerateAuthenticatedPacket(void) { |
59 struct key testkey; 60 testkey.next = NULL; 61 testkey.key_id = 30; 62 testkey.key_len = 9; 63 memcpy(testkey.key_seq, "123456789", testkey.key_len); 64 memcpy(testkey.type, "MD5", 3); 65 66 struct pkt testpkt; --- 19 unchanged lines hidden (view full) --- 86 NTOHL_FP(&testpkt.xmt, &actual_xmt); 87 TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt)); 88 89 TEST_ASSERT_EQUAL(testkey.key_id, ntohl(testpkt.exten[0])); 90 91 char expected_mac[MAX_MD5_LEN]; 92 TEST_ASSERT_EQUAL(MAX_MD5_LEN - 4, // Remove the key_id, only keep the mac. 93 make_mac((char*)&testpkt, LEN_PKT_NOMAC, MAX_MD5_LEN, &testkey, expected_mac)); | 71 struct key testkey; 72 testkey.next = NULL; 73 testkey.key_id = 30; 74 testkey.key_len = 9; 75 memcpy(testkey.key_seq, "123456789", testkey.key_len); 76 memcpy(testkey.type, "MD5", 3); 77 78 struct pkt testpkt; --- 19 unchanged lines hidden (view full) --- 98 NTOHL_FP(&testpkt.xmt, &actual_xmt); 99 TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt)); 100 101 TEST_ASSERT_EQUAL(testkey.key_id, ntohl(testpkt.exten[0])); 102 103 char expected_mac[MAX_MD5_LEN]; 104 TEST_ASSERT_EQUAL(MAX_MD5_LEN - 4, // Remove the key_id, only keep the mac. 105 make_mac((char*)&testpkt, LEN_PKT_NOMAC, MAX_MD5_LEN, &testkey, expected_mac)); |
94 TEST_ASSERT_TRUE(memcmp(expected_mac, (char*)&testpkt.exten[1], MAX_MD5_LEN -4) == 0); | 106 TEST_ASSERT_EQUAL_MEMORY(expected_mac, (char*)&testpkt.exten[1], MAX_MD5_LEN -4); |
95} 96 | 107} 108 |
97void test_OffsetCalculationPositiveOffset(void) { | 109 110void 111test_OffsetCalculationPositiveOffset(void) { |
98 struct pkt rpkt; 99 100 rpkt.precision = -16; // 0,000015259 101 rpkt.rootdelay = HTONS_FP(DTOUFP(0.125)); 102 rpkt.rootdisp = HTONS_FP(DTOUFP(0.25)); 103 // Synch Distance: (0.125+0.25)/2.0 == 0.1875 104 l_fp reftime; 105 get_systime(&reftime); --- 21 unchanged lines hidden (view full) --- 127 tmp.l_uf = 0UL; 128 struct timeval dst; 129 TSTOTV(&tmp, &dst); 130 dst.tv_sec -= JAN_1970; 131 132 double offset, precision, synch_distance; 133 offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance); 134 | 112 struct pkt rpkt; 113 114 rpkt.precision = -16; // 0,000015259 115 rpkt.rootdelay = HTONS_FP(DTOUFP(0.125)); 116 rpkt.rootdisp = HTONS_FP(DTOUFP(0.25)); 117 // Synch Distance: (0.125+0.25)/2.0 == 0.1875 118 l_fp reftime; 119 get_systime(&reftime); --- 21 unchanged lines hidden (view full) --- 141 tmp.l_uf = 0UL; 142 struct timeval dst; 143 TSTOTV(&tmp, &dst); 144 dst.tv_sec -= JAN_1970; 145 146 double offset, precision, synch_distance; 147 offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance); 148 |
135 TEST_ASSERT_EQUAL_FLOAT(1.25, offset); 136 TEST_ASSERT_EQUAL_FLOAT(1. / ULOGTOD(16), precision); | 149 TEST_ASSERT_EQUAL_DOUBLE(1.25, offset); 150 TEST_ASSERT_EQUAL_DOUBLE(1. / ULOGTOD(16), precision); |
137 // 1.1250150000000001 ? | 151 // 1.1250150000000001 ? |
138 TEST_ASSERT_EQUAL_FLOAT(1.125015, synch_distance); | 152 TEST_ASSERT_EQUAL_DOUBLE(1.125015, synch_distance); |
139} 140 | 153} 154 |
141void test_OffsetCalculationNegativeOffset(void) { | 155 156void 157test_OffsetCalculationNegativeOffset(void) { |
142 struct pkt rpkt; 143 144 rpkt.precision = -1; 145 rpkt.rootdelay = HTONS_FP(DTOUFP(0.5)); 146 rpkt.rootdisp = HTONS_FP(DTOUFP(0.5)); 147 // Synch Distance is (0.5+0.5)/2.0, or 0.5 148 l_fp reftime; 149 get_systime(&reftime); --- 21 unchanged lines hidden (view full) --- 171 tmp.l_uf = 0UL; 172 struct timeval dst; 173 TSTOTV(&tmp, &dst); 174 dst.tv_sec -= JAN_1970; 175 176 double offset, precision, synch_distance; 177 offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance); 178 | 158 struct pkt rpkt; 159 160 rpkt.precision = -1; 161 rpkt.rootdelay = HTONS_FP(DTOUFP(0.5)); 162 rpkt.rootdisp = HTONS_FP(DTOUFP(0.5)); 163 // Synch Distance is (0.5+0.5)/2.0, or 0.5 164 l_fp reftime; 165 get_systime(&reftime); --- 21 unchanged lines hidden (view full) --- 187 tmp.l_uf = 0UL; 188 struct timeval dst; 189 TSTOTV(&tmp, &dst); 190 dst.tv_sec -= JAN_1970; 191 192 double offset, precision, synch_distance; 193 offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance); 194 |
179 TEST_ASSERT_EQUAL_FLOAT(-1, offset); 180 TEST_ASSERT_EQUAL_FLOAT(1. / ULOGTOD(1), precision); 181 TEST_ASSERT_EQUAL_FLOAT(1.3333483333333334, synch_distance); | 195 TEST_ASSERT_EQUAL_DOUBLE(-1, offset); 196 TEST_ASSERT_EQUAL_DOUBLE(1. / ULOGTOD(1), precision); 197 TEST_ASSERT_EQUAL_DOUBLE(1.3333483333333334, synch_distance); |
182} 183 | 198} 199 |
184void test_HandleUnusableServer(void) { | 200 201void 202test_HandleUnusableServer(void) { |
185 struct pkt rpkt; 186 sockaddr_u host; 187 int rpktl; 188 189 ZERO(rpkt); 190 ZERO(host); 191 rpktl = SERVER_UNUSEABLE; 192 TEST_ASSERT_EQUAL(-1, handle_pkt(rpktl, &rpkt, &host, "")); 193} 194 | 203 struct pkt rpkt; 204 sockaddr_u host; 205 int rpktl; 206 207 ZERO(rpkt); 208 ZERO(host); 209 rpktl = SERVER_UNUSEABLE; 210 TEST_ASSERT_EQUAL(-1, handle_pkt(rpktl, &rpkt, &host, "")); 211} 212 |
195void test_HandleUnusablePacket(void) { | 213 214void 215test_HandleUnusablePacket(void) { |
196 struct pkt rpkt; 197 sockaddr_u host; 198 int rpktl; 199 200 ZERO(rpkt); 201 ZERO(host); 202 rpktl = PACKET_UNUSEABLE; 203 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, "")); 204} 205 | 216 struct pkt rpkt; 217 sockaddr_u host; 218 int rpktl; 219 220 ZERO(rpkt); 221 ZERO(host); 222 rpktl = PACKET_UNUSEABLE; 223 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, "")); 224} 225 |
206void test_HandleServerAuthenticationFailure(void) { | 226 227void 228test_HandleServerAuthenticationFailure(void) { |
207 struct pkt rpkt; 208 sockaddr_u host; 209 int rpktl; 210 211 ZERO(rpkt); 212 ZERO(host); 213 rpktl = SERVER_AUTH_FAIL; 214 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, "")); 215} 216 | 229 struct pkt rpkt; 230 sockaddr_u host; 231 int rpktl; 232 233 ZERO(rpkt); 234 ZERO(host); 235 rpktl = SERVER_AUTH_FAIL; 236 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, "")); 237} 238 |
217void test_HandleKodDemobilize(void) { | 239 240void 241test_HandleKodDemobilize(void) { |
218 const char * HOSTNAME = "192.0.2.1"; 219 const char * REASON = "DENY"; 220 struct pkt rpkt; 221 sockaddr_u host; 222 int rpktl; 223 struct kod_entry * entry; 224 225 rpktl = KOD_DEMOBILIZE; --- 4 unchanged lines hidden (view full) --- 230 host.sa4.sin_addr.s_addr = inet_addr(HOSTNAME); 231 232 // Test that the KOD-entry is added to the database. 233 kod_init_kod_db("/dev/null", TRUE); 234 235 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, HOSTNAME)); 236 237 TEST_ASSERT_EQUAL(1, search_entry(HOSTNAME, &entry)); | 242 const char * HOSTNAME = "192.0.2.1"; 243 const char * REASON = "DENY"; 244 struct pkt rpkt; 245 sockaddr_u host; 246 int rpktl; 247 struct kod_entry * entry; 248 249 rpktl = KOD_DEMOBILIZE; --- 4 unchanged lines hidden (view full) --- 254 host.sa4.sin_addr.s_addr = inet_addr(HOSTNAME); 255 256 // Test that the KOD-entry is added to the database. 257 kod_init_kod_db("/dev/null", TRUE); 258 259 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, HOSTNAME)); 260 261 TEST_ASSERT_EQUAL(1, search_entry(HOSTNAME, &entry)); |
238 TEST_ASSERT_TRUE(memcmp(REASON, entry->type, 4) == 0); | 262 TEST_ASSERT_EQUAL_MEMORY(REASON, entry->type, 4); |
239} 240 | 263} 264 |
241void test_HandleKodRate(void) { | 265 266void 267test_HandleKodRate(void) { |
242 struct pkt rpkt; 243 sockaddr_u host; 244 int rpktl; 245 246 ZERO(rpkt); 247 ZERO(host); 248 rpktl = KOD_RATE; 249 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, "")); 250} 251 | 268 struct pkt rpkt; 269 sockaddr_u host; 270 int rpktl; 271 272 ZERO(rpkt); 273 ZERO(host); 274 rpktl = KOD_RATE; 275 TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, "")); 276} 277 |
252void test_HandleCorrectPacket(void) { | 278 279void 280test_HandleCorrectPacket(void) { |
253 struct pkt rpkt; 254 sockaddr_u host; 255 int rpktl; 256 l_fp now; 257 258 // We don't want our testing code to actually change the system clock. 259 TEST_ASSERT_FALSE(ENABLED_OPT(STEP)); 260 TEST_ASSERT_FALSE(ENABLED_OPT(SLEW)); --- 14 unchanged lines hidden --- | 281 struct pkt rpkt; 282 sockaddr_u host; 283 int rpktl; 284 l_fp now; 285 286 // We don't want our testing code to actually change the system clock. 287 TEST_ASSERT_FALSE(ENABLED_OPT(STEP)); 288 TEST_ASSERT_FALSE(ENABLED_OPT(SLEW)); --- 14 unchanged lines hidden --- |