Source file src/os/user/user_test.go

     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  	"os"
     9  	"testing"
    10  )
    11  
    12  var (
    13  	hasCgo  = false
    14  	hasUSER = os.Getenv("USER") != ""
    15  	hasHOME = os.Getenv("HOME") != ""
    16  )
    17  
    18  func checkUser(t *testing.T) {
    19  	t.Helper()
    20  	if !userImplemented {
    21  		t.Skip("user: not implemented; skipping tests")
    22  	}
    23  }
    24  
    25  func TestCurrent(t *testing.T) {
    26  	old := userBuffer
    27  	defer func() {
    28  		userBuffer = old
    29  	}()
    30  	userBuffer = 1 // force use of retry code
    31  	u, err := Current()
    32  	if err != nil {
    33  		if hasCgo || (hasUSER && hasHOME) {
    34  			t.Fatalf("Current: %v (got %#v)", err, u)
    35  		} else {
    36  			t.Skipf("skipping: %v", err)
    37  		}
    38  	}
    39  	if u.HomeDir == "" {
    40  		t.Errorf("didn't get a HomeDir")
    41  	}
    42  	if u.Username == "" {
    43  		t.Errorf("didn't get a username")
    44  	}
    45  }
    46  
    47  func BenchmarkCurrent(b *testing.B) {
    48  	// Benchmark current instead of Current because Current caches the result.
    49  	for i := 0; i < b.N; i++ {
    50  		current()
    51  	}
    52  }
    53  
    54  func compare(t *testing.T, want, got *User) {
    55  	if want.Uid != got.Uid {
    56  		t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
    57  	}
    58  	if want.Username != got.Username {
    59  		t.Errorf("got Username=%q; want %q", got.Username, want.Username)
    60  	}
    61  	if want.Name != got.Name {
    62  		t.Errorf("got Name=%q; want %q", got.Name, want.Name)
    63  	}
    64  	if want.HomeDir != got.HomeDir {
    65  		t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
    66  	}
    67  	if want.Gid != got.Gid {
    68  		t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
    69  	}
    70  }
    71  
    72  func TestLookup(t *testing.T) {
    73  	checkUser(t)
    74  
    75  	want, err := Current()
    76  	if err != nil {
    77  		if hasCgo || (hasUSER && hasHOME) {
    78  			t.Fatalf("Current: %v", err)
    79  		} else {
    80  			t.Skipf("skipping: %v", err)
    81  		}
    82  	}
    83  
    84  	// TODO: Lookup() has a fast path that calls Current() and returns if the
    85  	// usernames match, so this test does not exercise very much. It would be
    86  	// good to try and test finding a different user than the current user.
    87  	got, err := Lookup(want.Username)
    88  	if err != nil {
    89  		t.Fatalf("Lookup: %v", err)
    90  	}
    91  	compare(t, want, got)
    92  }
    93  
    94  func TestLookupId(t *testing.T) {
    95  	checkUser(t)
    96  
    97  	want, err := Current()
    98  	if err != nil {
    99  		if hasCgo || (hasUSER && hasHOME) {
   100  			t.Fatalf("Current: %v", err)
   101  		} else {
   102  			t.Skipf("skipping: %v", err)
   103  		}
   104  	}
   105  
   106  	got, err := LookupId(want.Uid)
   107  	if err != nil {
   108  		t.Fatalf("LookupId: %v", err)
   109  	}
   110  	compare(t, want, got)
   111  }
   112  
   113  func checkGroup(t *testing.T) {
   114  	t.Helper()
   115  	if !groupImplemented {
   116  		t.Skip("user: group not implemented; skipping test")
   117  	}
   118  }
   119  
   120  func TestLookupGroup(t *testing.T) {
   121  	old := groupBuffer
   122  	defer func() {
   123  		groupBuffer = old
   124  	}()
   125  	groupBuffer = 1 // force use of retry code
   126  	checkGroup(t)
   127  
   128  	user, err := Current()
   129  	if err != nil {
   130  		if hasCgo || (hasUSER && hasHOME) {
   131  			t.Fatalf("Current: %v", err)
   132  		} else {
   133  			t.Skipf("skipping: %v", err)
   134  		}
   135  	}
   136  
   137  	g1, err := LookupGroupId(user.Gid)
   138  	if err != nil {
   139  		// NOTE(rsc): Maybe the group isn't defined. That's fine.
   140  		// On my OS X laptop, rsc logs in with group 5000 even
   141  		// though there's no name for group 5000. Such is Unix.
   142  		t.Logf("LookupGroupId(%q): %v", user.Gid, err)
   143  		return
   144  	}
   145  	if g1.Gid != user.Gid {
   146  		t.Errorf("LookupGroupId(%q).Gid = %s; want %s", user.Gid, g1.Gid, user.Gid)
   147  	}
   148  
   149  	g2, err := LookupGroup(g1.Name)
   150  	if err != nil {
   151  		t.Fatalf("LookupGroup(%q): %v", g1.Name, err)
   152  	}
   153  	if g1.Gid != g2.Gid || g1.Name != g2.Name {
   154  		t.Errorf("LookupGroup(%q) = %+v; want %+v", g1.Name, g2, g1)
   155  	}
   156  }
   157  
   158  func checkGroupList(t *testing.T) {
   159  	t.Helper()
   160  	if !groupListImplemented {
   161  		t.Skip("user: group list not implemented; skipping test")
   162  	}
   163  }
   164  
   165  func TestGroupIds(t *testing.T) {
   166  	checkGroupList(t)
   167  
   168  	user, err := Current()
   169  	if err != nil {
   170  		if hasCgo || (hasUSER && hasHOME) {
   171  			t.Fatalf("Current: %v", err)
   172  		} else {
   173  			t.Skipf("skipping: %v", err)
   174  		}
   175  	}
   176  
   177  	gids, err := user.GroupIds()
   178  	if err != nil {
   179  		t.Fatalf("%+v.GroupIds(): %v", user, err)
   180  	}
   181  	if !containsID(gids, user.Gid) {
   182  		t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
   183  	}
   184  }
   185  
   186  func containsID(ids []string, id string) bool {
   187  	for _, x := range ids {
   188  		if x == id {
   189  			return true
   190  		}
   191  	}
   192  	return false
   193  }
   194  

View as plain text