Пример #1
0
  /** SKI ptest dumper. */
  public void SKIPtestDumper() {

    gen.start(); // Generate network traffic - async method

    System.out.printf("tmpFile=%s\n", tmpFile.getAbsoluteFile());

    Pcap pcap = Pcap.openLive(device, snaplen, promisc, oneSecond, errbuf);
    assertNotNull(errbuf.toString(), pcap);

    PcapDumper dumper = pcap.dumpOpen(tmpFile.getAbsolutePath());
    assertNotNull(pcap.getErr(), dumper);

    PcapHandler<PcapDumper> dumpHandler =
        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);
          }
        };

    pcap.loop(10, dumpHandler, dumper);

    assertTrue("Empty dump file " + tmpFile.getAbsolutePath(), tmpFile.length() > 0);

    // System.out.printf("Temp dumpfile size=%s\n", tmpFile.length());
    pcap.close();
  }
Пример #2
0
  /** Test open live and loop with breakloop. */
  public void testOpenLiveAndLoopWithBreakloop() {

    Pcap pcap = Pcap.openLive(device, 10000, 1, 60 * 1000, errbuf);
    assertNotNull(errbuf.toString(), pcap);

    PcapHandler<String> handler =
        new PcapHandler<String>() {

          public void nextPacket(
              String user, long seconds, int useconds, int caplen, int len, ByteBuffer buffer) {

            // System.out.printf("%s, ts=%s caplen=%d len=%d capacity=%d\n", user
            // .toString(), new Date(seconds * 1000).toString(), caplen, len,
            // buffer.capacity());

          }
        };

    pcap.breakloop(); // Should cause it to exit immediately
    assertEquals(
        "Error code does not indicate breakloop interrupted the loop when it should have",
        -2,
        pcap.loop(10, handler, "Hello"));

    pcap.close();
  }
Пример #3
0
  /** _test stats. */
  public void _testStats() {
    PcapStat stats = new PcapStat();

    Pcap pcap = Pcap.openLive(device, snaplen, promisc, oneSecond, errbuf);
    assertNotNull(errbuf.toString(), pcap);

    pcap.loop(5, doNothingHandler, null);
    pcap.stats(stats);
    // System.out.printf("stats=%s\n", stats.toString());

    pcap.loop(5, doNothingHandler, null);
    pcap.stats(stats);
    // System.out.printf("stats=%s\n", stats.toString());

    pcap.close();
  }
Пример #4
0
  /** Test filter compile and set filter. */
  public void testFilterCompileAndSetFilter() {
    PcapBpfProgram bpf = new PcapBpfProgram();
    String str = "host 192.168.101";

    // System.out.println("trying to compiler the filter() OK\n");
    // System.out.flush();
    Pcap pcap = Pcap.openOffline(fname, errbuf);
    // System.out.println("filter was compiled OK\n"); System.out.flush();
    assertNotNull(errbuf.toString(), pcap);

    int r = pcap.compile(bpf, str, 0, 0);
    assertEquals(pcap.getErr(), 0, r);

    PcapHandler<String> handler =
        new PcapHandler<String>() {
          public void nextPacket(
              String user, long seconds, int useconds, int caplen, int len, ByteBuffer buffer) {

            // System.out.printf("%s, ts=%s caplen=%d len=%d capacity=%d\n", user
            // .toString(), new Date(seconds * 1000).toString(), caplen, len,
            // buffer.capacity());
          }
        };

    // System.out.println("trying to set the filter() OK\n");
    // System.out.flush();
    assertEquals(OK, pcap.setFilter(bpf));
    // System.out.println("filter was set OK\n"); System.out.flush();
    assertEquals(OK, pcap.loop(10, handler, str));

    Pcap.freecode(bpf);

    pcap.close();
  }
Пример #5
0
 /** Test loop null ptr handling. */
 public void testLoopNullPtrHandling() {
   Pcap pcap = Pcap.openOffline(fname, errbuf);
   try {
     pcap.loop(1, (ByteBufferHandler<String>) null, null);
     fail("Expected a NULL pointer exception.");
   } catch (NullPointerException e) {
     // OK
   } finally {
     pcap.close();
   }
 }
Пример #6
0
  /**
   * Test case in response to <code>
   * Bug #1767744 - PcapHandler object ptr error in loop() and dispatch()</code>. The bug was that
   * PcapHandler jobject ptr in JNI jnetpcap.cpp, was set incorrectly to jobject of the parent which
   * is the Pcap object itself. The neccessary method "nextPacket" was looked up correctly using the
   * proper object but method execution was based on the parent Pcap object not the PcapHandler
   * object passed in. Therefore Java code when it was setting and accessing properties within the
   * PcapHandler sub-class, it was actually clobering data within the Pcap object. Both object's
   * states were terribly incosinstent, private fields had bogus values, that changed for no reason,
   * etc... Very easy fix, the jobject for 'jhandler' was substituted for jobject 'obj' that was
   * used to fix this problem. The problem was both in dispatch() and loop() methods since they are
   * nearly an identical copy of each other.
   *
   * <p>To test this we have to create a PcapHandler object set private fields within it, we'll use
   * an anonymous class, then read in the contents of the entire contents of a test datafile, while
   * updating the value of the field. Then at the end we should have consitent value in that private
   * field. Since the problem seemed complex, but was actually a very easy fix, this should never
   * really break again, but we will check for it anyhow.
   */
  public void testPcapHandlerParentOverrideBugUsingLoop() {

    Pcap pcap = Pcap.openOffline(fname, errbuf);
    if (pcap == null) {
      fail("Unable to open test data file because " + errbuf.toString());
    }
    final Pcap parent = pcap;

    // Tracking variable #1
    final AtomicInteger pcapCount = new AtomicInteger();

    final PcapHandler<?> handler =
        new PcapHandler<Object>() {

          // Tracking variable #2
          private int count = 0;

          public void nextPacket(
              Object userObject,
              long seconds,
              int useconds,
              int caplen,
              int len,
              ByteBuffer buffer) {

            pcapCount.addAndGet(1);
            count++;

            if (pcapCount.get() != count) {
              parent.breakloop(); // We exit with breakloop which means FAIL
            }
          }
        };

    int r = pcap.loop(Pcap.LOOP_INFINATE, handler, null);
    if (r == Pcap.LOOP_INTERRUPTED) {

      /*
       * Tracking variables are used to make sure they can sustain their
       * assigned values. The bug caused fields and object state to be overriden
       * by object of PcapHandler type.
       */
      fail("Handler indicates that 2 tracking variables in 2 objects, " + "did not match");
    } else if (r != Pcap.OK) {
      fail("Error occured: " + pcap.getErr());
    }

    pcap.close();
  }
Пример #7
0
  /** Test open offline and loop. */
  public void testOpenOfflineAndLoop() {

    Pcap pcap = Pcap.openOffline(fname, errbuf);
    assertNotNull(errbuf.toString(), pcap);

    PcapHandler<String> handler =
        new PcapHandler<String>() {

          public void nextPacket(
              String user, long seconds, int useconds, int caplen, int len, ByteBuffer buffer) {

            // System.out.printf("%s, ts=%s caplen=%d len=%d capacity=%d\n", user
            // .toString(), new Date(seconds * 1000).toString(), caplen, len,
            // buffer.capacity());

          }
        };

    assertEquals(OK, pcap.loop(10, handler, "Hello"));

    pcap.close();
  }
Пример #8
0
  /** 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());
  }