Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.pcap.core.tests.stream; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
a1d21447 VP |
16 | import static org.junit.Assert.fail; |
17 | import static org.junit.Assume.assumeTrue; | |
18 | ||
19 | import java.io.IOException; | |
a1d21447 | 20 | |
c88feda9 | 21 | import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol; |
93d1d135 AM |
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; | |
a1d21447 | 25 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; |
a1d21447 VP |
26 | import org.junit.Test; |
27 | ||
a1d21447 VP |
28 | /** |
29 | * JUnit Class that tests whether packet streams are built correctly. | |
30 | * | |
31 | * @author Vincent Perot | |
32 | */ | |
33 | public 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 { | |
a1d21447 | 46 | // Test Ethernet II stream |
c88feda9 | 47 | PacketStreamBuilder builder = new PacketStreamBuilder(PcapProtocol.ETHERNET_II); |
333a2acb | 48 | builder.parsePcapFile(trace.getPath()); |
c88feda9 | 49 | assertEquals(PcapProtocol.ETHERNET_II, builder.getProtocol()); |
a1d21447 VP |
50 | // Should do one loop only, so hardcoded values are okay. |
51 | for (PacketStream stream : builder.getStreams()) { | |
f2fb631e VP |
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); | |
a1d21447 VP |
64 | } |
65 | ||
66 | // Test TCP streams and other constructor | |
c88feda9 | 67 | builder = new PacketStreamBuilder(PcapProtocol.TCP); |
333a2acb | 68 | builder.parsePcapFile(trace.getPath()); |
c88feda9 | 69 | assertEquals(PcapProtocol.TCP, builder.getProtocol()); |
a1d21447 VP |
70 | |
71 | PacketStream stream = builder.getStream(0); | |
72 | if (stream == null) { | |
73 | fail("StreamBuildingTest has failed!"); | |
74 | return; | |
75 | } | |
c88feda9 | 76 | assertEquals(PcapProtocol.TCP, stream.getProtocol()); |
a1d21447 VP |
77 | assertEquals(0, stream.getID()); |
78 | assertEquals("tcp.0", stream.getUniqueID()); | |
f2fb631e VP |
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); | |
a1d21447 VP |
90 | |
91 | stream = builder.getStream(1); | |
92 | if (stream == null) { | |
93 | fail("StreamBuildingTest has failed!"); | |
94 | return; | |
95 | } | |
c88feda9 | 96 | assertEquals(PcapProtocol.TCP, stream.getProtocol()); |
a1d21447 VP |
97 | assertEquals(1, stream.getID()); |
98 | assertEquals("tcp.1", stream.getUniqueID()); | |
f2fb631e VP |
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); | |
a1d21447 VP |
110 | |
111 | builder.clear(); | |
112 | assertEquals(0, builder.getNbStreams()); | |
113 | } catch (IOException | BadPcapFileException e) { | |
114 | fail("StreamBuildingTest has failed!"); | |
115 | } | |
116 | ||
117 | } | |
118 | } |