Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.pcap.core.tests.packet; | |
14 | ||
15 | import static org.junit.Assert.fail; | |
16 | import static org.junit.Assume.assumeTrue; | |
17 | ||
18 | import java.io.IOException; | |
19 | import java.nio.ByteBuffer; | |
20 | import java.nio.ByteOrder; | |
21 | ||
93d1d135 AM |
22 | import org.eclipse.linuxtools.internal.pcap.core.packet.BadPacketException; |
23 | import org.eclipse.linuxtools.internal.pcap.core.protocol.ethernet2.EthernetIIPacket; | |
24 | import org.eclipse.linuxtools.internal.pcap.core.trace.BadPcapFileException; | |
25 | import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile; | |
a1d21447 | 26 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; |
a1d21447 VP |
27 | import org.junit.Before; |
28 | import org.junit.Test; | |
29 | ||
30 | /** | |
31 | * JUnit Class that tests if BadPacketExceptions are thrown correctly. | |
32 | * | |
33 | * @author Vincent Perot | |
34 | */ | |
35 | public class BadPacketTest { | |
36 | ||
37 | private ByteBuffer fEthernetPacket; | |
38 | ||
39 | /** | |
40 | * Initialize the packet. | |
41 | */ | |
42 | @Before | |
43 | public void initialize() { | |
44 | fEthernetPacket = ByteBuffer.allocate(8); | |
45 | fEthernetPacket.order(ByteOrder.BIG_ENDIAN); | |
46 | ||
47 | // This packet is erroneous. It contains 8 bytes while the minimum is 14 | |
48 | // bytes for an Ethernet II packet. | |
49 | ||
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); | |
57 | ||
58 | // Source MAC - 2 bytes | |
59 | fEthernetPacket.put((byte) 0x10); | |
60 | fEthernetPacket.put((byte) 0xF8); | |
61 | ||
62 | fEthernetPacket.flip(); | |
63 | ||
64 | } | |
65 | ||
66 | /** | |
67 | * Test that verify if a BadPacketException is correctly thrown (when a | |
68 | * packet is erroneous). | |
69 | * | |
70 | * @throws BadPcapFileException | |
71 | * Thrown when the file is erroneous. Fails the test. | |
72 | * @throws IOException | |
73 | * Thrown when an IO error occurs. Fails the test. | |
74 | * @throws BadPacketException | |
75 | * Thrown when a packet is erroneous. Expected from the test. | |
76 | */ | |
77 | @Test(expected = BadPacketException.class) | |
78 | public void PacketExceptionTest() throws BadPacketException, IOException, BadPcapFileException { | |
79 | PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP; | |
80 | assumeTrue(trace.exists()); | |
333a2acb | 81 | try (PcapFile dummy = new PcapFile(trace.getPath())) { |
a1d21447 VP |
82 | ByteBuffer packet = fEthernetPacket; |
83 | if (packet != null) { | |
84 | new EthernetIIPacket(dummy, null, packet); | |
85 | } | |
86 | fail("PacketExceptionTest has failed!"); | |
87 | } | |
88 | } | |
89 | } |