Skip to content

Commit 63c350f

Browse files
Merge pull request #280 from DukeGatto/master
Fix ppolicy control response decoding - thanks for contributing!
2 parents 1e5af8f + a820a45 commit 63c350f

File tree

8 files changed

+220
-78
lines changed

8 files changed

+220
-78
lines changed

control.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -404,33 +404,26 @@ func DecodeControl(packet *ber.Packet) (Control, error) {
404404
if child.Tag == 0 {
405405
//Warning
406406
warningPacket := child.Children[0]
407-
packet, err := ber.DecodePacketErr(warningPacket.Data.Bytes())
407+
val, err := ber.ParseInt64(warningPacket.Data.Bytes())
408408
if err != nil {
409409
return nil, fmt.Errorf("failed to decode data bytes: %s", err)
410410
}
411-
val, ok := packet.Value.(int64)
412-
if ok {
413-
if warningPacket.Tag == 0 {
414-
//timeBeforeExpiration
415-
c.Expire = val
416-
warningPacket.Value = c.Expire
417-
} else if warningPacket.Tag == 1 {
418-
//graceAuthNsRemaining
419-
c.Grace = val
420-
warningPacket.Value = c.Grace
421-
}
411+
if warningPacket.Tag == 0 {
412+
//timeBeforeExpiration
413+
c.Expire = val
414+
warningPacket.Value = c.Expire
415+
} else if warningPacket.Tag == 1 {
416+
//graceAuthNsRemaining
417+
c.Grace = val
418+
warningPacket.Value = c.Grace
422419
}
423420
} else if child.Tag == 1 {
424421
// Error
425-
packet, err := ber.DecodePacketErr(child.Data.Bytes())
426-
if err != nil {
427-
return nil, fmt.Errorf("failed to decode data bytes: %s", err)
428-
}
429-
val, ok := packet.Value.(int8)
430-
if !ok {
431-
// what to do?
432-
val = -1
422+
bs := child.Data.Bytes()
423+
if len(bs) != 1 || bs[0] > 8 {
424+
return nil, fmt.Errorf("failed to decode data bytes: %s", "invalid PasswordPolicyResponse enum value")
433425
}
426+
val := int8(bs[0])
434427
c.Error = val
435428
child.Value = c.Error
436429
c.ErrorString = BeheraPasswordPolicyErrorMap[c.Error]

control_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,58 @@ func runAddControlDescriptions(t *testing.T, originalControl Control, childDescr
119119
t.Errorf("%sdescription not as expected: %s != %s", header, encodedPacket.Children[i].Description, desc)
120120
}
121121
}
122+
}
123+
124+
func TestDecodeControl(t *testing.T) {
125+
type args struct {
126+
packet *ber.Packet
127+
}
128+
129+
tests := []struct {
130+
name string
131+
args args
132+
want Control
133+
wantErr bool
134+
}{
135+
{name: "timeBeforeExpiration", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x29, 0x30, 0x27, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0xa, 0x30, 0x8, 0xa0, 0x6, 0x80, 0x4, 0x7f, 0xff, 0xf6, 0x5c})},
136+
want: &ControlBeheraPasswordPolicy{Expire: 2147481180, Grace: -1, Error: -1, ErrorString: ""}, wantErr: false},
137+
{name: "graceAuthNsRemaining", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x26, 0x30, 0x24, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x7, 0x30, 0x5, 0xa0, 0x3, 0x81, 0x1, 0x11})},
138+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: 17, Error: -1, ErrorString: ""}, wantErr: false},
139+
{name: "passwordExpired", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x0})},
140+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 0, ErrorString: "Password expired"}, wantErr: false},
141+
{name: "accountLocked", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x1})},
142+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 1, ErrorString: "Account locked"}, wantErr: false},
143+
{name: "passwordModNotAllowed", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x3})},
144+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 3, ErrorString: "Policy prevents password modification"}, wantErr: false},
145+
{name: "mustSupplyOldPassword", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x4})},
146+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 4, ErrorString: "Policy requires old password in order to change password"}, wantErr: false},
147+
{name: "insufficientPasswordQuality", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x5})},
148+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 5, ErrorString: "Password fails quality checks"}, wantErr: false},
149+
{name: "passwordTooShort", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x6})},
150+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 6, ErrorString: "Password is too short for policy"}, wantErr: false},
151+
{name: "passwordTooYoung", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x7})},
152+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 7, ErrorString: "Password has been changed too recently"}, wantErr: false},
153+
{name: "passwordInHistory", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x8})},
154+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 8, ErrorString: "New password is in list of old passwords"}, wantErr: false},
155+
}
156+
for i := range tests {
157+
err := addControlDescriptions(tests[i].args.packet)
158+
if err != nil {
159+
t.Fatal(err)
160+
}
161+
tests[i].args.packet = tests[i].args.packet.Children[0]
162+
}
122163

