btf: Move the plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.pcap.core.tests / src / org / eclipse / tracecompass / pcap / core / tests / file / PcapFileReadTest.java
CommitLineData
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
71f2817f 13package org.eclipse.tracecompass.pcap.core.tests.file;
a1d21447
VP
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNull;
17import static org.junit.Assert.fail;
18import static org.junit.Assume.assumeTrue;
19
20import java.io.IOException;
21
71f2817f
AM
22import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException;
23import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
24import org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket;
25import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException;
26import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
27import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace;
a1d21447
VP
28import org.junit.Test;
29
30/**
31 * JUnit Class that tests if packets are read without error.
32 *
33 * @author Vincent Perot
34 */
35public 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());
c88feda9 65 assertEquals(PcapProtocol.PCAP, packet.getProtocol());
a1d21447
VP
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
c88feda9 73 if (!packet.hasProtocol(PcapProtocol.ETHERNET_II)) {
a1d21447
VP
74 fail("Packet doesn't have ethernet!");
75 }
76 // Verify IPv4 Packet
c88feda9 77 if (!packet.hasProtocol(PcapProtocol.IPV4)) {
a1d21447
VP
78 fail("Packet doesn't have IPv4!");
79 }
80 // Verify UDP Packet
c88feda9 81 if (!packet.hasProtocol(PcapProtocol.UDP)) {
a1d21447
VP
82 fail("Packet doesn't have UDP!");
83 }
84 // Verify Unknown Packet
c88feda9 85 if (!packet.hasProtocol(PcapProtocol.UNKNOWN)) {
a1d21447
VP
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());
c88feda9 99 assertEquals(PcapProtocol.PCAP, packet.getProtocol());
a1d21447
VP
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
c88feda9 107 if (!packet.hasProtocol(PcapProtocol.ETHERNET_II)) {
a1d21447
VP
108 fail("Packet doesn't have ethernet!");
109 }
110 // Verify IPv4 Packet
c88feda9 111 if (!packet.hasProtocol(PcapProtocol.IPV4)) {
a1d21447
VP
112 fail("Packet doesn't have IPv4!");
113 }
114 // Verify TCP Packet
c88feda9 115 if (!packet.hasProtocol(PcapProtocol.TCP)) {
a1d21447
VP
116 fail("Packet doesn't have TCP!");
117 }
118 // Verify Unknown Packet
c88feda9 119 if (!packet.hasProtocol(PcapProtocol.UNKNOWN)) {
a1d21447
VP
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}
This page took 0.046404 seconds and 5 git commands to generate.