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 	if runtime.GOOS == "android" {
     20 		t.Skipf("skipping on %s", runtime.GOOS)
     21 	}
     22 	u, err := Current()
     23 	if err != nil {
     24 		t.Fatalf("Current: %v (got %#v)", err, u)
     25 	}
     26 	if u.HomeDir == "" {
     27 		t.Errorf("didn't get a HomeDir")
     28 	}
     29 	if u.Username == "" {
     30 		t.Errorf("didn't get a username")
     31 	}
     32 }
     33 
     34 func compare(t *testing.T, want, got *User) {
     35 	if want.Uid != got.Uid {
     36 		t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
     37 	}
     38 	if want.Username != got.Username {
     39 		t.Errorf("got Username=%q; want %q", got.Username, want.Username)
     40 	}
     41 	if want.Name != got.Name {
     42 		t.Errorf("got Name=%q; want %q", got.Name, want.Name)
     43 	}
     44 	// TODO(brainman): fix it once we know how.
     45 	if runtime.GOOS == "windows" {
     46 		t.Skip("skipping Gid and HomeDir comparisons")
     47 	}
     48 	if want.Gid != got.Gid {
     49 		t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
     50 	}
     51 	if want.HomeDir != got.HomeDir {
     52 		t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
     53 	}
     54 }
     55 
     56 func TestLookup(t *testing.T) {
     57 	checkUser(t)
     58 
     59 	if runtime.GOOS == "plan9" {
     60 		t.Skipf("Lookup not implemented on %q", runtime.GOOS)
     61 	}
     62 
     63 	want, err := Current()
     64 	if err != nil {
     65 		t.Fatalf("Current: %v", err)
     66 	}
     67 	got, err := Lookup(want.Username)
     68 	if err != nil {
     69 		t.Fatalf("Lookup: %v", err)
     70 	}
     71 	compare(t, want, got)
     72 }
     73 
     74 func TestLookupId(t *testing.T) {
     75 	checkUser(t)
     76 
     77 	if runtime.GOOS == "plan9" {
     78 		t.Skipf("LookupId not implemented on %q", runtime.GOOS)
     79 	}
     80 
     81 	want, err := Current()
     82 	if err != nil {
     83 		t.Fatalf("Current: %v", err)
     84 	}
     85 	got, err := LookupId(want.Uid)
     86 	if err != nil {
     87 		t.Fatalf("LookupId: %v", err)
     88 	}
     89 	compare(t, want, got)
     90 }
     91 
     92 func checkGroup(t *testing.T) {
     93 	if !groupImplemented {
     94 		t.Skip("user: group not implemented; skipping test")
     95 	}
     96 }
     97 
     98 func TestLookupGroup(t *testing.T) {
     99 	checkGroup(t)
    100 	user, err := Current()
    101 	if err != nil {
    102 		t.Fatalf("Current(): %v", err)
    103 	}
    104 
    105 	g1, err := LookupGroupId(user.Gid)
    106 	if err != nil {
    107 		// NOTE(rsc): Maybe the group isn't defined. That's fine.
    108 		// On my OS X laptop, rsc logs in with group 5000 even
    109 		// though there's no name for group 5000. Such is Unix.
    110 		t.Logf("LookupGroupId(%q): %v", user.Gid, err)
    111 		return
    112 	}
    113 	if g1.Gid != user.Gid {
    114 		t.Errorf("LookupGroupId(%q).Gid = %s; want %s", user.Gid, g1.Gid, user.Gid)
    115 	}
    116 
    117 	g2, err := LookupGroup(g1.Name)
    118 	if err != nil {
    119 		t.Fatalf("LookupGroup(%q): %v", g1.Name, err)
    120 	}
    121 	if g1.Gid != g2.Gid || g1.Name != g2.Name {
    122 		t.Errorf("LookupGroup(%q) = %+v; want %+v", g1.Name, g2, g1)
    123 	}
    124 }
    125 
    126 func TestGroupIds(t *testing.T) {
    127 	checkGroup(t)
    128 	if runtime.GOOS == "solaris" {
    129 		t.Skip("skipping GroupIds, see golang.org/issue/14709")
    130 	}
    131 	user, err := Current()
    132 	if err != nil {
    133 		t.Fatalf("Current(): %v", err)
    134 	}
    135 	gids, err := user.GroupIds()
    136 	if err != nil {
    137 		t.Fatalf("%+v.GroupIds(): %v", user, err)
    138 	}
    139 	if !containsID(gids, user.Gid) {
    140 		t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
    141 	}
    142 }
    143 
    144 func containsID(ids []string, id string) bool {
    145 	for _, x := range ids {
    146 		if x == id {
    147 			return true
    148 		}
    149 	}
    150 	return false
    151 }
    152