~ubuntu-branches/ubuntu/vivid/eclipse-linuxtools/vivid-proposed

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam, Jakub Adam, tony mancill
  • Date: 2014-10-11 11:44:05 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20141011114405-yazjvxfzzhmi5sgj
Tags: 3.1.0-1
[ Jakub Adam ]
* New upstream release (Closes: #761524).
* Refreshed d/patches.
* Don't build removed feature org.eclipse.linuxtools.tools.launch
  - merged into org.eclipse.linuxtools.profiling.
* Use javac target 1.7.
* Build new feature org.eclipse.linuxtools.dataviewers.feature
  - required by Valgrind integration.
* Build-depend on eclipse-remote-services-api and eclipse-cdt-autotools.
* Bump Standards-Version to 3.9.6.
* Override incompatible-java-bytecode-format - linuxtools needs Java 7.
* Remove unused codeless-jar override.

[ tony mancill ]
* Tweak short package description to make lintian happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
26
import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol;
 
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;
 
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());
 
78
        try (PcapFile dummy = new PcapFile(trace.getPath())) {
 
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
 
87
            assertEquals(PcapProtocol.UNKNOWN, packet.getProtocol());
 
88
            assertTrue(packet.hasProtocol(PcapProtocol.UNKNOWN));
 
89
            assertFalse(packet.hasProtocol(PcapProtocol.UDP));
 
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
}