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
.packet
;
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
;
22 import java
.io
.IOException
;
23 import java
.nio
.ByteBuffer
;
24 import java
.nio
.ByteOrder
;
26 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.BadPacketException
;
27 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.Packet
;
28 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.Protocol
;
29 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.ethernet2
.EthernetIIPacket
;
30 import org
.eclipse
.linuxtools
.pcap
.core
.tests
.shared
.PcapTestTrace
;
31 import org
.eclipse
.linuxtools
.pcap
.core
.trace
.BadPcapFileException
;
32 import org
.eclipse
.linuxtools
.pcap
.core
.trace
.PcapFile
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
37 * JUnit Class that tests the generic Packet class and its method.
39 * @author Vincent Perot
41 public class PacketTest
{
43 private ByteBuffer fEthernetPacket
;
46 * Initialize the packet.
49 public void initialize() {
50 fEthernetPacket
= ByteBuffer
.allocate(15);
51 fEthernetPacket
.order(ByteOrder
.BIG_ENDIAN
);
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);
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);
69 // Ethertype - 2 bytes
70 fEthernetPacket
.put((byte) 0xA2);
71 fEthernetPacket
.put((byte) 0x56);
74 fEthernetPacket
.put((byte) 0xA6);
76 fEthernetPacket
.flip();
81 * Test that verify the correctness of the Packet's methods.
82 * @throws BadPcapFileException
83 * Thrown when the file is erroneous. Fails the test.
85 * Thrown when an IO error occurs. Fails the test.
86 * @throws BadPacketException
87 * Thrown when a packet is erroneous. Fails the test.
90 public void GenericPacketTest() throws BadPacketException
, IOException
, BadPcapFileException
{
91 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
92 assumeTrue(trace
.exists());
93 String file
= trace
.getPath();
94 try (PcapFile dummy
= new PcapFile(file
)) {
95 ByteBuffer byteBuffer
= fEthernetPacket
;
96 if (byteBuffer
== null) {
97 fail("GenericPacketTest has failed!");
101 Packet packet
= new EthernetIIPacket(dummy
, null, byteBuffer
);
102 assertTrue(packet
.hasProtocol(Protocol
.ETHERNET_II
));
103 assertTrue(packet
.hasProtocol(Protocol
.UNKNOWN
));
104 assertFalse(packet
.hasProtocol(Protocol
.TCP
));
105 assertEquals(Protocol
.ETHERNET_II
, packet
.getProtocol());
107 assertEquals(packet
, packet
.getPacket(Protocol
.ETHERNET_II
));
108 assertNull(packet
.getPacket(Protocol
.TCP
));
109 assertEquals(packet
.getChildPacket(), packet
.getPacket(Protocol
.UNKNOWN
));
110 assertEquals(packet
.getPacket(Protocol
.ETHERNET_II
), packet
.getMostEcapsulatedPacket());
112 assertNull(packet
.getParentPacket());
113 assertFalse(packet
.getPcapFile().equals(null));
115 Packet child
= packet
.getChildPacket();
117 fail("GenericPacketTest has failed!");
120 assertEquals(packet
.getPayload(), child
.getPayload());
121 assertEquals(packet
.getGlobalSummaryString(), "Source MAC: 10:f8:82:b3:44:78 , Destination MAC: 34:67:0c:d2:91:51");