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 | ||
71f2817f | 13 | package org.eclipse.tracecompass.pcap.core.tests.protocol.ethernet2; |
a1d21447 VP |
14 | |
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.assertFalse; | |
17 | import static org.junit.Assert.assertTrue; | |
18 | import static org.junit.Assert.fail; | |
19 | import static org.junit.Assume.assumeTrue; | |
20 | ||
21 | import java.io.IOException; | |
22 | import java.nio.ByteBuffer; | |
23 | import java.nio.ByteOrder; | |
24 | import java.util.Arrays; | |
25 | import java.util.Map; | |
26 | ||
71f2817f AM |
27 | import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException; |
28 | import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol; | |
29 | import org.eclipse.tracecompass.internal.pcap.core.protocol.ethernet2.EthernetIIEndpoint; | |
30 | import org.eclipse.tracecompass.internal.pcap.core.protocol.ethernet2.EthernetIIPacket; | |
31 | import org.eclipse.tracecompass.internal.pcap.core.protocol.ethernet2.EthernetIIValues; | |
32 | import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException; | |
33 | import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile; | |
34 | import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace; | |
a1d21447 VP |
35 | import org.junit.Before; |
36 | import org.junit.Test; | |
37 | ||
38 | import com.google.common.collect.ImmutableMap; | |
39 | ||
40 | /** | |
41 | * JUnit Class that tests the EthernetIIPacket class and its method. | |
42 | * | |
43 | * @author Vincent Perot | |
44 | */ | |
45 | public class EthernetIIPacketTest { | |
46 | ||
47 | private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of( | |
48 | "Source MAC Address", "10:f8:82:b3:44:78", | |
49 | "Destination MAC Address", "34:67:0c:d2:91:51", | |
50 | "Ethertype", "Unknown (0xa256)" | |
51 | ); | |
52 | ||
53 | private static final String EXPECTED_TOSTRING = | |
54 | "Ethernet II, Source: 10:f8:82:b3:44:78, Destination: 34:67:0c:d2:91:51, Type: Unknown (0xa256)\nPayload: a6"; | |
55 | ||
56 | private ByteBuffer fPacket; | |
57 | ||
58 | /** | |
59 | * Initialize the packet. | |
60 | */ | |
61 | @Before | |
62 | public void initialize() { | |
63 | fPacket = ByteBuffer.allocate(15); | |
64 | fPacket.order(ByteOrder.BIG_ENDIAN); | |
65 | ||
66 | // Destination MAC - 6 bytes | |
67 | fPacket.put((byte) 0x34); | |
68 | fPacket.put((byte) 0x67); | |
69 | fPacket.put((byte) 0x0C); | |
70 | fPacket.put((byte) 0xD2); | |
71 | fPacket.put((byte) 0x91); | |
72 | fPacket.put((byte) 0x51); | |
73 | ||
74 | // Source MAC - 6 bytes | |
75 | fPacket.put((byte) 0x10); | |
76 | fPacket.put((byte) 0xF8); | |
77 | fPacket.put((byte) 0x82); | |
78 | fPacket.put((byte) 0xB3); | |
79 | fPacket.put((byte) 0x44); | |
80 | fPacket.put((byte) 0x78); | |
81 | ||
82 | // Ethertype - 2 bytes | |
83 | fPacket.put((byte) 0xA2); | |
84 | fPacket.put((byte) 0x56); | |
85 | ||
86 | // Payload - 1 byte | |
87 | fPacket.put((byte) 0xA6); | |
88 | ||
89 | fPacket.flip(); | |
90 | } | |
91 | ||
92 | /** | |
93 | * Test that verify the correctness of the EthernetIIPacket's methods. | |
94 | * @throws BadPcapFileException | |
95 | * Thrown when the file is erroneous. Fails the test. | |
96 | * @throws IOException | |
97 | * Thrown when an IO error occurs. Fails the test. | |
98 | * @throws BadPacketException | |
99 | * Thrown when a packet is erroneous. Fails the test. | |
100 | */ | |
101 | @Test | |
102 | public void CompleteEthernetIIPacketTest() throws IOException, BadPcapFileException, BadPacketException { | |
103 | PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP; | |
104 | assumeTrue(trace.exists()); | |
333a2acb | 105 | try (PcapFile dummy = new PcapFile(trace.getPath())) { |
a1d21447 VP |
106 | ByteBuffer byteBuffer = fPacket; |
107 | if (byteBuffer == null) { | |
108 | fail("CompleteEthernetIIPacketTest has failed!"); | |
109 | return; | |
110 | } | |
111 | EthernetIIPacket packet = new EthernetIIPacket(dummy, null, byteBuffer); | |
112 | ||
113 | // Protocol Testing | |
c88feda9 AM |
114 | assertEquals(PcapProtocol.ETHERNET_II, packet.getProtocol()); |
115 | assertTrue(packet.hasProtocol(PcapProtocol.ETHERNET_II)); | |
116 | assertTrue(packet.hasProtocol(PcapProtocol.UNKNOWN)); | |
117 | assertFalse(packet.hasProtocol(PcapProtocol.TCP)); | |
a1d21447 VP |
118 | |
119 | // Abstract methods Testing | |
120 | assertTrue(packet.validate()); | |
121 | assertEquals(-653947816, packet.hashCode()); | |
122 | assertFalse(packet.equals(null)); | |
123 | assertEquals(new EthernetIIPacket(dummy, null, byteBuffer), packet); | |
124 | ||
125 | assertEquals(EXPECTED_FIELDS, packet.getFields()); | |
126 | assertEquals(EXPECTED_TOSTRING, packet.toString()); | |
127 | assertEquals("Src: 10:f8:82:b3:44:78 , Dst: 34:67:0c:d2:91:51", packet.getLocalSummaryString()); | |
128 | assertEquals("Source MAC: 10:f8:82:b3:44:78 , Destination MAC: 34:67:0c:d2:91:51", packet.getGlobalSummaryString()); | |
129 | ||
130 | assertEquals(new EthernetIIEndpoint(packet, true), packet.getSourceEndpoint()); | |
131 | assertEquals(new EthernetIIEndpoint(packet, false), packet.getDestinationEndpoint()); | |
132 | ||
133 | fPacket.position(14); | |
134 | byte[] payload = new byte[1]; | |
135 | fPacket.get(payload); | |
136 | assertEquals(ByteBuffer.wrap(payload), packet.getPayload()); | |
137 | ||
138 | // Packet-specific methods Testing | |
139 | assertTrue(Arrays.equals(packet.getSourceMacAddress(), Arrays.copyOfRange(fPacket.array(), EthernetIIValues.MAC_ADDRESS_SIZE, EthernetIIValues.MAC_ADDRESS_SIZE + EthernetIIValues.MAC_ADDRESS_SIZE))); | |
140 | assertTrue(Arrays.equals(packet.getDestinationMacAddress(), Arrays.copyOfRange(fPacket.array(), 0, 0 + EthernetIIValues.MAC_ADDRESS_SIZE))); | |
141 | assertEquals(0xA256, packet.getEthertype()); | |
142 | ||
143 | } | |
144 | } | |
145 | } |