permset_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package permset
  2. import (
  3. "context"
  4. "testing"
  5. )
  6. const (
  7. TestPerm1 Perm = iota
  8. TestPerm2
  9. TestPerm3
  10. )
  11. func TestNew(t *testing.T) {
  12. var newtests = []struct {
  13. perms []Perm
  14. }{
  15. {[]Perm{TestPerm1}},
  16. {[]Perm{TestPerm1, TestPerm2}},
  17. {[]Perm{TestPerm3, TestPerm2}},
  18. {[]Perm{TestPerm2, TestPerm2}},
  19. {[]Perm{TestPerm3, TestPerm2, TestPerm3}},
  20. }
  21. for _, tt := range newtests {
  22. permset := New(tt.perms...)
  23. // See if perms within the permset checks out with the ones provided to the New().
  24. for _, perm := range tt.perms {
  25. if _, ok := permset.permset[perm]; !ok {
  26. t.Fatalf("perm should exist in the permset: %v", perm)
  27. }
  28. }
  29. // See if there are any extra perms in the permset that is not provided to the New().
  30. for perm := range permset.permset {
  31. var found bool
  32. for _, ttPerm := range tt.perms {
  33. if ttPerm == perm {
  34. found = true
  35. }
  36. }
  37. if !found {
  38. t.Fatalf("perm should not exist in the permset: %v", perm)
  39. }
  40. }
  41. }
  42. }
  43. func TestAdd(t *testing.T) {
  44. permset := New(TestPerm2)
  45. permset.Add(TestPerm3)
  46. if _, ok := permset.permset[TestPerm3]; !ok {
  47. t.Fatal("perm TestPerm3 should exist in the permset")
  48. }
  49. if _, ok := permset.permset[TestPerm2]; !ok {
  50. t.Fatal("perm TestPerm2 should exist in the permset")
  51. }
  52. }
  53. func TestRemove(t *testing.T) {
  54. // See if remove works OK.
  55. permset := New(TestPerm2, TestPerm3)
  56. permset.Remove(TestPerm3)
  57. if _, ok := permset.permset[TestPerm3]; ok {
  58. t.Fatal("perm TestPerm3 should not exist in the permset")
  59. }
  60. if _, ok := permset.permset[TestPerm2]; !ok {
  61. t.Fatal("perm TestPerm2 should exist in the permset")
  62. }
  63. // See if double remove breaks it.
  64. permset = New(TestPerm2, TestPerm3)
  65. permset.Remove(TestPerm3)
  66. permset.Remove(TestPerm3)
  67. if _, ok := permset.permset[TestPerm3]; ok {
  68. t.Fatal("perm TestPerm3 should not exist in the permset")
  69. }
  70. if _, ok := permset.permset[TestPerm2]; !ok {
  71. t.Fatal("perm TestPerm2 should exist in the permset")
  72. }
  73. }
  74. func TestPerms(t *testing.T) {
  75. permset := New(TestPerm2, TestPerm3)
  76. perms := permset.Perms()
  77. var found bool
  78. for _, perm := range perms {
  79. if perm == TestPerm2 {
  80. found = true
  81. }
  82. }
  83. if !found {
  84. t.Fatal("Perms() should return all the perms within the permset")
  85. }
  86. }
  87. func TestContains(t *testing.T) {
  88. permset := New(TestPerm2, TestPerm3)
  89. if !permset.Contains(TestPerm2) {
  90. t.Fatal("permset should contain TestPerm2")
  91. }
  92. if !permset.Contains(TestPerm3) {
  93. t.Fatal("permset should contain TestPerm3")
  94. }
  95. if permset.Contains(TestPerm1) {
  96. t.Fatal("permset should not contain TestPerm1")
  97. }
  98. }
  99. func TestContainsAll(t *testing.T) {
  100. permset := New(TestPerm2, TestPerm3)
  101. if !permset.ContainsAll(TestPerm2) {
  102. t.Fatal("permset should contain TestPerm2")
  103. }
  104. if !permset.ContainsAll(TestPerm3) {
  105. t.Fatal("permset should contain TestPerm3")
  106. }
  107. if permset.ContainsAll(TestPerm1) {
  108. t.Fatal("permset should not contain TestPerm1")
  109. }
  110. if !permset.ContainsAll(TestPerm2, TestPerm3) {
  111. t.Fatal("permset should contain TestPerm2 and TestPerm3")
  112. }
  113. if !permset.ContainsAll(TestPerm3, TestPerm2) {
  114. t.Fatal("permset should contain TestPerm2 and TestPerm3")
  115. }
  116. if permset.ContainsAll(TestPerm1, TestPerm2) {
  117. t.Fatal("permset should not contain TestPerm1 and TestPerm3")
  118. }
  119. }
  120. func TestContainsSome(t *testing.T) {
  121. permset := New(TestPerm2, TestPerm3)
  122. if !permset.ContainsSome(TestPerm2) {
  123. t.Fatal("permset should contain TestPerm2")
  124. }
  125. if !permset.ContainsSome(TestPerm1, TestPerm3) {
  126. t.Fatal("permset should contain TestPerm1 and TestPerm3")
  127. }
  128. if permset.ContainsSome(TestPerm1) {
  129. t.Fatal("permset should contain TestPerm1")
  130. }
  131. }
  132. func TestContainsNone(t *testing.T) {
  133. permset := New(TestPerm2, TestPerm3)
  134. if !permset.ContainsNone(TestPerm1) {
  135. t.Fatal("ContainsNone should return true")
  136. }
  137. if permset.ContainsNone(TestPerm2) {
  138. t.Fatal("ContainsNone should return false")
  139. }
  140. }
  141. func TestNewContext(t *testing.T) {
  142. permset := New(TestPerm2, TestPerm3)
  143. ctx := NewContext(context.Background(), permset)
  144. permsetFromCtx, ok := ctx.Value(permsetKey).(Permset)
  145. if !ok {
  146. t.Fatal("can't extract permset from ctx")
  147. }
  148. if !permset.ContainsAll(permsetFromCtx.Perms()...) {
  149. t.Fatal("permsets should match")
  150. }
  151. }
  152. func TestFromContext(t *testing.T) {
  153. permset := New(TestPerm2, TestPerm3)
  154. ctx := NewContext(context.Background(), permset)
  155. permsetFromCtx, err := FromContext(ctx)
  156. if err != nil {
  157. t.Fatalf("error is not expected here: %v", err)
  158. }
  159. if !permset.ContainsAll(permsetFromCtx.Perms()...) {
  160. t.Fatal("permsets should match")
  161. }
  162. }