Skip to content

Commit c873359

Browse files
authored
Merge pull request #226 from thaJeztah/user_test_cleanups
user: test cleanups
2 parents f41a5ef + 5c2e8a0 commit c873359

1 file changed

Lines changed: 131 additions & 85 deletions

File tree

user/user_test.go

Lines changed: 131 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
)
1212

13-
func TestUserParseLine(t *testing.T) {
13+
func TestParseLine(t *testing.T) {
1414
var (
1515
a, b string
1616
c []string
@@ -58,7 +58,7 @@ func TestUserParseLine(t *testing.T) {
5858
}
5959
}
6060

61-
func TestUserParsePasswd(t *testing.T) {
61+
func TestParsePasswdFilter(t *testing.T) {
6262
users, err := ParsePasswdFilter(strings.NewReader(`
6363
root:x:0:0:root:/root:/bin/bash
6464
adm:x:3:4:adm:/var/adm:/bin/false
@@ -78,7 +78,7 @@ this is just some garbage data
7878
}
7979
}
8080

81-
func TestUserParseGroup(t *testing.T) {
81+
func TestParseGroupFilter(t *testing.T) {
8282
groups, err := ParseGroupFilter(strings.NewReader(`
8383
root:x:0:root
8484
adm:x:4:root,adm,daemon
@@ -98,12 +98,14 @@ this is just some garbage data
9898
}
9999
}
100100

101-
func TestValidGetExecUser(t *testing.T) {
101+
func TestGetExecUser(t *testing.T) {
102102
const passwdContent = `
103103
root:x:0:0:root user:/root:/bin/bash
104104
adm:x:42:43:adm:/var/adm:/bin/false
105105
111:x:222:333::/var/garbage
106106
odd:x:111:112::/home/odd:::::
107+
2147483647:x:0:0:maxint32:/root:/bin/bash
108+
2147483648:x:0:0:toolarge:/root:/bin/bash
107109
user7456:x:7456:100:Vasya:/home/user7456
108110
this is just some garbage data
109111
`
@@ -113,6 +115,8 @@ adm:x:43:
113115
grp:x:1234:root,adm,user7456
114116
444:x:555:111
115117
odd:x:444:
118+
2147483647:x:1235:
119+
2147483648:x:1236:
116120
this is just some garbage data
117121
` + largeGroup()
118122

@@ -229,40 +233,74 @@ this is just some garbage data
229233
Home: "/home/user7456",
230234
},
231235
},
236+
{
237+
ref: "7456:2147483647",
238+
expected: ExecUser{
239+
Uid: 7456,
240+
Gid: 2147483647, // maxID
241+
Sgids: defaultExecUser.Sgids,
242+
Home: "/home/user7456",
243+
},
244+
},
245+
{
246+
ref: "2147483647:43",
247+
expected: ExecUser{
248+
Uid: 2147483647, // maxID
249+
Gid: 43,
250+
Sgids: defaultExecUser.Sgids,
251+
Home: defaultExecUser.Home,
252+
},
253+
},
254+
{
255+
ref: "2147483647",
256+
expected: ExecUser{
257+
Uid: 2147483647, // maxID
258+
Gid: defaultExecUser.Gid,
259+
Sgids: defaultExecUser.Sgids,
260+
Home: defaultExecUser.Home,
261+
},
262+
},
232263
}
233264

234-
for _, test := range tests {
235-
passwd := strings.NewReader(passwdContent)
236-
group := strings.NewReader(groupContent)
237-
238-
execUser, err := GetExecUser(test.ref, &defaultExecUser, passwd, group)
239-
if err != nil {
240-
t.Logf("got unexpected error when parsing '%s': %s", test.ref, err.Error())
241-
t.Fail()
242-
continue
243-
}
244-
245-
if !reflect.DeepEqual(test.expected, *execUser) {
246-
t.Logf("ref: %v", test.ref)
247-
t.Logf("got: %#v", execUser)
248-
t.Logf("expected: %#v", test.expected)
249-
t.Fail()
250-
continue
265+
for _, tc := range tests {
266+
name := tc.ref
267+
if name == "" {
268+
name = "<empty>"
251269
}
270+
t.Run(name, func(t *testing.T) {
271+
passwd := strings.NewReader(passwdContent)
272+
group := strings.NewReader(groupContent)
273+
274+
execUser, err := GetExecUser(tc.ref, &defaultExecUser, passwd, group)
275+
if err != nil {
276+
t.Fatalf("got unexpected error when parsing '%s': %s", tc.ref, err.Error())
277+
}
278+
279+
if !reflect.DeepEqual(tc.expected, *execUser) {
280+
t.Logf("ref: %v", tc.ref)
281+
t.Logf("got: %#v", execUser)
282+
t.Logf("expected: %#v", tc.expected)
283+
t.Fail()
284+
}
285+
})
252286
}
253287
}
254288

255-
func TestInvalidGetExecUser(t *testing.T) {
289+
func TestGetExecUserInvalid(t *testing.T) {
256290
const passwdContent = `
257291
root:x:0:0:root user:/root:/bin/bash
258292
adm:x:42:43:adm:/var/adm:/bin/false
259293
-42:x:12:13:broken:/very/broken
294+
2147483647:x:0:0:maxint32:/root:/bin/bash
295+
2147483648:x:0:0:toolarge:/root:/bin/bash
260296
this is just some garbage data
261297
`
262298
const groupContent = `
263299
root:x:0:root
264300
adm:x:43:
265301
grp:x:1234:root,adm
302+
2147483647:x:1235:
303+
2147483648:x:1236:
266304
this is just some garbage data
267305
`
268306

@@ -281,18 +319,21 @@ this is just some garbage data
281319
"-5:-2",
282320
"-42",
283321
"-43",
322+
"42:2147483648", // maxID + 1
323+
"2147483648:43", // maxID + 1
324+
"2147483648", // maxID + 1
284325
}
285326

286-
for _, test := range tests {
287-
passwd := strings.NewReader(passwdContent)
288-
group := strings.NewReader(groupContent)
327+
for _, tc := range tests {
328+
t.Run(tc, func(t *testing.T) {
329+
passwd := strings.NewReader(passwdContent)
330+
group := strings.NewReader(groupContent)
289331

290-
execUser, err := GetExecUser(test, nil, passwd, group)
291-
if err == nil {
292-
t.Logf("got unexpected success when parsing '%s': %#v", test, execUser)
293-
t.Fail()
294-
continue
295-
}
332+
execUser, err := GetExecUser(tc, nil, passwd, group)
333+
if err == nil {
334+
t.Fatalf("got unexpected success when parsing '%s': %#v", tc, execUser)
335+
}
336+
})
296337
}
297338
}
298339

@@ -367,30 +408,33 @@ this is just some garbage data
367408
},
368409
}
369410

370-
for _, test := range tests {
371-
var passwd, group io.Reader
372-
373-
if test.passwd {
374-
passwd = strings.NewReader(passwdContent)
375-
}
376-
377-
if test.group {
378-
group = strings.NewReader(groupContent)
379-
}
380-
381-
execUser, err := GetExecUser(test.ref, &defaultExecUser, passwd, group)
382-
if err != nil {
383-
t.Logf("got unexpected error when parsing '%s': %s", test.ref, err.Error())
384-
t.Fail()
385-
continue
386-
}
387-
388-
if !reflect.DeepEqual(test.expected, *execUser) {
389-
t.Logf("got: %#v", execUser)
390-
t.Logf("expected: %#v", test.expected)
391-
t.Fail()
392-
continue
411+
for _, tc := range tests {
412+
name := tc.ref
413+
if name == "" {
414+
name = "<empty>"
393415
}
416+
t.Run(name, func(t *testing.T) {
417+
var passwd, group io.Reader
418+
419+
if tc.passwd {
420+
passwd = strings.NewReader(passwdContent)
421+
}
422+
423+
if tc.group {
424+
group = strings.NewReader(groupContent)
425+
}
426+
427+
execUser, err := GetExecUser(tc.ref, &defaultExecUser, passwd, group)
428+
if err != nil {
429+
t.Fatalf("got unexpected error when parsing '%s': %s", tc.ref, err.Error())
430+
}
431+
432+
if !reflect.DeepEqual(tc.expected, *execUser) {
433+
t.Logf("got: %#v", execUser)
434+
t.Logf("expected: %#v", tc.expected)
435+
t.Fail()
436+
}
437+
})
394438
}
395439
}
396440

@@ -464,22 +508,23 @@ this is just some garbage data
464508
},
465509
}
466510

467-
for _, test := range tests {
468-
group := strings.NewReader(groupContent)
469-
470-
gids, err := GetAdditionalGroups(test.groups, group)
471-
if test.hasError && err == nil {
472-
t.Errorf("Parse(%#v) expects error but has none", test)
473-
continue
474-
}
475-
if !test.hasError && err != nil {
476-
t.Errorf("Parse(%#v) has error %v", test, err)
477-
continue
478-
}
479-
sort.Ints(gids)
480-
if !reflect.DeepEqual(gids, test.expected) {
481-
t.Errorf("Gids(%v), expect %v from groups %v", gids, test.expected, test.groups)
482-
}
511+
for _, tc := range tests {
512+
name := strings.Join(tc.groups, ",")
513+
t.Run(name, func(t *testing.T) {
514+
group := strings.NewReader(groupContent)
515+
516+
gids, err := GetAdditionalGroups(tc.groups, group)
517+
if tc.hasError && err == nil {
518+
t.Fatalf("Parse(%#v) expects error but has none", tc)
519+
}
520+
if !tc.hasError && err != nil {
521+
t.Fatalf("Parse(%#v) has error %v", tc, err)
522+
}
523+
sort.Ints(gids)
524+
if !reflect.DeepEqual(gids, tc.expected) {
525+
t.Errorf("Gids(%v), expect %v from groups %v", gids, tc.expected, tc.groups)
526+
}
527+
})
483528
}
484529
}
485530

@@ -502,20 +547,21 @@ func TestGetAdditionalGroupsNumeric(t *testing.T) {
502547
},
503548
}
504549

505-
for _, test := range tests {
506-
gids, err := GetAdditionalGroups(test.groups, nil)
507-
if test.hasError && err == nil {
508-
t.Errorf("Parse(%#v) expects error but has none", test)
509-
continue
510-
}
511-
if !test.hasError && err != nil {
512-
t.Errorf("Parse(%#v) has error %v", test, err)
513-
continue
514-
}
515-
sort.Ints(gids)
516-
if !reflect.DeepEqual(gids, test.expected) {
517-
t.Errorf("Gids(%v), expect %v from groups %v", gids, test.expected, test.groups)
518-
}
550+
for _, tc := range tests {
551+
name := strings.Join(tc.groups, ",")
552+
t.Run(name, func(t *testing.T) {
553+
gids, err := GetAdditionalGroups(tc.groups, nil)
554+
if tc.hasError && err == nil {
555+
t.Fatalf("Parse(%#v) expects error but has none", tc)
556+
}
557+
if !tc.hasError && err != nil {
558+
t.Fatalf("Parse(%#v) has error %v", tc, err)
559+
}
560+
sort.Ints(gids)
561+
if !reflect.DeepEqual(gids, tc.expected) {
562+
t.Errorf("Gids(%v), expect %v from groups %v", gids, tc.expected, tc.groups)
563+
}
564+
})
519565
}
520566
}
521567

@@ -524,7 +570,7 @@ func largeGroup() (res string) {
524570
var b strings.Builder
525571
b.WriteString("largegroup:x:1000:user1")
526572
for i := 2; i <= 7500; i++ {
527-
fmt.Fprintf(&b, ",user%d", i)
573+
_, _ = fmt.Fprintf(&b, ",user%d", i)
528574
}
529575
return b.String()
530576
}

0 commit comments

Comments
 (0)