iptables_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2015 CoreOS, Inc.
  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. "crypto/rand"
  17. "fmt"
  18. "math/big"
  19. "reflect"
  20. "testing"
  21. )
  22. func randChain(t *testing.T) string {
  23. n, err := rand.Int(rand.Reader, big.NewInt(1000000))
  24. if err != nil {
  25. t.Fatalf("Failed to generate random chain name: %v", err)
  26. }
  27. return "TEST-" + n.String()
  28. }
  29. // Create an array of IPTables with different hasWait/hasCheck to
  30. // test different behaviours
  31. func mustTestableIptables() []*IPTables {
  32. ipt, err := New()
  33. if err != nil {
  34. panic(fmt.Sprintf("New failed: %v", err))
  35. }
  36. ipts := []*IPTables{ipt}
  37. // ensure we check one variant without built-in locking
  38. if ipt.hasWait {
  39. iptNoWait := &IPTables{
  40. path: ipt.path,
  41. hasWait: false,
  42. }
  43. ipts = append(ipts, iptNoWait)
  44. }
  45. // ensure we check one variant without built-in checking
  46. if ipt.hasCheck {
  47. iptNoCheck := &IPTables{
  48. path: ipt.path,
  49. hasCheck: false,
  50. }
  51. ipts = append(ipts, iptNoCheck)
  52. }
  53. return ipts
  54. }
  55. func TestChain(t *testing.T) {
  56. for _, ipt := range mustTestableIptables() {
  57. runChainTests(t, ipt)
  58. }
  59. }
  60. func runChainTests(t *testing.T, ipt *IPTables) {
  61. t.Logf("testing iptables (hasWait=%t, hasCheck=%t)", ipt.hasWait, ipt.hasCheck)
  62. chain := randChain(t)
  63. // chain shouldn't exist, this will create new
  64. err := ipt.ClearChain("filter", chain)
  65. if err != nil {
  66. t.Fatalf("ClearChain (of missing) failed: %v", err)
  67. }
  68. // chain now exists
  69. err = ipt.ClearChain("filter", chain)
  70. if err != nil {
  71. t.Fatalf("ClearChain (of empty) failed: %v", err)
  72. }
  73. // put a simple rule in
  74. err = ipt.Append("filter", chain, "-s", "0.0.0.0/0", "-j", "ACCEPT")
  75. if err != nil {
  76. t.Fatalf("Append failed: %v", err)
  77. }
  78. // can't delete non-empty chain
  79. err = ipt.DeleteChain("filter", chain)
  80. if err == nil {
  81. t.Fatalf("DeleteChain of non-empty chain did not fail")
  82. }
  83. err = ipt.ClearChain("filter", chain)
  84. if err != nil {
  85. t.Fatalf("ClearChain (of non-empty) failed: %v", err)
  86. }
  87. // rename the chain
  88. newChain := randChain(t)
  89. err = ipt.RenameChain("filter", chain, newChain)
  90. if err != nil {
  91. t.Fatalf("RenameChain failed: %v", err)
  92. }
  93. // chain empty, should be ok
  94. err = ipt.DeleteChain("filter", newChain)
  95. if err != nil {
  96. t.Fatalf("DeleteChain of empty chain failed: %v", err)
  97. }
  98. }
  99. func TestRules(t *testing.T) {
  100. for _, ipt := range mustTestableIptables() {
  101. runRulesTests(t, ipt)
  102. }
  103. }
  104. func runRulesTests(t *testing.T, ipt *IPTables) {
  105. t.Logf("testing iptables (hasWait=%t, hasCheck=%t)", ipt.hasWait, ipt.hasCheck)
  106. chain := randChain(t)
  107. // chain shouldn't exist, this will create new
  108. err := ipt.ClearChain("filter", chain)
  109. if err != nil {
  110. t.Fatalf("ClearChain (of missing) failed: %v", err)
  111. }
  112. err = ipt.Append("filter", chain, "-s", "10.1.0.0/16", "-d", "8.8.8.8/32", "-j", "ACCEPT")
  113. if err != nil {
  114. t.Fatalf("Append failed: %v", err)
  115. }
  116. err = ipt.AppendUnique("filter", chain, "-s", "10.1.0.0/16", "-d", "8.8.8.8/32", "-j", "ACCEPT")
  117. if err != nil {
  118. t.Fatalf("AppendUnique failed: %v", err)
  119. }
  120. err = ipt.Append("filter", chain, "-s", "10.2.0.0/16", "-d", "8.8.8.8/32", "-j", "ACCEPT")
  121. if err != nil {
  122. t.Fatalf("Append failed: %v", err)
  123. }
  124. err = ipt.Insert("filter", chain, 2, "-s", "10.2.0.0/16", "-d", "9.9.9.9/32", "-j", "ACCEPT")
  125. if err != nil {
  126. t.Fatalf("Insert failed: %v", err)
  127. }
  128. err = ipt.Insert("filter", chain, 1, "-s", "10.1.0.0/16", "-d", "9.9.9.9/32", "-j", "ACCEPT")
  129. if err != nil {
  130. t.Fatalf("Insert failed: %v", err)
  131. }
  132. err = ipt.Delete("filter", chain, "-s", "10.1.0.0/16", "-d", "9.9.9.9/32", "-j", "ACCEPT")
  133. if err != nil {
  134. t.Fatalf("Delete failed: %v", err)
  135. }
  136. rules, err := ipt.List("filter", chain)
  137. if err != nil {
  138. t.Fatalf("List failed: %v", err)
  139. }
  140. expected := []string{
  141. "-N " + chain,
  142. "-A " + chain + " -s 10.1.0.0/16 -d 8.8.8.8/32 -j ACCEPT",
  143. "-A " + chain + " -s 10.2.0.0/16 -d 9.9.9.9/32 -j ACCEPT",
  144. "-A " + chain + " -s 10.2.0.0/16 -d 8.8.8.8/32 -j ACCEPT",
  145. }
  146. if !reflect.DeepEqual(rules, expected) {
  147. t.Fatalf("List mismatch: \ngot %#v \nneed %#v", rules, expected)
  148. }
  149. // Clear the chain that was created.
  150. err = ipt.ClearChain("filter", chain)
  151. if err != nil {
  152. t.Fatalf("Failed to clear test chain: %v", err)
  153. }
  154. // Delete the chain that was created
  155. err = ipt.DeleteChain("filter", chain)
  156. if err != nil {
  157. t.Fatalf("Failed to delete test chain: %v", err)
  158. }
  159. }