changeset 1174:f19a7d241c8e

pcapng support in PcapSessionEventSource
author Devel 1
date Tue, 16 Jun 2020 16:22:11 +0200
parents 00b81be47b1c
children c47f6206fd9e
files stress-tester/pom.xml stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java stress-tester/src/main/java/com/passus/st/utils/NetExceptionsCategory.java stress-tester/src/test/java/com/passus/st/reader/pcap/PcapDataBlockReaderTest.java stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java
diffstat 7 files changed, 78 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/pom.xml	Tue Jun 16 15:57:46 2020 +0200
+++ b/stress-tester/pom.xml	Tue Jun 16 16:22:11 2020 +0200
@@ -195,7 +195,7 @@
         <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
-            <version>6.9.10</version>
+            <version>6.14.3</version>
             <scope>test</scope>
         </dependency>
         <dependency>
--- a/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java	Tue Jun 16 15:57:46 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java	Tue Jun 16 16:22:11 2020 +0200
@@ -110,4 +110,12 @@
         return position;
     }
 
+    public static boolean isFileSupported(File file) throws IOException {
+        try (PcapDataBlockReader reader = new PcapDataBlockReader(file)) {
+            reader.open();
+            return true;
+        } catch (Exception ignore) {
+            return false;
+        }
+    }
 }
--- a/stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java	Tue Jun 16 15:57:46 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java	Tue Jun 16 16:22:11 2020 +0200
@@ -94,7 +94,7 @@
     public Block readNg() throws IOException {
         if (position >= fileSize) {
             return null;
-        }  
+        }
 
         FileInputStreamUtils.read(bis, headerBuffer);
         position += headerBuffer.length;
@@ -151,4 +151,14 @@
         return result;
     }
 
+    public static boolean isFileSupported(File file) throws IOException {
+        try (PcapNgReader reader = new PcapNgReader(file)) {
+            reader.open();
+            Block block = reader.readNg();
+            return (block instanceof SectionHeaderBlock);
+        } catch (Exception ignore) {
+            return false;
+        }
+    }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Jun 16 15:57:46 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Jun 16 16:22:11 2020 +0200
@@ -31,13 +31,16 @@
 import com.passus.st.metric.MetricSource;
 import com.passus.st.metric.MetricsContainer;
 import com.passus.st.plugin.PluginConstants;
+import com.passus.st.reader.DataBlockReader;
 import com.passus.st.reader.pcap.PcapDataBlock;
 import com.passus.st.reader.pcap.PcapDataBlockReader;
+import com.passus.st.reader.pcapng.PcapNgReader;
 import com.passus.st.validation.FileValidator;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
 import java.io.IOException;
+import java.io.File;
 import java.util.*;
 
 import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
@@ -287,11 +290,21 @@
         pcapThread.join();
     }
 
+    static DataBlockReader<PcapDataBlock> createReader(File file) throws IOException {
+        if (PcapDataBlockReader.isFileSupported(file)) {
+            return new PcapDataBlockReader(file);
+        } else if (PcapNgReader.isFileSupported(file)) {
+            return new PcapNgReader(file);
+        } else {
+            throw new IOException("File format unsupported.");
+        }
+    }
+
     private class PcapThread extends Thread {
 
         private final DataBlockHandler dataBlockHandler = new DataBlockHandler(loopDelay);
 
-        private final PcapDataBlockReader reader;
+        private final DataBlockReader<PcapDataBlock> reader;
 
         private int loop;
 
@@ -301,7 +314,7 @@
 
         private PcapThread(String pcapFile, SessionPacketHandler sessionPacketHandler, int loop) throws IOException {
             super(PcapSessionEventSource.class.getSimpleName() + ".PcapThread");
-            reader = new PcapDataBlockReader(pcapFile);
+            reader = createReader(new File(pcapFile));
             reader.open();
             this.sessionPacketHandler = sessionPacketHandler;
             this.loop = loop;
--- a/stress-tester/src/main/java/com/passus/st/utils/NetExceptionsCategory.java	Tue Jun 16 15:57:46 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/NetExceptionsCategory.java	Tue Jun 16 16:22:11 2020 +0200
@@ -49,6 +49,6 @@
             }
         }
 
-        return UNKNOWN_ERROR;
+        return cause.getClass().getName() + ":" + cause.getMessage();
     }
 }
