示例#1
0
  @Test
  public void testRead() throws Exception {
    String pcapFile =
        new StringBuilder()
            .append(resourceDirPath)
            .append("/")
            .append(getClass().getSimpleName())
            .append(".pcap")
            .toString();
    PcapHandle ph = Pcaps.openOffline(pcapFile);
    StringBuilder sb = new StringBuilder(1000);
    sb.append(ph.getNextPacket().toString())
        .append(System.getProperty("line.separator"))
        .append(ph.getNextPacket().toString());
    ph.close();

    FileReader fr =
        new FileReader(
            new StringBuilder()
                .append(resourceDirPath)
                .append("/")
                .append(getClass().getSimpleName())
                .append(".log")
                .toString());
    BufferedReader fbr = new BufferedReader(fr);
    StringReader sr = new StringReader(sb.toString());
    BufferedReader sbr = new BufferedReader(sr);

    String line;
    while ((line = fbr.readLine()) != null) {
      assertEquals(line, sbr.readLine());
    }

    assertNull(sbr.readLine());

    fbr.close();
    fr.close();
    sr.close();
    sbr.close();
  }
示例#2
0
  @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();
  }