Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/user/user_test.go

Documentation: os/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  	t.Helper()
    14  	if !userImplemented {
    15  		t.Skip("user: not implemented; skipping tests")
    16  	}
    17  }
    18  
    19  func TestCurrent(t *testing.T) {
    20  	u, err := Current()
    21  	if err != nil {
    22  		t.Fatalf("Current: %v (got %#v)", err, u)
    23  	}
    24  	if u.HomeDir == "" {
    25  		t.Errorf("didn't get a HomeDir")
    26  	}
    27  	if u.Username == "" {
    28  		t.Errorf("didn't get a username")
    29  	}
    30  }
    31  
    32  func BenchmarkCurrent(b *testing.B) {
    33  	for i := 0; i < b.N; i++ {
    34  		Current()
    35  	}
    36  }
    37  
    38  func compare(t *testing.T, want, got *User) {
    39  	if want.Uid != got.Uid {
    40  		t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
    41  	}
    42  	if want.Username != got.Username {
    43  		t.Errorf("got Username=%q; want %q", got.Username, want.Username)
    44  	}
    45  	if want.Name != got.Name {
    46  		t.Errorf("got Name=%q; want %q", got.Name, want.Name)
    47  	}
    48  	if want.HomeDir != got.HomeDir {
    49  		t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
    50  	}
    51  	if want.Gid != got.Gid {
    52  		t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
    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  	// TODO: Lookup() has a fast path that calls Current() and returns if the
    68  	// usernames match, so this test does not exercise very much. It would be
    69  	// good to try and test finding a different user than the current user.
    70  	got, err := Lookup(want.Username)
    71  	if err != nil {
    72  		t.Fatalf("Lookup: %v", err)
    73  	}
    74  	compare(t, want, got)
    75  }
    76  
    77  func TestLookupId(t *testing.T) {
    78  	checkUser(t)
    79  
    80  	if runtime.GOOS == "plan9" {
    81  		t.Skipf("LookupId not implemented on %q", runtime.GOOS)
    82  	}
    83  
    84  	want, err := Current()
    85  	if err != nil {
    86  		t.Fatalf("Current: %v", err)
    87  	}
    88  	got, err := LookupId(want.Uid)
    89  	if err != nil {
    90  		t.Fatalf("LookupId: %v", err)
    91  	}
    92  	compare(t, want, got)
    93  }
    94  
    95  func checkGroup(t *testing.T) {
    96  	t.Helper()
    97  	if !groupImplemented {
    98  		t.Skip("user: group not implemented; skipping test")
    99  	}
   100  }
   101  
   102  func TestLookupGroup(t *testing.T) {
   103  	checkGroup(t)
   104  	user, err := Current()
   105  	if err != nil {
   106  		t.Fatalf("Current(): %v", err)
   107  	}
   108  
   109  	g1, err := LookupGroupId(user.Gid)
   110  	if err != nil {
   111  		// NOTE(rsc): Maybe the group isn't defined. That's fine.
   112  		// On my OS X laptop, rsc logs in with group 5000 even
   113  		// though there's no name for group 5000. Such is Unix.
   114  		t.Logf("LookupGroupId(%q): %v", user.Gid, err)
   115  		return
   116  	}
   117  	if g1.Gid != user.Gid {
   118  		t.Errorf("LookupGroupId(%q).Gid = %s; want %s", user.Gid, g1.Gid, user.Gid)
   119  	}
   120  
   121  	g2, err := LookupGroup(g1.Name)
   122  	if err != nil {
   123  		t.Fatalf("LookupGroup(%q): %v", g1.Name, err)
   124  	}
   125  	if g1.Gid != g2.Gid || g1.Name != g2.Name {
   126  		t.Errorf("LookupGroup(%q) = %+v; want %+v", g1.Name, g2, g1)
   127  	}
   128  }
   129  
   130  func TestGroupIds(t *testing.T) {
   131  	checkGroup(t)
   132  	if runtime.GOOS == "aix" {
   133  		t.Skip("skipping GroupIds, see golang.org/issue/30563")
   134  	}
   135  	if runtime.GOOS == "illumos" {
   136  		t.Skip("skipping GroupIds, see golang.org/issue/14709")
   137  	}
   138  	user, err := Current()
   139  	if err != nil {
   140  		t.Fatalf("Current(): %v", err)
   141  	}
   142  	gids, err := user.GroupIds()
   143  	if err != nil {
   144  		t.Fatalf("%+v.GroupIds(): %v", user, err)
   145  	}
   146  	if !containsID(gids, user.Gid) {
   147  		t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
   148  	}
   149  }
   150  
   151  func containsID(ids []string, id string) bool {
   152  	for _, x := range ids {
   153  		if x == id {
   154  			return true
   155  		}
   156  	}
   157  	return false
   158  }
   159  

View as plain text