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 try (PcapFile file
= new PcapFile(trace
.getPath());) {
56 PcapPacket packet
= file
.parseNextPacket();
58 fail("FileReadTest() failed!");
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!");
77 if (!packet
.hasProtocol(Protocol
.IPV4
)) {
78 fail("Packet doesn't have IPv4!");
81 if (!packet
.hasProtocol(Protocol
.UDP
)) {
82 fail("Packet doesn't have UDP!");
84 // Verify Unknown Packet
85 if (!packet
.hasProtocol(Protocol
.UNKNOWN
)) {
86 fail("Packet doesn't have payload!");
89 // Parse a "random" packet
91 packet
= file
.parseNextPacket();
93 fail("FileReadTest() failed!");
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!");
110 // Verify IPv4 Packet
111 if (!packet
.hasProtocol(Protocol
.IPV4
)) {
112 fail("Packet doesn't have IPv4!");
115 if (!packet
.hasProtocol(Protocol
.TCP
)) {
116 fail("Packet doesn't have TCP!");
118 // Verify Unknown Packet
119 if (!packet
.hasProtocol(Protocol
.UNKNOWN
)) {
120 fail("Packet doesn't have payload!");
124 file
.skipNextPacket();
125 assertEquals(60, file
.getCurrentRank());
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();
This page took 0.032821 seconds and 5 git commands to generate.