aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Perot2014-06-27 18:25:09 (EDT)
committerMatthew Khouzam2014-07-24 07:04:49 (EDT)
commit2fc169e1f0e9b6352d36bbaa60da8aa684b98758 (patch)
tree8cebeb1b41381479c3907c44052c31798b66c885
parent97ebea5571c62c189c6f666cf427749c56c95f3a (diff)
downloadorg.eclipse.linuxtools-2fc169e1f0e9b6352d36bbaa60da8aa684b98758.zip
org.eclipse.linuxtools-2fc169e1f0e9b6352d36bbaa60da8aa684b98758.tar.gz
org.eclipse.linuxtools-2fc169e1f0e9b6352d36bbaa60da8aa684b98758.tar.bz2
pcap: Add unit tests to pcap.corerefs/changes/46/29146/16
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>
-rw-r--r--lttng/org.eclipse.linuxtools.lttng.alltests/META-INF/MANIFEST.MF3
-rw-r--r--lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllPerfTests.java1
-rw-r--r--lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllTests.java1
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/.classpath2
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/META-INF/MANIFEST.MF21
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/about.html12
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/build.properties4
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/AllPerfTests.java29
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapReadBenchmark.java83
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapSeekBenchmark.java110
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/pom.xml77
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/.gitignore5
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/get-traces.xml56
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/shared/org/eclipse/linuxtools/pcap/core/tests/shared/PcapTestTrace.java90
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllPcapCoreTests.java36
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/AllTests.java30
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileEndiannessTest.java65
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenFailTest.java89
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenTest.java96
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileReadTest.java137
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/AllTests.java28
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/BadPacketTest.java90
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/PacketTest.java125
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/AllTests.java (renamed from lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllTests.java)8
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java87
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/EthernetIIPacketTest.java146
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/IPv4PacketTest.java211
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/PcapPacketTest.java228
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/TCPPacketTest.java214
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/UDPPacketTest.java152
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java121
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/AllTests.java27
-rw-r--r--lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/StreamBuildTest.java131
-rw-r--r--lttng/pom.xml1
41 files changed, 2686 insertions, 19 deletions
diff --git a/lttng/org.eclipse.linuxtools.lttng.alltests/META-INF/MANIFEST.MF b/lttng/org.eclipse.linuxtools.lttng.alltests/META-INF/MANIFEST.MF
index aba5cc9..c389628 100644
--- a/lttng/org.eclipse.linuxtools.lttng.alltests/META-INF/MANIFEST.MF
+++ b/lttng/org.eclipse.linuxtools.lttng.alltests/META-INF/MANIFEST.MF
@@ -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
diff --git a/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllPerfTests.java b/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllPerfTests.java
index 3aea99a..2d9b4bb 100644
--- a/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllPerfTests.java
+++ b/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllPerfTests.java
@@ -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 {
diff --git a/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllTests.java b/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllTests.java
index 95bd182..894dd9f 100644
--- a/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllTests.java
+++ b/lttng/org.eclipse.linuxtools.lttng.alltests/src/org/eclipse/linuxtools/lttng/alltests/RunAllTests.java
@@ -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,
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/.classpath b/lttng/org.eclipse.linuxtools.pcap.core.tests/.classpath
index 098194c..8fbc9a2 100644
--- a/lttng/org.eclipse.linuxtools.pcap.core.tests/.classpath
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/.classpath
@@ -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>
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/META-INF/MANIFEST.MF b/lttng/org.eclipse.linuxtools.pcap.core.tests/META-INF/MANIFEST.MF
index b8e5f94..8f3403d 100644
--- a/lttng/org.eclipse.linuxtools.pcap.core.tests/META-INF/MANIFEST.MF
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/META-INF/MANIFEST.MF
@@ -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
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/about.html b/lttng/org.eclipse.linuxtools.pcap.core.tests/about.html
index c258ef5..28737f6 100644
--- a/lttng/org.eclipse.linuxtools.pcap.core.tests/about.html
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/about.html
@@ -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>
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/build.properties b/lttng/org.eclipse.linuxtools.pcap.core.tests/build.properties
index 2ccaf45..7de632e 100644
--- a/lttng/org.eclipse.linuxtools.pcap.core.tests/build.properties
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/build.properties
@@ -10,7 +10,9 @@
# Ericsson - Initial API and implementation
###############################################################################
-source.. = src/
+source.. = src/,\
+ perf/,\
+ shared/
output.. = bin/
bin.includes = META-INF/,\
.,\
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/AllPerfTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/AllPerfTests.java
new file mode 100644
index 0000000..3482713
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/AllPerfTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/AllTests.java
new file mode 100644
index 0000000..4eb62a4
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapReadBenchmark.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapReadBenchmark.java
new file mode 100644
index 0000000..30952ef
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapReadBenchmark.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapSeekBenchmark.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapSeekBenchmark.java
new file mode 100644
index 0000000..c242a91
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/perf/org/eclipse/linuxtools/pcap/core/tests/perf/trace/PcapSeekBenchmark.java
@@ -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();
+ }
+}
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/pom.xml b/lttng/org.eclipse.linuxtools.pcap.core.tests/pom.xml
index 3a8e772..530f192 100644
--- a/lttng/org.eclipse.linuxtools.pcap.core.tests/pom.xml
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/pom.xml
@@ -16,16 +16,51 @@
<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>
@@ -33,12 +68,46 @@
<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/lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/.gitignore b/lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/.gitignore
new file mode 100644
index 0000000..a8ddf0a
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/.gitignore
@@ -0,0 +1,5 @@
+*.zip
+*.pcap
+*.cap
+*.tar*
+/kernel
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/get-traces.xml b/lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/get-traces.xml
new file mode 100644
index 0000000..c764df0
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/rsc/get-traces.xml
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/shared/org/eclipse/linuxtools/pcap/core/tests/shared/PcapTestTrace.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/shared/org/eclipse/linuxtools/pcap/core/tests/shared/PcapTestTrace.java
new file mode 100644
index 0000000..ac79b34
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/shared/org/eclipse/linuxtools/pcap/core/tests/shared/PcapTestTrace.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllPcapCoreTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllPcapCoreTests.java
new file mode 100644
index 0000000..86e1cb8
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllPcapCoreTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/AllTests.java
new file mode 100644
index 0000000..5f5b9a8
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileEndiannessTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileEndiannessTest.java
new file mode 100644
index 0000000..f409234
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileEndiannessTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenFailTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenFailTest.java
new file mode 100644
index 0000000..7bd1a60
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenFailTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenTest.java
new file mode 100644
index 0000000..7f6b0f2
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileOpenTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileReadTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileReadTest.java
new file mode 100644
index 0000000..144185b
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/file/PcapFileReadTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/AllTests.java
new file mode 100644
index 0000000..d53f197
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/BadPacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/BadPacketTest.java
new file mode 100644
index 0000000..71a8ce7
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/BadPacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/PacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/PacketTest.java
new file mode 100644
index 0000000..4dd7e6d
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/packet/PacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/AllTests.java
index 59db68f..57a5457 100644
--- a/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/AllTests.java
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/AllTests.java
@@ -7,20 +7,20 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
- * Alexandre Montplaisir - Initial API and implementation
+ * Vincent Perot - Initial API and implementation
*******************************************************************************/
-package org.eclipse.linuxtools.pcap.core.tests;
+package org.eclipse.linuxtools.pcap.core.tests.protocol;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
- * Master test suite
+ * Protocol test suite
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
- //example: StateSystemPushPopTest.class,
+ ProtocolTest.class
})
public class AllTests {
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java
new file mode 100644
index 0000000..38b9922
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/AllTests.java
new file mode 100644
index 0000000..b640d9c
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/EthernetIIPacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/EthernetIIPacketTest.java
new file mode 100644
index 0000000..7414c24
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ethernet2/EthernetIIPacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/AllTests.java
new file mode 100644
index 0000000..e431897
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/IPv4PacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/IPv4PacketTest.java
new file mode 100644
index 0000000..2877b25
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ipv4/IPv4PacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/AllTests.java
new file mode 100644
index 0000000..c5d70f7
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/PcapPacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/PcapPacketTest.java
new file mode 100644
index 0000000..3492bc9
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/pcap/PcapPacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/AllTests.java
new file mode 100644
index 0000000..201e83d
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/TCPPacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/TCPPacketTest.java
new file mode 100644
index 0000000..7023a29
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/tcp/TCPPacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/AllTests.java
new file mode 100644
index 0000000..13c541e
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/UDPPacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/UDPPacketTest.java
new file mode 100644
index 0000000..2a4965c
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/udp/UDPPacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/AllTests.java
new file mode 100644
index 0000000..f33d53b
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java
new file mode 100644
index 0000000..1e1b627
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/unknown/UnknownPacketTest.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/AllTests.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/AllTests.java
new file mode 100644
index 0000000..df90513
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/AllTests.java
@@ -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/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/StreamBuildTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/StreamBuildTest.java
new file mode 100644
index 0000000..90c8e6b
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/stream/StreamBuildTest.java
@@ -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/lttng/pom.xml b/lttng/pom.xml
index e85a03c..ff5d613 100644
--- a/lttng/pom.xml
+++ b/lttng/pom.xml
@@ -101,6 +101,7 @@
<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>