2
0

rulecache_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2021 the Kilo authors
  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 iptables
  15. import (
  16. "testing"
  17. )
  18. func TestRuleCache(t *testing.T) {
  19. for _, tc := range []struct {
  20. name string
  21. rules []Rule
  22. check []Rule
  23. out []bool
  24. calls uint64
  25. }{
  26. {
  27. name: "empty",
  28. rules: nil,
  29. check: []Rule{rules[0]},
  30. out: []bool{false},
  31. calls: 1,
  32. },
  33. {
  34. name: "single negative",
  35. rules: []Rule{rules[1]},
  36. check: []Rule{rules[0]},
  37. out: []bool{false},
  38. calls: 1,
  39. },
  40. {
  41. name: "single positive",
  42. rules: []Rule{rules[1]},
  43. check: []Rule{rules[1]},
  44. out: []bool{true},
  45. calls: 1,
  46. },
  47. {
  48. name: "single chain",
  49. rules: []Rule{&chain{"nat", "KILO-NAT", ProtocolIPv4}},
  50. check: []Rule{&chain{"nat", "KILO-NAT", ProtocolIPv4}},
  51. out: []bool{true},
  52. calls: 1,
  53. },
  54. {
  55. name: "rule on chain means chain exists",
  56. rules: []Rule{rules[0]},
  57. check: []Rule{rules[0], &chain{"filter", "FORWARD", ProtocolIPv4}},
  58. out: []bool{true, true},
  59. calls: 1,
  60. },
  61. {
  62. name: "rule on chain does not mean table is fully populated",
  63. rules: []Rule{rules[0], &chain{"filter", "INPUT", ProtocolIPv4}},
  64. check: []Rule{rules[0], &chain{"filter", "OUTPUT", ProtocolIPv4}, &chain{"filter", "INPUT", ProtocolIPv4}},
  65. out: []bool{true, false, true},
  66. calls: 2,
  67. },
  68. {
  69. name: "multiple rules on chain",
  70. rules: []Rule{rules[0], rules[1]},
  71. check: []Rule{rules[0], rules[1], &chain{"filter", "FORWARD", ProtocolIPv4}},
  72. out: []bool{true, true, true},
  73. calls: 1,
  74. },
  75. {
  76. name: "checking rule on chain does not mean chain exists",
  77. rules: nil,
  78. check: []Rule{rules[0], &chain{"filter", "FORWARD", ProtocolIPv4}},
  79. out: []bool{false, false},
  80. calls: 2,
  81. },
  82. {
  83. name: "multiple chains on same table",
  84. rules: nil,
  85. check: []Rule{&chain{"filter", "INPUT", ProtocolIPv4}, &chain{"filter", "FORWARD", ProtocolIPv4}},
  86. out: []bool{false, false},
  87. calls: 1,
  88. },
  89. {
  90. name: "multiple chains on different table",
  91. rules: nil,
  92. check: []Rule{&chain{"filter", "INPUT", ProtocolIPv4}, &chain{"nat", "POSTROUTING", ProtocolIPv4}},
  93. out: []bool{false, false},
  94. calls: 2,
  95. },
  96. } {
  97. controller := &Controller{}
  98. client := &fakeClient{}
  99. controller.v4 = client
  100. controller.v6 = client
  101. if err := controller.Set(tc.rules); err != nil {
  102. t.Fatalf("test case %q: Set should not fail: %v", tc.name, err)
  103. }
  104. // Reset the client's calls so we can examine how many times
  105. // the rule cache performs operations.
  106. client.calls = 0
  107. var rc ruleCache
  108. for i := range tc.check {
  109. ok, err := rc.exists(controller.client(tc.check[i].Proto()), tc.check[i])
  110. if err != nil {
  111. t.Fatalf("test case %q check %d: check should not fail: %v", tc.name, i, err)
  112. }
  113. if ok != tc.out[i] {
  114. t.Errorf("test case %q check %d: expected %t, got %t", tc.name, i, tc.out[i], ok)
  115. }
  116. }
  117. if client.calls != tc.calls {
  118. t.Errorf("test case %q: expected client to be called %d times, got %d", tc.name, tc.calls, client.calls)
  119. }
  120. }
  121. }