package ldap import ( "reflect" "testing" ber "4a.si/asn1-ber" ) type compileTest struct { filterStr string filterType uint8 } var testFilters = []compileTest{ {filterStr: "(&(sn=Müller)(givenName=Bob))", filterType: FilterAnd}, {filterStr: "(|(sn=Möller)(givenName=Bob))", filterType: FilterOr}, {filterStr: "(!(sn=Møller))", filterType: FilterNot}, {filterStr: "(sn=Müller)", filterType: FilterEqualityMatch}, {filterStr: "(sn=Möll*)", filterType: FilterSubstrings}, {filterStr: "(sn=*Møll)", filterType: FilterSubstrings}, {filterStr: "(sn=*Müll*)", filterType: FilterSubstrings}, {filterStr: "(sn>=Möller)", filterType: FilterGreaterOrEqual}, {filterStr: "(sn<=Møller)", filterType: FilterLessOrEqual}, {filterStr: "(sn=*)", filterType: FilterPresent}, {filterStr: "(sn~=Müller)", filterType: FilterApproxMatch}, // { filterStr: "()", filterType: FilterExtensibleMatch }, } func TestFilter(t *testing.T) { // Test Compiler and Decompiler for _, i := range testFilters { filter, err := CompileFilter(i.filterStr) if err != nil { t.Errorf("Problem compiling %s - %s", i.filterStr, err.Error()) } else if filter.Tag != uint8(i.filterType) { t.Errorf("%q Expected %q got %q", i.filterStr, FilterMap[i.filterType], FilterMap[filter.Tag]) } else { o, err := DecompileFilter(filter) if err != nil { t.Errorf("Problem compiling %s - %s", i.filterStr, err.Error()) } else if i.filterStr != o { t.Errorf("%q expected, got %q", i.filterStr, o) } } } } type binTestFilter struct { bin []byte str string } var binTestFilters = []binTestFilter{ {bin: []byte{0x87, 0x06, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72}, str: "(member=*)"}, } func TestFiltersDecode(t *testing.T) { for i, test := range binTestFilters { p := ber.DecodePacket(test.bin) if filter, err := DecompileFilter(p); err != nil { t.Errorf("binTestFilters[%d], DecompileFilter returned : %s", i, err) } else if filter != test.str { t.Errorf("binTestFilters[%d], %q expected, got %q", i, test.str, filter) } } } func TestFiltersEncode(t *testing.T) { for i, test := range binTestFilters { p, err := CompileFilter(test.str) if err != nil { t.Errorf("binTestFilters[%d], CompileFilter returned : %s", i, err) continue } b := p.Bytes() if !reflect.DeepEqual(b, test.bin) { t.Errorf("binTestFilters[%d], %q expected for CompileFilter(%q), got %q", i, test.bin, test.str, b) } } } func BenchmarkFilterCompile(b *testing.B) { b.StopTimer() filters := make([]string, len(testFilters)) // Test Compiler and Decompiler for idx, i := range testFilters { filters[idx] = i.filterStr } maxIdx := len(filters) b.StartTimer() for i := 0; i < b.N; i++ { CompileFilter(filters[i%maxIdx]) } } func BenchmarkFilterDecompile(b *testing.B) { b.StopTimer() filters := make([]*ber.Packet, len(testFilters)) // Test Compiler and Decompiler for idx, i := range testFilters { filters[idx], _ = CompileFilter(i.filterStr) } maxIdx := len(filters) b.StartTimer() for i := 0; i < b.N; i++ { DecompileFilter(filters[i%maxIdx]) } } func TestGetFilterObjectClass(t *testing.T) { c, err := GetFilterObjectClass("(objectClass=*)") if err != nil { t.Errorf("GetFilterObjectClass failed") } if c != "" { t.Errorf("GetFilterObjectClass failed") } c, err = GetFilterObjectClass("(objectClass=posixAccount)") if err != nil { t.Errorf("GetFilterObjectClass failed") } if c != "posixaccount" { t.Errorf("GetFilterObjectClass failed") } c, err = GetFilterObjectClass("(&(cn=awesome)(objectClass=posixGroup))") if err != nil { t.Errorf("GetFilterObjectClass failed") } if c != "posixgroup" { t.Errorf("GetFilterObjectClass failed") } }