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
.assertTrue
;
17 import static org
.junit
.Assert
.fail
;
18 import static org
.junit
.Assume
.assumeTrue
;
20 import java
.io
.IOException
;
22 import java
.util
.TreeSet
;
24 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.Protocol
;
25 import org
.eclipse
.linuxtools
.pcap
.core
.stream
.PacketStream
;
26 import org
.eclipse
.linuxtools
.pcap
.core
.stream
.PacketStreamBuilder
;
27 import org
.eclipse
.linuxtools
.pcap
.core
.tests
.shared
.PcapTestTrace
;
28 import org
.eclipse
.linuxtools
.pcap
.core
.trace
.BadPcapFileException
;
29 import org
.junit
.Test
;
31 import com
.google
.common
.collect
.ImmutableSet
;
34 * JUnit Class that tests whether packet streams are built correctly.
36 * @author Vincent Perot
38 public class StreamBuildTest
{
40 // Values taken from wireshark
41 private static final Set
<Long
> TCP_INDEX_SET_STREAM_7_PACKETS
= ImmutableSet
.of(
42 // This stream contains 7 packets.
52 private static final Set
<Long
> TCP_INDEX_SET_STREAM_34_PACKETS
= new TreeSet
<>();
54 // This stream contains many packet (34). Some packets doesn't
55 // belong to it like the 17 or the 23.
56 for (Long i
= new Long(0); i
< 43; i
++) {
57 TCP_INDEX_SET_STREAM_34_PACKETS
.add(i
);
59 TCP_INDEX_SET_STREAM_34_PACKETS
.removeAll(TCP_INDEX_SET_STREAM_7_PACKETS
);
63 * Test that verify that stream building is done correctly.
66 public void StreamBuildingTest() {
67 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
68 assumeTrue(trace
.exists());
71 String file
= trace
.getPath();
72 // Test Ethernet II stream
73 PacketStreamBuilder builder
= new PacketStreamBuilder(Protocol
.ETHERNET_II
);
74 builder
.parsePcapFile(file
);
75 assertEquals(Protocol
.ETHERNET_II
, builder
.getProtocol());
76 // Should do one loop only, so hardcoded values are okay.
77 for (PacketStream stream
: builder
.getStreams()) {
78 assertTrue(stream
.toString().contains("Stream eth.0, Number of Packets: 43"));
79 for (int i
= 0; i
< stream
.size(); i
++) {
80 Long id
= stream
.get(i
).getIndex();
81 String path
= stream
.get(i
).getPath();
82 assertTrue(id
>= 0 && id
< 43);
83 assertEquals(file
, path
);
87 // Test TCP streams and other constructor
88 builder
= new PacketStreamBuilder(Protocol
.TCP
);
89 builder
.parsePcapFile(file
);
90 assertEquals(Protocol
.TCP
, builder
.getProtocol());
92 PacketStream stream
= builder
.getStream(0);
94 fail("StreamBuildingTest has failed!");
97 assertEquals(Protocol
.TCP
, stream
.getProtocol());
98 assertEquals(0, stream
.getID());
99 assertEquals("tcp.0", stream
.getUniqueID());
100 assertEquals(34, stream
.size());
101 for (int i
= 0; i
< stream
.size(); i
++) {
102 Long id
= stream
.get(i
).getIndex();
103 String path
= stream
.get(i
).getPath();
104 assertTrue(TCP_INDEX_SET_STREAM_34_PACKETS
.contains(id
));
105 assertEquals(file
, path
);
108 stream
= builder
.getStream(1);
109 if (stream
== null) {
110 fail("StreamBuildingTest has failed!");
113 assertEquals(Protocol
.TCP
, stream
.getProtocol());
114 assertEquals(1, stream
.getID());
115 assertEquals("tcp.1", stream
.getUniqueID());
116 assertEquals(7, stream
.size());
117 for (int i
= 0; i
< stream
.size(); i
++) {
118 Long id
= stream
.get(i
).getIndex();
119 String path
= stream
.get(i
).getPath();
120 assertTrue(TCP_INDEX_SET_STREAM_7_PACKETS
.contains(id
));
121 assertEquals(file
, path
);
125 assertEquals(0, builder
.getNbStreams());
126 } catch (IOException
| BadPcapFileException e
) {
127 fail("StreamBuildingTest has failed!");