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
.unknown
;
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
;
26 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.Protocol
;
27 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.unknown
.UnknownEndpoint
;
28 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.unknown
.UnknownPacket
;
29 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.BadPcapFileException
;
30 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.PcapFile
;
31 import org
.eclipse
.linuxtools
.pcap
.core
.tests
.shared
.PcapTestTrace
;
32 import org
.junit
.Before
;
33 import org
.junit
.Test
;
35 import com
.google
.common
.collect
.ImmutableMap
;
38 * JUnit Class that tests the UnknownPacket class and its method.
40 * @author Vincent Perot
42 public class UnknownPacketTest
{
44 private static final Map
<String
, String
> EXPECTED_FIELDS
= ImmutableMap
.of(
49 private static final String fToString
= "Payload: 61";
51 private ByteBuffer fPacket
;
54 * Initialize the packet.
57 public void initialize() {
58 fPacket
= ByteBuffer
.allocate(1);
59 fPacket
.order(ByteOrder
.BIG_ENDIAN
);
62 fPacket
.put((byte) 97);
68 * Test that verify the correctness of the UnknownPacket's methods.
69 * @throws BadPcapFileException
70 * Thrown when the file is erroneous. Fails the test.
72 * Thrown when an IO error occurs. Fails the test.
75 public void CompleteUnknownPacketTest() throws IOException
, BadPcapFileException
{
76 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
77 assumeTrue(trace
.exists());
78 try (PcapFile dummy
= new PcapFile(trace
.getPath())) {
79 ByteBuffer byteBuffer
= fPacket
;
80 if (byteBuffer
== null) {
81 fail("CompleteUnknownPacketTest has failed!");
84 UnknownPacket packet
= new UnknownPacket(dummy
, null, byteBuffer
);
87 assertEquals(Protocol
.UNKNOWN
, packet
.getProtocol());
88 assertTrue(packet
.hasProtocol(Protocol
.UNKNOWN
));
89 assertFalse(packet
.hasProtocol(Protocol
.UDP
));
91 // Abstract methods Testing
92 assertTrue(packet
.validate());
93 assertEquals(1089, packet
.hashCode());
94 assertFalse(packet
.equals(null));
95 assertEquals(new UnknownPacket(dummy
, null, byteBuffer
), packet
);
97 assertEquals(EXPECTED_FIELDS
, packet
.getFields());
98 assertEquals(fToString
, packet
.toString());
99 assertEquals("Len: 1 bytes", packet
.getLocalSummaryString());
100 assertEquals("Data: 1 bytes", packet
.getGlobalSummaryString());
101 // TODO take care of plural form.
103 // Unknown Endpoints are never equal!
104 assertFalse(packet
.getSourceEndpoint().equals(new UnknownEndpoint(packet
, true)));
105 assertFalse(packet
.getDestinationEndpoint().equals(new UnknownEndpoint(packet
, false)));
108 byte[] payload
= new byte[1];
109 fPacket
.get(payload
);
110 ByteBuffer payloadBB
= ByteBuffer
.wrap(payload
);
113 assertEquals(payloadBB
, packet
.getPayload());
115 // Packet-specific methods Testing
This page took 0.036462 seconds and 5 git commands to generate.