iptables_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. func contains(list []string, value string) bool {
  53. for _, val := range list {
  54. if val == value {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. // mustTestableIptables returns a list of ip(6)tables handles with various
  61. // features enabled & disabled, to test compatability.
  62. // We used to test noWait as well, but that was removed as of iptables v1.6.0
  63. func mustTestableIptables() []*IPTables {
  64. ipt, err := New()
  65. if err != nil {
  66. panic(fmt.Sprintf("New failed: %v", err))
  67. }
  68. ip6t, err := NewWithProtocol(ProtocolIPv6)
  69. if err != nil {
  70. panic(fmt.Sprintf("NewWithProtocol(ProtocolIPv6) failed: %v", err))
  71. }
  72. ipts := []*IPTables{ipt, ip6t}
  73. // ensure we check one variant without built-in checking
  74. if ipt.hasCheck {
  75. i := *ipt
  76. i.hasCheck = false
  77. ipts = append(ipts, &i)
  78. i6 := *ip6t
  79. i6.hasCheck = false
  80. ipts = append(ipts, &i6)
  81. } else {
  82. panic("iptables on this machine is too old -- missing -C")
  83. }
  84. return ipts
  85. }
  86. func TestChain(t *testing.T) {
  87. for _, ipt := range mustTestableIptables() {
  88. runChainTests(t, ipt)
  89. }
  90. }
  91. func runChainTests(t *testing.T, ipt *IPTables) {
  92. t.Logf("testing %s (hasWait=%t, hasCheck=%t)", ipt.path, ipt.hasWait, ipt.hasCheck)
  93. chain := randChain(t)
  94. // Saving the list of chains before executing tests
  95. originaListChain, err := ipt.ListChains("filter")
  96. if err != nil {
  97. t.Fatalf("ListChains of Initial failed: %v", err)
  98. }
  99. // chain shouldn't exist, this will create new
  100. err = ipt.ClearChain("filter", chain)
  101. if err != nil {
  102. t.Fatalf("ClearChain (of missing) failed: %v", err)
  103. }
  104. // chain should be in listChain
  105. listChain, err := ipt.ListChains("filter")
  106. if err != nil {
  107. t.Fatalf("ListChains failed: %v", err)
  108. }
  109. if !contains(listChain, chain) {
  110. t.Fatalf("ListChains doesn't contain the new chain %v", chain)
  111. }
  112. // chain now exists
  113. err = ipt.ClearChain("filter", chain)
  114. if err != nil {
  115. t.Fatalf("ClearChain (of empty) failed: %v", err)
  116. }
  117. // put a simple rule in
  118. err = ipt.Append("filter", chain, "-s", "0/0", "-j", "ACCEPT")
  119. if err != nil {
  120. t.Fatalf("Append failed: %v", err)
  121. }
  122. // can't delete non-empty chain
  123. err = ipt.DeleteChain("filter", chain)
  124. if err == nil {
  125. t.Fatalf("DeleteChain of non-empty chain did not fail")
  126. }
  127. err = ipt.ClearChain("filter", chain)
  128. if err != nil {
  129. t.Fatalf("ClearChain (of non-empty) failed: %v", err)
  130. }
  131. // rename the chain
  132. newChain := randChain(t)
  133. err = ipt.RenameChain("filter", chain, newChain)
  134. if err != nil {
  135. t.Fatalf("RenameChain failed: %v", err)
  136. }
  137. // chain empty, should be ok
  138. err = ipt.DeleteChain("filter", newChain)
  139. if err != nil {
  140. t.Fatalf("DeleteChain of empty chain failed: %v", err)
  141. }
  142. // check that chain is fully gone and that state similar to initial one
  143. listChain, err = ipt.ListChains("filter")
  144. if err != nil {
  145. t.Fatalf("ListChains failed: %v", err)
  146. }
  147. if !reflect.DeepEqual(originaListChain, listChain) {
  148. t.Fatalf("ListChains mismatch: \ngot %#v \nneed %#v", originaListChain, listChain)
  149. }
  150. }
  151. func TestRules(t *testing.T) {
  152. for _, ipt := range mustTestableIptables() {
  153. runRulesTests(t, ipt)
  154. }
  155. }
  156. func runRulesTests(t *testing.T, ipt *IPTables) {
  157. t.Logf("testing %s (hasWait=%t, hasCheck=%t)", getIptablesCommand(ipt.Proto()), ipt.hasWait, ipt.hasCheck)
  158. var address1, address2, subnet1, subnet2 string
  159. if ipt.Proto() == ProtocolIPv6 {
  160. address1 = "2001:db8::1/128"
  161. address2 = "2001:db8::2/128"
  162. subnet1 = "2001:db8:a::/48"
  163. subnet2 = "2001:db8:b::/48"
  164. } else {
  165. address1 = "203.0.113.1/32"
  166. address2 = "203.0.113.2/32"
  167. subnet1 = "192.0.2.0/24"
  168. subnet2 = "198.51.100.0/24"
  169. }
  170. chain := randChain(t)
  171. // chain shouldn't exist, this will create new
  172. err := ipt.ClearChain("filter", chain)
  173. if err != nil {
  174. t.Fatalf("ClearChain (of missing) failed: %v", err)
  175. }
  176. err = ipt.Append("filter", chain, "-s", subnet1, "-d", address1, "-j", "ACCEPT")
  177. if err != nil {
  178. t.Fatalf("Append failed: %v", err)
  179. }
  180. err = ipt.AppendUnique("filter", chain, "-s", subnet1, "-d", address1, "-j", "ACCEPT")
  181. if err != nil {
  182. t.Fatalf("AppendUnique failed: %v", err)
  183. }
  184. err = ipt.Append("filter", chain, "-s", subnet2, "-d", address1, "-j", "ACCEPT")
  185. if err != nil {
  186. t.Fatalf("Append failed: %v", err)
  187. }
  188. err = ipt.Insert("filter", chain, 2, "-s", subnet2, "-d", address2, "-j", "ACCEPT")
  189. if err != nil {
  190. t.Fatalf("Insert failed: %v", err)
  191. }
  192. err = ipt.Insert("filter", chain, 1, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  193. if err != nil {
  194. t.Fatalf("Insert failed: %v", err)
  195. }
  196. err = ipt.Delete("filter", chain, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  197. if err != nil {
  198. t.Fatalf("Delete failed: %v", err)
  199. }
  200. rules, err := ipt.List("filter", chain)
  201. if err != nil {
  202. t.Fatalf("List failed: %v", err)
  203. }
  204. expected := []string{
  205. "-N " + chain,
  206. "-A " + chain + " -s " + subnet1 + " -d " + address1 + " -j ACCEPT",
  207. "-A " + chain + " -s " + subnet2 + " -d " + address2 + " -j ACCEPT",
  208. "-A " + chain + " -s " + subnet2 + " -d " + address1 + " -j ACCEPT",
  209. }
  210. if !reflect.DeepEqual(rules, expected) {
  211. t.Fatalf("List mismatch: \ngot %#v \nneed %#v", rules, expected)
  212. }
  213. rules, err = ipt.ListWithCounters("filter", chain)
  214. if err != nil {
  215. t.Fatalf("ListWithCounters failed: %v", err)
  216. }
  217. expected = []string{
  218. "-N " + chain,
  219. "-A " + chain + " -s " + subnet1 + " -d " + address1 + " -c 0 0 -j ACCEPT",
  220. "-A " + chain + " -s " + subnet2 + " -d " + address2 + " -c 0 0 -j ACCEPT",
  221. "-A " + chain + " -s " + subnet2 + " -d " + address1 + " -c 0 0 -j ACCEPT",
  222. }
  223. if !reflect.DeepEqual(rules, expected) {
  224. t.Fatalf("ListWithCounters mismatch: \ngot %#v \nneed %#v", rules, expected)
  225. }
  226. // Clear the chain that was created.
  227. err = ipt.ClearChain("filter", chain)
  228. if err != nil {
  229. t.Fatalf("Failed to clear test chain: %v", err)
  230. }
  231. // Delete the chain that was created
  232. err = ipt.DeleteChain("filter", chain)
  233. if err != nil {
  234. t.Fatalf("Failed to delete test chain: %v", err)
  235. }
  236. }