164+
for _, tt := range tests {
165+
t.Run(tt.name, func(t *testing.T) {
166+
got, err := DecodeControl(tt.args.packet)
167+
if (err != nil) != tt.wantErr {
168+
t.Errorf("DecodeControl() error = %v, wantErr %v", err, tt.wantErr)
169+
return
170+
}
171+
if !reflect.DeepEqual(got, tt.want) {
172+
t.Errorf("DecodeControl() got = %v, want %v", got, tt.want)
173+
}
174+
})
175+
}
123176
}

ldap.go

+13-19
Original file line numberDiff line numberDiff line change
@@ -223,32 +223,26 @@ func addControlDescriptions(packet *ber.Packet) error {
223223
if child.Tag == 0 {
224224
//Warning
225225
warningPacket := child.Children[0]
226-
packet, err := ber.DecodePacketErr(warningPacket.Data.Bytes())
226+
val, err := ber.ParseInt64(warningPacket.Data.Bytes())
227227
if err != nil {
228228
return fmt.Errorf("failed to decode data bytes: %s", err)
229229
}
230-
val, ok := packet.Value.(int64)
231-
if ok {
232-
if warningPacket.Tag == 0 {
233-
//timeBeforeExpiration
234-
value.Description += " (TimeBeforeExpiration)"
235-
warningPacket.Value = val
236-
} else if warningPacket.Tag == 1 {
237-
//graceAuthNsRemaining
238-
value.Description += " (GraceAuthNsRemaining)"
239-
warningPacket.Value = val
240-
}
230+
if warningPacket.Tag == 0 {
231+
//timeBeforeExpiration
232+
value.Description += " (TimeBeforeExpiration)"
233+
warningPacket.Value = val
234+
} else if warningPacket.Tag == 1 {
235+
//graceAuthNsRemaining
236+
value.Description += " (GraceAuthNsRemaining)"
237+
warningPacket.Value = val
241238
}
242239
} else if child.Tag == 1 {
243240
// Error
244-
packet, err := ber.DecodePacketErr(child.Data.Bytes())
245-
if err != nil {
246-
return fmt.Errorf("failed to decode data bytes: %s", err)
247-
}
248-
val, ok := packet.Value.(int8)
249-
if !ok {
250-
val = -1
241+
bs := child.Data.Bytes()
242+
if len(bs) != 1 || bs[0] > 8 {
243+
return fmt.Errorf("failed to decode data bytes: %s", "invalid PasswordPolicyResponse enum value")
251244
}
245+
val := int8(bs[0])
252246
child.Description = "Error"
253247
child.Value = val
254248
}

ldap_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package ldap
33
import (
44
"crypto/tls"
55
"testing"
6+
7+
ber "github.com/go-asn1-ber/asn1-ber"
68
)
79

810
const ldapServer = "ldap://ldap.itd.umich.edu:389"
@@ -284,3 +286,32 @@ func TestMatchDNError(t *testing.T) {
284286

285287
t.Log("TestMatchDNError:", err)
286288
}
289+
290+
func Test_addControlDescriptions(t *testing.T) {
291+
type args struct {
292+
packet *ber.Packet
293+
}
294+
tests := []struct {
295+
name string
296+
args args
297+
wantErr bool
298+
}{
299+
{name: "timeBeforeExpiration", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x29, 0x30, 0x27, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0xa, 0x30, 0x8, 0xa0, 0x6, 0x80, 0x4, 0x7f, 0xff, 0xf6, 0x5c})}, wantErr: false},
300+
{name: "graceAuthNsRemaining", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x26, 0x30, 0x24, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x7, 0x30, 0x5, 0xa0, 0x3, 0x81, 0x1, 0x11})}, wantErr: false},
301+
{name: "passwordExpired", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x0})}, wantErr: false},
302+
{name: "accountLocked", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x1})}, wantErr: false},
303+
{name: "passwordModNotAllowed", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x3})}, wantErr: false},
304+
{name: "mustSupplyOldPassword", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x4})}, wantErr: false},
305+
{name: "insufficientPasswordQuality", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x5})}, wantErr: false},
306+
{name: "passwordTooShort", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x6})}, wantErr: false},
307+
{name: "passwordTooYoung", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x7})}, wantErr: false},
308+
{name: "passwordInHistory", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x8})}, wantErr: false},
309+
}
310+
for _, tt := range tests {
311+
t.Run(tt.name, func(t *testing.T) {
312+
if err := addControlDescriptions(tt.args.packet); (err != nil) != tt.wantErr {
313+
t.Errorf("addControlDescriptions() error = %v, wantErr %v", err, tt.wantErr)
314+
}
315+
})
316+
}
317+
}

