unittests/lib/packet_parser_test.c in trema-0.2.2.1 vs unittests/lib/packet_parser_test.c in trema-0.2.3
- old
+ new
@@ -1,10 +1,8 @@
/*
* Unit tests for packet_parser functions and macros.
*
- * Author: Kazuya Suzuki
- *
* Copyright (C) 2008-2012 NEC Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
@@ -44,24 +42,24 @@
FILE *fp = fopen( filename, "r" );
if ( fp == NULL ) {
// "Can't open a file of test data."
return NULL;
}
-
- // Skip
+
+ // Skip
if ( fseek( fp, sizeof( struct pcap_file_header ) + sizeof( uint32_t ) * 2,
SEEK_CUR ) != 0 ) {
fclose( fp );
return NULL;
- }
-
+ }
+
uint32_t length[ 2 ];
size_t size = fread( &length, 1, sizeof( length ), fp );
if ( size < sizeof( length ) ) {
fclose( fp );
return NULL;
- }
+ }
buffer *buffer = alloc_buffer();
if ( buffer == NULL ) {
fclose( fp );
return NULL;
@@ -69,11 +67,11 @@
size = fread( append_back_buffer( buffer, length[ 0 ] ), 1, length[ 0 ], fp );
if ( size < buffer->length ) {
free_buffer( buffer );
fclose( fp );
return NULL;
- }
+ }
fclose( fp );
return buffer;
}
@@ -90,11 +88,11 @@
assert_true( parse_packet( buffer ) );
packet_info *packet_info = buffer->user_data;
assert_int_equal( packet_info->format, ETH_8023_SNAP );
-
+
u_char macda[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
u_char macsa[] = { 0x00, 0x19, 0xdb, 0x17, 0xb9, 0x6f };
assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
assert_true( packet_info->eth_type < ETH_MTU );
@@ -139,17 +137,47 @@
assert_int_equal( packet_info->arp_ar_op, 1 );
assert_int_equal( packet_info->arp_spa, 0xc0a8642c );
assert_memory_equal( packet_info->arp_sha, macsa, ETH_ADDRLEN );
assert_int_equal( packet_info->arp_tpa, 0xc0a8642b );
u_char maczero[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- assert_memory_equal( packet_info->arp_tha, maczero, ETH_ADDRLEN );
+ assert_memory_equal( packet_info->arp_tha, maczero, ETH_ADDRLEN );
+ assert_true( packet_type_arp_request( buffer ) );
+ assert_false( packet_type_arp_reply( buffer ) );
+
free_buffer( buffer );
}
static void
+test_parse_packet_ipv6_succeeds() {
+ const char filename[] = "./unittests/lib/test_packets/icmp6_echo_req.cap";
+ buffer *buffer = store_packet_to_buffer( filename );
+
+ assert_true( parse_packet( buffer ) );
+
+ packet_info *packet_info = buffer->user_data;
+
+ assert_int_equal( packet_info->ipv6_version, 6 );
+ assert_int_equal( packet_info->ipv6_tc, 0 );
+ assert_int_equal( packet_info->ipv6_flowlabel, 0 );
+ assert_int_equal( packet_info->ipv6_plen, 0x40 );
+ assert_int_equal( packet_info->ipv6_nexthdr, 0x3a );
+ assert_int_equal( packet_info->ipv6_hoplimit, 0x40 );
+
+ u_char saddr[] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x8e, 0x89, 0xa5, 0xff, 0xfe, 0x16, 0x22, 0x09 };
+ u_char daddr[] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x8e, 0x89, 0xa5, 0xff, 0xfe, 0x15, 0x84, 0xcb };
+ assert_memory_equal( packet_info->ipv6_saddr, saddr, IPV6_ADDRLEN );
+ assert_memory_equal( packet_info->ipv6_daddr, daddr, IPV6_ADDRLEN );
+
+ free_buffer( buffer );
+}
+
+
+static void
test_parse_packet_udp_succeeds() {
const char filename[] = "./unittests/lib/test_packets/udp.cap";
buffer *buffer = store_packet_to_buffer( filename );
assert_true( parse_packet( buffer ) );
@@ -161,11 +189,11 @@
u_char macda[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
u_char macsa[] = { 0x00, 0x21, 0x85, 0x91, 0x92, 0xdb };
assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );
-
+
assert_int_equal( packet_info->l2_payload_length, 76 );
assert_int_equal( packet_info->ipv4_version, 4 );
assert_int_equal( packet_info->ipv4_ihl, 5 );
assert_int_equal( packet_info->ipv4_tos, 0 );
@@ -210,11 +238,11 @@
assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );
assert_int_equal( packet_info->l2_payload_length, 1500 );
-
+
assert_int_equal( packet_info->ipv4_version, 4 );
assert_int_equal( packet_info->ipv4_ihl, 5 );
assert_int_equal( packet_info->ipv4_tos, 0 );
assert_int_equal( packet_info->ipv4_tot_len, 0x05dc );
assert_int_equal( packet_info->ipv4_id, 0x2b33 );
@@ -255,11 +283,11 @@
u_char macda[] = { 0x8c, 0x89, 0xa5, 0x15, 0x84, 0xcb };
u_char macsa[] = { 0x8c, 0x89, 0xa5, 0x16, 0x22, 0x09 };
assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );
-
+
assert_int_equal( packet_info->l2_payload_length, 1500 );
assert_int_equal( packet_info->ipv4_version, 4 );
assert_int_equal( packet_info->ipv4_ihl, 5 );
assert_int_equal( packet_info->ipv4_tos, 0 );
@@ -304,11 +332,11 @@
u_char macda[] = { 0x00, 0x16, 0x17, 0x00, 0x43, 0xf3 };
u_char macsa[] = { 0x8c, 0x89, 0xa5, 0x15, 0x84, 0xcb };
assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );
-
+
assert_int_equal( packet_info->l2_payload_length, 60 );
assert_int_equal( packet_info->ipv4_version, 4 );
assert_int_equal( packet_info->ipv4_ihl, 5 );
assert_int_equal( packet_info->ipv4_tos, 0x10 );
@@ -354,11 +382,11 @@
u_char macda[] = { 0x8c, 0x89, 0xa5, 0x15, 0x84, 0xcb };
u_char macsa[] = { 0x00, 0x16, 0x17, 0x00, 0x43, 0xf3 };
assert_memory_equal( packet_info->eth_macda, macda, ETH_ADDRLEN );
assert_memory_equal( packet_info->eth_macsa, macsa, ETH_ADDRLEN );
assert_int_equal( packet_info->eth_type, ETH_ETHTYPE_IPV4 );
-
+
assert_int_equal( packet_info->l2_payload_length, 82 );
assert_int_equal( packet_info->ipv4_version, 4 );
assert_int_equal( packet_info->ipv4_ihl, 5 );
assert_int_equal( packet_info->ipv4_tos, 0 );
@@ -433,10 +461,15 @@
uint16_t sample = ntohs( * ( uint16_t * ) packet_info->l4_payload );
assert_int_equal( sample, 0xa0a9 );
assert_int_equal( packet_info->l4_payload_length, 56 );
+ assert_true( packet_type_icmpv4_echo_request( buffer ) );
+ assert_false( packet_type_icmpv4_dst_unreach( buffer ) );
+ assert_false( packet_type_icmpv4_redirect( buffer ) );
+ assert_false( packet_type_icmpv4_echo_reply( buffer ) );
+
free_buffer( buffer );
}
static void
@@ -486,10 +519,15 @@
uint16_t sample = ntohs( * ( uint16_t * ) packet_info->l4_payload );
assert_int_equal( sample, 0x6162 );
assert_int_equal( packet_info->l4_payload_length, 32 );
+ assert_true( packet_type_icmpv4_echo_request( buffer ) );
+ assert_false( packet_type_icmpv4_dst_unreach( buffer ) );
+ assert_false( packet_type_icmpv4_redirect( buffer ) );
+ assert_false( packet_type_icmpv4_echo_reply( buffer ) );
+
free_buffer( buffer );
}
static void
@@ -539,10 +577,15 @@
uint16_t sample = ntohs( * ( uint16_t * ) packet_info->l4_payload );
assert_int_equal( sample, 0x6162 );
assert_int_equal( packet_info->l4_payload_length, 32 );
+ assert_false( packet_type_icmpv4_echo_request( buffer ) );
+ assert_false( packet_type_icmpv4_dst_unreach( buffer ) );
+ assert_false( packet_type_icmpv4_redirect( buffer ) );
+ assert_true( packet_type_icmpv4_echo_reply( buffer ) );
+
free_buffer( buffer );
}
static void
@@ -669,9 +712,11 @@
main() {
UnitTest tests[] = {
unit_test( test_parse_packet_snap_succeeds ),
unit_test( test_parse_packet_arp_request_succeeds ),
+
+ unit_test( test_parse_packet_ipv6_succeeds ),
unit_test( test_parse_packet_udp_succeeds ),
unit_test( test_parse_packet_udp_fragmented_head_succeeds ),
unit_test( test_parse_packet_udp_fragmented_next_succeeds ),