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

50 lines
1.3 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)
}
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)}
}