iptables_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. iptNoWait.fmu, err = newXtablesFileLock()
  44. if err != nil {
  45. panic(fmt.Sprintf("NewXtablesFileLock failed: %v", err))
  46. }
  47. ipts = append(ipts, iptNoWait)
  48. }
  49. // ensure we check one variant without built-in checking
  50. if ipt.hasCheck {
  51. iptNoCheck := &IPTables{
  52. path: ipt.path,
  53. hasCheck: false,
  54. }
  55. ipts = append(ipts, iptNoCheck)
  56. }
  57. return ipts
  58. }
  59. func TestChain(t *testing.T) {
  60. for _, ipt := range mustTestableIptables() {
  61. runChainTests(t, ipt)
  62. }
  63. }
  64. func runChainTests(t *testing.T, ipt *IPTables) {
  65. t.Logf("testing iptables (hasWait=%t, hasCheck=%t)", ipt.hasWait, ipt.hasCheck)
  66. chain := randChain(t)
  67. // chain shouldn't exist, this will create new
  68. err := ipt.ClearChain("filter", chain)
  69. if err != nil {
  70. t.Fatalf("ClearChain (of missing) failed: %v", err)
  71. }
  72. // chain now exists
  73. err = ipt.ClearChain("filter", chain)
  74. if err != nil {
  75. t.Fatalf("ClearChain (of empty) failed: %v", err)
  76. }
  77. // put a simple rule in
  78. err = ipt.Append("filter", chain, "-s", "0.0.0.0/0", "-j", "ACCEPT")
  79. if err != nil {
  80. t.Fatalf("Append failed: %v", err)
  81. }
  82. // can't delete non-empty chain
  83. err = ipt.DeleteChain("filter", chain)
  84. if err == nil {
  85. t.Fatalf("DeleteChain of non-empty chain did not fail")
  86. }
  87. err = ipt.ClearChain("filter", chain)
  88. if err != nil {
  89. t.Fatalf("ClearChain (of non-empty) failed: %v", err)
  90. }
  91. // rename the chain
  92. newChain := randChain(t)
  93. err = ipt.RenameChain("filter", chain, newChain)
  94. if err != nil {
  95. t.Fatalf("RenameChain failed: %v", err)
  96. }
  97. // chain empty, should be ok
  98. err = ipt.DeleteChain("filter", newChain)
  99. if err != nil {
  100. t.Fatalf("DeleteChain of empty chain failed: %v", err)
  101. }
  102. }
  103. func TestRules(t *testing.T) {
  104. for _, ipt := range mustTestableIptables() {
  105. runRulesTests(t, ipt)
  106. }
  107. }
  108. func runRulesTests(t *testing.T, ipt *IPTables) {
  109. t.Logf("testing iptables (hasWait=%t, hasCheck=%t)", ipt.hasWait, ipt.hasCheck)
  110. chain := randChain(t)
  111. // chain shouldn't exist, this will create new
  112. err := ipt.ClearChain("filter", chain)
  113. if err != nil {
  114. t.Fatalf("ClearChain (of missing) failed: %v", err)
  115. }
  116. err = ipt.Append("filter", chain, "-s", "10.1.0.0/16", "-d", "8.8.8.8/32", "-j", "ACCEPT")
  117. if err != nil {
  118. t.Fatalf("Append failed: %v", err)
  119. }
  120. err = ipt.AppendUnique("filter", chain, "-s", "10.1.0.0/16", "-d", "8.8.8.8/32", "-j", "ACCEPT")
  121. if err != nil {
  122. t.Fatalf("AppendUnique failed: %v", err)
  123. }
  124. err = ipt.Append("filter", chain, "-s", "10.2.0.0/16", "-d", "8.8.8.8/32", "-j", "ACCEPT")
  125. if err != nil {
  126. t.Fatalf("Append failed: %v", err)
  127. }
  128. err = ipt.Insert("filter", chain, 2, "-s", "10.2.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.Insert("filter", chain, 1, "-s", "10.1.0.0/16", "-d", "9.9.9.9/32", "-j", "ACCEPT")
  133. if err != nil {
  134. t.Fatalf("Insert failed: %v", err)
  135. }
  136. err = ipt.Delete("filter", chain, "-s", "10.1.0.0/16", "-d", "9.9.9.9/32", "-j", "ACCEPT")
  137. if err != nil {
  138. t.Fatalf("Delete failed: %v", err)
  139. }
  140. rules, err := ipt.List("filter", chain)
  141. if err != nil {
  142. t.Fatalf("List failed: %v", err)
  143. }
  144. expected := []string{
  145. "-N " + chain,
  146. "-A " + chain + " -s 10.1.0.0/16 -d 8.8.8.8/32 -j ACCEPT",
  147. "-A " + chain + " -s 10.2.0.0/16 -d 9.9.9.9/32 -j ACCEPT",
  148. "-A " + chain + " -s 10.2.0.0/16 -d 8.8.8.8/32 -j ACCEPT",
  149. }
  150. if !reflect.DeepEqual(rules, expected) {
  151. t.Fatalf("List mismatch: \ngot %#v \nneed %#v", rules, expected)
  152. }
  153. // Clear the chain that was created.
  154. err = ipt.ClearChain("filter", chain)
  155. if err != nil {
  156. t.Fatalf("Failed to clear test chain: %v", err)
  157. }
  158. // Delete the chain that was created
  159. err = ipt.DeleteChain("filter", chain)
  160. if err != nil {
  161. t.Fatalf("Failed to delete test chain: %v", err)
  162. }
  163. }