iptables_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 TestProto(t *testing.T) {
  23. ipt, err := New()
  24. if err != nil {
  25. t.Fatalf("New failed: %v", err)
  26. }
  27. if ipt.Proto() != ProtocolIPv4 {
  28. t.Fatalf("Expected default protocol IPv4, got %v", ipt.Proto())
  29. }
  30. ip4t, err := NewWithProtocol(ProtocolIPv4)
  31. if err != nil {
  32. t.Fatalf("NewWithProtocol(ProtocolIPv4) failed: %v", err)
  33. }
  34. if ip4t.Proto() != ProtocolIPv4 {
  35. t.Fatalf("Expected protocol IPv4, got %v", ip4t.Proto())
  36. }
  37. ip6t, err := NewWithProtocol(ProtocolIPv6)
  38. if err != nil {
  39. t.Fatalf("NewWithProtocol(ProtocolIPv6) failed: %v", err)
  40. }
  41. if ip6t.Proto() != ProtocolIPv6 {
  42. t.Fatalf("Expected protocol IPv6, got %v", ip6t.Proto())
  43. }
  44. }
  45. func randChain(t *testing.T) string {
  46. n, err := rand.Int(rand.Reader, big.NewInt(1000000))
  47. if err != nil {
  48. t.Fatalf("Failed to generate random chain name: %v", err)
  49. }
  50. return "TEST-" + n.String()
  51. }
  52. // Create an array of IPTables with different hasWait/hasCheck to
  53. // test different behaviours
  54. func mustTestableIptables() []*IPTables {
  55. ipt, err := New()
  56. if err != nil {
  57. panic(fmt.Sprintf("New failed: %v", err))
  58. }
  59. ip6t, err := NewWithProtocol(ProtocolIPv6)
  60. if err != nil {
  61. panic(fmt.Sprintf("NewWithProtocol(ProtocolIPv6) failed: %v", err))
  62. }
  63. ipts := []*IPTables{ipt, ip6t}
  64. // ensure we check one variant without built-in locking
  65. if ipt.hasWait {
  66. iptNoWait := &IPTables{
  67. path: ipt.path,
  68. hasWait: false,
  69. }
  70. ipts = append(ipts, iptNoWait)
  71. }
  72. // ensure we check one variant without built-in checking
  73. if ipt.hasCheck {
  74. iptNoCheck := &IPTables{
  75. path: ipt.path,
  76. hasCheck: false,
  77. }
  78. ipts = append(ipts, iptNoCheck)
  79. }
  80. return ipts
  81. }
  82. func TestChain(t *testing.T) {
  83. for _, ipt := range mustTestableIptables() {
  84. runChainTests(t, ipt)
  85. }
  86. }
  87. func runChainTests(t *testing.T, ipt *IPTables) {
  88. t.Logf("testing %s (hasWait=%t, hasCheck=%t)", getIptablesCommand(ipt.Proto()), ipt.hasWait, ipt.hasCheck)
  89. chain := randChain(t)
  90. // chain shouldn't exist, this will create new
  91. err := ipt.ClearChain("filter", chain)
  92. if err != nil {
  93. t.Fatalf("ClearChain (of missing) failed: %v", err)
  94. }
  95. // chain now exists
  96. err = ipt.ClearChain("filter", chain)
  97. if err != nil {
  98. t.Fatalf("ClearChain (of empty) failed: %v", err)
  99. }
  100. // put a simple rule in
  101. err = ipt.Append("filter", chain, "-s", "0/0", "-j", "ACCEPT")
  102. if err != nil {
  103. t.Fatalf("Append failed: %v", err)
  104. }
  105. // can't delete non-empty chain
  106. err = ipt.DeleteChain("filter", chain)
  107. if err == nil {
  108. t.Fatalf("DeleteChain of non-empty chain did not fail")
  109. }
  110. err = ipt.ClearChain("filter", chain)
  111. if err != nil {
  112. t.Fatalf("ClearChain (of non-empty) failed: %v", err)
  113. }
  114. // rename the chain
  115. newChain := randChain(t)
  116. err = ipt.RenameChain("filter", chain, newChain)
  117. if err != nil {
  118. t.Fatalf("RenameChain failed: %v", err)
  119. }
  120. // chain empty, should be ok
  121. err = ipt.DeleteChain("filter", newChain)
  122. if err != nil {
  123. t.Fatalf("DeleteChain of empty chain failed: %v", err)
  124. }
  125. }
  126. func TestRules(t *testing.T) {
  127. for _, ipt := range mustTestableIptables() {
  128. runRulesTests(t, ipt)
  129. }
  130. }
  131. func runRulesTests(t *testing.T, ipt *IPTables) {
  132. t.Logf("testing %s (hasWait=%t, hasCheck=%t)", getIptablesCommand(ipt.Proto()), ipt.hasWait, ipt.hasCheck)
  133. var address1, address2, subnet1, subnet2 string
  134. if ipt.Proto() == ProtocolIPv6 {
  135. address1 = "2001:db8::1/128"
  136. address2 = "2001:db8::2/128"
  137. subnet1 = "2001:db8:a::/48"
  138. subnet2 = "2001:db8:b::/48"
  139. } else {
  140. address1 = "203.0.113.1/32"
  141. address2 = "203.0.113.2/32"
  142. subnet1 = "192.0.2.0/24"
  143. subnet2 = "198.51.100.0/24"
  144. }
  145. chain := randChain(t)
  146. // chain shouldn't exist, this will create new
  147. err := ipt.ClearChain("filter", chain)
  148. if err != nil {
  149. t.Fatalf("ClearChain (of missing) failed: %v", err)
  150. }
  151. err = ipt.Append("filter", chain, "-s", subnet1, "-d", address1, "-j", "ACCEPT")
  152. if err != nil {
  153. t.Fatalf("Append failed: %v", err)
  154. }
  155. err = ipt.AppendUnique("filter", chain, "-s", subnet1, "-d", address1, "-j", "ACCEPT")
  156. if err != nil {
  157. t.Fatalf("AppendUnique failed: %v", err)
  158. }
  159. err = ipt.Append("filter", chain, "-s", subnet2, "-d", address1, "-j", "ACCEPT")
  160. if err != nil {
  161. t.Fatalf("Append failed: %v", err)
  162. }
  163. err = ipt.Insert("filter", chain, 2, "-s", subnet2, "-d", address2, "-j", "ACCEPT")
  164. if err != nil {
  165. t.Fatalf("Insert failed: %v", err)
  166. }
  167. err = ipt.Insert("filter", chain, 1, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  168. if err != nil {
  169. t.Fatalf("Insert failed: %v", err)
  170. }
  171. err = ipt.Delete("filter", chain, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  172. if err != nil {
  173. t.Fatalf("Delete failed: %v", err)
  174. }
  175. rules, err := ipt.List("filter", chain)
  176. if err != nil {
  177. t.Fatalf("List failed: %v", err)
  178. }
  179. expected := []string{
  180. "-N " + chain,
  181. "-A " + chain + " -s " + subnet1 + " -d " + address1 + " -j ACCEPT",
  182. "-A " + chain + " -s " + subnet2 + " -d " + address2 + " -j ACCEPT",
  183. "-A " + chain + " -s " + subnet2 + " -d " + address1 + " -j ACCEPT",
  184. }
  185. if !reflect.DeepEqual(rules, expected) {
  186. t.Fatalf("List mismatch: \ngot %#v \nneed %#v", rules, expected)
  187. }
  188. // Clear the chain that was created.
  189. err = ipt.ClearChain("filter", chain)
  190. if err != nil {
  191. t.Fatalf("Failed to clear test chain: %v", err)
  192. }
  193. // Delete the chain that was created
  194. err = ipt.DeleteChain("filter", chain)
  195. if err != nil {
  196. t.Fatalf("Failed to delete test chain: %v", err)
  197. }
  198. }