...
Run Format

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  	"internal/testenv"
     9  	"os"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  func checkUser(t *testing.T) {
    15  	if !userImplemented {
    16  		t.Skip("user: not implemented; skipping tests")
    17  	}
    18  }
    19  
    20  func TestCurrent(t *testing.T) {
    21  	// The Go builders (in particular the ones using containers)
    22  	// often have minimal environments without $HOME or $USER set,
    23  	// which breaks Current which relies on those working as a
    24  	// fallback.
    25  	// TODO: we should fix that (Issue 24884) and remove these
    26  	// workarounds.
    27  	if testenv.Builder() != "" && runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
    28  		if os.Getenv("HOME") == "" {
    29  			os.Setenv("HOME", "/tmp")
    30  		}
    31  		if os.Getenv("USER") == "" {
    32  			os.Setenv("USER", "gobuilder")
    33  		}
    34  	}
    35  	u, err := Current()
    36  	if err != nil {
    37  		t.Fatalf("Current: %v (got %#v)", err, u)
    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  	for i := 0; i < b.N; i++ {
    49  		Current()
    50  	}
    51  }
    52  
    53  func compare(t *testing.T, want, got *User) {
    54  	if want.Uid != got.Uid {
    55  		t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
    56  	}
    57  	if want.Username != got.Username {
    58  		t.Errorf("got Username=%q; want %q", got.Username, want.Username)
    59  	}
    60  	if want.Name != got.Name {
    61  		t.Errorf("got Name=%q; want %q", got.Name, want.Name)
    62  	}
    63  	if want.HomeDir != got.HomeDir {
    64  		t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
    65  	}
    66  	if want.Gid != got.Gid {
    67  		t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
    68  	}
    69  }
    70  
    71  func TestLookup(t *testing.T) {
    72  	checkUser(t)
    73  
    74  	if runtime.GOOS == "plan9" {
    75  		t.Skipf("Lookup not implemented on %q", runtime.GOOS)
    76  	}
    77  
    78  	want, err := Current()
    79  	if err != nil {
    80  		t.Fatalf("Current: %v", err)
    81  	}
    82  	// TODO: Lookup() has a fast path that calls Current() and returns if the
    83  	// usernames match, so this test does not exercise very much. It would be
    84  	// good to try and test finding a different user than the current user.
    85  	got, err := Lookup(want.Username)
    86  	if err != nil {
    87  		t.Fatalf("Lookup: %v", err)
    88  	}
    89  	compare(t, want, got)
    90  }
    91  
    92  func TestLookupId(t *testing.T) {
    93  	checkUser(t)
    94  
    95  	if runtime.GOOS == "plan9" {
    96  		t.Skipf("LookupId not implemented on %q", runtime.GOOS)
    97  	}
    98  
    99  	want, err := Current()
   100  	if err != nil {
   101  		t.Fatalf("Current: %v", err)
   102  	}
   103  	got, err := LookupId(want.Uid)
   104  	if err != nil {
   105  		t.Fatalf("LookupId: %v", err)
   106  	}
   107  	compare(t, want, got)
   108  }
   109  
   110  func checkGroup(t *testing.T) {
   111  	if !groupImplemented {
   112  		t.Skip("user: group not implemented; skipping test")
   113  	}
   114  }
   115  
   116  func TestLookupGroup(t *testing.T) {
   117  	checkGroup(t)
   118  	user, err := Current()
   119  	if err != nil {
   120  		t.Fatalf("Current(): %v", err)
   121  	}
   122  
   123  	g1, err := LookupGroupId(user.Gid)
   124  	if err != nil {
   125  		// NOTE(rsc): Maybe the group isn't defined. That's fine.
   126  		// On my OS X laptop, rsc logs in with group 5000 even
   127  		// though there's no name for group 5000. Such is Unix.
   128  		t.Logf("LookupGroupId(%q): %v", user.Gid, err)
   129  		return
   130  	}
   131  	if g1.Gid != user.Gid {
   132  		t.Errorf("LookupGroupId(%q).Gid = %s; want %s", user.Gid, g1.Gid, user.Gid)
   133  	}
   134  
   135  	g2, err := LookupGroup(g1.Name)
   136  	if err != nil {
   137  		t.Fatalf("LookupGroup(%q): %v", g1.Name, err)
   138  	}
   139  	if g1.Gid != g2.Gid || g1.Name != g2.Name {
   140  		t.Errorf("LookupGroup(%q) = %+v; want %+v", g1.Name, g2, g1)
   141  	}
   142  }
   143  
   144  func TestGroupIds(t *testing.T) {
   145  	checkGroup(t)
   146  	if runtime.GOOS == "solaris" {
   147  		t.Skip("skipping GroupIds, see golang.org/issue/14709")
   148  	}
   149  	user, err := Current()
   150  	if err != nil {
   151  		t.Fatalf("Current(): %v", err)
   152  	}
   153  	gids, err := user.GroupIds()
   154  	if err != nil {
   155  		t.Fatalf("%+v.GroupIds(): %v", user, err)
   156  	}
   157  	if !containsID(gids, user.Gid) {
   158  		t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
   159  	}
   160  }
   161  
   162  func containsID(ids []string, id string) bool {
   163  	for _, x := range ids {
   164  		if x == id {
   165  			return true
   166  		}
   167  	}
   168  	return false
   169  }
   170  

View as plain text