use of org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket in project tracecompass by tracecompass.
the class PcapFileEndiannessTest method EndiannessTest.
/**
* Test that verify that two files with different endianness contain the
* same packets.
*
* @throws BadPcapFileException
* Thrown when the file is erroneous. Fails the test.
* @throws IOException
* Thrown when an IO error occurs. Fails the test.
* @throws BadPacketException
* Thrown when a packet is erroneous. Fails the test.
*/
@Test
public void EndiannessTest() throws IOException, BadPcapFileException, BadPacketException {
PcapTestTrace trace = PcapTestTrace.SHORT_LITTLE_ENDIAN;
assumeTrue(trace.exists());
// Get a right pcap/pcapNg trace
PcapFile littleEndian = trace.getTrace();
trace = PcapTestTrace.SHORT_BIG_ENDIAN;
assumeTrue(trace.exists());
// Get a right pcap/pcapNg trace
PcapFile bigEndian = trace.getTrace();
assertEquals(ByteOrder.BIG_ENDIAN, bigEndian.getByteOrder());
assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.getByteOrder());
while (true) {
PcapPacket littleEndianPacket = littleEndian.parseNextPacket();
PcapPacket bigEndianPacket = bigEndian.parseNextPacket();
assertEquals(littleEndianPacket, bigEndianPacket);
if (littleEndianPacket == null) {
break;
}
}
}
use of org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket in project tracecompass by tracecompass.
the class PcapFileReadTest method FileReadTest.
/**
* Test that verify that packets are well read for a pcap file or a pcapNg file and that no error happens in
* file index.
*
* @throws BadPcapFileException
* Thrown when the file is erroneous. Fails the test.
* @throws IOException
* Thrown when an IO error occurs. Fails the test.
* @throws BadPacketException
* Thrown when a packet is erroneous. Fails the test.
*/
@Test
public void FileReadTest() throws IOException, BadPcapFileException, BadPacketException {
PcapTestTrace trace = PcapTestTrace.MOSTLY_UDP;
assumeTrue(trace.exists());
// Get a right pcap/pcapNg trace
try (PcapFile file = trace.getTrace()) {
PcapPacket packet = file.parseNextPacket();
if (packet == null) {
fail("FileReadTest() failed!");
return;
}
assertEquals(1, file.getCurrentRank());
// Verify Pcap packet.
assertEquals(file, packet.getPcapFile());
assertEquals(PcapProtocol.PCAP, packet.getProtocol());
assertEquals(0, packet.getIndex());
assertEquals(1120469540839312L, packet.getTimestamp());
assertEquals(92, packet.getOriginalLength());
assertEquals(92, packet.getIncludedLength());
assertEquals(false, packet.isTruncated());
assertEquals(true, packet.validate());
// Verify Ethernet Packet
if (!packet.hasProtocol(PcapProtocol.ETHERNET_II)) {
fail("Packet doesn't have ethernet!");
}
// Verify IPv4 Packet
if (!packet.hasProtocol(PcapProtocol.IPV4)) {
fail("Packet doesn't have IPv4!");
}
// Verify UDP Packet
if (!packet.hasProtocol(PcapProtocol.UDP)) {
fail("Packet doesn't have UDP!");
}
// Verify Unknown Packet
if (!packet.hasProtocol(PcapProtocol.UNKNOWN)) {
fail("Packet doesn't have payload!");
}
// Parse a "random" packet
file.seekPacket(58);
packet = file.parseNextPacket();
if (packet == null) {
fail("FileReadTest() failed!");
return;
}
// Verify Pcap packet.
assertEquals(file, packet.getPcapFile());
assertEquals(PcapProtocol.PCAP, packet.getProtocol());
assertEquals(58, packet.getIndex());
assertEquals(1120469635045415L, packet.getTimestamp());
assertEquals(113, packet.getOriginalLength());
assertEquals(113, packet.getIncludedLength());
assertEquals(false, packet.isTruncated());
assertEquals(true, packet.validate());
// Verify Ethernet Packet
if (!packet.hasProtocol(PcapProtocol.ETHERNET_II)) {
fail("Packet doesn't have ethernet!");
}
// Verify IPv4 Packet
if (!packet.hasProtocol(PcapProtocol.IPV4)) {
fail("Packet doesn't have IPv4!");
}
// Verify TCP Packet
if (!packet.hasProtocol(PcapProtocol.TCP)) {
fail("Packet doesn't have TCP!");
}
// Verify Unknown Packet
if (!packet.hasProtocol(PcapProtocol.UNKNOWN)) {
fail("Packet doesn't have payload!");
}
// Skip packet
file.skipNextPacket();
assertEquals(60, file.getCurrentRank());
// Parse outside of file.
file.seekPacket(99999999);
assertEquals(647, file.getCurrentRank());
// Should be a no-op
file.skipNextPacket();
assertEquals(647, file.getCurrentRank());
packet = file.parseNextPacket();
assertNull(packet);
}
}
use of org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket in project tracecompass by tracecompass.
the class PcapPacketTest method CompletePcapPacketTest.
/**
* Test that verify the correctness of the PcapPacket's methods.
* @throws BadPcapFileException
* Thrown when the file is erroneous. Fails the test.
* @throws IOException
* Thrown when an IO error occurs. Fails the test.
* @throws BadPacketException
* Thrown when a packet is erroneous. Fails the test.
*/
@Test
public void CompletePcapPacketTest() throws IOException, BadPcapFileException, BadPacketException {
PcapTestTrace trace = PcapTestTrace.MOSTLY_UDP;
assumeTrue(trace.exists());
try (PcapFile file = trace.getTrace()) {
file.seekPacket(36);
PcapPacket packet = file.parseNextPacket();
if (packet == null) {
fail("CompletePcapPacketTest has failed!");
return;
}
// Protocol Testing
assertEquals(PcapProtocol.PCAP, packet.getProtocol());
assertTrue(packet.hasProtocol(PcapProtocol.PCAP));
assertTrue(packet.hasProtocol(PcapProtocol.UNKNOWN));
assertFalse(packet.hasProtocol(PcapProtocol.TCP));
// Abstract methods Testing
assertTrue(packet.validate());
PcapOldPacket expected = new PcapOldPacket((PcapOldFile) file, fHeader, fPayload, 36L);
assertEquals(expected.hashCode(), packet.hashCode());
assertEquals(expected, packet);
assertEquals(EXPECTED_FIELDS, packet.getFields());
assertEquals(EXPECTED_TOSTRING, packet.toString());
assertEquals("Frame 36: 75 bytes on wire, 75 bytes captured", packet.getLocalSummaryString());
assertEquals("Source Port: 2719, Destination Port: 53", packet.getGlobalSummaryString());
assertEquals(new PcapEndpoint(packet, true), packet.getSourceEndpoint());
assertEquals(new PcapEndpoint(packet, false), packet.getDestinationEndpoint());
ByteBuffer payload = packet.getPayload();
if (payload == null) {
fail("CompletePcapPacketTest has failed!");
return;
}
// Packet-specific methods Testing
assertEquals(36, packet.getIndex());
assertEquals(75, packet.getOriginalLength());
assertEquals(75, packet.getIncludedLength());
assertEquals(1120469632829277L, packet.getTimestamp());
assertFalse(packet.isTruncated());
}
}
use of org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket in project tracecompass by tracecompass.
the class PacketStream method add.
/**
* Add a packet to the stream.
*
* @param packet
* The packet that must be added.
*/
synchronized void add(PcapPacket packet) {
Packet newPacket = packet.getPacket(fProtocol);
if (newPacket == null) {
return;
}
// Update packet and byte number
if (fEndpointPair.getFirstEndpoint().equals(newPacket.getSourceEndpoint()) && fEndpointPair.getSecondEndpoint().equals(newPacket.getDestinationEndpoint())) {
fNbPacketsAtoB++;
fNbBytesAtoB += packet.getOriginalLength();
} else if (fEndpointPair.getFirstEndpoint().equals(newPacket.getDestinationEndpoint()) && fEndpointPair.getSecondEndpoint().equals(newPacket.getSourceEndpoint())) {
fNbPacketsBtoA++;
fNbBytesBtoA += packet.getOriginalLength();
} else {
throw new IllegalStateException();
}
// Update start and stop time
// Stream timestamp is ALWAYS in nanoseconds.
long timestamp;
switch(packet.getTimestampScale()) {
case MICROSECOND:
timestamp = packet.getTimestamp() * 1000;
break;
case NANOSECOND:
timestamp = packet.getTimestamp();
break;
default:
// $NON-NLS-1$
throw new IllegalArgumentException("The timestamp precision is not valid!");
}
fStartTime = Math.min(fStartTime, timestamp);
fEndTime = Math.max(fEndTime, timestamp);
}
use of org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket in project tracecompass by tracecompass.
the class PacketStreamBuilder method parsePcapFile.
/**
* Method that parse an entire file and build the streams contained in the
* file.
*
* @param filePath
* The file path.
* @throws IOException
* When an IO error occurs.
* @throws BadPcapFileException
* When the PcapFile is not valid.
*/
public synchronized void parsePcapFile(Path filePath) throws IOException, BadPcapFileException {
try (PcapFile pcapFile = PcapHelper.getPcapFile(filePath)) {
while (true) {
try {
PcapPacket packet = pcapFile.parseNextPacket();
if (packet == null) {
// end-of-file
return;
}
addPacketToStream(packet);
} catch (BadPacketException e) {
// Ignore packet. Do nothing.
}
}
}
}
Aggregations