Skip to content

Commit

Permalink
Merge branch 'master' into cifuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
cpuschma committed Sep 6, 2023
2 parents 994f4c3 + ba89d92 commit ba9c992
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func (e *Error) Error() string {
return fmt.Sprintf("LDAP Result Code %d %q: %s", e.ResultCode, LDAPResultCodeMap[e.ResultCode], e.Err.Error())
}

func (e *Error) Unwrap() error { return e.Err }

// GetLDAPError creates an Error out of a BER packet representing a LDAPResult
// The return is an error object. It can be casted to a Error structure.
// This function returns nil if resultCode in the LDAPResult sequence is success(0).
Expand Down
19 changes: 19 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ldap

import (
"errors"
"io"
"net"
"strings"
"testing"
Expand Down Expand Up @@ -103,6 +104,24 @@ func TestGetLDAPErrorInvalidResponse(t *testing.T) {
}
}

func TestErrorIs(t *testing.T) {
err := NewError(ErrorNetwork, io.EOF)
if !errors.Is(err, io.EOF) {
t.Errorf("Expected an io.EOF error: %v", err)
}
}

func TestErrorAs(t *testing.T) {
var netErr net.InvalidAddrError = "invalid addr"
err := NewError(ErrorNetwork, netErr)

var target net.InvalidAddrError
ok := errors.As(err, &target)
if !ok {
t.Error("Expected an InvalidAddrError")
}
}

// TestGetLDAPErrorSuccess tests parsing of a result with no error (resultCode == 0).
func TestGetLDAPErrorSuccess(t *testing.T) {
bindResponse := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindResponse, nil, "Bind Response")
Expand Down
13 changes: 9 additions & 4 deletions v3/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,12 @@ func (c *ControlSyncRequest) GetControlType() string {
func (c *ControlSyncRequest) Encode() *ber.Packet {
_mode := int64(c.Mode)
mode := ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, _mode, "Mode")
cookie := ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, nil, "Cookie")
cookie.Value = c.Cookie
cookie.Data.Write(c.Cookie)
var cookie *ber.Packet
if len(c.Cookie) > 0 {
cookie = ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, nil, "Cookie")
cookie.Value = c.Cookie
cookie.Data.Write(c.Cookie)
}
reloadHint := ber.NewBoolean(ber.ClassUniversal, ber.TypePrimitive, ber.TagBoolean, c.ReloadHint, "Reload Hint")

packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Control")
Expand All @@ -940,7 +943,9 @@ func (c *ControlSyncRequest) Encode() *ber.Packet {
val := ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, nil, "Control Value (Sync Request)")
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Sync Request Value")
seq.AppendChild(mode)
seq.AppendChild(cookie)
if cookie != nil {
seq.AppendChild(cookie)
}
seq.AppendChild(reloadHint)
val.AppendChild(seq)

Expand Down
2 changes: 2 additions & 0 deletions v3/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func (e *Error) Error() string {
return fmt.Sprintf("LDAP Result Code %d %q: %s", e.ResultCode, LDAPResultCodeMap[e.ResultCode], e.Err.Error())
}

func (e *Error) Unwrap() error { return e.Err }

// GetLDAPError creates an Error out of a BER packet representing a LDAPResult
// The return is an error object. It can be casted to a Error structure.
// This function returns nil if resultCode in the LDAPResult sequence is success(0).
Expand Down
19 changes: 19 additions & 0 deletions v3/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ldap

import (
"errors"
"io"
"net"
"strings"
"testing"
Expand Down Expand Up @@ -103,6 +104,24 @@ func TestGetLDAPErrorInvalidResponse(t *testing.T) {
}
}

func TestErrorIs(t *testing.T) {
err := NewError(ErrorNetwork, io.EOF)
if !errors.Is(err, io.EOF) {
t.Errorf("Expected an io.EOF error: %v", err)
}
}

func TestErrorAs(t *testing.T) {
var netErr net.InvalidAddrError = "invalid addr"
err := NewError(ErrorNetwork, netErr)

var target net.InvalidAddrError
ok := errors.As(err, &target)
if !ok {
t.Error("Expected an InvalidAddrError")
}
}

// TestGetLDAPErrorSuccess tests parsing of a result with no error (resultCode == 0).
func TestGetLDAPErrorSuccess(t *testing.T) {
bindResponse := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindResponse, nil, "Bind Response")
Expand Down

0 comments on commit ba9c992

Please sign in to comment.