iptables_test.go 7.5 KB

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