v3/control.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -404,33 +404,26 @@ func DecodeControl(packet *ber.Packet) (Control, error) {
404404
if child.Tag == 0 {
405405
//Warning
406406
warningPacket := child.Children[0]
407-
packet, err := ber.DecodePacketErr(warningPacket.Data.Bytes())
407+
val, err := ber.ParseInt64(warningPacket.Data.Bytes())
408408
if err != nil {
409409
return nil, fmt.Errorf("failed to decode data bytes: %s", err)
410410
}
411-
val, ok := packet.Value.(int64)
412-
if ok {
413-
if warningPacket.Tag == 0 {
414-
//timeBeforeExpiration
415-
c.Expire = val
416-
warningPacket.Value = c.Expire
417-
} else if warningPacket.Tag == 1 {
418-
//graceAuthNsRemaining
419-
c.Grace = val
420-
warningPacket.Value = c.Grace
421-
}
411+
if warningPacket.Tag == 0 {
412+
//timeBeforeExpiration
413+
c.Expire = val
414+
warningPacket.Value = c.Expire
415+
} else if warningPacket.Tag == 1 {
416+
//graceAuthNsRemaining
417+
c.Grace = val
418+
warningPacket.Value = c.Grace
422419
}
423420
} else if child.Tag == 1 {
424421
// Error
425-
packet, err := ber.DecodePacketErr(child.Data.Bytes())
426-
if err != nil {
427-
return nil, fmt.Errorf("failed to decode data bytes: %s", err)
428-
}
429-
val, ok := packet.Value.(int8)
430-
if !ok {
431-
// what to do?
432-
val = -1
422+
bs := child.Data.Bytes()
423+
if len(bs) != 1 || bs[0] > 8 {
424+
return nil, fmt.Errorf("failed to decode data bytes: %s", "invalid PasswordPolicyResponse enum value")
433425
}
426+
val := int8(bs[0])
434427
c.Error = val
435428
child.Value = c.Error
436429
c.ErrorString = BeheraPasswordPolicyErrorMap[c.Error]

v3/control_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,58 @@ func runAddControlDescriptions(t *testing.T, originalControl Control, childDescr
119119
t.Errorf("%sdescription not as expected: %s != %s", header, encodedPacket.Children[i].Description, desc)
120120
}
121121
}
122+
}
123+
124+
func TestDecodeControl(t *testing.T) {
125+
type args struct {
126+
packet *ber.Packet
127+
}
128+
129+
tests := []struct {
130+
name string
131+
args args
132+
want Control
133+
wantErr bool
134+
}{
135+
{name: "timeBeforeExpiration", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x29, 0x30, 0x27, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0xa, 0x30, 0x8, 0xa0, 0x6, 0x80, 0x4, 0x7f, 0xff, 0xf6, 0x5c})},
136+
want: &ControlBeheraPasswordPolicy{Expire: 2147481180, Grace: -1, Error: -1, ErrorString: ""}, wantErr: false},
137+
{name: "graceAuthNsRemaining", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x26, 0x30, 0x24, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x7, 0x30, 0x5, 0xa0, 0x3, 0x81, 0x1, 0x11})},
138+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: 17, Error: -1, ErrorString: ""}, wantErr: false},
139+
{name: "passwordExpired", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x0})},
140+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 0, ErrorString: "Password expired"}, wantErr: false},
141+
{name: "accountLocked", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x1})},
142+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 1, ErrorString: "Account locked"}, wantErr: false},
143+
{name: "passwordModNotAllowed", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x3})},
144+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 3, ErrorString: "Policy prevents password modification"}, wantErr: false},
145+
{name: "mustSupplyOldPassword", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x4})},
146+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 4, ErrorString: "Policy requires old password in order to change password"}, wantErr: false},
147+
{name: "insufficientPasswordQuality", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x5})},
148+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 5, ErrorString: "Password fails quality checks"}, wantErr: false},
149+
{name: "passwordTooShort", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x6})},
150+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 6, ErrorString: "Password is too short for policy"}, wantErr: false},
151+
{name: "passwordTooYoung", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x7})},
152+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 7, ErrorString: "Password has been changed too recently"}, wantErr: false},
153+
{name: "passwordInHistory", args: args{packet: ber.DecodePacket([]byte{0xa0, 0x24, 0x30, 0x22, 0x4, 0x19, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x32, 0x2e, 0x32, 0x2e, 0x32, 0x37, 0x2e, 0x38, 0x2e, 0x35, 0x2e, 0x31, 0x4, 0x5, 0x30, 0x3, 0x81, 0x1, 0x8})},
154+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 8, ErrorString: "New password is in list of old passwords"}, wantErr: false},
155+
}
156+
for i := range tests {
157+
err := addControlDescriptions(tests[i].args.packet)
158+
if err != nil {
159+
t.Fatal(err)
160+
}
161+
tests[i].args.packet = tests[i].args.packet.Children[0]
162+
}
122163

164+
for _, tt := range tests {
165+
t.Run(tt.name, func(t *testing.T) {
166+
got, err := DecodeControl(tt.args.packet)
167+
if (err != nil) != tt.wantErr {
168+
t.Errorf("DecodeControl() error = %v, wantErr %v", err, tt.wantErr)
169+
return
170+
}
171+
if !reflect.DeepEqual(got, tt.want) {
172+
t.Errorf("DecodeControl() got = %v, want %v", got, tt.want)
173+
}
174+
})
175+
}
123176
}

0 commit comments

Comments
 (0)