pcap: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.pcap.core.tests / src / org / eclipse / tracecompass / pcap / core / tests / packet / PacketTest.java
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
13 package org.eclipse.tracecompass.pcap.core.tests.packet;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20 import static org.junit.Assume.assumeTrue;
21
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.ByteOrder;
25
26 import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException;
27 import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
28 import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
29 import org.eclipse.tracecompass.internal.pcap.core.protocol.ethernet2.EthernetIIPacket;
30 import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException;
31 import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
32 import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 /**
37 * JUnit Class that tests the generic Packet class and its method.
38 *
39 * @author Vincent Perot
40 */
41 public class PacketTest {
42
43 private ByteBuffer fEthernetPacket;
44
45 /**
46 * Initialize the packet.
47 */
48 @Before
49 public void initialize() {
50 fEthernetPacket = ByteBuffer.allocate(15);
51 fEthernetPacket.order(ByteOrder.BIG_ENDIAN);
52
53 // Destination MAC - 6 bytes
54 fEthernetPacket.put((byte) 0x34);
55 fEthernetPacket.put((byte) 0x67);
56 fEthernetPacket.put((byte) 0x0C);
57 fEthernetPacket.put((byte) 0xD2);
58 fEthernetPacket.put((byte) 0x91);
59 fEthernetPacket.put((byte) 0x51);
60
61 // Source MAC - 6 bytes
62 fEthernetPacket.put((byte) 0x10);
63 fEthernetPacket.put((byte) 0xF8);
64 fEthernetPacket.put((byte) 0x82);
65 fEthernetPacket.put((byte) 0xB3);
66 fEthernetPacket.put((byte) 0x44);
67 fEthernetPacket.put((byte) 0x78);
68
69 // Ethertype - 2 bytes
70 fEthernetPacket.put((byte) 0xA2);
71 fEthernetPacket.put((byte) 0x56);
72
73 // Payload - 1 byte
74 fEthernetPacket.put((byte) 0xA6);
75
76 fEthernetPacket.flip();
77
78 }
79
80 /**
81 * Test that verify the correctness of the Packet's methods.
82 * @throws BadPcapFileException
83 * Thrown when the file is erroneous. Fails the test.
84 * @throws IOException
85 * Thrown when an IO error occurs. Fails the test.
86 * @throws BadPacketException
87 * Thrown when a packet is erroneous. Fails the test.
88 */
89 @Test
90 public void GenericPacketTest() throws BadPacketException, IOException, BadPcapFileException {
91 PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
92 assumeTrue(trace.exists());
93 try (PcapFile dummy = new PcapFile(trace.getPath())) {
94 ByteBuffer byteBuffer = fEthernetPacket;
95 if (byteBuffer == null) {
96 fail("GenericPacketTest has failed!");
97 return;
98 }
99
100 Packet packet = new EthernetIIPacket(dummy, null, byteBuffer);
101 assertTrue(packet.hasProtocol(PcapProtocol.ETHERNET_II));
102 assertTrue(packet.hasProtocol(PcapProtocol.UNKNOWN));
103 assertFalse(packet.hasProtocol(PcapProtocol.TCP));
104 assertEquals(PcapProtocol.ETHERNET_II, packet.getProtocol());
105
106 assertEquals(packet, packet.getPacket(PcapProtocol.ETHERNET_II));
107 assertNull(packet.getPacket(PcapProtocol.TCP));
108 assertEquals(packet.getChildPacket(), packet.getPacket(PcapProtocol.UNKNOWN));
109 assertEquals(packet.getPacket(PcapProtocol.ETHERNET_II), packet.getMostEcapsulatedPacket());
110
111 assertNull(packet.getParentPacket());
112 assertFalse(packet.getPcapFile().equals(null));
113
114 Packet child = packet.getChildPacket();
115 if (child == null) {
116 fail("GenericPacketTest has failed!");
117 return;
118 }
119 assertEquals(packet.getPayload(), child.getPayload());
120 assertEquals(packet.getGlobalSummaryString(), "Source MAC: 10:f8:82:b3:44:78 , Destination MAC: 34:67:0c:d2:91:51");
121
122 }
123 }
124 }
This page took 0.037137 seconds and 5 git commands to generate.