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; | |
16 | import static org.junit.Assert.assertTrue; | |
17 | import static org.junit.Assert.fail; | |
18 | import static org.junit.Assume.assumeTrue; | |
19 | ||
20 | import java.io.IOException; | |
21 | import java.util.Set; | |
22 | import java.util.TreeSet; | |
23 | ||
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; | |
30 | ||
31 | import com.google.common.collect.ImmutableSet; | |
32 | ||
33 | /** | |
34 | * JUnit Class that tests whether packet streams are built correctly. | |
35 | * | |
36 | * @author Vincent Perot | |
37 | */ | |
38 | public class StreamBuildTest { | |
39 | ||
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. | |
43 | 17L, | |
44 | 23L, | |
45 | 25L, | |
46 | 26L, | |
47 | 27L, | |
48 | 35L, | |
49 | 36L | |
50 | ); | |
51 | ||
52 | private static final Set<Long> TCP_INDEX_SET_STREAM_34_PACKETS = new TreeSet<>(); | |
53 | static { | |
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); | |
58 | } | |
59 | TCP_INDEX_SET_STREAM_34_PACKETS.removeAll(TCP_INDEX_SET_STREAM_7_PACKETS); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Test that verify that stream building is done correctly. | |
64 | */ | |
65 | @Test | |
66 | public void StreamBuildingTest() { | |
67 | PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP; | |
68 | assumeTrue(trace.exists()); | |
69 | ||
70 | try { | |
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); | |
84 | } | |
85 | } | |
86 | ||
87 | // Test TCP streams and other constructor | |
88 | builder = new PacketStreamBuilder(Protocol.TCP); | |
89 | builder.parsePcapFile(file); | |
90 | assertEquals(Protocol.TCP, builder.getProtocol()); | |
91 | ||
92 | PacketStream stream = builder.getStream(0); | |
93 | if (stream == null) { | |
94 | fail("StreamBuildingTest has failed!"); | |
95 | return; | |
96 | } | |
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); | |
106 | } | |
107 | ||
108 | stream = builder.getStream(1); | |
109 | if (stream == null) { | |
110 | fail("StreamBuildingTest has failed!"); | |
111 | return; | |
112 | } | |
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); | |
122 | } | |
123 | ||
124 | builder.clear(); | |
125 | assertEquals(0, builder.getNbStreams()); | |
126 | } catch (IOException | BadPcapFileException e) { | |
127 | fail("StreamBuildingTest has failed!"); | |
128 | } | |
129 | ||
130 | } | |
131 | } |