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
.tracecompass
.pcap
.core
.tests
.packet
;
15 import static org
.junit
.Assert
.fail
;
16 import static org
.junit
.Assume
.assumeTrue
;
18 import java
.io
.IOException
;
19 import java
.nio
.ByteBuffer
;
20 import java
.nio
.ByteOrder
;
22 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.BadPacketException
;
23 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.ethernet2
.EthernetIIPacket
;
24 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.BadPcapFileException
;
25 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
26 import org
.eclipse
.tracecompass
.pcap
.core
.tests
.shared
.PcapTestTrace
;
27 import org
.junit
.Before
;
28 import org
.junit
.Test
;
31 * JUnit Class that tests if BadPacketExceptions are thrown correctly.
33 * @author Vincent Perot
35 public class BadPacketTest
{
37 private ByteBuffer fEthernetPacket
;
40 * Initialize the packet.
43 public void initialize() {
44 fEthernetPacket
= ByteBuffer
.allocate(8);
45 fEthernetPacket
.order(ByteOrder
.BIG_ENDIAN
);
47 // This packet is erroneous. It contains 8 bytes while the minimum is 14
48 // bytes for an Ethernet II packet.
50 // Destination MAC - 6 bytes
51 fEthernetPacket
.put((byte) 0x34);
52 fEthernetPacket
.put((byte) 0x67);
53 fEthernetPacket
.put((byte) 0x0C);
54 fEthernetPacket
.put((byte) 0xD2);
55 fEthernetPacket
.put((byte) 0x91);
56 fEthernetPacket
.put((byte) 0x51);
58 // Source MAC - 2 bytes
59 fEthernetPacket
.put((byte) 0x10);
60 fEthernetPacket
.put((byte) 0xF8);
62 fEthernetPacket
.flip();
67 * Test that verify if a BadPacketException is correctly thrown (when a
68 * packet is erroneous).
70 * @throws BadPcapFileException
71 * Thrown when the file is erroneous. Fails the test.
73 * Thrown when an IO error occurs. Fails the test.
74 * @throws BadPacketException
75 * Thrown when a packet is erroneous. Expected from the test.
77 @Test(expected
= BadPacketException
.class)
78 public void PacketExceptionTest() throws BadPacketException
, IOException
, BadPcapFileException
{
79 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
80 assumeTrue(trace
.exists());
81 try (PcapFile dummy
= new PcapFile(trace
.getPath())) {
82 ByteBuffer packet
= fEthernetPacket
;
84 new EthernetIIPacket(dummy
, null, packet
);
86 fail("PacketExceptionTest has failed!");