Skip to content

Commit

Permalink
fix panic in addControlDescriptions()
Browse files Browse the repository at this point in the history
* apply similar patch like in go-ldap#83, closes go-ldap#86
* add tests for adding ControlDescriptions
  • Loading branch information
vetinari committed Sep 24, 2016
1 parent 0e685d7 commit 29b8f79
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
39 changes: 39 additions & 0 deletions control_test.go
Expand Up @@ -56,3 +56,42 @@ func runControlTest(t *testing.T, originalControl Control) {
t.Errorf("%sgot different type decoding from encoded bytes: %T vs %T", header, fromBytes, originalControl)
}
}

func TestDescribeControlManageDsaIT(t *testing.T) {
runAddControlDescriptions(t, NewControlManageDsaIT(false), "Control Type (Manage DSA IT)")
runAddControlDescriptions(t, NewControlManageDsaIT(true), "Control Type (Manage DSA IT)", "Criticality")
}

func TestDescribeControlPaging(t *testing.T) {
runAddControlDescriptions(t, NewControlPaging(100), "Control Type (Paging)", "Control Value (Paging)")
runAddControlDescriptions(t, NewControlPaging(0), "Control Type (Paging)", "Control Value (Paging)")
}

func TestDescribeControlString(t *testing.T) {
runAddControlDescriptions(t, NewControlString("x", true, "y"), "Control Type ()", "Criticality", "Control Value")
runAddControlDescriptions(t, NewControlString("x", true, ""), "Control Type ()", "Criticality", "Control Value")
runAddControlDescriptions(t, NewControlString("x", false, "y"), "Control Type ()", "Control Value")
runAddControlDescriptions(t, NewControlString("x", false, ""), "Control Type ()", "Control Value")
}

func runAddControlDescriptions(t *testing.T, originalControl Control, childDescriptions ...string) {
header := ""
if callerpc, _, line, ok := runtime.Caller(1); ok {
if caller := runtime.FuncForPC(callerpc); caller != nil {
header = fmt.Sprintf("%s:%d: ", caller.Name(), line)
}
}

encodedControls := encodeControls([]Control{originalControl})
addControlDescriptions(encodedControls)
encodedPacket := encodedControls.Children[0]
if len(encodedPacket.Children) != len(childDescriptions) {
t.Errorf("%sinvalid number of children: %d != %d", header, len(encodedPacket.Children), len(childDescriptions))
}
for i, desc := range childDescriptions {
if encodedPacket.Children[i].Description != desc {
t.Errorf("%sdescription not as expected: %s != %s", header, encodedPacket.Children[i].Description, desc)
}
}

}
43 changes: 37 additions & 6 deletions ldap.go
Expand Up @@ -153,16 +153,47 @@ func addLDAPDescriptions(packet *ber.Packet) (err error) {
func addControlDescriptions(packet *ber.Packet) {
packet.Description = "Controls"
for _, child := range packet.Children {
var value *ber.Packet
controlType := ""
child.Description = "Control"
child.Children[0].Description = "Control Type (" + ControlTypeMap[child.Children[0].Value.(string)] + ")"
value := child.Children[1]
if len(child.Children) == 3 {
switch len(child.Children) {
case 0:
// at least one child is required for control type
continue

case 1:
// just type, no criticality or value
controlType = child.Children[0].Value.(string)
child.Children[0].Description = "Control Type (" + ControlTypeMap[controlType] + ")"

case 2:
controlType = child.Children[0].Value.(string)
child.Children[0].Description = "Control Type (" + ControlTypeMap[controlType] + ")"
// Children[1] could be criticality or value (both are optional)
// duck-type on whether this is a boolean
if _, ok := child.Children[1].Value.(bool); ok {
child.Children[1].Description = "Criticality"
} else {
child.Children[1].Description = "Control Value"
value = child.Children[1]
}

case 3:
// criticality and value present
controlType = child.Children[0].Value.(string)
child.Children[0].Description = "Control Type (" + ControlTypeMap[controlType] + ")"
child.Children[1].Description = "Criticality"
child.Children[2].Description = "Control Value"
value = child.Children[2]
}
value.Description = "Control Value"

switch child.Children[0].Value.(string) {
default:
// more than 3 children is invalid
continue
}
if value == nil {
continue
}
switch controlType {
case ControlTypePaging:
value.Description += " (Paging)"
if value.Value != nil {
Expand Down

0 comments on commit 29b8f79

Please sign in to comment.