Ejemplo n.º 1
0
  /** Test filter compile no pcap and accessors. */
  public void testFilterCompileNoPcapAndAccessors() {
    PcapBpfProgram bpf = new PcapBpfProgram();

    String str = "host 192.168.1.1";

    int r = Pcap.compileNoPcap(1024, 1, bpf, str, 0, 0);
    assertEquals(OK, r);

    assertEquals(26, bpf.getInstructionCount());
    assertEquals(120259084320L, bpf.getInstruction(10));

    // Boundary checks
    try {
      bpf.getInstruction(-10);
      fail("Failed to generate exception on low index boundary");
    } catch (IndexOutOfBoundsException e) {
      // OK
    }

    // Boundary checks
    try {
      bpf.getInstruction(26);
      fail("Failed to generate exception on upper index boundary");
    } catch (IndexOutOfBoundsException e) {
      // OK
    }

    Pcap.freecode(bpf);
  }
Ejemplo n.º 2
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();
  }
Ejemplo n.º 3
0
 /** Test free code null ptr handling. */
 public void testFreeCodeNullPtrHandling() {
   try {
     Pcap.freecode(null);
     fail("Expected a NULL pointer exception.");
   } catch (NullPointerException e) {
     // OK
   }
 }