1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.pcap
.core
.tests
.stream
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.fail
;
17 import static org
.junit
.Assume
.assumeTrue
;
19 import java
.io
.IOException
;
21 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.PcapProtocol
;
22 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.stream
.PacketStream
;
23 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.stream
.PacketStreamBuilder
;
24 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.BadPcapFileException
;
25 import org
.eclipse
.linuxtools
.pcap
.core
.tests
.shared
.PcapTestTrace
;
26 import org
.junit
.Test
;
29 * JUnit Class that tests whether packet streams are built correctly.
31 * @author Vincent Perot
33 public class StreamBuildTest
{
35 private static final double DELTA
= 0.001;
38 * Test that verify that stream building is done correctly.
41 public void StreamBuildingTest() {
42 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
43 assumeTrue(trace
.exists());
46 // Test Ethernet II stream
47 PacketStreamBuilder builder
= new PacketStreamBuilder(PcapProtocol
.ETHERNET_II
);
48 builder
.parsePcapFile(trace
.getPath());
49 assertEquals(PcapProtocol
.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
);
66 // Test TCP streams and other constructor
67 builder
= new PacketStreamBuilder(PcapProtocol
.TCP
);
68 builder
.parsePcapFile(trace
.getPath());
69 assertEquals(PcapProtocol
.TCP
, builder
.getProtocol());
71 PacketStream stream
= builder
.getStream(0);
73 fail("StreamBuildingTest has failed!");
76 assertEquals(PcapProtocol
.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
);
91 stream
= builder
.getStream(1);
93 fail("StreamBuildingTest has failed!");
96 assertEquals(PcapProtocol
.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
);
112 assertEquals(0, builder
.getNbStreams());
113 } catch (IOException
| BadPcapFileException e
) {
114 fail("StreamBuildingTest has failed!");