1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.pcap
.core
.tests
.protocol
.ipv4
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
18 import static org
.junit
.Assert
.fail
;
19 import static org
.junit
.Assume
.assumeTrue
;
21 import java
.io
.IOException
;
22 import java
.net
.InetAddress
;
23 import java
.nio
.ByteBuffer
;
24 import java
.nio
.ByteOrder
;
25 import java
.util
.Arrays
;
26 import java
.util
.LinkedHashMap
;
29 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.packet
.BadPacketException
;
30 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.Protocol
;
31 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.ipv4
.IPv4Endpoint
;
32 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.ipv4
.IPv4Packet
;
33 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.BadPcapFileException
;
34 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.PcapFile
;
35 import org
.eclipse
.linuxtools
.pcap
.core
.tests
.shared
.PcapTestTrace
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
40 * JUnit Class that tests the IPv4Packet class and its method.
42 * @author Vincent Perot
44 public class IPv4PacketTest
{
46 private static final Map
<String
, String
> EXPECTED_FIELDS
;
48 EXPECTED_FIELDS
= new LinkedHashMap
<>();
49 EXPECTED_FIELDS
.put("Version", "4");
50 EXPECTED_FIELDS
.put("Header Length", "24 bytes");
51 EXPECTED_FIELDS
.put("Differentiated Services Field", "0x26");
52 EXPECTED_FIELDS
.put("Explicit Congestion Notification", "0x02");
53 EXPECTED_FIELDS
.put("Total Length", "255 bytes");
54 EXPECTED_FIELDS
.put("Identification", "0x0ff0");
55 EXPECTED_FIELDS
.put("Don't Fragment Flag", "false");
56 EXPECTED_FIELDS
.put("More Fragment Flag", "false");
57 EXPECTED_FIELDS
.put("Fragment Offset", "7905");
58 EXPECTED_FIELDS
.put("Time to live", "160");
59 EXPECTED_FIELDS
.put("Protocol", "Unknown (254)");
60 EXPECTED_FIELDS
.put("Checksum", "0x3344");
61 EXPECTED_FIELDS
.put("Source IP Address", "192.168.1.0");
62 EXPECTED_FIELDS
.put("Destination IP Address", "193.169.2.1");
63 EXPECTED_FIELDS
.put("Options", "a2 56 a2 56");
66 private static final String EXPECTED_TOSTRING
;
68 StringBuilder sb
= new StringBuilder();
69 sb
.append("Internet Protocol Version 4, Source: 192.168.1.0, Destination: 193.169.2.1\n");
70 sb
.append("Version: 4, Identification: 0x0ff0, Header Length: 24 bytes, Total Length: 255 bytes\n");
71 sb
.append("Differentiated Services Code Point: 0x26; Explicit Congestion Notification: 0x02\n");
72 sb
.append("Flags: 0x00 (Don't have more fragments), Fragment Offset: 7905\n");
73 sb
.append("Time to live: 160\n");
74 sb
.append("Protocol: 254\n");
75 sb
.append("Header Checksum: 0x3344\n");
76 sb
.append("Payload: a6");
78 EXPECTED_TOSTRING
= sb
.toString();
81 private ByteBuffer fPacket
;
84 * Initialize the packet.
87 public void initialize() {
88 fPacket
= ByteBuffer
.allocate(25);
89 fPacket
.order(ByteOrder
.BIG_ENDIAN
);
92 fPacket
.put((byte) 0x46);
95 fPacket
.put((byte) 0x9A);
97 // Total length - this is randomly chosen so that we verify that the
98 // packet handles wrong total length.
99 fPacket
.put((byte) 0x00);
100 fPacket
.put((byte) 0xFF);
103 fPacket
.put((byte) 0x0F);
104 fPacket
.put((byte) 0xF0);
106 // Flags + Fragment Offset
107 fPacket
.put((byte) 0x1E);
108 fPacket
.put((byte) 0xE1);
111 fPacket
.put((byte) 0xA0);
113 // Protocol - Unknown
114 fPacket
.put((byte) 0xFE);
116 // Header checksum - chosen randomly
117 fPacket
.put((byte) 0x33);
118 fPacket
.put((byte) 0x44);
120 // Source IP - 4 bytes
121 fPacket
.put((byte) 192);
122 fPacket
.put((byte) 168);
123 fPacket
.put((byte) 1);
124 fPacket
.put((byte) 0);
126 // Destination IP - 4 bytes
127 fPacket
.put((byte) 193);
128 fPacket
.put((byte) 169);
129 fPacket
.put((byte) 2);
130 fPacket
.put((byte) 1);
133 fPacket
.put((byte) 0xA2);
134 fPacket
.put((byte) 0x56);
135 fPacket
.put((byte) 0xA2);
136 fPacket
.put((byte) 0x56);
139 fPacket
.put((byte) 0xA6);
145 * Test that verify the correctness of the IPv4Packet's methods.
147 * @throws BadPcapFileException
148 * Thrown when the file is erroneous. Fails the test.
149 * @throws IOException
150 * Thrown when an IO error occurs. Fails the test.
151 * @throws BadPacketException
152 * Thrown when a packet is erroneous. Fails the test.
155 public void CompleteIPv4PacketTest() throws IOException
, BadPcapFileException
, BadPacketException
{
156 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
157 assumeTrue(trace
.exists());
158 try (PcapFile dummy
= new PcapFile(trace
.getPath())) {
159 ByteBuffer byteBuffer
= fPacket
;
160 if (byteBuffer
== null) {
161 fail("CompleteIPv4PacketTest has failed!");
164 IPv4Packet packet
= new IPv4Packet(dummy
, null, byteBuffer
);
167 assertEquals(Protocol
.IPV4
, packet
.getProtocol());
168 assertTrue(packet
.hasProtocol(Protocol
.IPV4
));
169 assertTrue(packet
.hasProtocol(Protocol
.UNKNOWN
));
170 assertFalse(packet
.hasProtocol(Protocol
.TCP
));
172 // Abstract methods Testing
173 assertTrue(packet
.validate());
174 assertEquals(-222021887, packet
.hashCode());
175 assertFalse(packet
.equals(null));
176 assertEquals(new IPv4Packet(dummy
, null, byteBuffer
), packet
);
178 assertEquals(EXPECTED_FIELDS
, packet
.getFields());
179 assertEquals(EXPECTED_TOSTRING
, packet
.toString());
180 assertEquals("Src: 192.168.1.0 , Dst: 193.169.2.1", packet
.getLocalSummaryString());
181 assertEquals("192.168.1.0 > 193.169.2.1 Id=4080 Len=1", packet
.getGlobalSummaryString());
183 assertEquals(new IPv4Endpoint(packet
, true), packet
.getSourceEndpoint());
184 assertEquals(new IPv4Endpoint(packet
, false), packet
.getDestinationEndpoint());
186 fPacket
.position(24);
187 byte[] payload
= new byte[1];
188 fPacket
.get(payload
);
189 assertEquals(ByteBuffer
.wrap(payload
), packet
.getPayload());
191 // Packet-specific methods Testing
192 assertEquals(InetAddress
.getByAddress(Arrays
.copyOfRange(fPacket
.array(), 12, 16)), packet
.getSourceIpAddress());
193 assertEquals(InetAddress
.getByAddress(Arrays
.copyOfRange(fPacket
.array(), 16, 20)), packet
.getDestinationIpAddress());
194 assertTrue(Arrays
.equals(packet
.getOptions(), Arrays
.copyOfRange(fPacket
.array(), 20, 24)));
195 assertEquals(4, packet
.getVersion());
196 assertEquals(24, packet
.getHeaderLength());
197 assertEquals(0x26, packet
.getDSCP());
198 assertEquals(0x02, packet
.getExplicitCongestionNotification());
199 assertEquals(255, packet
.getTotalLength());
200 assertEquals(0x0FF0, packet
.getIdentification());
201 assertFalse(packet
.getReservedFlag());
202 assertFalse(packet
.getDontFragmentFlag());
203 assertFalse(packet
.getHasMoreFragment());
204 assertEquals(7905, packet
.getFragmentOffset());
205 assertEquals(160, packet
.getTimeToLive());
206 assertEquals(0xFE, packet
.getIpDatagramProtocol());
207 assertEquals(0x3344, packet
.getHeaderChecksum());
This page took 0.037849 seconds and 6 git commands to generate.