Mercurial > stress-tester
changeset 1169:af04f9f94d35
cleanup
author | Devel 1 |
---|---|
date | Tue, 16 Jun 2020 14:07:29 +0200 |
parents | 5119482979a5 |
children | 93826d05cd95 |
files | stress-tester/src/main/java/com/passus/st/reader/ProgressListener.java stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgListener.java stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java |
diffstat | 4 files changed, 10 insertions(+), 134 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/ProgressListener.java Tue Jun 16 13:44:06 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -package com.passus.st.reader; - -/** - * - * @author mikolaj.podbielski - */ -public interface ProgressListener { - - void progress(long processedBytes, long totalBytes); - - public static final ProgressListener NULL_LISTENER = (long proc, long total) -> { - }; - - public static final ProgressListener PRINTER = (long proc, long total) -> { - System.out.format("position: %x %d of %d\n", proc, proc, total); - }; - -}
--- a/stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgListener.java Tue Jun 16 13:44:06 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -package com.passus.st.reader.pcapng; - -import com.passus.st.reader.pcapng.block.Block; - -/** - * - * @author mikolaj.podbielski - */ -public interface PcapNgListener { - - void blockRead(Block block); - - public static final PcapNgListener NULL_LISTENER = (block) -> {}; -}
--- a/stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java Tue Jun 16 13:44:06 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java Tue Jun 16 14:07:29 2020 +0200 @@ -2,7 +2,6 @@ import com.passus.commons.Assert; import com.passus.data.DataHelper; -import com.passus.st.reader.ProgressListener; import com.passus.st.reader.pcap.PcapDataBlock; import com.passus.st.reader.pcapng.block.Block; import com.passus.st.reader.pcapng.block.BlockFactory; @@ -14,7 +13,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteOrder; -import java.nio.channels.FileChannel; +import java.util.List; /** * @@ -23,8 +22,8 @@ public class PcapNgReader { private final File pcapFile; - private FileInputStream fis = null; - private BufferedInputStream bis = null; + private FileInputStream fis; + private BufferedInputStream bis; private long fileSize; private final byte[] discardBuffer = new byte[4]; @@ -130,65 +129,9 @@ } @Deprecated - public void read(String path, PcapNgListener pcapListener, ProgressListener progress) throws IOException { - - try (FileInputStream fis = new FileInputStream(path); - FileChannel channel = fis.getChannel()) { - - long fileSize = channel.size(); - progress.progress(0, fileSize); - - while (position < fileSize) { - FileInputStreamUtils.read(fis, headerBuffer); - position += headerBuffer.length; - - int type = mbu.getInt4(headerBuffer, 0); - int length = mbu.getInt4(headerBuffer, 4); - int recordDataLength; - - if (SectionHeaderBlock.isSHBHeader(headerBuffer)) { - shbProcessed = true; - - byte byteOrderMagic[] = new byte[4]; - FileInputStreamUtils.read(fis, byteOrderMagic); - position += 4; - - ByteOrder newByteOrder = SectionHeaderBlock.isLittleEndian(byteOrderMagic) - ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN; - if (byteOrder != newByteOrder) { - byteOrder = newByteOrder; - mbu = DataHelper.get(newByteOrder); - length = Integer.reverseBytes(length); - } - recordDataLength = length - 16; - } else { - recordDataLength = length - 12; - } - - if (!shbProcessed) { - throw new IllegalArgumentException(); - } - - if (recordDataLength > 4 * 1024 * 1024) { - throw new IllegalArgumentException("Pcap record is too big: " + recordDataLength); - } - byte[] recordData = new byte[recordDataLength]; - FileInputStreamUtils.read(fis, recordData); - position += recordData.length; - - FileInputStreamUtils.read(fis, discardBuffer); - position += discardBuffer.length; - Block block = factory.createBlock(type, recordData, byteOrder); - pcapListener.blockRead(block); - - long padded = DataHelper.pad4(position); - if (padded > position) { - FileInputStreamUtils.skip(fis, padded - position); - position = padded; - } - - progress.progress(position, fileSize); - } + void readAll(String path, List<Block> blocks) throws IOException { + while (position < fileSize) { + blocks.add(readNg()); } }
--- a/stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java Tue Jun 16 13:44:06 2020 +0200 +++ b/stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java Tue Jun 16 14:07:29 2020 +0200 @@ -1,10 +1,8 @@ package com.passus.st.reader.pcapng; -import com.passus.st.reader.ProgressListener; import com.passus.st.reader.pcap.LinkType; import com.passus.st.reader.pcap.PcapDataBlock; import com.passus.st.reader.pcapng.block.Block; -import com.passus.st.reader.pcapng.block.BlockFactory; import com.passus.st.reader.pcapng.block.EnhancedPacketBlock; import com.passus.st.reader.pcapng.block.InterfaceDescriptionBlock; import com.passus.st.reader.pcapng.block.InterfaceStatisticsBlock; @@ -14,11 +12,7 @@ import com.passus.st.utils.TestResourceUtils; import java.util.ArrayList; import java.util.List; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.core.Logger; import static org.testng.Assert.*; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; /** @@ -27,20 +21,13 @@ */ public class PcapNgReaderTest { - TestPcapNgListener listener = new TestPcapNgListener(); - - @BeforeMethod - public void setUpMethod() throws Exception { - listener.clear(); - } - @Test public void testRead() throws Exception { String path = TestResourceUtils.getFile("pcap/http/wget.pcapng").getAbsolutePath(); PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/wget.pcapng")); - instance.read(path, listener, ProgressListener.NULL_LISTENER); + List<Block> blocks = new ArrayList<>(); + instance.readAll(path, blocks); - List<Block> blocks = listener.blocks; assertEquals(blocks.size(), 70); assertTrue(blocks.get(0) instanceof SectionHeaderBlock); assertTrue(blocks.get(1) instanceof InterfaceDescriptionBlock); @@ -70,16 +57,15 @@ assertEquals(epb.getPacketData().length, 66); assertEquals(epb.getOptions(), null); assertEquals(epb.getTimestamp(), 1_482_233_232_872L); - } @Test public void testNtarWithPB() throws Exception { String path = TestResourceUtils.getFile("pcap/http/http.littleendian.ntar").getAbsolutePath(); PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/http.littleendian.ntar")); - instance.read(path, listener, ProgressListener.NULL_LISTENER); + List<Block> blocks = new ArrayList<>(); + instance.readAll(path, blocks); - List<Block> blocks = listener.blocks; assertEquals(blocks.size(), 45); SectionHeaderBlock shb = (SectionHeaderBlock) blocks.get(0); @@ -102,26 +88,6 @@ assertEquals(pb.getPacketData().length, 62); assertEquals(pb.getOptions(), null); assertEquals(pb.getTimestamp(), 1_084_439_132_343L); - - } - - static void setLogLevel(Level level) { - Logger LOGGER = (Logger) LogManager.getLogger(BlockFactory.class); - LOGGER.setLevel(level); - } - - private static class TestPcapNgListener implements PcapNgListener { - - List<Block> blocks = new ArrayList<>(); - - @Override - public void blockRead(Block block) { - blocks.add(block); - } - - public void clear() { - blocks.clear(); - } } @Test @@ -135,6 +101,5 @@ assertEquals(result.getLength(), 66); assertEquals(result.getOriginalLength(), 66); assertEquals(result.getData().length, 66); - } }