Skip to content

Commit

Permalink
fix SearchResultEntry encoding bug, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinz01 committed May 3, 2024
1 parent aeb5e99 commit e127864
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions ber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ldapserver_test

import (
"bytes"
"encoding/hex"
"errors"
"testing"

Expand Down Expand Up @@ -1307,3 +1308,37 @@ func TestParseSearchRequest(t *testing.T) {
t.Error("wrong attributes")
}
}

func TestSearchResultEntryEncode(t *testing.T) {
entry := ldapserver.SearchResultEntry{
ObjectName: "dc=example,dc=com",
Attributes: []ldapserver.Attribute{
{Description: "objectClass", Values: []string{"top", "domain"}},
{Description: "dc", Values: []string{"example"}},
},
}
encoded := entry.Encode()
expected := []byte{
0x04, 0x11, 0x64, 0x63, 0x3d, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x2c, 0x64, 0x63, 0x3d, 0x63, 0x6f,
0x6d,
0x30, 0x2f,
0x30, 0x1c,
0x04, 0x0b, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x43, 0x6c,
0x61, 0x73, 0x73,
0x31, 0x0d,
0x04, 0x03, 0x74, 0x6f, 0x70,
0x04, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e,
0x30, 0x0f,
0x04, 0x02, 0x64, 0x63,
0x31, 0x09,
0x04, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
}
if !slicesEqual(encoded, expected) {
t.Error("wrong encoding")
println("Expected:")
println(hex.Dump(expected))
println("Got:")
println(hex.Dump(encoded))
}
}
2 changes: 1 addition & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *SearchResultEntry) Encode() []byte {
b.Write(BerEncodeOctetString(s.ObjectName))
ab := bytes.NewBuffer(nil)
for _, attr := range s.Attributes {
ab.Write(attr.Encode())
ab.Write(BerEncodeSequence(attr.Encode()))
}
b.Write(BerEncodeSequence(ab.Bytes()))
return b.Bytes()
Expand Down

0 comments on commit e127864

Please sign in to comment.