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.
12 func checkUser(t
*testing
.T
) {
14 t
.Skip("user: not implemented; skipping tests")
18 func TestCurrent(t
*testing
.T
) {
21 t
.Fatalf("Current: %v (got %#v)", err
, u
)
24 t
.Errorf("didn't get a HomeDir")
27 t
.Errorf("didn't get a username")
31 func BenchmarkCurrent(b
*testing
.B
) {
32 for i
:= 0; i
< b
.N
; i
++ {
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
)
41 if want
.Username
!= got
.Username
{
42 t
.Errorf("got Username=%q; want %q", got
.Username
, want
.Username
)
44 if want
.Name
!= got
.Name
{
45 t
.Errorf("got Name=%q; want %q", got
.Name
, want
.Name
)
47 // TODO(brainman): fix it once we know how.
48 if runtime
.GOOS
== "windows" {
49 t
.Skip("skipping Gid and HomeDir comparisons")
51 if want
.Gid
!= got
.Gid
{
52 t
.Errorf("got Gid=%q; want %q", got
.Gid
, want
.Gid
)
54 if want
.HomeDir
!= got
.HomeDir
{
55 t
.Errorf("got HomeDir=%q; want %q", got
.HomeDir
, want
.HomeDir
)
59 func TestLookup(t
*testing
.T
) {
62 if runtime
.GOOS
== "plan9" {
63 t
.Skipf("Lookup not implemented on %q", runtime
.GOOS
)
66 want
, err
:= Current()
68 t
.Fatalf("Current: %v", err
)
70 // TODO: Lookup() has a fast path that calls Current() and returns if the
71 // usernames match, so this test does not exercise very much. It would be
72 // good to try and test finding a different user than the current user.
73 got
, err
:= Lookup(want
.Username
)
75 t
.Fatalf("Lookup: %v", err
)
80 func TestLookupId(t
*testing
.T
) {
83 if runtime
.GOOS
== "plan9" {
84 t
.Skipf("LookupId not implemented on %q", runtime
.GOOS
)
87 want
, err
:= Current()
89 t
.Fatalf("Current: %v", err
)
91 got
, err
:= LookupId(want
.Uid
)
93 t
.Fatalf("LookupId: %v", err
)
98 func checkGroup(t
*testing
.T
) {
99 if !groupImplemented
{
100 t
.Skip("user: group not implemented; skipping test")
104 func TestLookupGroup(t
*testing
.T
) {
106 user
, err
:= Current()
108 t
.Fatalf("Current(): %v", err
)
111 g1
, err
:= LookupGroupId(user
.Gid
)
113 // NOTE(rsc): Maybe the group isn't defined. That's fine.
114 // On my OS X laptop, rsc logs in with group 5000 even
115 // though there's no name for group 5000. Such is Unix.
116 t
.Logf("LookupGroupId(%q): %v", user
.Gid
, err
)
119 if g1
.Gid
!= user
.Gid
{
120 t
.Errorf("LookupGroupId(%q).Gid = %s; want %s", user
.Gid
, g1
.Gid
, user
.Gid
)
123 g2
, err
:= LookupGroup(g1
.Name
)
125 t
.Fatalf("LookupGroup(%q): %v", g1
.Name
, err
)
127 if g1
.Gid
!= g2
.Gid || g1
.Name
!= g2
.Name
{
128 t
.Errorf("LookupGroup(%q) = %+v; want %+v", g1
.Name
, g2
, g1
)
132 func TestGroupIds(t
*testing
.T
) {
134 if runtime
.GOOS
== "solaris" {
135 t
.Skip("skipping GroupIds, see golang.org/issue/14709")
137 user
, err
:= Current()
139 t
.Fatalf("Current(): %v", err
)
141 gids
, err
:= user
.GroupIds()
143 t
.Fatalf("%+v.GroupIds(): %v", user
, err
)
145 if !containsID(gids
, user
.Gid
) {
146 t
.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user
, gids
, user
.Gid
)
150 func containsID(ids
[]string, id
string) bool {
151 for _
, x
:= range ids
{