Home | History | Annotate | Download | only in user
      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 	"runtime"
      9 	"testing"
     10 )
     11 
     12 func checkUser(t *testing.T) {
     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 	// TODO(brainman): fix it once we know how.
     48 	if runtime.GOOS == "windows" {
     49 		t.Skip("skipping Gid and HomeDir comparisons")
     50 	}
     51 	if want.Gid != got.Gid {
     52 		t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
     53 	}
     54 	if want.HomeDir != got.HomeDir {
     55 		t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
     56 	}
     57 }
     58 
     59 func TestLookup(t *testing.T) {
     60 	checkUser(t)
     61 
     62 	if runtime.GOOS == "plan9" {
     63 		t.Skipf("Lookup not implemented on %q", runtime.GOOS)
     64 	}
     65 
     66 	want, err := Current()
     67 	if err != nil {
     68 		t.Fatalf("Current: %v", err)
     69 	}
     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)
     76 	}
     77 	compare(t, want, got)
     78 }
     79 
     80 func TestLookupId(t *testing.T) {
     81 	checkUser(t)
     82 
     83 	if runtime.GOOS == "plan9" {
     84 		t.Skipf("LookupId not implemented on %q", runtime.GOOS)
     85 	}
     86 
     87 	want, err := Current()
     88 	if err != nil {
     89 		t.Fatalf("Current: %v", err)
     90 	}
     91 	got, err := LookupId(want.Uid)
     92 	if err != nil {
     93 		t.Fatalf("LookupId: %v", err)
     94 	}
     95 	compare(t, want, got)
     96 }
     97 
     98 func checkGroup(t *testing.T) {
     99 	if !groupImplemented {
    100 		t.Skip("user: group not implemented; skipping test")
    101 	}
    102 }
    103 
    104 func TestLookupGroup(t *testing.T) {
    105 	checkGroup(t)
    106 	user, err := Current()
    107 	if err != nil {
    108 		t.Fatalf("Current(): %v", err)
    109 	}
    110 
    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
    118 	}
    119 	if g1.Gid != user.Gid {
    120 		t.Errorf("LookupGroupId(%q).Gid = %s; want %s", user.Gid, g1.Gid, user.Gid)
    121 	}
    122 
    123 	g2, err := LookupGroup(g1.Name)
    124 	if err != nil {
    125 		t.Fatalf("LookupGroup(%q): %v", g1.Name, err)
    126 	}
    127 	if g1.Gid != g2.Gid || g1.Name != g2.Name {
    128 		t.Errorf("LookupGroup(%q) = %+v; want %+v", g1.Name, g2, g1)
    129 	}
    130 }
    131 
    132 func TestGroupIds(t *testing.T) {
    133 	checkGroup(t)
    134 	if runtime.GOOS == "solaris" {
    135 		t.Skip("skipping GroupIds, see golang.org/issue/14709")
    136 	}
    137 	user, err := Current()
    138 	if err != nil {
    139 		t.Fatalf("Current(): %v", err)
    140 	}
    141 	gids, err := user.GroupIds()
    142 	if err != nil {
    143 		t.Fatalf("%+v.GroupIds(): %v", user, err)
    144 	}
    145 	if !containsID(gids, user.Gid) {
    146 		t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
    147 	}
    148 }
    149 
    150 func containsID(ids []string, id string) bool {
    151 	for _, x := range ids {
    152 		if x == id {
    153 			return true
    154 		}
    155 	}
    156 	return false
    157 }
    158