2011-05-20 02:18:15 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
func check(t *testing.T) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !implemented {
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skip("user: not implemented; skipping tests")
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux", "freebsd", "darwin", "windows":
|
2013-01-29 21:52:43 +01:00
|
|
|
// test supported
|
|
|
|
default:
|
|
|
|
t.Skipf("user: Lookup not implemented on %q; skipping test", runtime.GOOS)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func TestCurrent(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
check(t)
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
u, err := Current()
|
2011-05-20 02:18:15 +02:00
|
|
|
if err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
t.Fatalf("Current: %v", err)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
if u.HomeDir == "" {
|
|
|
|
t.Errorf("didn't get a HomeDir")
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
if u.Username == "" {
|
2012-10-23 06:31:11 +02:00
|
|
|
t.Errorf("didn't get a username")
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func compare(t *testing.T, want, got *User) {
|
|
|
|
if want.Uid != got.Uid {
|
|
|
|
t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
|
|
|
|
}
|
|
|
|
if want.Username != got.Username {
|
|
|
|
t.Errorf("got Username=%q; want %q", got.Username, want.Username)
|
|
|
|
}
|
|
|
|
if want.Name != got.Name {
|
|
|
|
t.Errorf("got Name=%q; want %q", got.Name, want.Name)
|
|
|
|
}
|
|
|
|
// TODO(brainman): fix it once we know how.
|
|
|
|
if runtime.GOOS == "windows" {
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skip("skipping Gid and HomeDir comparisons")
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
|
|
|
if want.Gid != got.Gid {
|
|
|
|
t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
|
|
|
|
}
|
|
|
|
if want.HomeDir != got.HomeDir {
|
|
|
|
t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookup(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
check(t)
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
want, err := Current()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Current: %v", err)
|
|
|
|
}
|
|
|
|
got, err := Lookup(want.Username)
|
2011-05-20 02:18:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
compare(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupId(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
check(t)
|
2012-02-09 09:19:58 +01:00
|
|
|
|
|
|
|
want, err := Current()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Current: %v", err)
|
|
|
|
}
|
|
|
|
got, err := LookupId(want.Uid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("LookupId: %v", err)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
compare(t, want, got)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|