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