From 1829548cd59531c208c5af270c047051e65ff83c Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Mon, 28 Oct 2024 08:30:07 +0100 Subject: [PATCH] More unit tests --- test/unit/unit.c | 110 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 107 insertions(+), 3 deletions(-) diff --git a/test/unit/unit.c b/test/unit/unit.c index 12b8804..88543da 100644 --- a/test/unit/unit.c +++ b/test/unit/unit.c @@ -582,6 +582,104 @@ START_TEST(test_arp_lookup_failure) { END_TEST +// Test for `transport_checksum` calculation +START_TEST(test_transport_checksum) { + union transport_pseudo_header ph; + struct ipstack_tcp_seg tcp_data; + memset(&ph, 0, sizeof(ph)); + memset(&tcp_data, 0, sizeof(tcp_data)); + + // Set up pseudo-header values for test + ph.ph.src = 0xc0a80101; // 192.168.1.1 + ph.ph.dst = 0xc0a80102; // 192.168.1.2 + ph.ph.proto = IPPROTO_TCP; + ph.ph.len = ee16(20); // TCP header length (without options) + + // Test with a simple TCP header with src/dst ports and no data + tcp_data.src_port = ee16(12345); + tcp_data.dst_port = ee16(80); + tcp_data.seq = ee32(1); + tcp_data.ack = ee32(0); + tcp_data.hlen = 5; // offset=5 (20 bytes) + tcp_data.flags = 0x02; // SYN + tcp_data.win = ee16(65535); + + uint16_t checksum = transport_checksum(&ph, &tcp_data.src_port); + ck_assert_msg(checksum != 0, "Transport checksum should not be zero"); +} +END_TEST + +// Test for `iphdr_set_checksum` calculation +START_TEST(test_iphdr_set_checksum) { + struct ipstack_ip_packet ip; + memset(&ip, 0, sizeof(ip)); + + ip.ver_ihl = 0x45; + ip.tos = 0; + ip.len = ee16(20); + ip.id = ee16(1); + ip.flags_fo = 0; + ip.ttl = 64; + ip.proto = IPPROTO_TCP; + ip.src = ee32(0xc0a80101); // 192.168.1.1 + ip.dst = ee32(0xc0a80102); // 192.168.1.2 + + iphdr_set_checksum(&ip); + ck_assert_msg(ip.csum != 0, "IP header checksum should not be zero"); +} +END_TEST + +// Test for `eth_output_add_header` to add Ethernet headers +START_TEST(test_eth_output_add_header) { + struct ipstack_eth_frame eth_frame; + struct ipstack S; + memset(&S, 0, sizeof(S)); + memset(ð_frame, 0, sizeof(eth_frame)); + + uint8_t test_mac[6] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; + memcpy(S.ll_dev.mac, test_mac, 6); + + eth_output_add_header(&S, NULL, ð_frame, ETH_TYPE_IP); + + ck_assert_mem_eq(eth_frame.dst, "\xff\xff\xff\xff\xff\xff", 6); // Broadcast + ck_assert_mem_eq(eth_frame.src, test_mac, 6); + ck_assert_uint_eq(eth_frame.type, ee16(ETH_TYPE_IP)); +} +END_TEST + +// Test for `ip_output_add_header` to set up IP headers and calculate checksums +START_TEST(test_ip_output_add_header) { + struct tsocket t; + struct ipstack_ip_packet ip; + struct ipstack S; + memset(&t, 0, sizeof(t)); + memset(&ip, 0, sizeof(ip)); + memset(&S, 0, sizeof(S)); + + // Setup socket and IP stack parameters + t.local_ip = 0xc0a80101; // 192.168.1.1 + t.remote_ip = 0xc0a80102; // 192.168.1.2 + t.S = &S; + + // Run the function for a TCP packet + int result = ip_output_add_header(&t, &ip, IPPROTO_TCP, 40); + ck_assert_int_eq(result, 0); + + // Validate IP header fields + ck_assert_uint_eq(ip.ver_ihl, 0x45); + ck_assert_uint_eq(ip.ttl, 64); + ck_assert_uint_eq(ip.proto, IPPROTO_TCP); + ck_assert_uint_eq(ip.src, ee32(t.local_ip)); + ck_assert_uint_eq(ip.dst, ee32(t.remote_ip)); + ck_assert_msg(ip.csum != 0, "IP header checksum should not be zero"); + + // Check the pseudo-header checksum calculation for TCP segment + struct ipstack_tcp_seg *tcp = (struct ipstack_tcp_seg *)&ip; + ck_assert_msg(tcp->csum != 0, "TCP checksum should not be zero"); +} +END_TEST + + Suite *femto_suite(void) { @@ -662,9 +760,15 @@ Suite *femto_suite(void) suite_add_tcase(s, tc_proto); tcase_add_test(tc_proto, test_arp_lookup_failure); suite_add_tcase(s, tc_proto); - - - + + tcase_add_test(tc_utils, test_transport_checksum); + suite_add_tcase(s, tc_proto); + tcase_add_test(tc_utils, test_iphdr_set_checksum); + suite_add_tcase(s, tc_proto); + tcase_add_test(tc_utils, test_eth_output_add_header); + suite_add_tcase(s, tc_proto); + tcase_add_test(tc_utils, test_ip_output_add_header); + suite_add_tcase(s, tc_proto); return s; }