Home | History | Annotate | Line # | Download | only in tests
packetProcessing.c revision 1.1.1.1
      1  1.1  christos #include "config.h"
      2  1.1  christos #include "sntptest.h"
      3  1.1  christos #include "networking.h"
      4  1.1  christos #include "ntp_stdlib.h"
      5  1.1  christos #include "unity.h"
      6  1.1  christos 
      7  1.1  christos const char * Version = "stub unit test Version string";
      8  1.1  christos 
      9  1.1  christos // Hacks into the key database.
     10  1.1  christos extern struct key* key_ptr;
     11  1.1  christos extern int key_cnt;
     12  1.1  christos 
     13  1.1  christos 
     14  1.1  christos static struct pkt testpkt;
     15  1.1  christos static struct pkt testspkt;
     16  1.1  christos static sockaddr_u testsock;
     17  1.1  christos bool restoreKeyDb;
     18  1.1  christos 
     19  1.1  christos void PrepareAuthenticationTest(int key_id,
     20  1.1  christos 							   int key_len,
     21  1.1  christos 							   const char* type,
     22  1.1  christos 							   const void* key_seq) {
     23  1.1  christos 	char str[25];
     24  1.1  christos 	sprintf(str, "%d", key_id);
     25  1.1  christos 	ActivateOption("-a", str);
     26  1.1  christos 
     27  1.1  christos 	key_cnt = 1;
     28  1.1  christos 	key_ptr = malloc(sizeof(struct key));
     29  1.1  christos 	key_ptr->next = NULL;
     30  1.1  christos 	key_ptr->key_id = key_id;
     31  1.1  christos 	key_ptr->key_len = key_len;
     32  1.1  christos 	memcpy(key_ptr->type, "MD5", 3);
     33  1.1  christos 
     34  1.1  christos 	TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));
     35  1.1  christos 
     36  1.1  christos 	memcpy(key_ptr->key_seq, key_seq, key_ptr->key_len);
     37  1.1  christos 	restoreKeyDb = true;
     38  1.1  christos }
     39  1.1  christos 
     40  1.1  christos void PrepareAuthenticationTestMD5(int key_id,
     41  1.1  christos 							   int key_len,
     42  1.1  christos 							   const void* key_seq) {
     43  1.1  christos 	PrepareAuthenticationTest(key_id, key_len, "MD5", key_seq);
     44  1.1  christos }
     45  1.1  christos 
     46  1.1  christos void setUp() {
     47  1.1  christos 
     48  1.1  christos 	sntptest();
     49  1.1  christos 	restoreKeyDb = false;
     50  1.1  christos 
     51  1.1  christos 	/* Initialize the test packet and socket,
     52  1.1  christos 	 * so they contain at least some valid data. */
     53  1.1  christos 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
     54  1.1  christos 										MODE_SERVER);
     55  1.1  christos 	testpkt.stratum = STRATUM_REFCLOCK;
     56  1.1  christos 	memcpy(&testpkt.refid, "GPS\0", 4);
     57  1.1  christos 
     58  1.1  christos 	/* Set the origin timestamp of the received packet to the
     59  1.1  christos 	 * same value as the transmit timestamp of the sent packet. */
     60  1.1  christos 	l_fp tmp;
     61  1.1  christos 	tmp.l_ui = 1000UL;
     62  1.1  christos 	tmp.l_uf = 0UL;
     63  1.1  christos 
     64  1.1  christos 	HTONL_FP(&tmp, &testpkt.org);
     65  1.1  christos 	HTONL_FP(&tmp, &testspkt.xmt);
     66  1.1  christos 
     67  1.1  christos }
     68  1.1  christos 
     69  1.1  christos void tearDown() {
     70  1.1  christos 
     71  1.1  christos 	if (restoreKeyDb) {
     72  1.1  christos 		key_cnt = 0;
     73  1.1  christos 		free(key_ptr);
     74  1.1  christos 		key_ptr = NULL;
     75  1.1  christos 	}
     76  1.1  christos 
     77  1.1  christos 	sntptest_destroy(); //only on the final test!! if counter == 0 etc...
     78  1.1  christos 
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos 
     82  1.1  christos 
     83  1.1  christos void test_TooShortLength(void) {
     84  1.1  christos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
     85  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
     86  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
     87  1.1  christos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
     88  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
     89  1.1  christos 						  MODE_BROADCAST, &testspkt, "UnitTest"));
     90  1.1  christos }
     91  1.1  christos 
     92  1.1  christos void test_LengthNotMultipleOfFour(void) {
     93  1.1  christos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
     94  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 6,
     95  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
     96  1.1  christos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
     97  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 3,
     98  1.1  christos 						  MODE_BROADCAST, &testspkt, "UnitTest"));
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos void test_TooShortExtensionFieldLength(void) {
    102  1.1  christos 	/* The lower 16-bits are the length of the extension field.
    103  1.1  christos 	 * This lengths must be multiples of 4 bytes, which gives
    104  1.1  christos 	 * a minimum of 4 byte extension field length. */
    105  1.1  christos 	testpkt.exten[7] = htonl(3); // 3 bytes is too short.
    106  1.1  christos 
    107  1.1  christos 	/* We send in a pkt_len of header size + 4 byte extension
    108  1.1  christos 	 * header + 24 byte MAC, this prevents the length error to
    109  1.1  christos 	 * be caught at an earlier stage */
    110  1.1  christos 	int pkt_len = LEN_PKT_NOMAC + 4 + 24;
    111  1.1  christos 
    112  1.1  christos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
    113  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    114  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos void test_UnauthenticatedPacketReject(void) {
    118  1.1  christos 	//sntptest();
    119  1.1  christos 	// Activate authentication option
    120  1.1  christos 	ActivateOption("-a", "123");
    121  1.1  christos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
    122  1.1  christos 
    123  1.1  christos 	int pkt_len = LEN_PKT_NOMAC;
    124  1.1  christos 
    125  1.1  christos 	// We demand authentication, but no MAC header is present.
    126  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
    127  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    128  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos void test_CryptoNAKPacketReject(void) {
    132  1.1  christos 	// Activate authentication option
    133  1.1  christos 	ActivateOption("-a", "123");
    134  1.1  christos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
    135  1.1  christos 
    136  1.1  christos 	int pkt_len = LEN_PKT_NOMAC + 4; // + 4 byte MAC = Crypto-NAK
    137  1.1  christos 
    138  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
    139  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    140  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos void test_AuthenticatedPacketInvalid(void) {
    144  1.1  christos 	// Activate authentication option
    145  1.1  christos 	PrepareAuthenticationTestMD5(50, 9, "123456789");
    146  1.1  christos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
    147  1.1  christos 
    148  1.1  christos 	// Prepare the packet.
    149  1.1  christos 	int pkt_len = LEN_PKT_NOMAC;
    150  1.1  christos 
    151  1.1  christos 	testpkt.exten[0] = htonl(50);
    152  1.1  christos 	int mac_len = make_mac((char*)&testpkt, pkt_len,
    153  1.1  christos 						   MAX_MD5_LEN, key_ptr,
    154  1.1  christos 						   (char*)&testpkt.exten[1]);
    155  1.1  christos 
    156  1.1  christos 	pkt_len += 4 + mac_len;
    157  1.1  christos 
    158  1.1  christos 	// Now, alter the MAC so it becomes invalid.
    159  1.1  christos 	testpkt.exten[1] += 1;
    160  1.1  christos 
    161  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
    162  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    163  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    164  1.1  christos }
    165  1.1  christos 
    166  1.1  christos void test_AuthenticatedPacketUnknownKey(void) {
    167  1.1  christos 	// Activate authentication option
    168  1.1  christos 	PrepareAuthenticationTestMD5(30, 9, "123456789");
    169  1.1  christos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
    170  1.1  christos 
    171  1.1  christos 	// Prepare the packet. Observe that the Key-ID expected is 30,
    172  1.1  christos 	// but the packet has a key id of 50.
    173  1.1  christos 	int pkt_len = LEN_PKT_NOMAC;
    174  1.1  christos 
    175  1.1  christos 	testpkt.exten[0] = htonl(50);
    176  1.1  christos 	int mac_len = make_mac((char*)&testpkt, pkt_len,
    177  1.1  christos 						   MAX_MD5_LEN, key_ptr,
    178  1.1  christos 						   (char*)&testpkt.exten[1]);
    179  1.1  christos 	pkt_len += 4 + mac_len;
    180  1.1  christos 
    181  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
    182  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    183  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    184  1.1  christos }
    185  1.1  christos 
    186  1.1  christos void test_ServerVersionTooOld(void) {
    187  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    188  1.1  christos 
    189  1.1  christos 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
    190  1.1  christos 										NTP_OLDVERSION - 1,
    191  1.1  christos 										MODE_CLIENT);
    192  1.1  christos 	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.li_vn_mode) < NTP_OLDVERSION);
    193  1.1  christos 
    194  1.1  christos 	int pkt_len = LEN_PKT_NOMAC;
    195  1.1  christos 
    196  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
    197  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    198  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    199  1.1  christos }
    200  1.1  christos 
    201  1.1  christos void test_ServerVersionTooNew(void) {
    202  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    203  1.1  christos 
    204  1.1  christos 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
    205  1.1  christos 										NTP_VERSION + 1,
    206  1.1  christos 										MODE_CLIENT);
    207  1.1  christos 	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.li_vn_mode) > NTP_VERSION);
    208  1.1  christos 
    209  1.1  christos 	int pkt_len = LEN_PKT_NOMAC;
    210  1.1  christos 
    211  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
    212  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    213  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    214  1.1  christos }
    215  1.1  christos 
    216  1.1  christos void test_NonWantedMode(void) {
    217  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    218  1.1  christos 
    219  1.1  christos 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
    220  1.1  christos 										NTP_VERSION,
    221  1.1  christos 										MODE_CLIENT);
    222  1.1  christos 
    223  1.1  christos 	// The packet has a mode of MODE_CLIENT, but process_pkt expects MODE_SERVER
    224  1.1  christos 
    225  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
    226  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
    227  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    228  1.1  christos }
    229  1.1  christos 
    230  1.1  christos /* Tests bug 1597 */
    231  1.1  christos void test_KoDRate(void) {
    232  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    233  1.1  christos 
    234  1.1  christos 	testpkt.stratum = STRATUM_PKT_UNSPEC;
    235  1.1  christos 	memcpy(&testpkt.refid, "RATE", 4);
    236  1.1  christos 
    237  1.1  christos 	TEST_ASSERT_EQUAL(KOD_RATE,
    238  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
    239  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    240  1.1  christos }
    241  1.1  christos 
    242  1.1  christos void test_KoDDeny(void) {
    243  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    244  1.1  christos 
    245  1.1  christos 	testpkt.stratum = STRATUM_PKT_UNSPEC;
    246  1.1  christos 	memcpy(&testpkt.refid, "DENY", 4);
    247  1.1  christos 
    248  1.1  christos 	TEST_ASSERT_EQUAL(KOD_DEMOBILIZE,
    249  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
    250  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    251  1.1  christos }
    252  1.1  christos 
    253  1.1  christos void test_RejectUnsyncedServer(void) {
    254  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    255  1.1  christos 
    256  1.1  christos 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
    257  1.1  christos 										NTP_VERSION,
    258  1.1  christos 										MODE_SERVER);
    259  1.1  christos 
    260  1.1  christos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
    261  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
    262  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    263  1.1  christos }
    264  1.1  christos 
    265  1.1  christos void test_RejectWrongResponseServerMode(void) {
    266  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    267  1.1  christos 
    268  1.1  christos 	l_fp tmp;
    269  1.1  christos 	tmp.l_ui = 1000UL;
    270  1.1  christos 	tmp.l_uf = 0UL;
    271  1.1  christos 	HTONL_FP(&tmp, &testpkt.org);
    272  1.1  christos 
    273  1.1  christos 	tmp.l_ui = 2000UL;
    274  1.1  christos 	tmp.l_uf = 0UL;
    275  1.1  christos 	HTONL_FP(&tmp, &testspkt.xmt);
    276  1.1  christos 
    277  1.1  christos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
    278  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
    279  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    280  1.1  christos }
    281  1.1  christos 
    282  1.1  christos void test_AcceptNoSentPacketBroadcastMode(void) {
    283  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    284  1.1  christos 
    285  1.1  christos 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
    286  1.1  christos 					    NTP_VERSION,
    287  1.1  christos 					    MODE_BROADCAST);
    288  1.1  christos 
    289  1.1  christos 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
    290  1.1  christos 		  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
    291  1.1  christos 			      MODE_BROADCAST, NULL, "UnitTest"));
    292  1.1  christos }
    293  1.1  christos 
    294  1.1  christos void test_CorrectUnauthenticatedPacket(void) {
    295  1.1  christos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
    296  1.1  christos 
    297  1.1  christos 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
    298  1.1  christos 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
    299  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    300  1.1  christos }
    301  1.1  christos 
    302  1.1  christos void test_CorrectAuthenticatedPacketMD5(void) {
    303  1.1  christos 	PrepareAuthenticationTestMD5(10, 15, "123456789abcdef");
    304  1.1  christos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
    305  1.1  christos 
    306  1.1  christos 	int pkt_len = LEN_PKT_NOMAC;
    307  1.1  christos 
    308  1.1  christos 	// Prepare the packet.
    309  1.1  christos 	testpkt.exten[0] = htonl(10);
    310  1.1  christos 	int mac_len = make_mac((char*)&testpkt, pkt_len,
    311  1.1  christos 						   MAX_MD5_LEN, key_ptr,
    312  1.1  christos 						   (char*)&testpkt.exten[1]);
    313  1.1  christos 
    314  1.1  christos 	pkt_len += 4 + mac_len;
    315  1.1  christos 
    316  1.1  christos 	TEST_ASSERT_EQUAL(pkt_len,
    317  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    318  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    319  1.1  christos 
    320  1.1  christos }
    321  1.1  christos 
    322  1.1  christos void test_CorrectAuthenticatedPacketSHA1(void) {
    323  1.1  christos 	PrepareAuthenticationTest(20, 15, "SHA1", "abcdefghijklmno");
    324  1.1  christos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
    325  1.1  christos 
    326  1.1  christos 	int pkt_len = LEN_PKT_NOMAC;
    327  1.1  christos 
    328  1.1  christos 	// Prepare the packet.
    329  1.1  christos 	testpkt.exten[0] = htonl(20);
    330  1.1  christos 	int mac_len = make_mac((char*)&testpkt, pkt_len,
    331  1.1  christos 						   MAX_MAC_LEN, key_ptr,
    332  1.1  christos 						   (char*)&testpkt.exten[1]);
    333  1.1  christos 
    334  1.1  christos 	pkt_len += 4 + mac_len;
    335  1.1  christos 
    336  1.1  christos 	TEST_ASSERT_EQUAL(pkt_len,
    337  1.1  christos 			  process_pkt(&testpkt, &testsock, pkt_len,
    338  1.1  christos 						  MODE_SERVER, &testspkt, "UnitTest"));
    339  1.1  christos }
    340