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.protocol.unknown; | |
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.Map; | |
25 | ||
c88feda9 | 26 | import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol; |
93d1d135 AM |
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; | |
a1d21447 | 31 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; |
a1d21447 VP |
32 | import org.junit.Before; |
33 | import org.junit.Test; | |
34 | ||
35 | import com.google.common.collect.ImmutableMap; | |
36 | ||
37 | /** | |
38 | * JUnit Class that tests the UnknownPacket class and its method. | |
39 | * | |
40 | * @author Vincent Perot | |
41 | */ | |
42 | public class UnknownPacketTest { | |
43 | ||
44 | private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of( | |
45 | "Binary", "61", | |
46 | "Character", "a" | |
47 | ); | |
48 | ||
49 | private static final String fToString = "Payload: 61"; | |
50 | ||
51 | private ByteBuffer fPacket; | |
52 | ||
53 | /** | |
54 | * Initialize the packet. | |
55 | */ | |
56 | @Before | |
57 | public void initialize() { | |
58 | fPacket = ByteBuffer.allocate(1); | |
59 | fPacket.order(ByteOrder.BIG_ENDIAN); | |
60 | ||
61 | // Payload - 1 byte | |
62 | fPacket.put((byte) 97); | |
63 | ||
64 | fPacket.flip(); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Test that verify the correctness of the UnknownPacket's methods. | |
69 | * @throws BadPcapFileException | |
70 | * Thrown when the file is erroneous. Fails the test. | |
71 | * @throws IOException | |
72 | * Thrown when an IO error occurs. Fails the test. | |
73 | */ | |
74 | @Test | |
75 | public void CompleteUnknownPacketTest() throws IOException, BadPcapFileException { | |
76 | PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP; | |
77 | assumeTrue(trace.exists()); | |
333a2acb | 78 | try (PcapFile dummy = new PcapFile(trace.getPath())) { |
a1d21447 VP |
79 | ByteBuffer byteBuffer = fPacket; |
80 | if (byteBuffer == null) { | |
81 | fail("CompleteUnknownPacketTest has failed!"); | |
82 | return; | |
83 | } | |
84 | UnknownPacket packet = new UnknownPacket(dummy, null, byteBuffer); | |
85 | ||
86 | // Protocol Testing | |
c88feda9 AM |
87 | assertEquals(PcapProtocol.UNKNOWN, packet.getProtocol()); |
88 | assertTrue(packet.hasProtocol(PcapProtocol.UNKNOWN)); | |
89 | assertFalse(packet.hasProtocol(PcapProtocol.UDP)); | |
a1d21447 VP |
90 | |
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); | |
96 | ||
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. | |
102 | ||
103 | // Unknown Endpoints are never equal! | |
104 | assertFalse(packet.getSourceEndpoint().equals(new UnknownEndpoint(packet, true))); | |
105 | assertFalse(packet.getDestinationEndpoint().equals(new UnknownEndpoint(packet, false))); | |
106 | ||
107 | fPacket.position(0); | |
108 | byte[] payload = new byte[1]; | |
109 | fPacket.get(payload); | |
110 | ByteBuffer payloadBB = ByteBuffer.wrap(payload); | |
111 | payloadBB.flip(); | |
112 | ||
113 | assertEquals(payloadBB, packet.getPayload()); | |
114 | ||
115 | // Packet-specific methods Testing | |
116 | // None | |
117 | ||
118 | } | |
119 | } | |
120 | } |