pcap: Add unit tests to pcap.core
authorVincent Perot <vincent.perot@ericsson.com>
Fri, 27 Jun 2014 22:25:09 +0000 (18:25 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 24 Jul 2014 11:04:49 +0000 (07:04 -0400)
Change-Id: I6cb472acb44a6108242188dd24ae5b3386388c62
Signed-off-by: Vincent Perot <vincent.perot@ericsson.com>
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/29146
Tested-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
42 files changed:
org.eclipse.linuxtools.lttng.alltests/META-INF/MANIFEST.MF
org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllPerfTests.java
org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllTests.java
org.eclipse.linuxtools.pcap.core.tests/.classpath
org.eclipse.linuxtools.pcap.core.tests/META-INF/MANIFEST.MF
org.eclipse.linuxtools.pcap.core.tests/about.html
org.eclipse.linuxtools.pcap.core.tests/build.properties
org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/AllPerfTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapReadBenchmark.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapSeekBenchmark.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/pom.xml
org.eclipse.linuxtools.pcap.core.tests/rsc/.gitignore [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/rsc/get-traces.xml [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/shared/org/eclipse/linuxtools/pcap/core/tests/shared/PcapTestTrace.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllPcapCoreTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllTests.java [deleted file]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileEndiannessTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenFailTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileReadTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/BadPacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/PacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/EthernetIIPacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/IPv4PacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/PcapPacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/TCPPacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/UDPPacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/AllTests.java [new file with mode: 0644]
org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/StreamBuildTest.java [new file with mode: 0644]
pom.xml

index aba5cc95f1d6a888198e7b3846c69799d0cc0563..c3896282fe00c030a4e0652167e9403fbaa426c9 100644 (file)
@@ -27,5 +27,6 @@ Require-Bundle: org.junit;bundle-version="4.0.0",
  org.eclipse.linuxtools.ctf.parser.tests;bundle-version="3.0.0",
  org.eclipse.linuxtools.btf.core.tests;bundle-version="3.0.0",
  org.eclipse.linuxtools.tmf.analysis.xml.core.tests;bundle-version="1.0.0",
- org.eclipse.linuxtools.tmf.analysis.xml.ui.tests;bundle-version="1.0.0"
+ org.eclipse.linuxtools.tmf.analysis.xml.ui.tests;bundle-version="1.0.0",
+ org.eclipse.linuxtools.pcap.core.tests;bundle-version="3.0.0"
 Export-Package: org.eclipse.linuxtools.lttng.alltests;x-internal:=true
index 3aea99a6c40cb462e96a1e2c06413b5f3b006630..2d9b4bb85f74be9b941efc4bf24b0ee03826449e 100644 (file)
@@ -22,6 +22,7 @@ import org.junit.runners.Suite;
 @Suite.SuiteClasses({
     org.eclipse.linuxtools.ctf.core.tests.perf.AllPerfTests.class,
     org.eclipse.linuxtools.lttng2.kernel.core.tests.perf.AllPerfTests.class,
+    org.eclipse.linuxtools.pcap.core.tests.perf.AllPerfTests.class,
     org.eclipse.linuxtools.tmf.ctf.core.tests.perf.AllPerfTests.class
 })
 public class RunAllPerfTests {
index 95bd182c1eba808e5015bc8e665feee39d5351fa..894dd9fb26549113dfb0e428502c9df01a0f4c8a 100644 (file)
@@ -31,6 +31,7 @@ import org.junit.runners.Suite;
     org.eclipse.linuxtools.lttng2.kernel.ui.tests.AllTests.class,
     org.eclipse.linuxtools.lttng2.ust.core.tests.AllTests.class,
     org.eclipse.linuxtools.lttng2.ust.ui.tests.AllTests.class,
+    org.eclipse.linuxtools.pcap.core.tests.AllPcapCoreTests.class,
     org.eclipse.linuxtools.statesystem.core.tests.AllTests.class,
     org.eclipse.linuxtools.tmf.analysis.xml.core.tests.AllAnalysisXmlCoreTests.class,
     org.eclipse.linuxtools.tmf.analysis.xml.ui.tests.AllAnalysisXmlUiTests.class,
index 098194ca4b7d8f45177f94e735506ae3a26b5c94..8fbc9a2058ff20ae42178d66b0b5eb7154172dad 100644 (file)
@@ -3,5 +3,7 @@
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
        <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
        <classpathentry kind="src" path="src"/>
+       <classpathentry kind="src" path="perf"/>
+       <classpathentry kind="src" path="shared"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
index b8e5f94d92b9ac854d9ac7c443ffcdc681518c33..8f3403da5a061bd722bfe3493b215e64f6c38ef5 100644 (file)
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: %Bundle-Name
 Bundle-Vendor: %Bundle-Vendor
-Bundle-Version: 3.0.0.qualifier
+Bundle-Version: 3.1.0.qualifier
 Bundle-Localization: plugin
 Bundle-SymbolicName: org.eclipse.linuxtools.pcap.core.tests;singleton:=true
 Bundle-ActivationPolicy: lazy
@@ -10,5 +10,20 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.7
 Require-Bundle: org.junit;bundle-version="4.0.0",
  org.eclipse.core.runtime,
  org.eclipse.core.resources,
- org.eclipse.linuxtools.pcap.core;bundle-version="3.0.0"
-Export-Package: org.eclipse.linuxtools.pcap.core.tests
+ org.eclipse.linuxtools.pcap.core;bundle-version="3.1.0"
+Export-Package: org.eclipse.linuxtools.pcap.core.tests,
+ org.eclipse.linuxtools.pcap.core.tests.file;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.packet;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.perf,
+ org.eclipse.linuxtools.pcap.core.tests.perf.trace;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.protocol;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.protocol.ethernet2;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.protocol.ipv4;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.protocol.pcap;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.protocol.tcp;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.protocol.udp;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.protocol.unknown;x-internal:=true,
+ org.eclipse.linuxtools.pcap.core.tests.shared,
+ org.eclipse.linuxtools.pcap.core.tests.stream;x-internal:=true
+Import-Package: com.google.common.collect,
+ org.eclipse.test.performance
index c258ef55d834cecf0bdaa2fb193b6da357922f89..28737f60b77df73a371970d45ee319c5e4cff6bd 100644 (file)
@@ -7,19 +7,19 @@
 </head>
 <body lang="EN-US">
 <h2>About This Content</h2>
-<p>June 5, 2006</p>    
+
+<p>June 5, 2006</p>
 <h3>License</h3>
 
-<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;).  Unless otherwise 
+<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;).  Unless otherwise
 indicated below, the Content is provided to you under the terms and conditions of the
-Eclipse Public License Version 1.0 (&quot;EPL&quot;).  A copy of the EPL is available 
+Eclipse Public License Version 1.0 (&quot;EPL&quot;).  A copy of the EPL is available
 at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
 For purposes of the EPL, &quot;Program&quot; will mean the Content.</p>
 
-<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is 
+<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is
 being redistributed by another party (&quot;Redistributor&quot;) and different terms and conditions may
-apply to your use of any object code in the Content.  Check the Redistributor's license that was 
+apply to your use of any object code in the Content.  Check the Redistributor's license that was
 provided with the Content.  If no such license exists, contact the Redistributor.  Unless otherwise
 indicated below, the terms and conditions of the EPL still apply to any source code in the Content
 and such source code may be obtained at <a href="http://www.eclipse.org/">http://www.eclipse.org</a>.</p>
index 2ccaf4558e184e29ee21e672059e99de48cff5c9..7de632eb60c99513f1470a0108009dadc9fc0872 100644 (file)
@@ -10,7 +10,9 @@
 #     Ericsson - Initial API and implementation
 ###############################################################################
 
-source.. = src/
+source.. = src/,\
+           perf/,\
+           shared/
 output.. = bin/
 bin.includes = META-INF/,\
                .,\
diff --git a/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/AllPerfTests.java b/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/AllPerfTests.java
new file mode 100644 (file)
index 0000000..3482713
--- /dev/null
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial implementation and API
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.perf;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Run all performance test suites.
+ *
+ * @author Vincent Perot
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        org.eclipse.linuxtools.pcap.core.tests.perf.trace.AllTests.class
+})
+public class AllPerfTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/AllTests.java
new file mode 100644 (file)
index 0000000..4eb62a4
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.perf.trace;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        PcapReadBenchmark.class,
+        PcapSeekBenchmark.class
+})
+public class AllTests {
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapReadBenchmark.java b/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapReadBenchmark.java
new file mode 100644 (file)
index 0000000..30952ef
--- /dev/null
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.perf.trace;
+
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.packet.Packet;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.eclipse.test.performance.Dimension;
+import org.eclipse.test.performance.Performance;
+import org.eclipse.test.performance.PerformanceMeter;
+import org.junit.Test;
+
+
+/**
+ * Benchmark of the Pcap parser for reading a trace. Note: We should get a bigger trace. One
+ * that has WAYYYY more events since this current trace is just parsed too fast.
+ *
+ * @author Vincent Perot
+ */
+public class PcapReadBenchmark {
+
+    private static final String TEST_SUITE_NAME = "Pcap Read Benchmark";
+    private static final String TEST_ID = "org.eclipse.linuxtools#" + TEST_SUITE_NAME;
+    private static final int LOOP_COUNT = 25;
+
+    /**
+     * Benchmark reading the pcap trace
+     */
+    @Test
+    public void testPcapTrace() {
+        readTrace(PcapTestTrace.MOSTLY_UDP, "trace-pcap", true);
+    }
+
+    private static void readTrace(PcapTestTrace testTrace, String testName, boolean inGlobalSummary) {
+        assumeTrue(testTrace.exists());
+
+        Performance perf = Performance.getDefault();
+        PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + '#' + testName);
+        perf.tagAsSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
+
+        if (inGlobalSummary) {
+            perf.tagAsGlobalSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
+        }
+
+        for (int loop = 0; loop < LOOP_COUNT; loop++) {
+            pm.start();
+            try (PcapFile trace = testTrace.getTrace();) {
+                trace.seekPacket(0);
+                while (trace.hasNextPacket()) {
+                    Packet packet = trace.parseNextPacket();
+                    if (packet == null) {
+                        fail("Test failed at iteration " + loop + " packet " + trace.getCurrentRank());
+                        return;
+                    }
+                    /* Do something with the packet because we are awesome */
+                    packet.getPayload();
+                }
+
+            } catch (IOException | BadPcapFileException | BadPacketException e) {
+                fail("Test failed at iteration " + loop + ':' + e.getMessage());
+            }
+            pm.stop();
+        }
+        pm.commit();
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapSeekBenchmark.java b/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapSeekBenchmark.java
new file mode 100644 (file)
index 0000000..c242a91
--- /dev/null
@@ -0,0 +1,110 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.perf.trace;
+
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Random;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.eclipse.test.performance.Dimension;
+import org.eclipse.test.performance.Performance;
+import org.eclipse.test.performance.PerformanceMeter;
+import org.junit.Test;
+
+/**
+ * Tests for performance regressions of the pcap reader. It only tests the pcap
+ * reader, not tmf. <br>
+ * This test runs in 3 passes.
+ * <ul>
+ * <li>first it opens a trace</li>
+ * <li>then it reads the trace completely</li>
+ * <li>then it randomly (seeded) seeks NB_SEEKS locations in the trace and reads
+ * one event at each position.</li>
+ * </ul>
+ * <li>Note: We should make more seeks, since the current number is just too
+ * fast.</li>
+ *
+ * @author Vincent Perot
+ */
+public class PcapSeekBenchmark {
+
+    private static final Random RND = new Random(1000);
+
+    private static final int LOOP_COUNT = 25;
+    private static final int NB_SEEKS = 500;
+    private static final String TEST_SUITE_NAME = "Pcap Read & Seek Benchmark (" + NB_SEEKS + " seeks)";
+    private static final String TEST_ID = "org.eclipse.linuxtools#" + TEST_SUITE_NAME;
+
+    /**
+     * Run the benchmark scenario for the pcap trace.
+     */
+    @Test
+    public void testPcapTrace() {
+        readAndSeekTrace(PcapTestTrace.MOSTLY_UDP, "trace-pcap", true);
+    }
+
+    private static void readAndSeekTrace(PcapTestTrace testTrace, String testName, boolean inGlobalSummary) {
+        assumeTrue(testTrace.exists());
+
+        Performance perf = Performance.getDefault();
+        PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + '#' + testName);
+        perf.tagAsSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
+
+        if (inGlobalSummary) {
+            perf.tagAsGlobalSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
+        }
+
+        for (int loop = 0; loop < LOOP_COUNT; loop++) {
+            try (PcapFile trace = testTrace.getTrace()) {
+                trace.seekPacket(0);
+
+                /* Read the whole trace to find out the number of packets */
+                long nbPackets = trace.getTotalNbPackets();
+
+                /* Generate the timestamps we will seek to */
+                List<Long> seekTimestamps = new LinkedList<>();
+                final long range = nbPackets;
+                for (int i = 0; i < NB_SEEKS; i++) {
+                    long rank = (RND.nextLong() % range);
+                    if (rank < 0) {
+                        // This is needed since modulus can return a negative
+                        // number.
+                        rank += range;
+                    }
+                    seekTimestamps.add(rank);
+                }
+
+                /* Benchmark seeking to the generated timestamps */
+                pm.start();
+                for (Long rank : seekTimestamps) {
+                    trace.seekPacket(rank);
+                    trace.parseNextPacket();
+                }
+                pm.stop();
+
+            } catch (IOException | BadPcapFileException | BadPacketException e) {
+                /* Should not happen if assumeTrue() passed above */
+                fail("Test failed at iteration " + loop + ':' + e.getMessage());
+            }
+        }
+        pm.commit();
+    }
+}
index 3a8e7723573e4da6b053b6cfb3f950048624081a..530f192794c94df4ff83d26311d1a63c9d7634d1 100644 (file)
   <parent>
     <artifactId>linuxtools-lttng-parent</artifactId>
     <groupId>org.eclipse.linuxtools.lttng</groupId>
-    <version>3.0.0-SNAPSHOT</version>
+    <version>3.1.0-SNAPSHOT</version>
   </parent>
 
   <artifactId>org.eclipse.linuxtools.pcap.core.tests</artifactId>
-  <version>1.0.0-SNAPSHOT</version>
+  <version>3.1.0-SNAPSHOT</version>
   <packaging>eclipse-test-plugin</packaging>
 
   <name>Linux Tools Pcap Parser Core Tests Plug-in</name>
 
- <build>
+  <!-- Do not download the test traces if "maven.test.skip" is set -->
+  <profiles>
+    <profile>
+      <id>download-traces</id>
+      <activation>
+        <property>
+          <name>!maven.test.skip</name>
+        </property>
+      </activation>
+
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-antrun-plugin</artifactId>
+            <executions>
+              <execution>
+                <id>prepare</id>
+                <phase>pre-integration-test</phase>
+                <configuration>
+                  <target>
+                    <ant antfile="get-traces.xml" dir="rsc" />
+                  </target>
+                </configuration>
+                <goals>
+                  <goal>run</goal>
+                </goals>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+  </profiles>
+
+  <build>
     <plugins>
       <plugin>
         <groupId>org.eclipse.tycho</groupId>
         <version>${tycho-version}</version>
         <configuration>
           <testSuite>org.eclipse.linuxtools.pcap.core.tests</testSuite>
-          <testClass>org.eclipse.linuxtools.pcap.core.tests.AllTests</testClass>
+          <testClass>org.eclipse.linuxtools.pcap.core.tests.AllPcapCoreTests</testClass>
           <useUIHarness>false</useUIHarness>
           <useUIThread>false</useUIThread>
           <product>org.eclipse.platform.ide</product>
         </configuration>
       </plugin>
+
+      <!-- We don't use the ant file to clean because of http://jira.codehaus.org/browse/MANTRUN-78 -->
+      <plugin>
+        <artifactId>maven-clean-plugin</artifactId>
+        <version>2.5</version>
+        <executions>
+          <execution>
+            <id>clean-traces</id>
+            <phase>clean</phase>
+            <configuration>
+              <filesets>
+                <fileset>
+                  <directory>rsc</directory>
+                  <includes>
+                    <include>*.zip</include>
+                    <include>*.pcap</include>
+                    <include>*.cap</include>
+                    <include>*.tar*</include>
+                  </includes>
+                  <excludes>
+                    <exclude>*.xml</exclude>
+                    <exclude>*.sh</exclude>
+                  </excludes>
+                </fileset>
+                <fileset><directory>rsc/kernel</directory></fileset>
+              </filesets>
+            </configuration>
+            <goals>
+              <goal>clean</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+
     </plugins>
   </build>
 
diff --git a/org.eclipse.linuxtools.pcap.core.tests/rsc/.gitignore b/org.eclipse.linuxtools.pcap.core.tests/rsc/.gitignore
new file mode 100644 (file)
index 0000000..a8ddf0a
--- /dev/null
@@ -0,0 +1,5 @@
+*.zip
+*.pcap
+*.cap
+*.tar*
+/kernel
diff --git a/org.eclipse.linuxtools.pcap.core.tests/rsc/get-traces.xml b/org.eclipse.linuxtools.pcap.core.tests/rsc/get-traces.xml
new file mode 100644 (file)
index 0000000..c764df0
--- /dev/null
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+   Copyright (C) 2013, Red Hat, Inc. and others
+
+   All rights reserved. This program and the accompanying materials
+   are made available under the terms of the Eclipse Public License v1.0
+   which accompanies this distribution, and is available at
+   http://www.eclipse.org/legal/epl-v10.html
+-->
+<!--
+Most traces are taken from the Wireshark website and are available under the GNU General Public License.
+http://wiki.wireshark.org/SampleCaptures
+-->
+
+<project name="Extract Traces" default="main" >
+
+<target name="main">
+  <echo message="Attempting to download test traces"/>
+  <get ignoreerrors="true" maxtime="20" dest="Short_LittleEndian.pcap.zip" skipexisting="true" src="http://wiki.eclipse.org/images/1/18/Short_LittleEndian.pcap.zip" />
+  <get ignoreerrors="true" maxtime="20" dest="Short_BigEndian.pcap.zip" skipexisting="true" src="http://wiki.eclipse.org/images/2/25/Short_BigEndian.pcap.zip" />
+  <get ignoreerrors="true" maxtime="20" dest="MostlyUDP.pcap.zip" skipexisting="true" src="http://wiki.eclipse.org/images/6/64/MostlyUDP.pcap.zip" />
+  <get ignoreerrors="true" maxtime="20" dest="MostlyTCP.pcap.zip" skipexisting="true" src="http://wiki.eclipse.org/images/8/8d/MostlyTCP.pcap.zip" />
+  <get ignoreerrors="true" maxtime="20" dest="EmptyPcap.pcap.zip" skipexisting="true" src="http://wiki.eclipse.org/images/c/c1/EmptyPcap.pcap.zip" />
+  <get ignoreerrors="true" maxtime="20" dest="BadPcapFile.pcap.zip" skipexisting="true" src="http://wiki.eclipse.org/images/5/5e/BadPcapFile.pcap.zip" />
+  <get ignoreerrors="true" maxtime="20" dest="sample-ctf-trace-20120412.tar.bz2" skipexisting="true" src="http://lttng.org/files/samples/sample-ctf-trace-20120412.tar.bz2"/>
+  <condition property="testSuiteExists">
+    <and>
+      <available file="pcap-testsuite" type="dir"/>
+    </and>
+  </condition>
+  <condition property="tracesExist">
+    <and>
+        <available file="Short_LittleEndian.pcap.zip"/>
+        <available file="Short_BigEndian.pcap.zip"/>
+        <available file="MostlyUDP.pcap.zip"/>
+        <available file="MostlyTCP.pcap.zip"/>
+        <available file="EmptyPcap.pcap.zip"/>
+        <available file="BadPcapFile.pcap.zip"/>
+        <available file="sample-ctf-trace-20120412.tar.bz2"/>
+    </and>
+  </condition>
+  <antcall target="extractTraces"/>
+</target>
+
+<target name="extractTraces" if="tracesExist">
+    <unzip src="Short_LittleEndian.pcap.zip" dest="." />
+    <unzip src="Short_BigEndian.pcap.zip" dest="." />
+    <unzip src="MostlyUDP.pcap.zip" dest="." />
+    <unzip src="MostlyTCP.pcap.zip" dest="." />
+    <unzip src="EmptyPcap.pcap.zip" dest="." />
+    <unzip src="BadPcapFile.pcap.zip" dest="." />
+    <bunzip2 src="sample-ctf-trace-20120412.tar.bz2"/>
+    <untar src="sample-ctf-trace-20120412.tar" dest="." />
+    <echo message="Traces extracted successfully"/>
+</target>
+</project>
diff --git a/org.eclipse.linuxtools.pcap.core.tests/shared/org/eclipse/linuxtools/pcap/core/tests/shared/PcapTestTrace.java b/org.eclipse.linuxtools.pcap.core.tests/shared/org/eclipse/linuxtools/pcap/core/tests/shared/PcapTestTrace.java
new file mode 100644 (file)
index 0000000..ac79b34
--- /dev/null
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.shared;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+
+/**
+ * Here is the list of the available test traces for the Pcap parser.
+ *
+ * @author Vincent Perot
+ */
+public enum PcapTestTrace {
+
+    /** A bad pcap file. */
+    BAD_PCAPFILE("../org.eclipse.linuxtools.pcap.core.tests/rsc/BadPcapFile.pcap"),
+
+    /** A Valid Pcap that is empty. */
+    EMPTY_PCAP("../org.eclipse.linuxtools.pcap.core.tests/rsc/EmptyPcap.pcap"),
+
+    /** A Pcap that mostly contains TCP packets. */
+    MOSTLY_TCP("../org.eclipse.linuxtools.pcap.core.tests/rsc/mostlyTCP.pcap"),
+
+    /** A Pcap that mostly contains UDP packets. */
+    MOSTLY_UDP("../org.eclipse.linuxtools.pcap.core.tests/rsc/mostlyUDP.pcap"),
+
+    /** A big-endian trace that contains two packets. */
+    SHORT_BIG_ENDIAN("../org.eclipse.linuxtools.pcap.core.tests/rsc/Short_BigEndian.pcap"),
+
+    /** A little-endian trace that contains two packets. */
+    SHORT_LITTLE_ENDIAN("../org.eclipse.linuxtools.pcap.core.tests/rsc/Short_LittleEndian.pcap"),
+
+    /** A Kernel trace directory. */
+    KERNEL_DIRECTORY("../org.eclipse.linuxtools.pcap.core.tests/rsc/kernel/"),
+
+    /** A Kernel trace file. */
+    KERNEL_TRACE("../org.eclipse.linuxtools.pcap.core.tests/rsc/kernel/channel0_0");
+
+    private final @NonNull String fPath;
+
+    private PcapTestTrace(@NonNull String path) {
+        fPath = path;
+    }
+
+    /** @return The path to the test trace */
+    public @NonNull String getPath() {
+        return fPath;
+    }
+
+    /**
+     * Get a Pcap Trace instance of a test trace. Make sure to call
+     * {@link #exists()} before calling this!
+     *
+     * @return The PcapFile object
+     * @throws IOException
+     *             Thrown when some IO error occurs.
+     * @throws BadPcapFileException
+     *             Thrown when the file is not a valid Pcap File.
+     */
+    public PcapFile getTrace() throws BadPcapFileException, IOException {
+        return new PcapFile(fPath);
+    }
+
+    /**
+     * Check if this test trace actually exists on disk.
+     *
+     * @return If the trace exists
+     */
+    public boolean exists() {
+        File file = new File(fPath);
+        if (!file.exists()) {
+            return false;
+        }
+        return true;
+    }
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllPcapCoreTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllPcapCoreTests.java
new file mode 100644 (file)
index 0000000..86e1cb8
--- /dev/null
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Master test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        org.eclipse.linuxtools.pcap.core.tests.file.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.packet.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.protocol.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.protocol.ethernet2.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.protocol.ipv4.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.protocol.pcap.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.protocol.tcp.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.protocol.udp.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.protocol.unknown.AllTests.class,
+        org.eclipse.linuxtools.pcap.core.tests.stream.AllTests.class
+})
+public class AllPcapCoreTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllTests.java
deleted file mode 100644 (file)
index 59db68f..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2014 Ericsson
- *
- * All rights reserved. This program and the accompanying materials are
- * made available under the terms of the Eclipse Public License v1.0 which
- * accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *   Alexandre Montplaisir - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.linuxtools.pcap.core.tests;
-
-import org.junit.runner.RunWith;
-import org.junit.runners.Suite;
-
-/**
- * Master test suite
- */
-@RunWith(Suite.class)
-@Suite.SuiteClasses({
-    //example: StateSystemPushPopTest.class,
-})
-public class AllTests {
-
-}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/AllTests.java
new file mode 100644 (file)
index 0000000..5f5b9a8
--- /dev/null
@@ -0,0 +1,30 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.file;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * File test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        PcapFileOpenTest.class,
+        PcapFileOpenFailTest.class,
+        PcapFileReadTest.class,
+        PcapFileEndiannessTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileEndiannessTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileEndiannessTest.java
new file mode 100644 (file)
index 0000000..f409234
--- /dev/null
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.file;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteOrder;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests whether the Pcap parser can read both big endian and
+ * little endian files.
+ *
+ * @author Vincent Perot
+ */
+public class PcapFileEndiannessTest {
+
+    /**
+     * Test that verify that two files with different endianness contain the
+     * same packets.
+     *
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void EndiannessTest() throws IOException, BadPcapFileException, BadPacketException {
+        PcapTestTrace trace = PcapTestTrace.SHORT_LITTLE_ENDIAN;
+        assumeTrue(trace.exists());
+        String path1 = trace.getPath();
+
+        trace = PcapTestTrace.SHORT_LITTLE_ENDIAN;
+        assumeTrue(trace.exists());
+        String path2 = PcapTestTrace.SHORT_BIG_ENDIAN.getPath();
+
+        try (PcapFile littleEndian = new PcapFile(path1);
+                PcapFile bigEndian = new PcapFile(path2);) {
+            assertEquals(ByteOrder.BIG_ENDIAN, bigEndian.getByteOrder());
+            assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.getByteOrder());
+            while (littleEndian.hasNextPacket() && bigEndian.hasNextPacket()) {
+                assertEquals(littleEndian.parseNextPacket(), bigEndian.parseNextPacket());
+            }
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenFailTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenFailTest.java
new file mode 100644 (file)
index 0000000..7bd1a60
--- /dev/null
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.file;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests the opening of non-valid pcap files.
+ *
+ * @author Vincent Perot
+ */
+public class PcapFileOpenFailTest {
+
+    /**
+     * Test that tries to open a pcap with a bad magic number
+     *
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     */
+    @Test
+    public void FileOpenBadPcapTest() throws IOException {
+        PcapTestTrace trace = PcapTestTrace.BAD_PCAPFILE;
+        assumeTrue(trace.exists());
+
+        String path = trace.getPath();
+        try (PcapFile file = new PcapFile(path);) {
+            fail("The pcap was accepted even though the magic number is invalid!");
+        } catch (BadPcapFileException e) {
+            assertEquals("c3d4a1b2 is not a known magic number.", e.getMessage());
+        }
+    }
+
+    /**
+     * Test that tries to open a non-pcap binary file
+     *
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     */
+    @Test
+    public void FileOpenBinaryFile() throws IOException {
+        PcapTestTrace trace = PcapTestTrace.KERNEL_TRACE;
+        assumeTrue(trace.exists());
+
+        String path = trace.getPath();
+        try (PcapFile file = new PcapFile(path);) {
+            fail("The file was accepted even though it is not a pcap file!");
+        } catch (BadPcapFileException e) {
+            assertEquals("Bad Pcap File.", e.getMessage());
+        }
+    }
+
+    /**
+     * Test that tries to open a directory
+     *
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     */
+    @Test
+    public void FileOpenDirectory() throws IOException {
+        PcapTestTrace trace = PcapTestTrace.KERNEL_DIRECTORY;
+        assumeTrue(trace.exists());
+
+        String path = trace.getPath();
+        try (PcapFile file = new PcapFile(path);) {
+            fail("The file was accepted even though it is not a pcap file!");
+        } catch (BadPcapFileException e) {
+            assertEquals("Bad Pcap File.", e.getMessage());
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenTest.java
new file mode 100644 (file)
index 0000000..7f6b0f2
--- /dev/null
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.file;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteOrder;
+
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests the opening of valid pcap files.
+ *
+ * @author Vincent Perot
+ */
+public class PcapFileOpenTest {
+
+    /**
+     * Test that verify that an empty pcap file is properly opened and that the
+     * file properties are correct.
+     *
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     */
+    @Test
+    public void FileOpenEmptyTest() throws IOException, BadPcapFileException {
+
+        PcapTestTrace trace = PcapTestTrace.EMPTY_PCAP;
+        assumeTrue(trace.exists());
+
+        String path = trace.getPath();
+        try (PcapFile file = new PcapFile(path);) {
+            assertEquals(PcapTestTrace.EMPTY_PCAP.getPath(), file.getPath());
+            assertEquals(2, file.getMajorVersion());
+            assertEquals(4, file.getMinorVersion());
+            assertEquals(1, file.getDataLinkType());
+            assertEquals(65535, file.getSnapLength());
+            assertEquals(0, file.getTimeAccuracy());
+            assertEquals(0, file.getTimeZoneCorrection());
+            assertEquals(ByteOrder.LITTLE_ENDIAN, file.getByteOrder());
+
+            assertEquals(0, file.getTotalNbPackets());
+
+        }
+    }
+
+    /**
+     * Test that verify that an non-empty pcap file is properly opened and that
+     * the file properties are correct.
+     *
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     */
+
+    @Test
+    public void FileOpenTest() throws IOException, BadPcapFileException {
+
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+
+        String path = trace.getPath();
+        try (PcapFile file = new PcapFile(path);) {
+            assertEquals(PcapTestTrace.MOSTLY_TCP.getPath(), file.getPath());
+            assertEquals(2, file.getMajorVersion());
+            assertEquals(4, file.getMinorVersion());
+            assertEquals(1, file.getDataLinkType());
+            assertEquals(65535, file.getSnapLength());
+            assertEquals(0, file.getTimeAccuracy());
+            assertEquals(0, file.getTimeZoneCorrection());
+            assertEquals(ByteOrder.LITTLE_ENDIAN, file.getByteOrder());
+
+            assertEquals(43, file.getTotalNbPackets());
+
+        }
+    }
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileReadTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileReadTest.java
new file mode 100644 (file)
index 0000000..144185b
--- /dev/null
@@ -0,0 +1,137 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.file;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.pcap.PcapPacket;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests if packets are read without error.
+ *
+ * @author Vincent Perot
+ */
+public class PcapFileReadTest {
+
+    /**
+     * Test that verify that packets are well read and that no error happens in
+     * file index.
+     *
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void FileReadTest() throws IOException, BadPcapFileException, BadPacketException {
+
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_UDP;
+        assumeTrue(trace.exists());
+
+        String path = trace.getPath();
+        try (PcapFile file = new PcapFile(path);) {
+
+            PcapPacket packet = file.parseNextPacket();
+            if (packet == null) {
+                fail("FileReadTest() failed!");
+                return;
+            }
+
+            assertEquals(1, file.getCurrentRank());
+            // Verify Pcap packet.
+            assertEquals(file, packet.getPcapFile());
+            assertEquals(Protocol.PCAP, packet.getProtocol());
+            assertEquals(0, packet.getIndex());
+            assertEquals(1120469540839312L, packet.getTimestamp());
+            assertEquals(92, packet.getOriginalLength());
+            assertEquals(92, packet.getIncludedLength());
+            assertEquals(false, packet.isTruncated());
+            assertEquals(true, packet.validate());
+            // Verify Ethernet Packet
+            if (!packet.hasProtocol(Protocol.ETHERNET_II)) {
+                fail("Packet doesn't have ethernet!");
+            }
+            // Verify IPv4 Packet
+            if (!packet.hasProtocol(Protocol.IPV4)) {
+                fail("Packet doesn't have IPv4!");
+            }
+            // Verify UDP Packet
+            if (!packet.hasProtocol(Protocol.UDP)) {
+                fail("Packet doesn't have UDP!");
+            }
+            // Verify Unknown Packet
+            if (!packet.hasProtocol(Protocol.UNKNOWN)) {
+                fail("Packet doesn't have payload!");
+            }
+
+            // Parse a "random" packet
+            file.seekPacket(58);
+            packet = file.parseNextPacket();
+            if (packet == null) {
+                fail("FileReadTest() failed!");
+                return;
+            }
+
+            // Verify Pcap packet.
+            assertEquals(file, packet.getPcapFile());
+            assertEquals(Protocol.PCAP, packet.getProtocol());
+            assertEquals(58, packet.getIndex());
+            assertEquals(1120469635045415L, packet.getTimestamp());
+            assertEquals(113, packet.getOriginalLength());
+            assertEquals(113, packet.getIncludedLength());
+            assertEquals(false, packet.isTruncated());
+            assertEquals(true, packet.validate());
+            // Verify Ethernet Packet
+            if (!packet.hasProtocol(Protocol.ETHERNET_II)) {
+                fail("Packet doesn't have ethernet!");
+            }
+            // Verify IPv4 Packet
+            if (!packet.hasProtocol(Protocol.IPV4)) {
+                fail("Packet doesn't have IPv4!");
+            }
+            // Verify TCP Packet
+            if (!packet.hasProtocol(Protocol.TCP)) {
+                fail("Packet doesn't have TCP!");
+            }
+            // Verify Unknown Packet
+            if (!packet.hasProtocol(Protocol.UNKNOWN)) {
+                fail("Packet doesn't have payload!");
+            }
+
+            // Skip packet
+            file.skipNextPacket();
+            assertEquals(60, file.getCurrentRank());
+
+            // Parse outside of file.
+            file.seekPacket(99999999);
+            assertEquals(file.getTotalNbPackets(), file.getCurrentRank());
+            file.skipNextPacket(); // Should be a no-op
+            assertEquals(file.getTotalNbPackets(), file.getCurrentRank());
+            packet = file.parseNextPacket();
+            assertNull(packet);
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/AllTests.java
new file mode 100644 (file)
index 0000000..d53f197
--- /dev/null
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.packet;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Generic Packet test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        PacketTest.class,
+        BadPacketTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/BadPacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/BadPacketTest.java
new file mode 100644 (file)
index 0000000..71a8ce7
--- /dev/null
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.packet;
+
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.protocol.ethernet2.EthernetIIPacket;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests if BadPacketExceptions are thrown correctly.
+ *
+ * @author Vincent Perot
+ */
+public class BadPacketTest {
+
+    private ByteBuffer fEthernetPacket;
+
+    /**
+     * Initialize the packet.
+     */
+    @Before
+    public void initialize() {
+        fEthernetPacket = ByteBuffer.allocate(8);
+        fEthernetPacket.order(ByteOrder.BIG_ENDIAN);
+
+        // This packet is erroneous. It contains 8 bytes while the minimum is 14
+        // bytes for an Ethernet II packet.
+
+        // Destination MAC - 6 bytes
+        fEthernetPacket.put((byte) 0x34);
+        fEthernetPacket.put((byte) 0x67);
+        fEthernetPacket.put((byte) 0x0C);
+        fEthernetPacket.put((byte) 0xD2);
+        fEthernetPacket.put((byte) 0x91);
+        fEthernetPacket.put((byte) 0x51);
+
+        // Source MAC - 2 bytes
+        fEthernetPacket.put((byte) 0x10);
+        fEthernetPacket.put((byte) 0xF8);
+
+        fEthernetPacket.flip();
+
+    }
+
+    /**
+     * Test that verify if a BadPacketException is correctly thrown (when a
+     * packet is erroneous).
+     *
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Expected from the test.
+     */
+    @Test(expected = BadPacketException.class)
+    public void PacketExceptionTest() throws BadPacketException, IOException, BadPcapFileException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+        String file = trace.getPath();
+        try (PcapFile dummy = new PcapFile(file)) {
+            ByteBuffer packet = fEthernetPacket;
+            if (packet != null) {
+                new EthernetIIPacket(dummy, null, packet);
+            }
+            fail("PacketExceptionTest has failed!");
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/PacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/PacketTest.java
new file mode 100644 (file)
index 0000000..4dd7e6d
--- /dev/null
@@ -0,0 +1,125 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.packet;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.packet.Packet;
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.ethernet2.EthernetIIPacket;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests the generic Packet class and its method.
+ *
+ * @author Vincent Perot
+ */
+public class PacketTest {
+
+    private ByteBuffer fEthernetPacket;
+
+    /**
+     * Initialize the packet.
+     */
+    @Before
+    public void initialize() {
+        fEthernetPacket = ByteBuffer.allocate(15);
+        fEthernetPacket.order(ByteOrder.BIG_ENDIAN);
+
+        // Destination MAC - 6 bytes
+        fEthernetPacket.put((byte) 0x34);
+        fEthernetPacket.put((byte) 0x67);
+        fEthernetPacket.put((byte) 0x0C);
+        fEthernetPacket.put((byte) 0xD2);
+        fEthernetPacket.put((byte) 0x91);
+        fEthernetPacket.put((byte) 0x51);
+
+        // Source MAC - 6 bytes
+        fEthernetPacket.put((byte) 0x10);
+        fEthernetPacket.put((byte) 0xF8);
+        fEthernetPacket.put((byte) 0x82);
+        fEthernetPacket.put((byte) 0xB3);
+        fEthernetPacket.put((byte) 0x44);
+        fEthernetPacket.put((byte) 0x78);
+
+        // Ethertype - 2 bytes
+        fEthernetPacket.put((byte) 0xA2);
+        fEthernetPacket.put((byte) 0x56);
+
+        // Payload - 1 byte
+        fEthernetPacket.put((byte) 0xA6);
+
+        fEthernetPacket.flip();
+
+    }
+
+    /**
+     * Test that verify the correctness of the Packet's methods.
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void GenericPacketTest() throws BadPacketException, IOException, BadPcapFileException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+        String file = trace.getPath();
+        try (PcapFile dummy = new PcapFile(file)) {
+            ByteBuffer byteBuffer = fEthernetPacket;
+            if (byteBuffer == null) {
+                fail("GenericPacketTest has failed!");
+                return;
+            }
+
+            Packet packet = new EthernetIIPacket(dummy, null, byteBuffer);
+            assertTrue(packet.hasProtocol(Protocol.ETHERNET_II));
+            assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
+            assertFalse(packet.hasProtocol(Protocol.TCP));
+            assertEquals(Protocol.ETHERNET_II, packet.getProtocol());
+
+            assertEquals(packet, packet.getPacket(Protocol.ETHERNET_II));
+            assertNull(packet.getPacket(Protocol.TCP));
+            assertEquals(packet.getChildPacket(), packet.getPacket(Protocol.UNKNOWN));
+            assertEquals(packet.getPacket(Protocol.ETHERNET_II), packet.getMostEcapsulatedPacket());
+
+            assertNull(packet.getParentPacket());
+            assertFalse(packet.getPcapFile().equals(null));
+
+            Packet child = packet.getChildPacket();
+            if (child == null) {
+                fail("GenericPacketTest has failed!");
+                return;
+            }
+            assertEquals(packet.getPayload(), child.getPayload());
+            assertEquals(packet.getGlobalSummaryString(), "Source MAC: 10:f8:82:b3:44:78 , Destination MAC: 34:67:0c:d2:91:51");
+
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/AllTests.java
new file mode 100644 (file)
index 0000000..57a5457
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Protocol test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        ProtocolTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java
new file mode 100644 (file)
index 0000000..38b9922
--- /dev/null
@@ -0,0 +1,87 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.ProtocolValues;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests whether protocol operation are happening without
+ * error.
+ *
+ * @author Vincent Perot
+ */
+public class ProtocolTest {
+
+    /**
+     * Test that verify if the protocol attributes are as expected.
+     */
+    @Test
+    public void TestProtocolAttributes() {
+        assertEquals(Protocol.PCAP.getName(), "Packet Capture");
+        assertEquals(Protocol.PCAP.getShortName(), "pcap");
+        assertEquals(Protocol.PCAP.getLayer(), ProtocolValues.LAYER_0);
+    }
+
+    /**
+     * Test that verify if the protocol getter methods are working properly.
+     */
+    @Test
+    public void TestgetProtocols() {
+        List<Protocol> list = new ArrayList<>();
+        List<Protocol> manualListLayer = new ArrayList<>();
+        for (int i = ProtocolValues.LAYER_0; i <= ProtocolValues.LAYER_7; i++) {
+            List<Protocol> listLayer = Protocol.getProtocolsOnLayer(i);
+            list.addAll(listLayer);
+
+            manualListLayer.clear();
+            switch (i) {
+            case ProtocolValues.LAYER_0:
+                manualListLayer.add(Protocol.PCAP);
+                break;
+            case ProtocolValues.LAYER_1:
+                break;
+            case ProtocolValues.LAYER_2:
+                manualListLayer.add(Protocol.ETHERNET_II);
+                break;
+            case ProtocolValues.LAYER_3:
+                manualListLayer.add(Protocol.IPV4);
+                break;
+            case ProtocolValues.LAYER_4:
+                manualListLayer.add(Protocol.TCP);
+                manualListLayer.add(Protocol.UDP);
+                break;
+            case ProtocolValues.LAYER_5:
+                break;
+            case ProtocolValues.LAYER_6:
+                break;
+            case ProtocolValues.LAYER_7:
+                manualListLayer.add(Protocol.UNKNOWN);
+                break;
+            default:
+                fail("Illegal layer value!");
+            }
+            assertEquals(manualListLayer, listLayer);
+        }
+        assertEquals(Protocol.getAllProtocols(), list);
+
+    }
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/AllTests.java
new file mode 100644 (file)
index 0000000..b640d9c
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.ethernet2;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Ethernet II test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        EthernetIIPacketTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/EthernetIIPacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/EthernetIIPacketTest.java
new file mode 100644 (file)
index 0000000..7414c24
--- /dev/null
@@ -0,0 +1,146 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.ethernet2;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+import java.util.Map;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.ethernet2.EthernetIIEndpoint;
+import org.eclipse.linuxtools.pcap.core.protocol.ethernet2.EthernetIIPacket;
+import org.eclipse.linuxtools.pcap.core.protocol.ethernet2.EthernetIIValues;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * JUnit Class that tests the EthernetIIPacket class and its method.
+ *
+ * @author Vincent Perot
+ */
+public class EthernetIIPacketTest {
+
+    private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of(
+            "Source MAC Address", "10:f8:82:b3:44:78",
+            "Destination MAC Address", "34:67:0c:d2:91:51",
+            "Ethertype", "Unknown (0xa256)"
+            );
+
+    private static final String EXPECTED_TOSTRING =
+            "Ethernet II, Source: 10:f8:82:b3:44:78, Destination: 34:67:0c:d2:91:51, Type: Unknown (0xa256)\nPayload: a6";
+
+    private ByteBuffer fPacket;
+
+    /**
+     * Initialize the packet.
+     */
+    @Before
+    public void initialize() {
+        fPacket = ByteBuffer.allocate(15);
+        fPacket.order(ByteOrder.BIG_ENDIAN);
+
+        // Destination MAC - 6 bytes
+        fPacket.put((byte) 0x34);
+        fPacket.put((byte) 0x67);
+        fPacket.put((byte) 0x0C);
+        fPacket.put((byte) 0xD2);
+        fPacket.put((byte) 0x91);
+        fPacket.put((byte) 0x51);
+
+        // Source MAC - 6 bytes
+        fPacket.put((byte) 0x10);
+        fPacket.put((byte) 0xF8);
+        fPacket.put((byte) 0x82);
+        fPacket.put((byte) 0xB3);
+        fPacket.put((byte) 0x44);
+        fPacket.put((byte) 0x78);
+
+        // Ethertype - 2 bytes
+        fPacket.put((byte) 0xA2);
+        fPacket.put((byte) 0x56);
+
+        // Payload - 1 byte
+        fPacket.put((byte) 0xA6);
+
+        fPacket.flip();
+    }
+
+    /**
+     * Test that verify the correctness of the EthernetIIPacket's methods.
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void CompleteEthernetIIPacketTest() throws IOException, BadPcapFileException, BadPacketException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+        String file = trace.getPath();
+        try (PcapFile dummy = new PcapFile(file)) {
+            ByteBuffer byteBuffer = fPacket;
+            if (byteBuffer == null) {
+                fail("CompleteEthernetIIPacketTest has failed!");
+                return;
+            }
+            EthernetIIPacket packet = new EthernetIIPacket(dummy, null, byteBuffer);
+
+            // Protocol Testing
+            assertEquals(Protocol.ETHERNET_II, packet.getProtocol());
+            assertTrue(packet.hasProtocol(Protocol.ETHERNET_II));
+            assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
+            assertFalse(packet.hasProtocol(Protocol.TCP));
+
+            // Abstract methods Testing
+            assertTrue(packet.validate());
+            assertEquals(-653947816, packet.hashCode());
+            assertFalse(packet.equals(null));
+            assertEquals(new EthernetIIPacket(dummy, null, byteBuffer), packet);
+
+            assertEquals(EXPECTED_FIELDS, packet.getFields());
+            assertEquals(EXPECTED_TOSTRING, packet.toString());
+            assertEquals("Src: 10:f8:82:b3:44:78 , Dst: 34:67:0c:d2:91:51", packet.getLocalSummaryString());
+            assertEquals("Source MAC: 10:f8:82:b3:44:78 , Destination MAC: 34:67:0c:d2:91:51", packet.getGlobalSummaryString());
+
+            assertEquals(new EthernetIIEndpoint(packet, true), packet.getSourceEndpoint());
+            assertEquals(new EthernetIIEndpoint(packet, false), packet.getDestinationEndpoint());
+
+            fPacket.position(14);
+            byte[] payload = new byte[1];
+            fPacket.get(payload);
+            assertEquals(ByteBuffer.wrap(payload), packet.getPayload());
+
+            // Packet-specific methods Testing
+            assertTrue(Arrays.equals(packet.getSourceMacAddress(), Arrays.copyOfRange(fPacket.array(), EthernetIIValues.MAC_ADDRESS_SIZE, EthernetIIValues.MAC_ADDRESS_SIZE + EthernetIIValues.MAC_ADDRESS_SIZE)));
+            assertTrue(Arrays.equals(packet.getDestinationMacAddress(), Arrays.copyOfRange(fPacket.array(), 0, 0 + EthernetIIValues.MAC_ADDRESS_SIZE)));
+            assertEquals(0xA256, packet.getEthertype());
+
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/AllTests.java
new file mode 100644 (file)
index 0000000..e431897
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.ipv4;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * IPv4 test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        IPv4PacketTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/IPv4PacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/IPv4PacketTest.java
new file mode 100644 (file)
index 0000000..2877b25
--- /dev/null
@@ -0,0 +1,211 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.ipv4;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.ipv4.IPv4Endpoint;
+import org.eclipse.linuxtools.pcap.core.protocol.ipv4.IPv4Packet;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests the IPv4Packet class and its method.
+ *
+ * @author Vincent Perot
+ */
+public class IPv4PacketTest {
+
+
+    private static final Map<String, String> EXPECTED_FIELDS;
+    static {
+        EXPECTED_FIELDS = new LinkedHashMap<>();
+        EXPECTED_FIELDS.put("Version", "4");
+        EXPECTED_FIELDS.put("Header Length", "24 bytes");
+        EXPECTED_FIELDS.put("Differentiated Services Field", "0x26");
+        EXPECTED_FIELDS.put("Explicit Congestion Notification", "0x02");
+        EXPECTED_FIELDS.put("Total Length", "255 bytes");
+        EXPECTED_FIELDS.put("Identification", "0x0ff0");
+        EXPECTED_FIELDS.put("Don't Fragment Flag", "false");
+        EXPECTED_FIELDS.put("More Fragment Flag", "false");
+        EXPECTED_FIELDS.put("Fragment Offset", "-31");
+        EXPECTED_FIELDS.put("Time to live", "160");
+        EXPECTED_FIELDS.put("Protocol", "Unknown (254)");
+        EXPECTED_FIELDS.put("Checksum", "0x3344");
+        EXPECTED_FIELDS.put("Source IP Address", "192.168.1.0");
+        EXPECTED_FIELDS.put("Destination IP Address", "193.169.2.1");
+        EXPECTED_FIELDS.put("Options", "a2 56 a2 56");
+    }
+
+    private static final String EXPECTED_TOSTRING;
+    static {
+        StringBuilder sb = new StringBuilder();
+        sb.append("Internet Protocol Version 4, Source: 192.168.1.0, Destination: 193.169.2.1\n");
+        sb.append("Version: 4, Identification: 0x0ff0, Header Length: 24 bytes, Total Length: 255 bytes\n");
+        sb.append("Differentiated Services Code Point: 0x26; Explicit Congestion Notification: 0x02\n");
+        sb.append("Flags: 0x00 (Don't have more fragments), Fragment Offset: -31\n");
+        sb.append("Time to live: 160\n");
+        sb.append("Protocol: 254\n");
+        sb.append("Header Checksum: 0x3344\n");
+        sb.append("Payload: a6");
+
+        EXPECTED_TOSTRING = sb.toString();
+    }
+
+    private ByteBuffer fPacket;
+
+    /**
+     * Initialize the packet.
+     */
+    @Before
+    public void initialize() {
+        fPacket = ByteBuffer.allocate(25);
+        fPacket.order(ByteOrder.BIG_ENDIAN);
+
+        // Version + IHL
+        fPacket.put((byte) 0x46);
+
+        // DSCP + ECN
+        fPacket.put((byte) 0x9A);
+
+        // Total length - this is randomly chosen so that we verify that the
+        // packet handles wrong total length.
+        fPacket.put((byte) 0x00);
+        fPacket.put((byte) 0xFF);
+
+        // Identification
+        fPacket.put((byte) 0x0F);
+        fPacket.put((byte) 0xF0);
+
+        // Flags + Fragment Offset
+        fPacket.put((byte) 0x1E);
+        fPacket.put((byte) 0xE1);
+
+        // Time to live
+        fPacket.put((byte) 0xA0);
+
+        // Protocol - Unknown
+        fPacket.put((byte) 0xFE);
+
+        // Header checksum - chosen randomly
+        fPacket.put((byte) 0x33);
+        fPacket.put((byte) 0x44);
+
+        // Source IP - 4 bytes
+        fPacket.put((byte) 192);
+        fPacket.put((byte) 168);
+        fPacket.put((byte) 1);
+        fPacket.put((byte) 0);
+
+        // Destination IP - 4 bytes
+        fPacket.put((byte) 193);
+        fPacket.put((byte) 169);
+        fPacket.put((byte) 2);
+        fPacket.put((byte) 1);
+
+        // Options - 4 bytes
+        fPacket.put((byte) 0xA2);
+        fPacket.put((byte) 0x56);
+        fPacket.put((byte) 0xA2);
+        fPacket.put((byte) 0x56);
+
+        // Payload - 1 byte
+        fPacket.put((byte) 0xA6);
+
+        fPacket.flip();
+    }
+
+    /**
+     * Test that verify the correctness of the IPv4Packet's methods.
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void CompleteIPv4PacketTest() throws IOException, BadPcapFileException, BadPacketException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+        String file = trace.getPath();
+        try (PcapFile dummy = new PcapFile(file)) {
+            ByteBuffer byteBuffer = fPacket;
+            if (byteBuffer == null) {
+                fail("CompleteIPv4PacketTest has failed!");
+                return;
+            }
+            IPv4Packet packet = new IPv4Packet(dummy, null, byteBuffer);
+
+            // Protocol Testing
+            assertEquals(Protocol.IPV4, packet.getProtocol());
+            assertTrue(packet.hasProtocol(Protocol.IPV4));
+            assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
+            assertFalse(packet.hasProtocol(Protocol.TCP));
+
+            // Abstract methods Testing
+            assertTrue(packet.validate());
+            assertEquals(1910842322, packet.hashCode());
+            assertFalse(packet.equals(null));
+            assertEquals(new IPv4Packet(dummy, null, byteBuffer), packet);
+
+            assertEquals(EXPECTED_FIELDS, packet.getFields());
+            assertEquals(EXPECTED_TOSTRING, packet.toString());
+            assertEquals("Src: 192.168.1.0 , Dst: 193.169.2.1", packet.getLocalSummaryString());
+            assertEquals("192.168.1.0 > 193.169.2.1 Id=4080 Len=1", packet.getGlobalSummaryString());
+
+            assertEquals(new IPv4Endpoint(packet, true), packet.getSourceEndpoint());
+            assertEquals(new IPv4Endpoint(packet, false), packet.getDestinationEndpoint());
+
+            fPacket.position(24);
+            byte[] payload = new byte[1];
+            fPacket.get(payload);
+            assertEquals(ByteBuffer.wrap(payload), packet.getPayload());
+
+            // Packet-specific methods Testing
+            assertTrue(Arrays.equals(packet.getSourceIpAddress(), Arrays.copyOfRange(fPacket.array(), 12, 16)));
+            assertTrue(Arrays.equals(packet.getDestinationIpAddress(), Arrays.copyOfRange(fPacket.array(), 16, 20)));
+            assertTrue(Arrays.equals(packet.getOptions(), Arrays.copyOfRange(fPacket.array(), 20, 24)));
+            assertEquals(4, packet.getVersion());
+            assertEquals(24, packet.getHeaderLength());
+            assertEquals(0x26, packet.getDSCP());
+            assertEquals(0x02, packet.getExplicitCongestionNotification());
+            assertEquals(255, packet.getTotalLength());
+            assertEquals(0x0FF0, packet.getIdentification());
+            assertFalse(packet.getReservedFlag());
+            assertFalse(packet.getDontFragmentFlag());
+            assertFalse(packet.getHasMoreFragment());
+            assertEquals(-31, packet.getFragmentOffset());
+            assertEquals(160, packet.getTimeToLive());
+            assertEquals(0xFE, packet.getIpDatagramProtocol());
+            assertEquals(0x3344, packet.getHeaderChecksum());
+
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/AllTests.java
new file mode 100644 (file)
index 0000000..c5d70f7
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.pcap;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Pcap test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        PcapPacketTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/PcapPacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/PcapPacketTest.java
new file mode 100644 (file)
index 0000000..3492bc9
--- /dev/null
@@ -0,0 +1,228 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.pcap;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Map;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.pcap.PcapEndpoint;
+import org.eclipse.linuxtools.pcap.core.protocol.pcap.PcapPacket;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * JUnit Class that tests the PcapPacket class and its method.
+ *
+ * @author Vincent Perot
+ */
+public class PcapPacketTest {
+
+    private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of(
+            "Frame", "36",
+            "Frame Length", "75 bytes",
+            "Capture Length", "75 bytes",
+            "Capture Time", "2005-07-04 05:33:52.829.277.000"
+            );
+
+    private static final String EXPECTED_TOSTRING;
+    static {
+        StringBuilder sb = new StringBuilder();
+        sb.append("Packet Capture 36: 75 bytes on wire, 75 bytes captured.\n");
+        sb.append("Arrival time: 2005-07-04 05:33:52.829.277.000\n");
+        sb.append("Ethernet II, Source: 00:e0:ed:01:6e:bd, Destination: 00:30:54:00:34:56, Type: Internet Protocol Version 4 (0x0800)\n");
+        sb.append("Internet Protocol Version 4, Source: 192.168.1.2, Destination: 192.168.1.1\n");
+        sb.append("Version: 4, Identification: 0x69aa, Header Length: 20 bytes, Total Length: 61 bytes\n");
+        sb.append("Differentiated Services Code Point: 0x00; Explicit Congestion Notification: 0x00\n");
+        sb.append("Flags: 0x00 (Don't have more fragments), Fragment Offset: 0\n");
+        sb.append("Time to live: 128\n");
+        sb.append("Protocol: 17\n");
+        sb.append("Header Checksum: 0x4db2\n");
+        sb.append("User Datagram Protocol, Source Port: 2719, Destination Port: 53, Length: 41, Checksum: 19038\n");
+        sb.append("Payload: ed d4 01 00 00 01 00 00 00 00 00 00 03 66 74 70 07 65 63 69 74 65 6c 65 03 63 6f 6d 00 00 01 00 01");
+
+        EXPECTED_TOSTRING = sb.toString();
+    }
+
+    private ByteBuffer fPayload;
+
+    /**
+     * Initialize the payload.
+     */
+    @Before
+    public void initialize() {
+        fPayload = ByteBuffer.allocate(75);
+        fPayload.order(ByteOrder.BIG_ENDIAN);
+
+        // Values copied from wireshark
+
+        // Bytes 0x01-0x10
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x30);
+        fPayload.put((byte) 0x54);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x34);
+        fPayload.put((byte) 0x56);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0xE0);
+        fPayload.put((byte) 0xED);
+        fPayload.put((byte) 0x01);
+        fPayload.put((byte) 0x6E);
+        fPayload.put((byte) 0xBD);
+        fPayload.put((byte) 0x08);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x45);
+        fPayload.put((byte) 0x00);
+
+        // Bytes 0x11-0x20
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x3D);
+        fPayload.put((byte) 0x69);
+        fPayload.put((byte) 0xAA);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x80);
+        fPayload.put((byte) 0x11);
+        fPayload.put((byte) 0x4D);
+        fPayload.put((byte) 0xB2);
+        fPayload.put((byte) 0xC0);
+        fPayload.put((byte) 0xA8);
+        fPayload.put((byte) 0x01);
+        fPayload.put((byte) 0x02);
+        fPayload.put((byte) 0xC0);
+        fPayload.put((byte) 0xA8);
+
+        // Bytes 0x21-0x30
+        fPayload.put((byte) 0x01);
+        fPayload.put((byte) 0x01);
+        fPayload.put((byte) 0x0A);
+        fPayload.put((byte) 0x9F);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x35);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x29);
+        fPayload.put((byte) 0x4A);
+        fPayload.put((byte) 0x5E);
+        fPayload.put((byte) 0xED);
+        fPayload.put((byte) 0xd4);
+        fPayload.put((byte) 0x01);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x01);
+
+        // Bytes 0x31-0x40
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x03);
+        fPayload.put((byte) 0x66);
+        fPayload.put((byte) 0x74);
+        fPayload.put((byte) 0x70);
+        fPayload.put((byte) 0x07);
+        fPayload.put((byte) 0x65);
+        fPayload.put((byte) 0x63);
+        fPayload.put((byte) 0x69);
+        fPayload.put((byte) 0x74);
+        fPayload.put((byte) 0x65);
+
+        // Bytes 0x41-0x4B
+        fPayload.put((byte) 0x6C);
+        fPayload.put((byte) 0x65);
+        fPayload.put((byte) 0x03);
+        fPayload.put((byte) 0x63);
+        fPayload.put((byte) 0x6F);
+        fPayload.put((byte) 0x6D);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x01);
+        fPayload.put((byte) 0x00);
+        fPayload.put((byte) 0x01);
+
+        fPayload.flip();
+    }
+
+    /**
+     * Test that verify the correctness of the PcapPacket's methods.
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void CompletePcapPacketTest() throws IOException, BadPcapFileException, BadPacketException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_UDP;
+        assumeTrue(trace.exists());
+        String path = trace.getPath();
+        try (PcapFile file = new PcapFile(path);) {
+
+            file.seekPacket(36);
+            PcapPacket packet = file.parseNextPacket();
+            if (packet == null) {
+                fail("CompletePcapPacketTest has failed!");
+                return;
+            }
+            // Protocol Testing
+            assertEquals(Protocol.PCAP, packet.getProtocol());
+            assertTrue(packet.hasProtocol(Protocol.PCAP));
+            assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
+            assertFalse(packet.hasProtocol(Protocol.TCP));
+
+            // Abstract methods Testing
+            assertTrue(packet.validate());
+            assertEquals(842182851, packet.hashCode());
+            assertFalse(packet.equals(null));
+            assertFalse(packet.equals(file.parseNextPacket()));
+
+            assertEquals(EXPECTED_FIELDS, packet.getFields());
+            assertEquals(EXPECTED_TOSTRING, packet.toString());
+            assertEquals("Frame 36: 75 bytes on wire, 75 bytes captured", packet.getLocalSummaryString());
+            assertEquals("Source Port: 2719, Destination Port: 53", packet.getGlobalSummaryString());
+
+            assertEquals(new PcapEndpoint(packet, true), packet.getSourceEndpoint());
+            assertEquals(new PcapEndpoint(packet, false), packet.getDestinationEndpoint());
+
+            ByteBuffer payload = packet.getPayload();
+            if (payload == null) {
+                fail("CompletePcapPacketTest has failed!");
+                return;
+            }
+            assertEquals(fPayload, payload.flip());
+
+            // Packet-specific methods Testing
+            assertEquals(36, packet.getIndex());
+            assertEquals(75, packet.getOriginalLength());
+            assertEquals(75, packet.getIncludedLength());
+            assertEquals(1120469632829277L, packet.getTimestamp());
+            assertFalse(packet.isTruncated());
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/AllTests.java
new file mode 100644 (file)
index 0000000..201e83d
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.tcp;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * TCP test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        TCPPacketTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/TCPPacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/TCPPacketTest.java
new file mode 100644 (file)
index 0000000..7023a29
--- /dev/null
@@ -0,0 +1,214 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.tcp;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.tcp.TCPEndpoint;
+import org.eclipse.linuxtools.pcap.core.protocol.tcp.TCPPacket;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * JUnit Class that tests the TCPPacket class and its method.
+ *
+ * @author Vincent Perot
+ */
+public class TCPPacketTest {
+
+    private static final Map<String, String> EXPECTED_FIELDS;
+    static {
+        EXPECTED_FIELDS = new LinkedHashMap<>();
+        EXPECTED_FIELDS.put("Source Port", "18057");
+        EXPECTED_FIELDS.put("Destination Port", "39611");
+        EXPECTED_FIELDS.put("Sequence Number", "2575857510");
+        EXPECTED_FIELDS.put("Acknowledgement Number", "1430532898");
+        EXPECTED_FIELDS.put("Length", "24 bytes");
+        EXPECTED_FIELDS.put("ECN-Nonce Flag", "true");
+        EXPECTED_FIELDS.put("Congestion Window Reduced Flag", "false");
+        EXPECTED_FIELDS.put("ECN-Echo Flag", "true");
+        EXPECTED_FIELDS.put("Urgent Flag", "false");
+        EXPECTED_FIELDS.put("ACK Flag", "true");
+        EXPECTED_FIELDS.put("PSH Flag", "false");
+        EXPECTED_FIELDS.put("RST Flag", "true");
+        EXPECTED_FIELDS.put("SYN Flag", "false");
+        EXPECTED_FIELDS.put("FIN Flag", "true");
+        EXPECTED_FIELDS.put("Window Size Value", "4352");
+        EXPECTED_FIELDS.put("Checksum", "0xffee");
+        EXPECTED_FIELDS.put("Urgent Pointer", "0xddcc");
+        EXPECTED_FIELDS.put("Options", "ad da bc cb");
+    }
+
+    private static final String EXPECTED_TOSTRING;
+    static {
+        StringBuilder sb = new StringBuilder();
+        sb.append("Transmission Control Protocol, Source Port: 18057, Destination Port: 39611\n");
+        sb.append("Sequence Number: 2575857510, Acknowledgment Number: 1430532898\n");
+        sb.append("Header length: 24 bytes, Data length: 4\n");
+        sb.append("Window size value: 4352, Urgent Pointer: 0xddcc\n");
+        sb.append("Checksum: 0xffee\n");
+        sb.append("Payload: 99 88 77 66");
+
+        EXPECTED_TOSTRING = sb.toString();
+    }
+
+    private ByteBuffer fPacket;
+
+    /**
+     * Initialize the packet.
+     */
+    @Before
+    public void initialize() {
+        fPacket = ByteBuffer.allocate(28);
+        fPacket.order(ByteOrder.BIG_ENDIAN);
+
+        // Source Port
+        fPacket.put((byte) 0x46);
+        fPacket.put((byte) 0x89);
+
+        // Destination Port
+        fPacket.put((byte) 0x9A);
+        fPacket.put((byte) 0xBB);
+
+        // Sequence Number
+        fPacket.put((byte) 0x99);
+        fPacket.put((byte) 0x88);
+        fPacket.put((byte) 0x77);
+        fPacket.put((byte) 0x66);
+
+        // Acknowledgment Number
+        fPacket.put((byte) 0x55);
+        fPacket.put((byte) 0x44);
+        fPacket.put((byte) 0x33);
+        fPacket.put((byte) 0x22);
+
+        // Data Offset + Reserved + NS
+        fPacket.put((byte) 0x61);
+
+        // Other flags
+        fPacket.put((byte) 0b01010101);
+
+        // Window Size
+        fPacket.put((byte) 0x11);
+        fPacket.put((byte) 0x00);
+
+        // Checksum
+        fPacket.put((byte) 0xFF);
+        fPacket.put((byte) 0xEE);
+
+        // Urgent Pointer
+        fPacket.put((byte) 0xDD);
+        fPacket.put((byte) 0xCC);
+
+        // Options - 4 bytes
+        fPacket.put((byte) 0xAD);
+        fPacket.put((byte) 0xDA);
+        fPacket.put((byte) 0xBC);
+        fPacket.put((byte) 0xCB);
+
+        // Payload - 4 bytes
+        fPacket.put((byte) 0x99);
+        fPacket.put((byte) 0x88);
+        fPacket.put((byte) 0x77);
+        fPacket.put((byte) 0x66);
+
+        fPacket.flip();
+    }
+
+    /**
+     * Test that verify the correctness of the TCPPacket's methods.
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void CompleteTCPPacketTest() throws BadPacketException, IOException, BadPcapFileException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+        String file = trace.getPath();
+        try (PcapFile dummy = new PcapFile(file)) {
+            ByteBuffer byteBuffer = fPacket;
+            if (byteBuffer == null) {
+                fail("CompleteTCPPacketTest has failed!");
+                return;
+            }
+            TCPPacket packet = new TCPPacket(dummy, null, byteBuffer);
+
+            // Protocol Testing
+            assertEquals(Protocol.TCP, packet.getProtocol());
+            assertTrue(packet.hasProtocol(Protocol.TCP));
+            assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
+            assertFalse(packet.hasProtocol(Protocol.IPV4));
+
+            // Abstract methods Testing
+            assertTrue(packet.validate());
+            assertEquals(-677046102, packet.hashCode());
+            assertFalse(packet.equals(null));
+            assertEquals(new TCPPacket(dummy, null, byteBuffer), packet);
+
+            assertEquals(EXPECTED_FIELDS, packet.getFields());
+            assertEquals(EXPECTED_TOSTRING, packet.toString());
+            assertEquals("Src Port: 18057, Dst Port: 39611, Seq: 2575857510, Ack: 1430532898, Len: 24", packet.getLocalSummaryString());
+            assertEquals("18057 > 39611 [ACK, FIN, RST, NS, ECE] Seq=2575857510 Ack=1430532898 Len=24", packet.getGlobalSummaryString());
+
+            assertEquals(new TCPEndpoint(packet, true), packet.getSourceEndpoint());
+            assertEquals(new TCPEndpoint(packet, false), packet.getDestinationEndpoint());
+
+            fPacket.position(24);
+            byte[] payload = new byte[4];
+            fPacket.get(payload);
+            assertEquals(ByteBuffer.wrap(payload), packet.getPayload());
+
+            // Packet-specific methods Testing
+            assertEquals(0x4689, packet.getSourcePort());
+            assertEquals(0x9ABB, packet.getDestinationPort());
+            assertEquals(2575857510L, packet.getSequenceNumber());
+            assertEquals(1430532898L, packet.getAcknowledgmentNumber());
+            assertEquals(6, packet.getDataOffset());
+            assertEquals(0, packet.getReservedField());
+            assertEquals(true, packet.isNSFlagSet());
+            assertEquals(false, packet.isCongestionWindowReducedFlagSet());
+            assertEquals(true, packet.isECNEchoFlagSet());
+            assertEquals(false, packet.isUrgentFlagSet());
+            assertEquals(true, packet.isAcknowledgeFlagSet());
+            assertEquals(false, packet.isPushFlagSet());
+            assertEquals(true, packet.isResetFlagSet());
+            assertEquals(false, packet.isSynchronizationFlagSet());
+            assertEquals(true, packet.isFinalFlagSet());
+            assertEquals(4352, packet.getWindowSize());
+            assertEquals(65518, packet.getChecksum());
+            assertEquals(56780, packet.getUrgentPointer());
+            assertTrue(Arrays.equals(packet.getOptions(), Arrays.copyOfRange(fPacket.array(), 20, 24)));
+
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/AllTests.java
new file mode 100644 (file)
index 0000000..13c541e
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.udp;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * UDP test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        UDPPacketTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/UDPPacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/UDPPacketTest.java
new file mode 100644 (file)
index 0000000..2a4965c
--- /dev/null
@@ -0,0 +1,152 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.udp;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Map;
+
+import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.udp.UDPEndpoint;
+import org.eclipse.linuxtools.pcap.core.protocol.udp.UDPPacket;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * JUnit Class that tests the UDPPacket class and its method.
+ *
+ * @author Vincent Perot
+ */
+public class UDPPacketTest {
+
+    private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of(
+            "Source Port", "18057",
+            "Destination Port", "39611",
+            "Length", "41452 bytes",
+            "Checksum", "0xfaaf"
+            );
+
+    private static final String EXPTECTED_TOSTRING;
+    static {
+        StringBuilder sb = new StringBuilder();
+        sb.append("User Datagram Protocol, Source Port: 18057, Destination Port: 39611, Length: 41452, Checksum: 64175\n");
+        sb.append("Payload: 99 88 77 66");
+
+        EXPTECTED_TOSTRING = sb.toString();
+    }
+
+    private ByteBuffer fPacket;
+
+    /**
+     * Initialize the packet.
+     */
+    @Before
+    public void initialize() {
+        fPacket = ByteBuffer.allocate(12);
+        fPacket.order(ByteOrder.BIG_ENDIAN);
+
+        // Source Port
+        fPacket.put((byte) 0x46);
+        fPacket.put((byte) 0x89);
+
+        // Destination Port
+        fPacket.put((byte) 0x9A);
+        fPacket.put((byte) 0xBB);
+
+        // Total length - this is randomly chosen so that we verify that the
+        // packet handles wrong total length.
+        fPacket.put((byte) 0xA1);
+        fPacket.put((byte) 0xEC);
+
+        // Checksum
+        fPacket.put((byte) 0xFA);
+        fPacket.put((byte) 0xAF);
+
+        // Payload - 4 bytes
+        fPacket.put((byte) 0x99);
+        fPacket.put((byte) 0x88);
+        fPacket.put((byte) 0x77);
+        fPacket.put((byte) 0x66);
+
+        fPacket.flip();
+    }
+
+    /**
+     * Test that verify the correctness of the UDPPacket's methods.
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     * @throws BadPacketException
+     *             Thrown when a packet is erroneous. Fails the test.
+     */
+    @Test
+    public void CompleteUDPPacketTest() throws IOException, BadPcapFileException, BadPacketException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+        String file = trace.getPath();
+        try (PcapFile dummy = new PcapFile(file)) {
+            ByteBuffer byteBuffer = fPacket;
+            if (byteBuffer == null) {
+                fail("CompleteUDPPacketTest has failed!");
+                return;
+            }
+            UDPPacket packet = new UDPPacket(dummy, null, byteBuffer);
+
+            // Protocol Testing
+            assertEquals(Protocol.UDP, packet.getProtocol());
+            assertTrue(packet.hasProtocol(Protocol.UDP));
+            assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
+            assertFalse(packet.hasProtocol(Protocol.ETHERNET_II));
+
+            // Abstract methods Testing
+            assertTrue(packet.validate());
+            assertEquals(473000225, packet.hashCode());
+            assertFalse(packet.equals(null));
+            assertEquals(new UDPPacket(dummy, null, byteBuffer), packet);
+
+            assertEquals(EXPECTED_FIELDS, packet.getFields());
+            assertEquals(EXPTECTED_TOSTRING, packet.toString());
+            assertEquals("Src Port: 18057, Dst Port: 39611", packet.getLocalSummaryString());
+            assertEquals("Source Port: 18057, Destination Port: 39611", packet.getGlobalSummaryString());
+
+            assertEquals(new UDPEndpoint(packet, true), packet.getSourceEndpoint());
+            assertEquals(new UDPEndpoint(packet, false), packet.getDestinationEndpoint());
+
+            fPacket.position(8);
+            byte[] payload = new byte[4];
+            fPacket.get(payload);
+            assertEquals(ByteBuffer.wrap(payload), packet.getPayload());
+
+            // Packet-specific methods Testing
+            assertEquals(0x4689, packet.getSourcePort());
+            assertEquals(0x9ABB, packet.getDestinationPort());
+            assertEquals(0xA1EC, packet.getTotalLength());
+            assertEquals(0xFAAF, packet.getChecksum());
+
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/AllTests.java
new file mode 100644 (file)
index 0000000..f33d53b
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.unknown;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Unknown test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        UnknownPacketTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java
new file mode 100644 (file)
index 0000000..1e1b627
--- /dev/null
@@ -0,0 +1,121 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.protocol.unknown;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Map;
+
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.protocol.unknown.UnknownEndpoint;
+import org.eclipse.linuxtools.pcap.core.protocol.unknown.UnknownPacket;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * JUnit Class that tests the UnknownPacket class and its method.
+ *
+ * @author Vincent Perot
+ */
+public class UnknownPacketTest {
+
+    private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of(
+            "Binary", "61",
+            "Character", "a"
+            );
+
+    private static final String fToString = "Payload: 61";
+
+    private ByteBuffer fPacket;
+
+    /**
+     * Initialize the packet.
+     */
+    @Before
+    public void initialize() {
+        fPacket = ByteBuffer.allocate(1);
+        fPacket.order(ByteOrder.BIG_ENDIAN);
+
+        // Payload - 1 byte
+        fPacket.put((byte) 97);
+
+        fPacket.flip();
+    }
+
+    /**
+     * Test that verify the correctness of the UnknownPacket's methods.
+     * @throws BadPcapFileException
+     *             Thrown when the file is erroneous. Fails the test.
+     * @throws IOException
+     *             Thrown when an IO error occurs. Fails the test.
+     */
+    @Test
+    public void CompleteUnknownPacketTest() throws IOException, BadPcapFileException {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+        String file = trace.getPath();
+        try (PcapFile dummy = new PcapFile(file)) {
+            ByteBuffer byteBuffer = fPacket;
+            if (byteBuffer == null) {
+                fail("CompleteUnknownPacketTest has failed!");
+                return;
+            }
+            UnknownPacket packet = new UnknownPacket(dummy, null, byteBuffer);
+
+            // Protocol Testing
+            assertEquals(Protocol.UNKNOWN, packet.getProtocol());
+            assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
+            assertFalse(packet.hasProtocol(Protocol.UDP));
+
+            // Abstract methods Testing
+            assertTrue(packet.validate());
+            assertEquals(1089, packet.hashCode());
+            assertFalse(packet.equals(null));
+            assertEquals(new UnknownPacket(dummy, null, byteBuffer), packet);
+
+            assertEquals(EXPECTED_FIELDS, packet.getFields());
+            assertEquals(fToString, packet.toString());
+            assertEquals("Len: 1 bytes", packet.getLocalSummaryString());
+            assertEquals("Data: 1 bytes", packet.getGlobalSummaryString());
+            // TODO take care of plural form.
+
+            // Unknown Endpoints are never equal!
+            assertFalse(packet.getSourceEndpoint().equals(new UnknownEndpoint(packet, true)));
+            assertFalse(packet.getDestinationEndpoint().equals(new UnknownEndpoint(packet, false)));
+
+            fPacket.position(0);
+            byte[] payload = new byte[1];
+            fPacket.get(payload);
+            ByteBuffer payloadBB = ByteBuffer.wrap(payload);
+            payloadBB.flip();
+
+            assertEquals(payloadBB, packet.getPayload());
+
+            // Packet-specific methods Testing
+            // None
+
+        }
+    }
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/AllTests.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/AllTests.java
new file mode 100644 (file)
index 0000000..df90513
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.stream;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Stream test suite
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+        StreamBuildTest.class
+})
+public class AllTests {
+
+}
diff --git a/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/StreamBuildTest.java b/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/StreamBuildTest.java
new file mode 100644 (file)
index 0000000..90c8e6b
--- /dev/null
@@ -0,0 +1,131 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Vincent Perot - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.pcap.core.tests.stream;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
+import org.eclipse.linuxtools.pcap.core.stream.PacketStream;
+import org.eclipse.linuxtools.pcap.core.stream.PacketStreamBuilder;
+import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
+import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * JUnit Class that tests whether packet streams are built correctly.
+ *
+ * @author Vincent Perot
+ */
+public class StreamBuildTest {
+
+    // Values taken from wireshark
+    private static final Set<Long> TCP_INDEX_SET_STREAM_7_PACKETS = ImmutableSet.of(
+            // This stream contains 7 packets.
+            17L,
+            23L,
+            25L,
+            26L,
+            27L,
+            35L,
+            36L
+            );
+
+    private static final Set<Long> TCP_INDEX_SET_STREAM_34_PACKETS = new TreeSet<>();
+    static {
+        // This stream contains many packet (34). Some packets doesn't
+        // belong to it like the 17 or the 23.
+        for (Long i = new Long(0); i < 43; i++) {
+            TCP_INDEX_SET_STREAM_34_PACKETS.add(i);
+        }
+        TCP_INDEX_SET_STREAM_34_PACKETS.removeAll(TCP_INDEX_SET_STREAM_7_PACKETS);
+    }
+
+    /**
+     * Test that verify that stream building is done correctly.
+     */
+    @Test
+    public void StreamBuildingTest() {
+        PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
+        assumeTrue(trace.exists());
+
+        try {
+            String file = trace.getPath();
+            // Test Ethernet II stream
+            PacketStreamBuilder builder = new PacketStreamBuilder(Protocol.ETHERNET_II);
+            builder.parsePcapFile(file);
+            assertEquals(Protocol.ETHERNET_II, builder.getProtocol());
+            // Should do one loop only, so hardcoded values are okay.
+            for (PacketStream stream : builder.getStreams()) {
+                assertTrue(stream.toString().contains("Stream eth.0, Number of Packets: 43"));
+                for (int i = 0; i < stream.size(); i++) {
+                    Long id = stream.get(i).getIndex();
+                    String path = stream.get(i).getPath();
+                    assertTrue(id >= 0 && id < 43);
+                    assertEquals(file, path);
+                }
+            }
+
+            // Test TCP streams and other constructor
+            builder = new PacketStreamBuilder(Protocol.TCP);
+            builder.parsePcapFile(file);
+            assertEquals(Protocol.TCP, builder.getProtocol());
+
+            PacketStream stream = builder.getStream(0);
+            if (stream == null) {
+                fail("StreamBuildingTest has failed!");
+                return;
+            }
+            assertEquals(Protocol.TCP, stream.getProtocol());
+            assertEquals(0, stream.getID());
+            assertEquals("tcp.0", stream.getUniqueID());
+            assertEquals(34, stream.size());
+            for (int i = 0; i < stream.size(); i++) {
+                Long id = stream.get(i).getIndex();
+                String path = stream.get(i).getPath();
+                assertTrue(TCP_INDEX_SET_STREAM_34_PACKETS.contains(id));
+                assertEquals(file, path);
+            }
+
+            stream = builder.getStream(1);
+            if (stream == null) {
+                fail("StreamBuildingTest has failed!");
+                return;
+            }
+            assertEquals(Protocol.TCP, stream.getProtocol());
+            assertEquals(1, stream.getID());
+            assertEquals("tcp.1", stream.getUniqueID());
+            assertEquals(7, stream.size());
+            for (int i = 0; i < stream.size(); i++) {
+                Long id = stream.get(i).getIndex();
+                String path = stream.get(i).getPath();
+                assertTrue(TCP_INDEX_SET_STREAM_7_PACKETS.contains(id));
+                assertEquals(file, path);
+            }
+
+            builder.clear();
+            assertEquals(0, builder.getNbStreams());
+        } catch (IOException | BadPcapFileException e) {
+            fail("StreamBuildingTest has failed!");
+        }
+
+    }
+}
diff --git a/pom.xml b/pom.xml
index e85a03cfea0062ca4fc2c2f280a23aa14346f437..ff5d613ae2418aeb99815ab6029941e993258f74 100644 (file)
--- a/pom.xml
+++ b/pom.xml
     <module>org.eclipse.linuxtools.ctf.parser.tests</module>
 
     <module>org.eclipse.linuxtools.pcap.core</module>
+    <module>org.eclipse.linuxtools.pcap.core.tests</module>
 
     <module>org.eclipse.linuxtools.statesystem.core</module>
     <module>org.eclipse.linuxtools.statesystem.core.tests</module>
This page took 0.058677 seconds and 5 git commands to generate.