lttng: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core.tests / src / org / eclipse / linuxtools / pcap / core / tests / protocol / ipv4 / IPv4PacketTest.java
CommitLineData
a1d21447
VP
1/*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.pcap.core.tests.protocol.ipv4;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertFalse;
17import static org.junit.Assert.assertTrue;
18import static org.junit.Assert.fail;
19import static org.junit.Assume.assumeTrue;
20
21import java.io.IOException;
d6fca387 22import java.net.InetAddress;
a1d21447
VP
23import java.nio.ByteBuffer;
24import java.nio.ByteOrder;
25import java.util.Arrays;
26import java.util.LinkedHashMap;
27import java.util.Map;
28
93d1d135 29import org.eclipse.linuxtools.internal.pcap.core.packet.BadPacketException;
c88feda9 30import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol;
93d1d135
AM
31import org.eclipse.linuxtools.internal.pcap.core.protocol.ipv4.IPv4Endpoint;
32import org.eclipse.linuxtools.internal.pcap.core.protocol.ipv4.IPv4Packet;
33import org.eclipse.linuxtools.internal.pcap.core.trace.BadPcapFileException;
34import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile;
a1d21447 35import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
a1d21447
VP
36import org.junit.Before;
37import org.junit.Test;
38
39/**
40 * JUnit Class that tests the IPv4Packet class and its method.
41 *
42 * @author Vincent Perot
43 */
44public class IPv4PacketTest {
45
a1d21447
VP
46 private static final Map<String, String> EXPECTED_FIELDS;
47 static {
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");
d6fca387 57 EXPECTED_FIELDS.put("Fragment Offset", "7905");
a1d21447
VP
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");
64 }
65
66 private static final String EXPECTED_TOSTRING;
67 static {
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");
d6fca387 72 sb.append("Flags: 0x00 (Don't have more fragments), Fragment Offset: 7905\n");
a1d21447
VP
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");
77
78 EXPECTED_TOSTRING = sb.toString();
79 }
80
81 private ByteBuffer fPacket;
82
83 /**
84 * Initialize the packet.
85 */
86 @Before
87 public void initialize() {
88 fPacket = ByteBuffer.allocate(25);
89 fPacket.order(ByteOrder.BIG_ENDIAN);
90
91 // Version + IHL
92 fPacket.put((byte) 0x46);
93
94 // DSCP + ECN
95 fPacket.put((byte) 0x9A);
96
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);
101
102 // Identification
103 fPacket.put((byte) 0x0F);
104 fPacket.put((byte) 0xF0);
105
106 // Flags + Fragment Offset
107 fPacket.put((byte) 0x1E);
108 fPacket.put((byte) 0xE1);
109
110 // Time to live
111 fPacket.put((byte) 0xA0);
112
113 // Protocol - Unknown
114 fPacket.put((byte) 0xFE);
115
116 // Header checksum - chosen randomly
117 fPacket.put((byte) 0x33);
118 fPacket.put((byte) 0x44);
119
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);
125
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);
131
132 // Options - 4 bytes
133 fPacket.put((byte) 0xA2);
134 fPacket.put((byte) 0x56);
135 fPacket.put((byte) 0xA2);
136 fPacket.put((byte) 0x56);
137
138 // Payload - 1 byte
139 fPacket.put((byte) 0xA6);
140
141 fPacket.flip();
142 }
143
144 /**
145 * Test that verify the correctness of the IPv4Packet's methods.
d6fca387 146 *
a1d21447
VP
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.
153 */
154 @Test
155 public void CompleteIPv4PacketTest() throws IOException, BadPcapFileException, BadPacketException {
156 PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
157 assumeTrue(trace.exists());
333a2acb 158 try (PcapFile dummy = new PcapFile(trace.getPath())) {
a1d21447
VP
159 ByteBuffer byteBuffer = fPacket;
160 if (byteBuffer == null) {
161 fail("CompleteIPv4PacketTest has failed!");
162 return;
163 }
164 IPv4Packet packet = new IPv4Packet(dummy, null, byteBuffer);
165
166 // Protocol Testing
c88feda9
AM
167 assertEquals(PcapProtocol.IPV4, packet.getProtocol());
168 assertTrue(packet.hasProtocol(PcapProtocol.IPV4));
169 assertTrue(packet.hasProtocol(PcapProtocol.UNKNOWN));
170 assertFalse(packet.hasProtocol(PcapProtocol.TCP));
a1d21447
VP
171
172 // Abstract methods Testing
173 assertTrue(packet.validate());
d6fca387 174 assertEquals(-222021887, packet.hashCode());
a1d21447
VP
175 assertFalse(packet.equals(null));
176 assertEquals(new IPv4Packet(dummy, null, byteBuffer), packet);
177
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());
182
183 assertEquals(new IPv4Endpoint(packet, true), packet.getSourceEndpoint());
184 assertEquals(new IPv4Endpoint(packet, false), packet.getDestinationEndpoint());
185
186 fPacket.position(24);
187 byte[] payload = new byte[1];
188 fPacket.get(payload);
189 assertEquals(ByteBuffer.wrap(payload), packet.getPayload());
190
191 // Packet-specific methods Testing
d6fca387
VP
192 assertEquals(InetAddress.getByAddress(Arrays.copyOfRange(fPacket.array(), 12, 16)), packet.getSourceIpAddress());
193 assertEquals(InetAddress.getByAddress(Arrays.copyOfRange(fPacket.array(), 16, 20)), packet.getDestinationIpAddress());
a1d21447
VP
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());
d6fca387 204 assertEquals(7905, packet.getFragmentOffset());
a1d21447
VP
205 assertEquals(160, packet.getTimeToLive());
206 assertEquals(0xFE, packet.getIpDatagramProtocol());
207 assertEquals(0x3344, packet.getHeaderChecksum());
208
209 }
210 }
211}
This page took 0.036391 seconds and 5 git commands to generate.