pcap: Convert filePath to java.nio.file.Path
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core.tests / src / org / eclipse / linuxtools / pcap / core / tests / stream / StreamBuildTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.pcap.core.tests.stream;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.fail;
17 import static org.junit.Assume.assumeTrue;
18
19 import java.io.IOException;
20
21 import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
22 import org.eclipse.linuxtools.pcap.core.stream.PacketStream;
23 import org.eclipse.linuxtools.pcap.core.stream.PacketStreamBuilder;
24 import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
25 import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
26 import org.junit.Test;
27
28 /**
29 * JUnit Class that tests whether packet streams are built correctly.
30 *
31 * @author Vincent Perot
32 */
33 public class StreamBuildTest {
34
35 private static final double DELTA = 0.001;
36
37 /**
38 * Test that verify that stream building is done correctly.
39 */
40 @Test
41 public void StreamBuildingTest() {
42 PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
43 assumeTrue(trace.exists());
44
45 try {
46 // Test Ethernet II stream
47 PacketStreamBuilder builder = new PacketStreamBuilder(Protocol.ETHERNET_II);
48 builder.parsePcapFile(trace.getPath());
49 assertEquals(Protocol.ETHERNET_II, builder.getProtocol());
50 // Should do one loop only, so hardcoded values are okay.
51 for (PacketStream stream : builder.getStreams()) {
52 assertEquals("Stream eth.0, Number of Packets: 43\n", stream.toString());
53 assertEquals(43, stream.getNbPackets());
54 assertEquals(25091, stream.getNbBytes());
55 assertEquals(20, stream.getNbPacketsAtoB());
56 assertEquals(2323, stream.getNbBytesAtoB());
57 assertEquals(23, stream.getNbPacketsBtoA());
58 assertEquals(22768, stream.getNbBytesBtoA());
59 assertEquals(1084443427311224000L, stream.getStartTime());
60 assertEquals(1084443457704928000L, stream.getStopTime());
61 assertEquals(30.393704, stream.getDuration(), DELTA);
62 assertEquals(76.43030280218561, stream.getBPSAtoB(), DELTA);
63 assertEquals(749.1025114938278, stream.getBPSBtoA(), DELTA);
64 }
65
66 // Test TCP streams and other constructor
67 builder = new PacketStreamBuilder(Protocol.TCP);
68 builder.parsePcapFile(trace.getPath());
69 assertEquals(Protocol.TCP, builder.getProtocol());
70
71 PacketStream stream = builder.getStream(0);
72 if (stream == null) {
73 fail("StreamBuildingTest has failed!");
74 return;
75 }
76 assertEquals(Protocol.TCP, stream.getProtocol());
77 assertEquals(0, stream.getID());
78 assertEquals("tcp.0", stream.getUniqueID());
79 assertEquals(34, stream.getNbPackets());
80 assertEquals(20695, stream.getNbBytes());
81 assertEquals(16, stream.getNbPacketsAtoB());
82 assertEquals(1351, stream.getNbBytesAtoB());
83 assertEquals(18, stream.getNbPacketsBtoA());
84 assertEquals(19344, stream.getNbBytesBtoA());
85 assertEquals(1084443427311224000L, stream.getStartTime());
86 assertEquals(1084443457704928000L, stream.getStopTime());
87 assertEquals(30.393704, stream.getDuration(), DELTA);
88 assertEquals(44.449995301658525, stream.getBPSAtoB(), DELTA);
89 assertEquals(636.4476011216008, stream.getBPSBtoA(), DELTA);
90
91 stream = builder.getStream(1);
92 if (stream == null) {
93 fail("StreamBuildingTest has failed!");
94 return;
95 }
96 assertEquals(Protocol.TCP, stream.getProtocol());
97 assertEquals(1, stream.getID());
98 assertEquals("tcp.1", stream.getUniqueID());
99 assertEquals(7, stream.getNbPackets());
100 assertEquals(4119, stream.getNbBytes());
101 assertEquals(3, stream.getNbPacketsAtoB());
102 assertEquals(883, stream.getNbBytesAtoB());
103 assertEquals(4, stream.getNbPacketsBtoA());
104 assertEquals(3236, stream.getNbBytesBtoA());
105 assertEquals(1084443430295515000L, stream.getStartTime());
106 assertEquals(1084443432088092000L, stream.getStopTime());
107 assertEquals(1.792577, stream.getDuration(), DELTA);
108 assertEquals(492.58692932019096, stream.getBPSAtoB(), DELTA);
109 assertEquals(1805.2223140205413, stream.getBPSBtoA(), DELTA);
110
111 builder.clear();
112 assertEquals(0, builder.getNbStreams());
113 } catch (IOException | BadPcapFileException e) {
114 fail("StreamBuildingTest has failed!");
115 }
116
117 }
118 }
This page took 0.049409 seconds and 5 git commands to generate.