Skip to content

Commit

Permalink
implement NewControlServerSideSorting to decode server side sorting r…
Browse files Browse the repository at this point in the history
…equest, add server side sorting type to ControlMap
  • Loading branch information
Vincent Morvan committed Feb 17, 2023
1 parent c9b94d6 commit 3b0317d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 9 deletions.
54 changes: 45 additions & 9 deletions v3/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ const (

// ControlTypeMap maps controls to text descriptions
var ControlTypeMap = map[string]string{
ControlTypePaging: "Paging",
ControlTypeBeheraPasswordPolicy: "Password Policy - Behera Draft",
ControlTypeManageDsaIT: "Manage DSA IT",
ControlTypeSubtreeDelete: "Subtree Delete Control",
ControlTypeMicrosoftNotification: "Change Notification - Microsoft",
ControlTypeMicrosoftShowDeleted: "Show Deleted Objects - Microsoft",
ControlTypeMicrosoftServerLinkTTL: "Return TTL-DNs for link values with associated expiry times - Microsoft",
ControlTypePaging: "Paging",
ControlTypeBeheraPasswordPolicy: "Password Policy - Behera Draft",
ControlTypeManageDsaIT: "Manage DSA IT",
ControlTypeSubtreeDelete: "Subtree Delete Control",
ControlTypeMicrosoftNotification: "Change Notification - Microsoft",
ControlTypeMicrosoftShowDeleted: "Show Deleted Objects - Microsoft",
ControlTypeMicrosoftServerLinkTTL: "Return TTL-DNs for link values with associated expiry times - Microsoft",
ControlTypeServerSideSorting: "Server Side Sorting Request - LDAP Control Extension for Server Side Sorting of Search Results (RFC2891)",
ControlTypeServerSideSortingResult: "Server Side Sorting Results - LDAP Control Extension for Server Side Sorting of Search Results (RFC2891)",
}

// Control defines an interface controls provide to encode and describe themselves
Expand Down Expand Up @@ -495,8 +497,10 @@ func DecodeControl(packet *ber.Packet) (Control, error) {
return NewControlMicrosoftServerLinkTTL(), nil
case ControlTypeSubtreeDelete:
return NewControlSubtreeDelete(), nil
case ControlTypeServerSideSorting:
return NewControlServerSideSorting(value)
case ControlTypeServerSideSortingResult:
return NewControlServerSideSortingResult(ber.DecodePacket(value.Data.Bytes()))
return NewControlServerSideSortingResult(value)
default:
c := new(ControlString)
c.ControlType = ControlType
Expand Down Expand Up @@ -579,7 +583,38 @@ func (c *ControlServerSideSorting) GetControlType() string {
return ControlTypeServerSideSorting
}

func NewControlServerSideSorting(sortKeys []*SortKey) *ControlServerSideSorting {
func NewControlServerSideSorting(value *ber.Packet) (*ControlServerSideSorting, error) {
sortKeys := []*SortKey{}

val := value.Children[1].Children

if len(val) != 1 {
return nil, fmt.Errorf("no sequence value in packet")
}

sequences := val[0].Children

for i, sequence := range sequences {
sortKey := &SortKey{}

if len(sequence.Children) < 2 {
return nil, fmt.Errorf("attributeType or matchingRule is missing from sequence %d", i)
}

sortKey.AttributeType = sequence.Children[0].Value.(string)
sortKey.MatchingRule = sequence.Children[1].Value.(string)

if len(sequence.Children) == 3 {
sortKey.Reverse = sequence.Children[2].Value.(bool)
}

sortKeys = append(sortKeys, sortKey)
}

return &ControlServerSideSorting{SortKeys: sortKeys}, nil
}

func NewControlServerSideSortingWithSortKeys(sortKeys []*SortKey) *ControlServerSideSorting {
return &ControlServerSideSorting{SortKeys: sortKeys}
}

Expand Down Expand Up @@ -658,6 +693,7 @@ var ControlServerSideSortingCodes = []ControlServerSideSortingCode{

type ControlServerSideSortingCode int64

// Valid test the code contained in the control against the ControlServerSideSortingCodes slice and return an error if the code is unknown.
func (c ControlServerSideSortingCode) Valid() error {
for _, validRet := range ControlServerSideSortingCodes {
if c == validRet {
Expand Down
53 changes: 53 additions & 0 deletions v3/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,56 @@ func TestDecodeControl(t *testing.T) {
})
}
}

func TestControlServerSideSortingDecoding(t *testing.T) {
control := NewControlServerSideSortingWithSortKeys([]*SortKey{{
MatchingRule: "foo",
AttributeType: "foobar",
Reverse: true,
}, {
MatchingRule: "foo",
AttributeType: "foobar",
Reverse: false,
}, {
MatchingRule: "",
AttributeType: "",
Reverse: false,
}, {
MatchingRule: "totoRule",
AttributeType: "",
Reverse: false,
}, {
MatchingRule: "",
AttributeType: "totoType",
Reverse: false,
}})

controlDecoded, err := NewControlServerSideSorting(control.Encode())
if err != nil {
t.Fatal(err)
}

if control.GetControlType() != controlDecoded.GetControlType() {
t.Fatalf("control type mismatch: control:%s - decoded:%s", control.GetControlType(), controlDecoded.GetControlType())
}

if len(control.SortKeys) != len(controlDecoded.SortKeys) {
t.Fatalf("sort keys length mismatch (control: %d - decoded: %d)", len(control.SortKeys), len(controlDecoded.SortKeys))
}

for i, sk := range control.SortKeys {
dsk := controlDecoded.SortKeys[i]

if sk.AttributeType != dsk.AttributeType {
t.Fatalf("attribute type mismatch for sortkey %d", i)
}

if sk.MatchingRule != dsk.MatchingRule {
t.Fatalf("matching rule mismatch for sortkey %d", i)
}

if sk.Reverse != dsk.Reverse {
t.Fatalf("reverse mismtach for sortkey %d", i)
}
}
}

0 comments on commit 3b0317d

Please sign in to comment.