Пример #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 set and get nonblock. */
  public void testSetAndGetNonblock() {
    Pcap pcap = Pcap.openLive(device, 10000, 1, 60 * 1000, errbuf);
    assertNotNull(errbuf.toString(), pcap);

    assertEquals(OK, pcap.getNonBlock(errbuf));

    pcap.close();
  }
Пример #4
0
 /** Test pcap open live null ptr handling. */
 public void testPcapOpenLiveNullPtrHandling() {
   try {
     Pcap.openLive(null, 1, 1, 1, null);
     fail("Expected a NULL pointer exception.");
   } catch (NullPointerException e) {
     // OK
   }
 }
Пример #5
0
  /**
   * Test errbuf.
   *
   * @throws SocketException the socket exception
   * @throws InterruptedException the interrupted exception
   */
  public void testErrbuf() throws SocketException, InterruptedException {

    // Test using a bogus device name that's sure to fail
    errbuf.append("def"); // Set dummy message and it should be replaced
    Pcap pcap = Pcap.openLive("abc", 101, 1, 60, errbuf);
    assertNull(pcap);

    assertFalse(
        "Our pre-initialized error message should have been cleared",
        "def".equals(errbuf.toString()));

    assertTrue("Error buffer should contain an error message", errbuf.length() != 0);
  }
Пример #6
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();
  }
Пример #7
0
  /**
   * Test open live and datalink and close.
   *
   * @throws SocketException the socket exception
   * @throws InterruptedException the interrupted exception
   */
  public void testOpenLiveAndDatalinkAndClose() throws SocketException, InterruptedException {

    //		System.out.println(System.getProperty("os.name"));
    Pcap pcap = Pcap.openLive(device, 101, 1, 60, errbuf);
    assertNotNull(errbuf.toString(), pcap);

    // Physical field initialized from JNI space
    assertFalse("0".equals(pcap.toString()));

    // Check linklayer 1 is for DLT_EN10MB
    // assertEquals(113, pcap.datalink());

    pcap.close();

    try {
      pcap.close();
      fail();
    } catch (IllegalStateException e) {
      // Expecting this exception on second call to close()
    }
  }
Пример #8
0
  /**
   * Test disabled, as it requires live packets to capture. To enable the test just rename the
   * method, by removing the prefix SKIP. Then make sure there are live packets to be captured.
   */
  public void SKIPtestOpenLiveAndDispatch() {

    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.dispatch(10, handler, "Hello");

    pcap.close();
  }