func TestEmpty(t *testing.T) { bld := filter.NewBuilder() flt := bld.Build() if flt.Len() != 0 { t.Fatalf("Len mismatch: %d", flt.Len()) } flt.Cleanup() }
func TestARP(t *testing.T) { arp := filter.NewBuilder(). LD(filter.Half, filter.ABS, 12). JEQ(filter.Const, "", "fail", 0x806). RET(filter.Const, 0x40000). Label("fail"). RET(filter.Const, 0x0). Build() if arp.String() != test_arp { t.Fatalf("Program mismatch: %s", arp.String()) } }
func ExampleBuilder() { // Build a filter to match ARP packets on top of Ethernet flt := filter.NewBuilder(). LD(filter.Half, filter.ABS, 12). JEQ(filter.Const, "", "fail", 0x806). RET(filter.Const, 0x40000). Label("fail"). RET(filter.Const, 0x0). Build() if flt.Match([]byte("random data")) { log.Println("MATCH!!!") } }
func TestDNS(t *testing.T) { dns := filter.NewBuilder(). LD(filter.Word, filter.IMM, 20). LDX(filter.Byte, filter.MSH, 0). ADD(filter.Index, 0). TAX(). Label("lb_0"). LD(filter.Word, filter.IND, 0). JEQ(filter.Const, "", "lb_1", 0x07657861). LD(filter.Word, filter.IND, 4). JEQ(filter.Const, "", "lb_1", 0x6d706c65). LD(filter.Word, filter.IND, 8). JEQ(filter.Const, "", "lb_1", 0x03636f6d). LD(filter.Byte, filter.IND, 12). JEQ(filter.Const, "", "lb_1", 0x00). RET(filter.Const, 1). Label("lb_1"). RET(filter.Const, 0). Build() if dns.String() != test_dns { t.Fatalf("Program mismatch: %s", dns.String()) } }