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
.file
;
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
;
20 import java
.io
.IOException
;
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
;
31 * JUnit Class that tests if packets are read without error.
33 * @author Vincent Perot
35 public class PcapFileReadTest
{
38 * Test that verify that packets are well read and that no error happens in
41 * @throws BadPcapFileException
42 * Thrown when the file is erroneous. Fails the test.
44 * Thrown when an IO error occurs. Fails the test.
45 * @throws BadPacketException
46 * Thrown when a packet is erroneous. Fails the test.
49 public void FileReadTest() throws IOException
, BadPcapFileException
, BadPacketException
{
51 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_UDP
;
52 assumeTrue(trace
.exists());
54 String path
= trace
.getPath();
55 try (PcapFile file
= new PcapFile(path
);) {
57 PcapPacket packet
= file
.parseNextPacket();
59 fail("FileReadTest() failed!");
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!");
78 if (!packet
.hasProtocol(Protocol
.IPV4
)) {
79 fail("Packet doesn't have IPv4!");
82 if (!packet
.hasProtocol(Protocol
.UDP
)) {
83 fail("Packet doesn't have UDP!");
85 // Verify Unknown Packet
86 if (!packet
.hasProtocol(Protocol
.UNKNOWN
)) {
87 fail("Packet doesn't have payload!");
90 // Parse a "random" packet
92 packet
= file
.parseNextPacket();
94 fail("FileReadTest() failed!");
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!");
111 // Verify IPv4 Packet
112 if (!packet
.hasProtocol(Protocol
.IPV4
)) {
113 fail("Packet doesn't have IPv4!");
116 if (!packet
.hasProtocol(Protocol
.TCP
)) {
117 fail("Packet doesn't have TCP!");
119 // Verify Unknown Packet
120 if (!packet
.hasProtocol(Protocol
.UNKNOWN
)) {
121 fail("Packet doesn't have payload!");
125 file
.skipNextPacket();
126 assertEquals(60, file
.getCurrentRank());
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();
This page took 0.033704 seconds and 5 git commands to generate.