libgo: update to go1.9
[official-gcc.git] / libgo / go / os / user / user_test.go
blobb3aeed883cdeb7fc471dea3233fafa6fcaa996a6
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.
5 package user
7 import (
8 "runtime"
9 "testing"
12 func checkUser(t *testing.T) {
13 if !userImplemented {
14 t.Skip("user: not implemented; skipping tests")
18 func TestCurrent(t *testing.T) {
19 u, err := Current()
20 if err != nil {
21 t.Fatalf("Current: %v (got %#v)", err, u)
23 if u.HomeDir == "" {
24 t.Errorf("didn't get a HomeDir")
26 if u.Username == "" {
27 t.Errorf("didn't get a username")
31 func BenchmarkCurrent(b *testing.B) {
32 for i := 0; i < b.N; i++ {
33 Current()
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) {
60 checkUser(t)
62 if runtime.GOOS == "plan9" {
63 t.Skipf("Lookup not implemented on %q", runtime.GOOS)
66 want, err := Current()
67 if err != nil {
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)
74 if err != nil {
75 t.Fatalf("Lookup: %v", err)
77 compare(t, want, got)
80 func TestLookupId(t *testing.T) {
81 checkUser(t)
83 if runtime.GOOS == "plan9" {
84 t.Skipf("LookupId not implemented on %q", runtime.GOOS)
87 want, err := Current()
88 if err != nil {
89 t.Fatalf("Current: %v", err)
91 got, err := LookupId(want.Uid)
92 if err != nil {
93 t.Fatalf("LookupId: %v", err)
95 compare(t, want, got)
98 func checkGroup(t *testing.T) {
99 if !groupImplemented {
100 t.Skip("user: group not implemented; skipping test")
104 func TestLookupGroup(t *testing.T) {
105 checkGroup(t)
106 user, err := Current()
107 if err != nil {
108 t.Fatalf("Current(): %v", err)
111 g1, err := LookupGroupId(user.Gid)
112 if err != nil {
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)
117 return
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)
124 if err != nil {
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) {
133 checkGroup(t)
134 if runtime.GOOS == "solaris" {
135 t.Skip("skipping GroupIds, see golang.org/issue/14709")
137 user, err := Current()
138 if err != nil {
139 t.Fatalf("Current(): %v", err)
141 gids, err := user.GroupIds()
142 if err != nil {
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 {
152 if x == id {
153 return true
156 return false