| 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package user |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func checkUser(t *testing.T) { |
| 12 |
t.Helper() |
| 13 |
if !userImplemented { |
| 14 |
t.Skip("user: not implemented; skipping tests") |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
func TestCurrent(t *testing.T) { |
| 19 |
u, err := Current() |
| 20 |
if err != nil { |
| 21 |
t.Fatalf("Current: %v (got %#v)", err, u) |
| 22 |
} |
| 23 |
if u.HomeDir == "" { |
| 24 |
t.Errorf("didn't get a HomeDir") |
| 25 |
} |
| 26 |
if u.Username == "" { |
| 27 |
t.Errorf("didn't get a username") |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func BenchmarkCurrent(b *testing.B) { |
| 32 |
for i := 0; i < b.N; i++ { |
| 33 |
Current() |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func compare(t *testing.T, want, got *User) { |
| 38 |
if want.Uid != got.Uid { |
| 39 |
t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid) |
| 40 |
} |
| 41 |
if want.Username != got.Username { |
| 42 |
t.Errorf("got Username=%q; want %q", got.Username, want.Username) |
| 43 |
} |
| 44 |
if want.Name != got.Name { |
| 45 |
t.Errorf("got Name=%q; want %q", got.Name, want.Name) |
| 46 |
} |
| 47 |
if want.HomeDir != got.HomeDir { |
| 48 |
t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir) |
| 49 |
} |
| 50 |
if want.Gid != got.Gid { |
| 51 |
t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestLookup(t *testing.T) { |
| 56 |
checkUser(t) |
| 57 |
|
| 58 |
want, err := Current() |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("Current: %v", err) |
| 61 |
} |
| 62 |
// TODO: Lookup() has a fast path that calls Current() and returns if the |
| 63 |
// usernames match, so this test does not exercise very much. It would be |
| 64 |
// good to try and test finding a different user than the current user. |
| 65 |
got, err := Lookup(want.Username) |
| 66 |
if err != nil { |
| 67 |
t.Fatalf("Lookup: %v", err) |
| 68 |
} |
| 69 |
compare(t, want, got) |
| 70 |
} |
| 71 |
|
| 72 |
func TestLookupId(t *testing.T) { |
| 73 |
checkUser(t) |
| 74 |
|
| 75 |
want, err := Current() |
| 76 |
if err != nil { |
| 77 |
t.Fatalf("Current: %v", err) |
| 78 |
} |
| 79 |
got, err := LookupId(want.Uid) |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("LookupId: %v", err) |
| 82 |
} |
| 83 |
compare(t, want, got) |
| 84 |
} |
| 85 |
|
| 86 |
func checkGroup(t *testing.T) { |
| 87 |
t.Helper() |
| 88 |
if !groupImplemented { |
| 89 |
t.Skip("user: group not implemented; skipping test") |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestLookupGroup(t *testing.T) { |
| 94 |
checkGroup(t) |
| 95 |
user, err := Current() |
| 96 |
if err != nil { |
| 97 |
t.Fatalf("Current(): %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
g1, err := LookupGroupId(user.Gid) |
| 101 |
if err != nil { |
| 102 |
// NOTE(rsc): Maybe the group isn't defined. That's fine. |
| 103 |
// On my OS X laptop, rsc logs in with group 5000 even |
| 104 |
// though there's no name for group 5000. Such is Unix. |
| 105 |
t.Logf("LookupGroupId(%q): %v", user.Gid, err) |
| 106 |
return |
| 107 |
} |
| 108 |
if g1.Gid != user.Gid { |
| 109 |
t.Errorf("LookupGroupId(%q).Gid = %s; want %s", user.Gid, g1.Gid, user.Gid) |
| 110 |
} |
| 111 |
|
| 112 |
g2, err := LookupGroup(g1.Name) |
| 113 |
if err != nil { |
| 114 |
t.Fatalf("LookupGroup(%q): %v", g1.Name, err) |
| 115 |
} |
| 116 |
if g1.Gid != g2.Gid || g1.Name != g2.Name { |
| 117 |
t.Errorf("LookupGroup(%q) = %+v; want %+v", g1.Name, g2, g1) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func checkGroupList(t *testing.T) { |
| 122 |
t.Helper() |
| 123 |
if !groupListImplemented { |
| 124 |
t.Skip("user: group list not implemented; skipping test") |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestGroupIds(t *testing.T) { |
| 129 |
checkGroupList(t) |
| 130 |
user, err := Current() |
| 131 |
if err != nil { |
| 132 |
t.Fatalf("Current(): %v", err) |
| 133 |
} |
| 134 |
gids, err := user.GroupIds() |
| 135 |
if err != nil { |
| 136 |
t.Fatalf("%+v.GroupIds(): %v", user, err) |
| 137 |
} |
| 138 |
if !containsID(gids, user.Gid) { |
| 139 |
t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func containsID(ids []string, id string) bool { |
| 144 |
for _, x := range ids { |
| 145 |
if x == id { |
| 146 |
return true |
| 147 |
} |
| 148 |
} |
| 149 |
return false |
| 150 |
} |