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

57 lines
1.5 KiB
Go

package ldap
import (
"testing"
"github.com/ps78674/goldap/message"
)
// resultCode round-trips a result response through its wire encoding
// and pulls the result code back out of it. The goldap result types
// expose setters but no getters, so the encoding is the only way to
// read it back.
func resultCode(t *testing.T, po message.ProtocolOp) int {
t.Helper()
m := message.NewLDAPMessageWithProtocolOp(po)
message.SetMessageID(m, 1)
b, err := m.Write()
if err != nil {
t.Fatalf("encoding response: %s", err)
}
raw := b.Bytes()
// Descend from the LDAPMessage SEQUENCE into the protocolOp,
// whose first element is always the resultCode for the result
// types this server sends.
var e error
off := 0
if _, off, e = message.ParseTagAndLength(raw, off); e != nil {
t.Fatalf("message tag: %s", e)
}
var id message.TagAndLength
if id, off, e = message.ParseTagAndLength(raw, off); e != nil {
t.Fatalf("messageID tag: %s", e)
}
off += id.Length
if _, off, e = message.ParseTagAndLength(raw, off); e != nil {
t.Fatalf("protocolOp tag: %s", e)
}
codeTL, off, e := message.ParseTagAndLength(raw, off)
if e != nil {
t.Fatalf("resultCode tag: %s", e)
}
if codeTL.Tag != 0x0a {
t.Fatalf("first element of the protocolOp is tag %d, want 10 (ENUMERATED)", codeTL.Tag)
}
if off+codeTL.Length > len(raw) {
t.Fatal("result code truncated")
}
code := 0
for _, b := range raw[off : off+codeTL.Length] {
code = code<<8 | int(b)
}
return code
}