@@ -35,14 +35,14 @@ public void testParsingTCPFrameInsideNetworkFrame() throws Exception {
3535 final JBBPBitInputStream netPacketStream = new JBBPBitInputStream (getResourceAsInputStream ("tcppacket.bin" ));
3636 try {
3737
38- // Minimal ethernet header without 802.1Q tag
38+ // Ethernet header Ethernet II
3939 final JBBPParser ethernetParserHeaderWithout802_1QTag = JBBPParser .prepare (
4040 "byte[6] MacDestination;"
4141 +"byte[6] MacSource;"
42- +"ushort EthertypeOrLength ;"
42+ +"ushort EtherTypeOrLength ;"
4343 );
4444
45- // Minimal IP header without options
45+ // IPv4 header
4646 final JBBPParser ipParserHeaderWithoutOptions = JBBPParser .prepare (
4747 "bit:4 InternetHeaderLength;"
4848 +"bit:4 Version;"
@@ -59,6 +59,7 @@ public void testParsingTCPFrameInsideNetworkFrame() throws Exception {
5959 +"byte [(InternetHeaderLength-5)*4] Options;"
6060 );
6161
62+ // TCP header
6263 final JBBPParser tcpHeader = JBBPParser .prepare (
6364 "ushort SourcePort;"
6465 + "ushort DestinationPort;"
@@ -84,36 +85,46 @@ public void testParsingTCPFrameInsideNetworkFrame() throws Exception {
8485
8586 // Check Ethernet header
8687 final JBBPFieldStruct parsedEthernetHeader = ethernetParserHeaderWithout802_1QTag .parse (netPacketStream );
87- assertArrayEquals (new byte []{(byte )0x60 , (byte )0x67 , (byte )0x20 , (byte )0xE1 , (byte )0xF9 , (byte )0xF8 },parsedEthernetHeader .findFieldForNameAndType ("MacDestination" , JBBPFieldArrayByte .class ).getArray ());
88- assertArrayEquals (new byte []{(byte )0x00 , (byte )0x26 , (byte )0x44 , (byte )0x74 , (byte )0xFE , (byte )0x66 },parsedEthernetHeader .findFieldForNameAndType ("MacSource" , JBBPFieldArrayByte .class ).getArray ());
89- assertEquals (0x800 , parsedEthernetHeader .findFieldForNameAndType ("EthertypeOrLength" , JBBPFieldUShort .class ).getAsInt ());
88+ assertArrayEquals ("Destination MAC" ,new byte []{(byte )0x60 , (byte )0x67 , (byte )0x20 , (byte )0xE1 , (byte )0xF9 , (byte )0xF8 },parsedEthernetHeader .findFieldForNameAndType ("MacDestination" , JBBPFieldArrayByte .class ).getArray ());
89+ assertArrayEquals ("Source MAC" ,new byte []{(byte )0x00 , (byte )0x26 , (byte )0x44 , (byte )0x74 , (byte )0xFE , (byte )0x66 },parsedEthernetHeader .findFieldForNameAndType ("MacSource" , JBBPFieldArrayByte .class ).getArray ());
90+
91+ final int etherTypeOrLength = parsedEthernetHeader .findFieldForNameAndType ("EtherTypeOrLength" , JBBPFieldUShort .class ).getAsInt ();
92+ assertEquals ("Ethernet type or length" , 0x800 , etherTypeOrLength );
93+
94+ if (etherTypeOrLength >=1536 ) {
95+ // list of protocols http://standards-oui.ieee.org/ethertype/eth.txt
96+ System .out .println ("Ethernet type is : 0x" +Integer .toHexString (etherTypeOrLength ).toUpperCase ());
97+ } else {
98+ System .out .println ("Payload length : " + etherTypeOrLength );
99+ }
100+
90101
91102 // Check IP header
92103 netPacketStream .resetCounter ();
93104 final JBBPFieldStruct parsedIPHeader = ipParserHeaderWithoutOptions .parse (netPacketStream );
94105
95- assertEquals (4 , parsedIPHeader .findFieldForNameAndType ("Version" ,JBBPFieldBit .class ).getAsInt ());
106+ assertEquals ("IP Version" , 4 , parsedIPHeader .findFieldForNameAndType ("Version" ,JBBPFieldBit .class ).getAsInt ());
96107
97108 final int internetHeaderLength = parsedIPHeader .findFieldForNameAndType ("InternetHeaderLength" , JBBPFieldBit .class ).getAsInt ();
98- assertEquals (5 , internetHeaderLength );
99- assertEquals (0 , parsedIPHeader .findFieldForNameAndType ("DSCP" ,JBBPFieldBit .class ).getAsInt ());
100- assertEquals (0 , parsedIPHeader .findFieldForNameAndType ("ECN" ,JBBPFieldBit .class ).getAsInt ());
109+ assertEquals ("Length of the IP header (in 4 byte items)" , 5 , internetHeaderLength );
110+ assertEquals ("Differentiated Services Code Point" , 0 , parsedIPHeader .findFieldForNameAndType ("DSCP" ,JBBPFieldBit .class ).getAsInt ());
111+ assertEquals ("Explicit Congestion Notification" , 0 , parsedIPHeader .findFieldForNameAndType ("ECN" ,JBBPFieldBit .class ).getAsInt ());
101112
102113 final int ipTotalPacketLength = parsedIPHeader .findFieldForNameAndType ("TotalPacketLength" , JBBPFieldUShort .class ).getAsInt ();
103114
104- assertEquals (159 , ipTotalPacketLength );
105- assertEquals (30810 , parsedIPHeader .findFieldForNameAndType ("Identification" ,JBBPFieldUShort .class ).getAsInt ());
115+ assertEquals ("Entire IP packet size, including header and data, in bytes" , 159 , ipTotalPacketLength );
116+ assertEquals ("Identification" , 30810 , parsedIPHeader .findFieldForNameAndType ("Identification" ,JBBPFieldUShort .class ).getAsInt ());
106117
107118 final int ipFlagsAndFragmentOffset = parsedIPHeader .findFieldForNameAndType ("IPFlagsAndFragmentOffset" , JBBPFieldUShort .class ).getAsInt ();
108119
109120 assertEquals ("Extracted IP flags" , 0x2 , ipFlagsAndFragmentOffset >>>13 );
110121 assertEquals ("Extracted Fragment offset" , 0x00 , ipFlagsAndFragmentOffset & 0x1FFF );
111122
112- assertEquals (0x39 , parsedIPHeader .findFieldForNameAndType ("TTL" ,JBBPFieldUByte .class ).getAsInt ());
113- assertEquals (0x06 , parsedIPHeader .findFieldForNameAndType ("Protocol" ,JBBPFieldUByte .class ).getAsInt ());
114- assertEquals (0x7DB6 , parsedIPHeader .findFieldForNameAndType ("HeaderChecksum" ,JBBPFieldUShort .class ).getAsInt ());
115- assertEquals (0xD5C7B393 , parsedIPHeader .findFieldForNameAndType ("SourceAddress" ,JBBPFieldInt .class ).getAsInt ());
116- assertEquals (0xC0A80145 , parsedIPHeader .findFieldForNameAndType ("DestinationAddress" ,JBBPFieldInt .class ).getAsInt ());
123+ assertEquals ("Time To Live" , 0x39 , parsedIPHeader .findFieldForNameAndType ("TTL" ,JBBPFieldUByte .class ).getAsInt ());
124+ assertEquals ("Protocol (RFC-790)" , 0x06 , parsedIPHeader .findFieldForNameAndType ("Protocol" ,JBBPFieldUByte .class ).getAsInt ());
125+ assertEquals ("IPv4 Header Checksum" , 0x7DB6 , parsedIPHeader .findFieldForNameAndType ("HeaderChecksum" ,JBBPFieldUShort .class ).getAsInt ());
126+ assertEquals ("Source IP address" , 0xD5C7B393 , parsedIPHeader .findFieldForNameAndType ("SourceAddress" ,JBBPFieldInt .class ).getAsInt ());
127+ assertEquals ("Destination IP address" , 0xC0A80145 , parsedIPHeader .findFieldForNameAndType ("DestinationAddress" ,JBBPFieldInt .class ).getAsInt ());
117128
118129 assertEquals (0 , parsedIPHeader .findFieldForNameAndType ("Options" , JBBPFieldArrayByte .class ).getArray ().length );
119130
@@ -150,10 +161,10 @@ public void testParsingTCPFrameInsideNetworkFrame() throws Exception {
150161 final byte [] data = netPacketStream .readByteArray (payloadDataLength );
151162 assertEquals (119 , data .length );
152163
153- System .out .println (new JBBPTextWriter (new StringWriter ()).Comment ("Payload data of the Net packet " ).Byte (data ).BR ().toString ());
164+ System .out .println (new JBBPTextWriter (new StringWriter ()).Comment ("Payload data extracted from the TCP part " ).Byte (data ).BR ().toString ());
154165
155- final byte [] rest = netPacketStream .readByteArray (-1 );
156- assertEquals (0 ,rest .length );
166+ final byte [] restOfFrame = netPacketStream .readByteArray (-1 );
167+ assertEquals (0 ,restOfFrame .length );
157168
158169 } finally {
159170 JBBPUtils .closeQuietly (netPacketStream );
0 commit comments