ldap-upstream/internal/ldap/testutil_test.go
2026-11-01 18:50:00 -06:00

66 lines
2 KiB
Go

package ldap
import (
"testing"
"github.com/ps78674/goldap/message"
ldap "github.com/ps78674/ldapserver"
)
// The goldap message types have unexported fields with no exported
// constructors, so test messages are built by hand-encoding the DER
// for the requests we care about and running them through the real
// decoder.
func berElement(tag byte, body ...[]byte) []byte {
length := 0
for _, b := range body {
length += len(b)
}
out := []byte{tag, byte(length)}
for _, b := range body {
out = append(out, b...)
}
return out
}
func berInteger(v byte) []byte { return berElement(0x02, []byte{v}) }
func berOctetString(s string) []byte { return berElement(0x04, []byte(s)) }
// berBindRequest encodes an LDAPMessage containing a simple
// BindRequest for the given DN and password.
func berBindRequest(dn, password string) []byte {
bind := berElement(0x60, // [APPLICATION 0] constructed
berInteger(3),
berOctetString(dn),
berElement(0x80, []byte(password)), // [0] simple, primitive
)
return berElement(0x30, berInteger(1), bind)
}
// berSearchRequest encodes an LDAPMessage containing a SearchRequest
// with an attribute-present filter on uid and no attribute selection.
func berSearchRequest(baseDN string) []byte {
search := berElement(0x63, // [APPLICATION 3] constructed
berOctetString(baseDN), // baseObject
berElement(0x0a, []byte{2}), // scope: wholeSubtree
berElement(0x0a, []byte{0}), // derefAliases: never
berInteger(0), // sizeLimit
berInteger(0), // timeLimit
berElement(0x01, []byte{0x00}), // typesOnly: false
berElement(0x87, []byte("uid")), // filter: (uid=*)
berElement(0x30), // attributes: empty selection
)
return berElement(0x30, berInteger(2), search)
}
func mustDecode(t *testing.T, raw []byte) *ldap.Message {
t.Helper()
m, err := message.ReadLDAPMessage(message.NewBytes(0, raw))
if err != nil {
t.Fatalf("decoding test message: %s", err)
}
return &ldap.Message{LDAPMessage: &m, Done: make(chan bool, 2)}
}