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 | ||
54 | String path = trace.getPath(); | |
55 | try (PcapFile file = new PcapFile(path);) { | |
56 | ||
57 | PcapPacket packet = file.parseNextPacket(); | |
58 | if (packet == null) { | |
59 | fail("FileReadTest() failed!"); | |
60 | return; | |
61 | } | |
62 | ||
63 | assertEquals(1, file.getCurrentRank()); | |
64 | // Verify Pcap packet. | |
65 | assertEquals(file, packet.getPcapFile()); | |
66 | assertEquals(Protocol.PCAP, packet.getProtocol()); | |
67 | assertEquals(0, packet.getIndex()); | |
68 | assertEquals(1120469540839312L, packet.getTimestamp()); | |
69 | assertEquals(92, packet.getOriginalLength()); | |
70 | assertEquals(92, packet.getIncludedLength()); | |
71 | assertEquals(false, packet.isTruncated()); | |
72 | assertEquals(true, packet.validate()); | |
73 | // Verify Ethernet Packet | |
74 | if (!packet.hasProtocol(Protocol.ETHERNET_II)) { | |
75 | fail("Packet doesn't have ethernet!"); | |
76 | } | |
77 | // Verify IPv4 Packet | |
78 | if (!packet.hasProtocol(Protocol.IPV4)) { | |
79 | fail("Packet doesn't have IPv4!"); | |
80 | } | |
81 | // Verify UDP Packet | |
82 | if (!packet.hasProtocol(Protocol.UDP)) { | |
83 | fail("Packet doesn't have UDP!"); | |
84 | } | |
85 | // Verify Unknown Packet | |
86 | if (!packet.hasProtocol(Protocol.UNKNOWN)) { | |
87 | fail("Packet doesn't have payload!"); | |
88 | } | |
89 | ||
90 | // Parse a "random" packet | |
91 | file.seekPacket(58); | |
92 | packet = file.parseNextPacket(); | |
93 | if (packet == null) { | |
94 | fail("FileReadTest() failed!"); | |
95 | return; | |
96 | } | |
97 | ||
98 | // Verify Pcap packet. | |
99 | assertEquals(file, packet.getPcapFile()); | |
100 | assertEquals(Protocol.PCAP, packet.getProtocol()); | |
101 | assertEquals(58, packet.getIndex()); | |
102 | assertEquals(1120469635045415L, packet.getTimestamp()); | |
103 | assertEquals(113, packet.getOriginalLength()); | |
104 | assertEquals(113, packet.getIncludedLength()); | |
105 | assertEquals(false, packet.isTruncated()); | |
106 | assertEquals(true, packet.validate()); | |
107 | // Verify Ethernet Packet | |
108 | if (!packet.hasProtocol(Protocol.ETHERNET_II)) { | |
109 | fail("Packet doesn't have ethernet!"); | |
110 | } | |
111 | // Verify IPv4 Packet | |
112 | if (!packet.hasProtocol(Protocol.IPV4)) { | |
113 | fail("Packet doesn't have IPv4!"); | |
114 | } | |
115 | // Verify TCP Packet | |
116 | if (!packet.hasProtocol(Protocol.TCP)) { | |
117 | fail("Packet doesn't have TCP!"); | |
118 | } | |
119 | // Verify Unknown Packet | |
120 | if (!packet.hasProtocol(Protocol.UNKNOWN)) { | |
121 | fail("Packet doesn't have payload!"); | |
122 | } | |
123 | ||
124 | // Skip packet | |
125 | file.skipNextPacket(); | |
126 | assertEquals(60, file.getCurrentRank()); | |
127 | ||
128 | // Parse outside of file. | |
129 | file.seekPacket(99999999); | |
130 | assertEquals(file.getTotalNbPackets(), file.getCurrentRank()); | |
131 | file.skipNextPacket(); // Should be a no-op | |
132 | assertEquals(file.getTotalNbPackets(), file.getCurrentRank()); | |
133 | packet = file.parseNextPacket(); | |
134 | assertNull(packet); | |
135 | } | |
136 | } | |
137 | } |