policyset_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rbacutils
  15. import "testing"
  16. func TestTPolicySet_Contains(t *testing.T) {
  17. cases := []struct {
  18. name string
  19. p1 TPolicySet
  20. p2 TPolicySet
  21. p1cp2 bool
  22. p2cp1 bool
  23. }{
  24. {
  25. name: "case1",
  26. p1: TPolicySet{
  27. SPolicy{
  28. Rules: TPolicy{
  29. {
  30. Service: "compute",
  31. Resource: "servers",
  32. Action: "list",
  33. Result: Deny,
  34. },
  35. {
  36. Service: "compute",
  37. Resource: "servers",
  38. Action: WILD_MATCH,
  39. Result: Allow,
  40. },
  41. },
  42. },
  43. },
  44. p2: TPolicySet{
  45. SPolicy{
  46. Rules: TPolicy{
  47. {
  48. Service: "compute",
  49. Resource: "servers",
  50. Action: WILD_MATCH,
  51. Result: Allow,
  52. },
  53. },
  54. },
  55. },
  56. p1cp2: false,
  57. p2cp1: true,
  58. },
  59. {
  60. name: "case2",
  61. p1: TPolicySet{
  62. SPolicy{
  63. Rules: TPolicy{
  64. {
  65. Service: "comptue",
  66. Resource: "servers",
  67. Action: "list",
  68. Result: Deny,
  69. },
  70. {
  71. Service: "compute",
  72. Resource: "servers",
  73. Action: WILD_MATCH,
  74. Result: Allow,
  75. },
  76. },
  77. },
  78. },
  79. p2: TPolicySet{
  80. SPolicy{
  81. Rules: TPolicy{
  82. {
  83. Service: WILD_MATCH,
  84. Result: Allow,
  85. },
  86. },
  87. },
  88. SPolicy{
  89. Rules: TPolicy{
  90. {
  91. Service: "compute",
  92. Resource: "servers",
  93. Action: "list",
  94. Result: Deny,
  95. },
  96. },
  97. },
  98. },
  99. p1cp2: false,
  100. p2cp1: true,
  101. },
  102. {
  103. name: "case3",
  104. p1: TPolicySet{
  105. SPolicy{
  106. Rules: TPolicy{
  107. {
  108. Service: WILD_MATCH,
  109. Result: Allow,
  110. },
  111. {
  112. Service: "compute",
  113. Resource: "servers",
  114. Action: "create",
  115. Result: Deny,
  116. },
  117. },
  118. },
  119. SPolicy{
  120. Rules: TPolicy{
  121. {
  122. Service: "comptue",
  123. Resource: "servers",
  124. Action: "list",
  125. Result: Deny,
  126. },
  127. {
  128. Service: "compute",
  129. Resource: "servers",
  130. Action: WILD_MATCH,
  131. Result: Allow,
  132. },
  133. },
  134. },
  135. },
  136. p2: TPolicySet{
  137. SPolicy{
  138. Rules: TPolicy{
  139. {
  140. Service: WILD_MATCH,
  141. Result: Deny,
  142. },
  143. },
  144. },
  145. SPolicy{
  146. Rules: TPolicy{
  147. {
  148. Service: "comptue",
  149. Resource: "servers",
  150. Action: WILD_MATCH,
  151. Result: Deny,
  152. },
  153. {
  154. Service: "compute",
  155. Resource: "servers",
  156. Action: "get",
  157. Result: Allow,
  158. },
  159. },
  160. },
  161. },
  162. p1cp2: true,
  163. p2cp1: false,
  164. },
  165. {
  166. name: "case4",
  167. p1: TPolicySet{
  168. SPolicy{
  169. Rules: TPolicy{
  170. {
  171. Service: WILD_MATCH,
  172. Result: Allow,
  173. },
  174. },
  175. },
  176. },
  177. p2: TPolicySet{
  178. SPolicy{
  179. Rules: TPolicy{
  180. {
  181. Service: WILD_MATCH,
  182. Result: Allow,
  183. },
  184. {
  185. Service: "compute",
  186. Resource: "servers",
  187. Action: "list",
  188. Result: Deny,
  189. },
  190. },
  191. },
  192. },
  193. p1cp2: true,
  194. p2cp1: false,
  195. },
  196. }
  197. for _, c := range cases {
  198. got := c.p1.Contains(c.p2)
  199. if got != c.p1cp2 {
  200. t.Errorf("[%s] p1 contains p2 want %v got %v", c.name, c.p1cp2, got)
  201. }
  202. got = c.p2.Contains(c.p1)
  203. if got != c.p2cp1 {
  204. t.Errorf("[%s] p2 contains p1 want %v got %v", c.name, c.p2cp1, got)
  205. }
  206. }
  207. }