5337c1b375b68604c5e1ce8ab41249e29b8dc7d7
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
.protocol
.ethernet2
;
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
;
21 import java
.io
.IOException
;
22 import java
.nio
.ByteBuffer
;
23 import java
.nio
.ByteOrder
;
24 import java
.util
.Arrays
;
27 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.packet
.BadPacketException
;
28 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.PcapProtocol
;
29 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.ethernet2
.EthernetIIEndpoint
;
30 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.ethernet2
.EthernetIIPacket
;
31 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.ethernet2
.EthernetIIValues
;
32 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.BadPcapFileException
;
33 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.PcapFile
;
34 import org
.eclipse
.linuxtools
.pcap
.core
.tests
.shared
.PcapTestTrace
;
35 import org
.junit
.Before
;
36 import org
.junit
.Test
;
38 import com
.google
.common
.collect
.ImmutableMap
;
41 * JUnit Class that tests the EthernetIIPacket class and its method.
43 * @author Vincent Perot
45 public class EthernetIIPacketTest
{
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)"
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";
56 private ByteBuffer fPacket
;
59 * Initialize the packet.
62 public void initialize() {
63 fPacket
= ByteBuffer
.allocate(15);
64 fPacket
.order(ByteOrder
.BIG_ENDIAN
);
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);
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);
82 // Ethertype - 2 bytes
83 fPacket
.put((byte) 0xA2);
84 fPacket
.put((byte) 0x56);
87 fPacket
.put((byte) 0xA6);
93 * Test that verify the correctness of the EthernetIIPacket's methods.
94 * @throws BadPcapFileException
95 * Thrown when the file is erroneous. Fails the test.
97 * Thrown when an IO error occurs. Fails the test.
98 * @throws BadPacketException
99 * Thrown when a packet is erroneous. Fails the test.
102 public void CompleteEthernetIIPacketTest() throws IOException
, BadPcapFileException
, BadPacketException
{
103 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
104 assumeTrue(trace
.exists());
105 try (PcapFile dummy
= new PcapFile(trace
.getPath())) {
106 ByteBuffer byteBuffer
= fPacket
;
107 if (byteBuffer
== null) {
108 fail("CompleteEthernetIIPacketTest has failed!");
111 EthernetIIPacket packet
= new EthernetIIPacket(dummy
, null, byteBuffer
);
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
));
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
);
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());
130 assertEquals(new EthernetIIEndpoint(packet
, true), packet
.getSourceEndpoint());
131 assertEquals(new EthernetIIEndpoint(packet
, false), packet
.getDestinationEndpoint());
133 fPacket
.position(14);
134 byte[] payload
= new byte[1];
135 fPacket
.get(payload
);
136 assertEquals(ByteBuffer
.wrap(payload
), packet
.getPayload());
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());
This page took 0.034704 seconds and 4 git commands to generate.