iptables_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package iptables
  2. import (
  3. "net"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "testing"
  9. _ "github.com/docker/libnetwork/netutils"
  10. )
  11. const chainName = "DOCKEREST"
  12. var natChain *Chain
  13. var filterChain *Chain
  14. func TestNewChain(t *testing.T) {
  15. var err error
  16. natChain, err = NewChain(chainName, "lo", Nat, false)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. filterChain, err = NewChain(chainName, "lo", Filter, false)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. }
  25. func TestForward(t *testing.T) {
  26. ip := net.ParseIP("192.168.1.1")
  27. port := 1234
  28. dstAddr := "172.17.0.1"
  29. dstPort := 4321
  30. proto := "tcp"
  31. err := natChain.Forward(Insert, ip, port, proto, dstAddr, dstPort)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. dnatRule := []string{
  36. "-d", ip.String(),
  37. "-p", proto,
  38. "--dport", strconv.Itoa(port),
  39. "-j", "DNAT",
  40. "--to-destination", dstAddr + ":" + strconv.Itoa(dstPort),
  41. }
  42. if !Exists(natChain.Table, natChain.Name, dnatRule...) {
  43. t.Fatalf("DNAT rule does not exist")
  44. }
  45. filterRule := []string{
  46. "!", "-i", filterChain.Bridge,
  47. "-o", filterChain.Bridge,
  48. "-d", dstAddr,
  49. "-p", proto,
  50. "--dport", strconv.Itoa(dstPort),
  51. "-j", "ACCEPT",
  52. }
  53. if !Exists(filterChain.Table, filterChain.Name, filterRule...) {
  54. t.Fatalf("filter rule does not exist")
  55. }
  56. masqRule := []string{
  57. "-d", dstAddr,
  58. "-s", dstAddr,
  59. "-p", proto,
  60. "--dport", strconv.Itoa(dstPort),
  61. "-j", "MASQUERADE",
  62. }
  63. if !Exists(natChain.Table, "POSTROUTING", masqRule...) {
  64. t.Fatalf("MASQUERADE rule does not exist")
  65. }
  66. }
  67. func TestLink(t *testing.T) {
  68. var err error
  69. ip1 := net.ParseIP("192.168.1.1")
  70. ip2 := net.ParseIP("192.168.1.2")
  71. port := 1234
  72. proto := "tcp"
  73. err = filterChain.Link(Append, ip1, ip2, port, proto)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. rule1 := []string{
  78. "-i", filterChain.Bridge,
  79. "-o", filterChain.Bridge,
  80. "-p", proto,
  81. "-s", ip1.String(),
  82. "-d", ip2.String(),
  83. "--dport", strconv.Itoa(port),
  84. "-j", "ACCEPT"}
  85. if !Exists(filterChain.Table, filterChain.Name, rule1...) {
  86. t.Fatalf("rule1 does not exist")
  87. }
  88. rule2 := []string{
  89. "-i", filterChain.Bridge,
  90. "-o", filterChain.Bridge,
  91. "-p", proto,
  92. "-s", ip2.String(),
  93. "-d", ip1.String(),
  94. "--sport", strconv.Itoa(port),
  95. "-j", "ACCEPT"}
  96. if !Exists(filterChain.Table, filterChain.Name, rule2...) {
  97. t.Fatalf("rule2 does not exist")
  98. }
  99. }
  100. func TestPrerouting(t *testing.T) {
  101. args := []string{
  102. "-i", "lo",
  103. "-d", "192.168.1.1"}
  104. err := natChain.Prerouting(Insert, args...)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. rule := []string{
  109. "-j", natChain.Name}
  110. rule = append(rule, args...)
  111. if !Exists(natChain.Table, "PREROUTING", rule...) {
  112. t.Fatalf("rule does not exist")
  113. }
  114. delRule := append([]string{"-D", "PREROUTING", "-t", string(Nat)}, rule...)
  115. if _, err = Raw(delRule...); err != nil {
  116. t.Fatal(err)
  117. }
  118. }
  119. func TestOutput(t *testing.T) {
  120. args := []string{
  121. "-o", "lo",
  122. "-d", "192.168.1.1"}
  123. err := natChain.Output(Insert, args...)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. rule := []string{
  128. "-j", natChain.Name}
  129. rule = append(rule, args...)
  130. if !Exists(natChain.Table, "OUTPUT", rule...) {
  131. t.Fatalf("rule does not exist")
  132. }
  133. delRule := append([]string{"-D", "OUTPUT", "-t",
  134. string(natChain.Table)}, rule...)
  135. if _, err = Raw(delRule...); err != nil {
  136. t.Fatal(err)
  137. }
  138. }
  139. func TestConcurrencyWithWait(t *testing.T) {
  140. RunConcurrencyTest(t, true)
  141. }
  142. func TestConcurrencyNoWait(t *testing.T) {
  143. RunConcurrencyTest(t, false)
  144. }
  145. // Runs 10 concurrent rule additions. This will fail if iptables
  146. // is actually invoked simultaneously without --wait.
  147. // Note that if iptables does not support the xtable lock on this
  148. // system, then allowXlock has no effect -- it will always be off.
  149. func RunConcurrencyTest(t *testing.T, allowXlock bool) {
  150. var wg sync.WaitGroup
  151. if !allowXlock && supportsXlock {
  152. supportsXlock = false
  153. defer func() { supportsXlock = true }()
  154. }
  155. ip := net.ParseIP("192.168.1.1")
  156. port := 1234
  157. dstAddr := "172.17.0.1"
  158. dstPort := 4321
  159. proto := "tcp"
  160. for i := 0; i < 10; i++ {
  161. wg.Add(1)
  162. go func() {
  163. defer wg.Done()
  164. err := natChain.Forward(Append, ip, port, proto, dstAddr, dstPort)
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. }()
  169. }
  170. wg.Wait()
  171. }
  172. func TestCleanup(t *testing.T) {
  173. var err error
  174. var rules []byte
  175. // Cleanup filter/FORWARD first otherwise output of iptables-save is dirty
  176. link := []string{"-t", string(filterChain.Table),
  177. string(Delete), "FORWARD",
  178. "-o", filterChain.Bridge,
  179. "-j", filterChain.Name}
  180. if _, err = Raw(link...); err != nil {
  181. t.Fatal(err)
  182. }
  183. filterChain.Remove()
  184. err = RemoveExistingChain(chainName, Nat)
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. rules, err = exec.Command("iptables-save").Output()
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. if strings.Contains(string(rules), chainName) {
  193. t.Fatalf("Removing chain failed. %s found in iptables-save", chainName)
  194. }
  195. }