/** Test pcap dumper using dispatch. */ public void testPcapDumperUsingDispatch() { Pcap pcap = Pcap.openOffline(fname, errbuf); assertNotNull(errbuf.toString(), pcap); PcapDumper dumper = pcap.dumpOpen(tmpFile.getPath()); assertNotNull(pcap.getErr(), dumper); PcapHandler<PcapDumper> handler = new PcapHandler<PcapDumper>() { public void nextPacket( PcapDumper dumper, long seconds, int useconds, int caplen, int len, ByteBuffer buffer) { dumper.dump(seconds, useconds, caplen, len, buffer); } }; /* * Our test file is small, about 24K bytes in size, should fit inside a * buffer full. */ int r = pcap.dispatch(Pcap.DISPATCH_BUFFER_FULL, handler, dumper); assertTrue("Something happened in dispatch", r == Pcap.OK); dumper.close(); pcap.close(); // System.out.printf("%s: tmp=%d, source=%d\n", tmpFile.getName(), tmpFile // .length(), new File(fname).length()); // assertEquals( "dumped file and source file lengths don't match", tmpFile.length(), new File(fname).length()); }
/** Test pcap dumper using loop. */ public void testPcapDumperUsingLoop() { Pcap pcap = Pcap.openOffline(fname, errbuf); assertNotNull(errbuf.toString(), pcap); PcapDumper dumper = pcap.dumpOpen(tmpFile.getPath()); assertNotNull(pcap.getErr(), dumper); PcapHandler<PcapDumper> handler = new PcapHandler<PcapDumper>() { public void nextPacket( PcapDumper dumper, long seconds, int useconds, int caplen, int len, ByteBuffer buffer) { dumper.dump(seconds, useconds, caplen, len, buffer); } }; int r = pcap.loop(Pcap.LOOP_INFINATE, handler, dumper); assertTrue("Something happened in the loop", r == Pcap.OK); dumper.close(); pcap.close(); // System.out.printf("%s: tmp=%d, source=%d\n", tmpFile.getName(), tmpFile // .length(), new File(fname).length()); // assertEquals( "dumped file and source file lengths don't match", tmpFile.length(), new File(fname).length()); }
@Test public void testDump() throws Exception { String dumpFile = new StringBuilder() .append(tmpDirPath) .append("/") .append(getClass().getSimpleName()) .append(".pcap") .toString(); PcapHandle handle = Pcaps.openDead(DataLinkType.RAW, 65536); PcapDumper dumper = handle.dumpOpen(dumpFile); dumper.dump(ipV4, 0, 0); dumper.dump(ipV6, 0, 0); dumper.close(); handle.close(); FileInputStream in1 = new FileInputStream( new StringBuilder() .append(resourceDirPath) .append("/") .append(getClass().getSimpleName()) .append(".pcap") .toString()); FileInputStream in2 = new FileInputStream(dumpFile); byte[] buffer1 = new byte[100]; byte[] buffer2 = new byte[100]; int size; while ((size = in1.read(buffer1)) != -1) { assertEquals(size, in2.read(buffer2)); assertArrayEquals(buffer1, buffer2); } in1.close(); in2.close(); }