uid_test.go (1706B)
1 //go:build unix || windows 2 3 package diskfs 4 5 import ( 6 "os/user" 7 "strconv" 8 "testing" 9 ) 10 11 func TestIsGroupLeader(t *testing.T) { 12 usr, err := user.Current() 13 if err != nil { 14 t.Fatalf("get current user: %v", err) 15 } 16 grpids, err := usr.GroupIds() 17 if err != nil { 18 t.Fatalf("get groups: %v", err) 19 } 20 tests := []struct { 21 name string 22 uid string 23 group string 24 want bool 25 }{} 26 for _, id := range grpids { 27 grp, err := user.LookupGroupId(id) 28 if err != nil { 29 t.Fatalf("lookupgroupid: %v", err) 30 } 31 tests = append(tests, struct { 32 name string 33 uid string 34 group string 35 want bool 36 }{strconv.Itoa(len(tests)), usr.Username, grp.Name, usr.Username == grp.Name}) 37 } 38 for _, test := range tests { 39 got := isGroupLeader(test.group, test.uid) 40 if got != test.want { 41 t.Errorf("%s: isGroupLeader(%s, %s) = %t, want: %t", 42 test.name, test.group, test.uid, got, test.want) 43 } 44 } 45 } 46 47 func TestIsGroupMember(t *testing.T) { 48 usr, err := user.Current() 49 if err != nil { 50 t.Fatalf("get current user: %v", err) 51 } 52 grpids, err := usr.GroupIds() 53 if err != nil { 54 t.Fatalf("get groups: %v", err) 55 } 56 tests := []struct { 57 name string 58 uid string 59 group string 60 want bool 61 }{} 62 for _, id := range grpids { 63 grp, err := user.LookupGroupId(id) 64 if err != nil { 65 t.Fatalf("lookupgroupid: %v", err) 66 } 67 tests = append(tests, struct { 68 name string 69 uid string 70 group string 71 want bool 72 }{strconv.Itoa(len(tests)), usr.Username, grp.Name, true}) 73 } 74 for _, test := range tests { 75 got := isGroupMember(test.group, test.uid) 76 if got != test.want { 77 t.Errorf("%s: isGroupMember(%s, %s) = %t, want: %t", 78 test.name, test.group, test.uid, got, test.want) 79 } 80 } 81 }