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.file; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.assertNull; | |
17 | import static org.junit.Assert.fail; | |
18 | import static org.junit.Assume.assumeTrue; | |
19 | ||
20 | import java.io.IOException; | |
21 | ||
22 | import org.eclipse.linuxtools.pcap.core.packet.BadPacketException; | |
23 | import org.eclipse.linuxtools.pcap.core.protocol.Protocol; | |
24 | import org.eclipse.linuxtools.pcap.core.protocol.pcap.PcapPacket; | |
25 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; | |
26 | import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException; | |
27 | import org.eclipse.linuxtools.pcap.core.trace.PcapFile; | |
28 | import org.junit.Test; | |
29 | ||
30 | /** | |
31 | * JUnit Class that tests if packets are read without error. | |
32 | * | |
33 | * @author Vincent Perot | |
34 | */ | |
35 | public class PcapFileReadTest { | |
36 | ||
37 | /** | |
38 | * Test that verify that packets are well read and that no error happens in | |
39 | * file index. | |
40 | * | |
41 | * @throws BadPcapFileException | |
42 | * Thrown when the file is erroneous. Fails the test. | |
43 | * @throws IOException | |
44 | * Thrown when an IO error occurs. Fails the test. | |
45 | * @throws BadPacketException | |
46 | * Thrown when a packet is erroneous. Fails the test. | |
47 | */ | |
48 | @Test | |
49 | public void FileReadTest() throws IOException, BadPcapFileException, BadPacketException { | |
50 | ||
51 | PcapTestTrace trace = PcapTestTrace.MOSTLY_UDP; | |
52 | assumeTrue(trace.exists()); | |
53 | ||
333a2acb | 54 | try (PcapFile file = new PcapFile(trace.getPath());) { |
a1d21447 VP |
55 | |
56 | PcapPacket packet = file.parseNextPacket(); | |
57 | if (packet == null) { | |
58 | fail("FileReadTest() failed!"); | |
59 | return; | |
60 | } | |
61 | ||
62 | assertEquals(1, file.getCurrentRank()); | |
63 | // Verify Pcap packet. | |
64 | assertEquals(file, packet.getPcapFile()); | |
65 | assertEquals(Protocol.PCAP, packet.getProtocol()); | |
66 | assertEquals(0, packet.getIndex()); | |
67 | assertEquals(1120469540839312L, packet.getTimestamp()); | |
68 | assertEquals(92, packet.getOriginalLength()); | |
69 | assertEquals(92, packet.getIncludedLength()); | |
70 | assertEquals(false, packet.isTruncated()); | |
71 | assertEquals(true, packet.validate()); | |
72 | // Verify Ethernet Packet | |
73 | if (!packet.hasProtocol(Protocol.ETHERNET_II)) { | |
74 | fail("Packet doesn't have ethernet!"); | |
75 | } | |
76 | // Verify IPv4 Packet | |
77 | if (!packet.hasProtocol(Protocol.IPV4)) { | |
78 | fail("Packet doesn't have IPv4!"); | |
79 | } | |
80 | // Verify UDP Packet | |
81 | if (!packet.hasProtocol(Protocol.UDP)) { | |
82 | fail("Packet doesn't have UDP!"); | |
83 | } | |
84 | // Verify Unknown Packet | |
85 | if (!packet.hasProtocol(Protocol.UNKNOWN)) { | |
86 | fail("Packet doesn't have payload!"); | |
87 | } | |
88 | ||
89 | // Parse a "random" packet | |
90 | file.seekPacket(58); | |
91 | packet = file.parseNextPacket(); | |
92 | if (packet == null) { | |
93 | fail("FileReadTest() failed!"); | |
94 | return; | |
95 | } | |
96 | ||
97 | // Verify Pcap packet. | |
98 | assertEquals(file, packet.getPcapFile()); | |
99 | assertEquals(Protocol.PCAP, packet.getProtocol()); | |
100 | assertEquals(58, packet.getIndex()); | |
101 | assertEquals(1120469635045415L, packet.getTimestamp()); | |
102 | assertEquals(113, packet.getOriginalLength()); | |
103 | assertEquals(113, packet.getIncludedLength()); | |
104 | assertEquals(false, packet.isTruncated()); | |
105 | assertEquals(true, packet.validate()); | |
106 | // Verify Ethernet Packet | |
107 | if (!packet.hasProtocol(Protocol.ETHERNET_II)) { | |
108 | fail("Packet doesn't have ethernet!"); | |
109 | } | |
110 | // Verify IPv4 Packet | |
111 | if (!packet.hasProtocol(Protocol.IPV4)) { | |
112 | fail("Packet doesn't have IPv4!"); | |
113 | } | |
114 | // Verify TCP Packet | |
115 | if (!packet.hasProtocol(Protocol.TCP)) { | |
116 | fail("Packet doesn't have TCP!"); | |
117 | } | |
118 | // Verify Unknown Packet | |
119 | if (!packet.hasProtocol(Protocol.UNKNOWN)) { | |
120 | fail("Packet doesn't have payload!"); | |
121 | } | |
122 | ||
123 | // Skip packet | |
124 | file.skipNextPacket(); | |
125 | assertEquals(60, file.getCurrentRank()); | |
126 | ||
127 | // Parse outside of file. | |
128 | file.seekPacket(99999999); | |
129 | assertEquals(file.getTotalNbPackets(), file.getCurrentRank()); | |
130 | file.skipNextPacket(); // Should be a no-op | |
131 | assertEquals(file.getTotalNbPackets(), file.getCurrentRank()); | |
132 | packet = file.parseNextPacket(); | |
133 | assertNull(packet); | |
134 | } | |
135 | } | |
136 | } |