policycore_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 TestTPolicy_Contains(t *testing.T) {
  17. cases := []struct {
  18. name string
  19. p1 TPolicy
  20. p2 TPolicy
  21. contains bool
  22. }{
  23. {
  24. name: "case1",
  25. p1: TPolicy{
  26. SRbacRule{
  27. Service: "*",
  28. Result: Allow,
  29. },
  30. },
  31. p2: TPolicy{
  32. SRbacRule{
  33. Service: "compute",
  34. Result: Allow,
  35. },
  36. },
  37. contains: true,
  38. },
  39. {
  40. name: "case2",
  41. p1: TPolicy{
  42. SRbacRule{
  43. Service: "compute",
  44. Resource: "servers",
  45. Action: "list",
  46. Result: Allow,
  47. },
  48. },
  49. p2: TPolicy{
  50. SRbacRule{
  51. Service: "compute",
  52. Result: Allow,
  53. },
  54. },
  55. contains: false,
  56. },
  57. {
  58. name: "case3",
  59. p1: TPolicy{
  60. SRbacRule{
  61. Service: "compute",
  62. Result: Allow,
  63. },
  64. SRbacRule{
  65. Service: "compute",
  66. Resource: "servers",
  67. Result: Deny,
  68. },
  69. },
  70. p2: TPolicy{
  71. SRbacRule{
  72. Service: "compute",
  73. Result: Allow,
  74. },
  75. },
  76. contains: false,
  77. },
  78. {
  79. name: "case4",
  80. p1: TPolicy{
  81. SRbacRule{
  82. Service: "compute",
  83. Resource: "servers",
  84. Action: "list",
  85. Result: Allow,
  86. },
  87. SRbacRule{
  88. Service: "compute",
  89. Resource: "servers",
  90. Action: "get",
  91. Result: Allow,
  92. },
  93. SRbacRule{
  94. Service: "compute",
  95. Resource: "servers",
  96. Result: Deny,
  97. },
  98. },
  99. p2: TPolicy{
  100. SRbacRule{
  101. Service: "compute",
  102. Resource: "servers",
  103. Action: "create",
  104. Result: Allow,
  105. },
  106. },
  107. contains: false,
  108. },
  109. }
  110. for i, c := range cases {
  111. got := c.p1.Contains(c.p2)
  112. if got != c.contains {
  113. t.Errorf("[%d] want %v got %v", i, c.contains, got)
  114. }
  115. }
  116. }