--- a/stress-tester/src/test/java/com/passus/st/reader/pcap/PcapDataBlockReaderTest.java	Tue Jun 16 15:57:46 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/reader/pcap/PcapDataBlockReaderTest.java	Tue Jun 16 16:22:11 2020 +0200
@@ -15,13 +15,30 @@
 public class PcapDataBlockReaderTest {
 
     @Test
+    public void testIsFileSupported() throws Exception {
+        File dir = TestResourceUtils.getFile("pcap/http/");
+        for (File file : dir.listFiles()) {
+            String fileName = file.getName();
+            if (fileName.equals("wget-ext.pcap")) {
+                // lacking support for FORMAT_EXT
+                continue;
+            }
+            String[] parts = fileName.split("[.]");
+            String ext = parts[parts.length - 1].toLowerCase();
+            boolean expected = ext.equals("pcap");
+            System.out.println(fileName);
+            assertEquals(PcapDataBlockReader.isFileSupported(file), expected, fileName);
+        }
+    }
+
+    @Test
     public void testRead() throws Exception {
-        String phpFileName = "pcap/http/wget.pcap";
-        File file = TestResourceUtils.getFile(phpFileName);
+        String pcapFileName = "pcap/http/wget.pcap";
+        File file = TestResourceUtils.getFile(pcapFileName);
         List<PcapDataBlock> blocks = new ArrayList<>();
         try (PcapDataBlockReader reader = new PcapDataBlockReader(file)) {
             reader.open();
-            
+
             PcapDataBlock dataBlock;
             while ((dataBlock = reader.read()) != null) {
                 blocks.add(dataBlock);
@@ -39,7 +56,7 @@
             assertArrayPartsEqual(record.getData(), 0, dstMac, 0, 6, "src");
         }
 
-        assertPayloads(phpFileName, blocks);
+        assertPayloads(pcapFileName, blocks);
     }
 
     @Test
@@ -49,7 +66,7 @@
         List<PcapDataBlock> blocks = new ArrayList<>();
         try (PcapDataBlockReader reader = new PcapDataBlockReader(file)) {
             reader.open();
-            
+
             PcapDataBlock dataBlock;
             while ((dataBlock = reader.read()) != null) {
                 blocks.add(dataBlock);
@@ -90,8 +107,8 @@
         assertEquals(blocks.size(), 67 * 2);
     }
 
-    private static void assertPayloads(String phpFileName, List<PcapDataBlock> blocks) {
-        List<byte[]> frames = PcapUtils.readBytes(phpFileName);
+    private static void assertPayloads(String pcapFileName, List<PcapDataBlock> blocks) {
+        List<byte[]> frames = PcapUtils.readBytes(pcapFileName);
         assertEquals(frames.size(), blocks.size());
 
         for (int i = 0; i < frames.size(); i++) {
--- a/stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java	Tue Jun 16 15:57:46 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java	Tue Jun 16 16:22:11 2020 +0200
@@ -10,6 +10,7 @@
 import com.passus.st.reader.pcapng.block.PacketBlock;
 import com.passus.st.reader.pcapng.block.SectionHeaderBlock;
 import com.passus.st.utils.TestResourceUtils;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 import static org.testng.Assert.*;
@@ -21,28 +22,44 @@
  */
 public class PcapNgReaderTest {
 
+    @Test
+    public void testIsFileSupported() throws Exception {
+        File dir = TestResourceUtils.getFile("pcap/http/");
+        for (File file : dir.listFiles()) {
+            String fileName = file.getName();
+            String[] parts = fileName.split("[.]");
+            String ext = parts[parts.length - 1].toLowerCase();
+            boolean expected = !ext.equals("pcap");
+            System.out.println(fileName);
+            assertEquals(PcapNgReader.isFileSupported(file), expected, fileName);
+        }
+    }
+
     static List<Block> readAllNgBlocks(PcapNgReader instance) throws Exception {
+        instance.open();
         List<Block> blocks = new ArrayList<>();
         Block block;
         while ((block = instance.readNg()) != null) {
             blocks.add(block);
         }
+        instance.close();
         return blocks;
     }
 
     static List<PcapDataBlock> readAllDataBlocks(PcapNgReader instance) throws Exception {
+        instance.open();
         List<PcapDataBlock> blocks = new ArrayList<>();
         PcapDataBlock block;
         while ((block = instance.read()) != null) {
             blocks.add(block);
         }
+        instance.close();
         return blocks;
     }
 
     @Test
     public void testRead() throws Exception {
         PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/wget.pcapng"));
-        instance.open();
         List<Block> blocks = readAllNgBlocks(instance);
 
         assertEquals(blocks.size(), 70);
@@ -79,7 +96,6 @@
     @Test
     public void testNtarWithPB() throws Exception {
         PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/http.littleendian.ntar"));
-        instance.open();
         List<Block> blocks = readAllNgBlocks(instance);
 
         assertEquals(blocks.size(), 45);
@@ -109,7 +125,6 @@
     @Test
     public void testReadSingleBlock() throws Exception {
         PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/wget.pcapng"));
-        instance.open();
         List<PcapDataBlock> blocks = readAllDataBlocks(instance);
 
         assertEquals(blocks.size(), 67);