summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Vanackere <vincent.vanackere@gmail.com>2014-08-01 21:02:51 +0200
committerVincent Vanackere <vincent.vanackere@gmail.com>2014-08-01 21:02:51 +0200
commitda9e157102f76b56945fd15669f832deb4a4fe9e (patch)
tree6914f68c64b3e6f4a7b118d44f212c2c35e2f928
parentSwitch asn1-ber dependency to "github.com/vanackere/asn1-ber" (diff)
parentfilter: bugfix for FilterPresent (diff)
downloadldap-da9e157102f76b56945fd15669f832deb4a4fe9e.tar
ldap-da9e157102f76b56945fd15669f832deb4a4fe9e.tar.gz
ldap-da9e157102f76b56945fd15669f832deb4a4fe9e.tar.bz2
ldap-da9e157102f76b56945fd15669f832deb4a4fe9e.tar.lz
ldap-da9e157102f76b56945fd15669f832deb4a4fe9e.tar.xz
ldap-da9e157102f76b56945fd15669f832deb4a4fe9e.tar.zst
ldap-da9e157102f76b56945fd15669f832deb4a4fe9e.zip
-rw-r--r--filter.go9
-rw-r--r--filter_test.go36
2 files changed, 43 insertions, 2 deletions
diff --git a/filter.go b/filter.go
index b3e4a97..784a6ae 100644
--- a/filter.go
+++ b/filter.go
@@ -124,7 +124,7 @@ func DecompileFilter(packet *ber.Packet) (ret string, err error) {
ret += "<="
ret += ber.DecodeString(packet.Children[1].Data.Bytes())
case FilterPresent:
- ret += ber.DecodeString(packet.Children[0].Data.Bytes())
+ ret += string(packet.Data.Bytes())
ret += "=*"
case FilterApproxMatch:
ret += ber.DecodeString(packet.Children[0].Data.Bytes())
@@ -213,13 +213,15 @@ func compileFilter(filter string, pos int) (*ber.Packet, int, error) {
err = NewError(ErrorFilterCompile, errors.New("ldap: error parsing filter"))
return packet, newPos, err
}
- packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attribute, "Attribute"))
switch {
case packet.Tag == FilterEqualityMatch && condition == "*":
+ packet.TagType = ber.TypePrimitive
packet.Tag = FilterPresent
packet.Description = FilterMap[uint64(packet.Tag)]
+ packet.Data.WriteString(attribute)
case packet.Tag == FilterEqualityMatch && condition[0] == '*' && condition[len(condition)-1] == '*':
// Any
+ packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attribute, "Attribute"))
packet.Tag = FilterSubstrings
packet.Description = FilterMap[uint64(packet.Tag)]
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Substrings")
@@ -227,6 +229,7 @@ func compileFilter(filter string, pos int) (*ber.Packet, int, error) {
packet.AppendChild(seq)
case packet.Tag == FilterEqualityMatch && condition[0] == '*':
// Final
+ packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attribute, "Attribute"))
packet.Tag = FilterSubstrings
packet.Description = FilterMap[uint64(packet.Tag)]
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Substrings")
@@ -234,12 +237,14 @@ func compileFilter(filter string, pos int) (*ber.Packet, int, error) {
packet.AppendChild(seq)
case packet.Tag == FilterEqualityMatch && condition[len(condition)-1] == '*':
// Initial
+ packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attribute, "Attribute"))
packet.Tag = FilterSubstrings
packet.Description = FilterMap[uint64(packet.Tag)]
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Substrings")
seq.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, FilterSubstringsInitial, condition[:len(condition)-1], "Initial Substring"))
packet.AppendChild(seq)
default:
+ packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attribute, "Attribute"))
packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, condition, "Condition"))
}
newPos++
diff --git a/filter_test.go b/filter_test.go
index 0a63949..e78caa1 100644
--- a/filter_test.go
+++ b/filter_test.go
@@ -45,6 +45,42 @@ func TestFilter(t *testing.T) {
}
}
+func TestDecodePresentFilter(t *testing.T) {
+
+ bs := []byte{0x87, 0x06, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72} // ~ (member=*)
+
+ p := ber.DecodePacket(bs)
+ f, err := DecompileFilter(p)
+ if err != nil {
+ t.Errorf("--- CAN'T DECODE & DECOMPILE FILTER")
+ return
+ }
+ if f != "(member=*)" {
+ t.Errorf("expected (member=*), got %s", f)
+ }
+
+}
+
+func TestEncodeDecodePresentFilter(t *testing.T) {
+ f := "(member=*)"
+ p, err := CompileFilter(f)
+ if err != nil {
+ t.Errorf("cant compile filter")
+ return
+ }
+ bytes := p.Bytes()
+
+ p2 := ber.DecodePacket(bytes)
+ f2, err := DecompileFilter(p2)
+ if err != nil {
+ t.Errorf("cant decompile filter")
+ return
+ }
+ if f != f2 {
+ t.Errorf("encode/decode changed filter")
+ }
+}
+
func BenchmarkFilterCompile(b *testing.B) {
b.StopTimer()
filters := make([]string, len(testFilters))