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 check(t *testing.T) {
     13 	if !implemented {
     14 		t.Skip("user: not implemented; skipping tests")
     15 	}
     16 }
     17 
     18 func TestCurrent(t *testing.T) {
     19 	check(t)
     20 
     21 	u, err := Current()
     22 	if err != nil {
     23 		t.Fatalf("Current: %v", err)
     24 	}
     25 	if u.HomeDir == "" {
     26 		t.Errorf("didn't get a HomeDir")
     27 	}
     28 	if u.Username == "" {
     29 		t.Errorf("didn't get a username")
     30 	}
     31 }
     32 
     33 func compare(t *testing.T, want, got *User) {
     34 	if want.Uid != got.Uid {
     35 		t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
     36 	}
     37 	if want.Username != got.Username {
     38 		t.Errorf("got Username=%q; want %q", got.Username, want.Username)
     39 	}
     40 	if want.Name != got.Name {
     41 		t.Errorf("got Name=%q; want %q", got.Name, want.Name)
     42 	}
     43 	// TODO(brainman): fix it once we know how.
     44 	if runtime.GOOS == "windows" {
     45 		t.Skip("skipping Gid and HomeDir comparisons")
     46 	}
     47 	if want.Gid != got.Gid {
     48 		t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
     49 	}
     50 	if want.HomeDir != got.HomeDir {
     51 		t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
     52 	}
     53 }
     54 
     55 func TestLookup(t *testing.T) {
     56 	check(t)
     57 
     58 	if runtime.GOOS == "plan9" {
     59 		t.Skipf("Lookup not implemented on %q", runtime.GOOS)
     60 	}
     61 
     62 	want, err := Current()
     63 	if err != nil {
     64 		t.Fatalf("Current: %v", err)
     65 	}
     66 	got, err := Lookup(want.Username)
     67 	if err != nil {
     68 		t.Fatalf("Lookup: %v", err)
     69 	}
     70 	compare(t, want, got)
     71 }
     72 
     73 func TestLookupId(t *testing.T) {
     74 	check(t)
     75 
     76 	if runtime.GOOS == "plan9" {
     77 		t.Skipf("LookupId not implemented on %q", runtime.GOOS)
     78 	}
     79 
     80 	want, err := Current()
     81 	if err != nil {
     82 		t.Fatalf("Current: %v", err)
     83 	}
     84 	got, err := LookupId(want.Uid)
     85 	if err != nil {
     86 		t.Fatalf("LookupId: %v", err)
     87 	}
     88 	compare(t, want, got)
     89 }
     90