Search in sources :

Example 1 with PcapPacket

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;
        }
    }
}
Also used : PcapPacket(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket) PcapFile(org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile) PcapTestTrace(org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace) Test(org.junit.Test)

Example 2 with PcapPacket

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);
    }
}
Also used : PcapPacket(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket) PcapFile(org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile) PcapTestTrace(org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace) Test(org.junit.Test)

Example 3 with PcapPacket

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());
    }
}
Also used : PcapPacket(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket) PcapFile(org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile) PcapTestTrace(org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace) PcapEndpoint(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapEndpoint) ByteBuffer(java.nio.ByteBuffer) PcapOldPacket(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapOldPacket) Test(org.junit.Test)

Example 4 with PcapPacket

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);
}
Also used : Packet(org.eclipse.tracecompass.internal.pcap.core.packet.Packet) PcapPacket(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket)

Example 5 with PcapPacket

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.
            }
        }
    }
}
Also used : PcapPacket(org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket) PcapFile(org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile) BadPacketException(org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException)

Aggregations

PcapPacket (org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket)9 PcapFile (org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile)5 Packet (org.eclipse.tracecompass.internal.pcap.core.packet.Packet)4 Nullable (org.eclipse.jdt.annotation.Nullable)3 PcapTestTrace (org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace)3 Test (org.junit.Test)3 BadPacketException (org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Path (java.nio.file.Path)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 NonNullUtils.nullToEmptyString (org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString)1 ProtocolEndpointPair (org.eclipse.tracecompass.internal.pcap.core.endpoint.ProtocolEndpointPair)1 PcapEndpoint (org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapEndpoint)1 PcapOldPacket (org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapOldPacket)1 BadPcapFileException (org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException)1 PcapTimestampScale (org.eclipse.tracecompass.internal.pcap.core.util.PcapTimestampScale)1 PcapEvent (org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent)1 PcapEventType (org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventType)1