Skip to content

Commit 3cdc8a3

Browse files
johnweldoncpuschma
authored andcommitted
Linter cleanup (go-ldap#368)
1 parent 1d61b6a commit 3cdc8a3

12 files changed

+128
-88
lines changed

conn_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestUnresponsiveConnection(t *testing.T) {
4949
if !ok {
5050
t.Fatalf("no PacketResponse in response channel")
5151
}
52-
packet, err = packetResponse.ReadPacket()
52+
_, err = packetResponse.ReadPacket()
5353
if err == nil {
5454
t.Fatalf("expected timeout error")
5555
}

control_test.go

+41-21
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func runAddControlDescriptions(t *testing.T, originalControl Control, childDescr
117117
}
118118

119119
encodedControls := encodeControls([]Control{originalControl})
120-
addControlDescriptions(encodedControls)
120+
_ = addControlDescriptions(encodedControls)
121121
encodedPacket := encodedControls.Children[0]
122122
if len(encodedPacket.Children) != len(childDescriptions) {
123123
t.Errorf("%sinvalid number of children: %d != %d", header, len(encodedPacket.Children), len(childDescriptions))
@@ -140,26 +140,46 @@ func TestDecodeControl(t *testing.T) {
140140
want Control
141141
wantErr bool
142142
}{
143-
{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})},
144-
want: &ControlBeheraPasswordPolicy{Expire: 2147481180, Grace: -1, Error: -1, ErrorString: ""}, wantErr: false},
145-
{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})},
146-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: 17, Error: -1, ErrorString: ""}, wantErr: false},
147-
{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})},
148-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 0, ErrorString: "Password expired"}, wantErr: false},
149-
{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})},
150-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 1, ErrorString: "Account locked"}, wantErr: false},
151-
{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})},
152-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 3, ErrorString: "Policy prevents password modification"}, wantErr: false},
153-
{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})},
154-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 4, ErrorString: "Policy requires old password in order to change password"}, wantErr: false},
155-
{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})},
156-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 5, ErrorString: "Password fails quality checks"}, wantErr: false},
157-
{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})},
158-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 6, ErrorString: "Password is too short for policy"}, wantErr: false},
159-
{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})},
160-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 7, ErrorString: "Password has been changed too recently"}, wantErr: false},
161-
{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})},
162-
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 8, ErrorString: "New password is in list of old passwords"}, wantErr: false},
143+
{
144+
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})},
145+
want: &ControlBeheraPasswordPolicy{Expire: 2147481180, Grace: -1, Error: -1, ErrorString: ""}, wantErr: false,
146+
},
147+
{
148+
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})},
149+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: 17, Error: -1, ErrorString: ""}, wantErr: false,
150+
},
151+
{
152+
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})},
153+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 0, ErrorString: "Password expired"}, wantErr: false,
154+
},
155+
{
156+
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})},
157+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 1, ErrorString: "Account locked"}, wantErr: false,
158+
},
159+
{
160+
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})},
161+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 3, ErrorString: "Policy prevents password modification"}, wantErr: false,
162+
},
163+
{
164+
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})},
165+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 4, ErrorString: "Policy requires old password in order to change password"}, wantErr: false,
166+
},
167+
{
168+
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})},
169+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 5, ErrorString: "Password fails quality checks"}, wantErr: false,
170+
},
171+
{
172+
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})},
173+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 6, ErrorString: "Password is too short for policy"}, wantErr: false,
174+
},
175+
{
176+
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})},
177+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 7, ErrorString: "Password has been changed too recently"}, wantErr: false,
178+
},
179+
{
180+
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})},
181+
want: &ControlBeheraPasswordPolicy{Expire: -1, Grace: -1, Error: 8, ErrorString: "New password is in list of old passwords"}, wantErr: false,
182+
},
163183
}
164184
for i := range tests {
165185
err := addControlDescriptions(tests[i].args.packet)

filter.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func compileFilter(filter string, pos int) (*ber.Packet, int, error) {
396396

397397
case packet.Tag == FilterEqualityMatch && bytes.Equal(condition.Bytes(), _SymbolAny):
398398
packet = ber.NewString(ber.ClassContext, ber.TypePrimitive, FilterPresent, attribute.String(), FilterMap[FilterPresent])
399-
case packet.Tag == FilterEqualityMatch && bytes.Index(condition.Bytes(), _SymbolAny) > -1:
399+
case packet.Tag == FilterEqualityMatch && bytes.Contains(condition.Bytes(), _SymbolAny):
400400
packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attribute.String(), "Attribute"))
401401
packet.Tag = FilterSubstrings
402402
packet.Description = FilterMap[uint64(packet.Tag)]
@@ -438,7 +438,6 @@ func compileFilter(filter string, pos int) (*ber.Packet, int, error) {
438438

439439
// Convert from "ABC\xx\xx\xx" form to literal bytes for transport
440440
func decodeEscapedSymbols(src []byte) (string, error) {
441-
442441
var (
443442
buffer bytes.Buffer
444443
offset int

filter_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ func TestFilter(t *testing.T) {
214214
}
215215

216216
func TestDecodeEscapedSymbols(t *testing.T) {
217-
218217
for _, testInfo := range []struct {
219218
Src string
220219
Err string
@@ -271,7 +270,7 @@ func BenchmarkFilterCompile(b *testing.B) {
271270
maxIdx := len(filters)
272271
b.StartTimer()
273272
for i := 0; i < b.N; i++ {
274-
CompileFilter(filters[i%maxIdx])
273+
_, _ = CompileFilter(filters[i%maxIdx])
275274
}
276275
}
277276

@@ -287,6 +286,6 @@ func BenchmarkFilterDecompile(b *testing.B) {
287286
maxIdx := len(filters)
288287
b.StartTimer()
289288
for i := 0; i < b.N; i++ {
290-
DecompileFilter(filters[i%maxIdx])
289+
_, _ = DecompileFilter(filters[i%maxIdx])
291290
}
292291
}

ldap_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ import (
77
ber "github.com/go-asn1-ber/asn1-ber"
88
)
99

10-
const ldapServer = "ldap://ldap.itd.umich.edu:389"
11-
const ldapsServer = "ldaps://ldap.itd.umich.edu:636"
12-
const baseDN = "dc=umich,dc=edu"
10+
const (
11+
ldapServer = "ldap://ldap.itd.umich.edu:389"
12+
ldapsServer = "ldaps://ldap.itd.umich.edu:636"
13+
baseDN = "dc=umich,dc=edu"
14+
)
1315

1416
var filter = []string{
1517
"(cn=cis-fac)",
1618
"(&(owner=*)(cn=cis-fac))",
1719
"(&(objectclass=rfc822mailgroup)(cn=*Computer*))",
18-
"(&(objectclass=rfc822mailgroup)(cn=*Mathematics*))"}
20+
"(&(objectclass=rfc822mailgroup)(cn=*Mathematics*))",
21+
}
22+
1923
var attributes = []string{
2024
"cn",
21-
"description"}
25+
"description",
26+
}
2227

2328
func TestUnsecureDialURL(t *testing.T) {
2429
l, err := DialURL(ldapServer)
@@ -168,7 +173,7 @@ func TestSearchWithPaging(t *testing.T) {
168173
filter[2],
169174
attributes,
170175
[]Control{NewControlPaging(500)})
171-
sr, err = l.SearchWithPaging(searchRequest, 5)
176+
_, err = l.SearchWithPaging(searchRequest, 5)
172177
if err == nil {
173178
t.Fatal("expected an error when paging size in control in search request doesn't match size given in call, got none")
174179
}

search.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,9 @@ func (l *Conn) SearchWithPaging(searchRequest *SearchRequest, pagingSize uint32)
401401
return searchResult, NewError(ErrorNetwork, errors.New("ldap: packet not received"))
402402
}
403403

404-
for _, entry := range result.Entries {
405-
searchResult.Entries = append(searchResult.Entries, entry)
406-
}
407-
for _, referral := range result.Referrals {
408-
searchResult.Referrals = append(searchResult.Referrals, referral)
409-
}
410-
for _, control := range result.Controls {
411-
searchResult.Controls = append(searchResult.Controls, control)
412-
}
404+
searchResult.Entries = append(searchResult.Entries, result.Entries...)
405+
searchResult.Referrals = append(searchResult.Referrals, result.Referrals...)
406+
searchResult.Controls = append(searchResult.Controls, result.Controls...)
413407

414408
l.Debug.Printf("Looking for Paging Control...")
415409
pagingResult := FindControl(result.Controls, ControlTypePaging)
@@ -431,7 +425,9 @@ func (l *Conn) SearchWithPaging(searchRequest *SearchRequest, pagingSize uint32)
431425
if pagingControl != nil {
432426
l.Debug.Printf("Abandoning Paging...")
433427
pagingControl.PagingSize = 0
434-
l.Search(searchRequest)
428+
if _, err := l.Search(searchRequest); err != nil {
429+
return searchResult, err
430+
}
435431
}
436432

437433
return searchResult, nil
@@ -448,7 +444,8 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
448444
result := &SearchResult{
449445
Entries: make([]*Entry, 0),
450446
Referrals: make([]string, 0),
451-
Controls: make([]Control, 0)}
447+
Controls: make([]Control, 0),
448+
}
452449

453450
for {
454451
packet, err := l.readPacket(msgCtx)

v3/conn_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestUnresponsiveConnection(t *testing.T) {
4949
if !ok {
5050
t.Fatalf("no PacketResponse in response channel")
5151
}
52-
packet, err = packetResponse.ReadPacket()
52+
_, err = packetResponse.ReadPacket()
5353
if err == nil {
5454
t.Fatalf("expected timeout error")
5555
}

0 commit comments

Comments
 (0)