user_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. package user
  5. import (
  6. "testing"
  7. )
  8. func checkUser(t *testing.T) {
  9. t.Helper()
  10. if !userImplemented {
  11. t.Skip("user: not implemented; skipping tests")
  12. }
  13. }
  14. func TestCurrent(t *testing.T) {
  15. u, err := Current()
  16. if err != nil {
  17. t.Fatalf("Current: %v (got %#v)", err, u)
  18. }
  19. if u.HomeDir == "" {
  20. t.Errorf("didn't get a HomeDir")
  21. }
  22. if u.Username == "" {
  23. t.Errorf("didn't get a username")
  24. }
  25. }
  26. func BenchmarkCurrent(b *testing.B) {
  27. for i := 0; i < b.N; i++ {
  28. Current()
  29. }
  30. }
  31. func compare(t *testing.T, want, got *User) {
  32. if want.Uid != got.Uid {
  33. t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
  34. }
  35. if want.Username != got.Username {
  36. t.Errorf("got Username=%q; want %q", got.Username, want.Username)
  37. }
  38. if want.Name != got.Name {
  39. t.Errorf("got Name=%q; want %q", got.Name, want.Name)
  40. }
  41. if want.HomeDir != got.HomeDir {
  42. t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
  43. }
  44. if want.Gid != got.Gid {
  45. t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
  46. }
  47. }
  48. func TestLookup(t *testing.T) {
  49. checkUser(t)
  50. want, err := Current()
  51. if err != nil {
  52. t.Fatalf("Current: %v", err)
  53. }
  54. // TODO: Lookup() has a fast path that calls Current() and returns if the
  55. // usernames match, so this test does not exercise very much. It would be
  56. // good to try and test finding a different user than the current user.
  57. got, err := Lookup(want.Username)
  58. if err != nil {
  59. t.Fatalf("Lookup: %v", err)
  60. }
  61. compare(t, want, got)
  62. }
  63. func TestLookupId(t *testing.T) {
  64. checkUser(t)
  65. want, err := Current()
  66. if err != nil {
  67. t.Fatalf("Current: %v", err)
  68. }
  69. got, err := LookupId(want.Uid)
  70. if err != nil {
  71. t.Fatalf("LookupId: %v", err)
  72. }
  73. compare(t, want, got)
  74. }
  75. func checkGroup(t *testing.T) {
  76. t.Helper()
  77. if !groupImplemented {
  78. t.Skip("user: group not implemented; skipping test")
  79. }
  80. }
  81. func TestLookupGroup(t *testing.T) {
  82. checkGroup(t)
  83. user, err := Current()
  84. if err != nil {
  85. t.Fatalf("Current(): %v", err)
  86. }
  87. g1, err := LookupGroupId(user.Gid)
  88. if err != nil {
  89. // NOTE(rsc): Maybe the group isn't defined. That's fine.
  90. // On my OS X laptop, rsc logs in with group 5000 even
  91. // though there's no name for group 5000. Such is Unix.
  92. t.Logf("LookupGroupId(%q): %v", user.Gid, err)
  93. return
  94. }
  95. if g1.Gid != user.Gid {
  96. t.Errorf("LookupGroupId(%q).Gid = %s; want %s", user.Gid, g1.Gid, user.Gid)
  97. }
  98. g2, err := LookupGroup(g1.Name)
  99. if err != nil {
  100. t.Fatalf("LookupGroup(%q): %v", g1.Name, err)
  101. }
  102. if g1.Gid != g2.Gid || g1.Name != g2.Name {
  103. t.Errorf("LookupGroup(%q) = %+v; want %+v", g1.Name, g2, g1)
  104. }
  105. }
  106. func checkGroupList(t *testing.T) {
  107. t.Helper()
  108. if !groupListImplemented {
  109. t.Skip("user: group list not implemented; skipping test")
  110. }
  111. }
  112. func TestGroupIds(t *testing.T) {
  113. checkGroupList(t)
  114. user, err := Current()
  115. if err != nil {
  116. t.Fatalf("Current(): %v", err)
  117. }
  118. gids, err := user.GroupIds()
  119. if err != nil {
  120. t.Fatalf("%+v.GroupIds(): %v", user, err)
  121. }
  122. if !containsID(gids, user.Gid) {
  123. t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
  124. }
  125. }
  126. func containsID(ids []string, id string) bool {
  127. for _, x := range ids {
  128. if x == id {
  129. return true
  130. }
  131. }
  132. return false
  133. }