iptables_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package iptables
  2. import (
  3. "net"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "testing"
  9. _ "github.com/docker/libnetwork/testutils"
  10. )
  11. const chainName = "DOCKEREST"
  12. var natChain *ChainInfo
  13. var filterChain *ChainInfo
  14. var bridgeName string
  15. func TestNewChain(t *testing.T) {
  16. var err error
  17. bridgeName = "lo"
  18. iptable := GetIptable(IPv4)
  19. natChain, err = iptable.NewChain(chainName, Nat, false)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. err = iptable.ProgramChain(natChain, bridgeName, false, true)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. filterChain, err = iptable.NewChain(chainName, Filter, false)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. err = iptable.ProgramChain(filterChain, bridgeName, false, true)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. }
  36. func TestForward(t *testing.T) {
  37. ip := net.ParseIP("192.168.1.1")
  38. port := 1234
  39. dstAddr := "172.17.0.1"
  40. dstPort := 4321
  41. proto := "tcp"
  42. bridgeName := "lo"
  43. iptable := GetIptable(IPv4)
  44. err := natChain.Forward(Insert, ip, port, proto, dstAddr, dstPort, bridgeName)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. dnatRule := []string{
  49. "-d", ip.String(),
  50. "-p", proto,
  51. "--dport", strconv.Itoa(port),
  52. "-j", "DNAT",
  53. "--to-destination", dstAddr + ":" + strconv.Itoa(dstPort),
  54. "!", "-i", bridgeName,
  55. }
  56. if !iptable.Exists(natChain.Table, natChain.Name, dnatRule...) {
  57. t.Fatal("DNAT rule does not exist")
  58. }
  59. filterRule := []string{
  60. "!", "-i", bridgeName,
  61. "-o", bridgeName,
  62. "-d", dstAddr,
  63. "-p", proto,
  64. "--dport", strconv.Itoa(dstPort),
  65. "-j", "ACCEPT",
  66. }
  67. if !iptable.Exists(filterChain.Table, filterChain.Name, filterRule...) {
  68. t.Fatal("filter rule does not exist")
  69. }
  70. masqRule := []string{
  71. "-d", dstAddr,
  72. "-s", dstAddr,
  73. "-p", proto,
  74. "--dport", strconv.Itoa(dstPort),
  75. "-j", "MASQUERADE",
  76. }
  77. if !iptable.Exists(natChain.Table, "POSTROUTING", masqRule...) {
  78. t.Fatal("MASQUERADE rule does not exist")
  79. }
  80. }
  81. func TestLink(t *testing.T) {
  82. var err error
  83. bridgeName := "lo"
  84. iptable := GetIptable(IPv4)
  85. ip1 := net.ParseIP("192.168.1.1")
  86. ip2 := net.ParseIP("192.168.1.2")
  87. port := 1234
  88. proto := "tcp"
  89. err = filterChain.Link(Append, ip1, ip2, port, proto, bridgeName)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. rule1 := []string{
  94. "-i", bridgeName,
  95. "-o", bridgeName,
  96. "-p", proto,
  97. "-s", ip1.String(),
  98. "-d", ip2.String(),
  99. "--dport", strconv.Itoa(port),
  100. "-j", "ACCEPT"}
  101. if !iptable.Exists(filterChain.Table, filterChain.Name, rule1...) {
  102. t.Fatal("rule1 does not exist")
  103. }
  104. rule2 := []string{
  105. "-i", bridgeName,
  106. "-o", bridgeName,
  107. "-p", proto,
  108. "-s", ip2.String(),
  109. "-d", ip1.String(),
  110. "--sport", strconv.Itoa(port),
  111. "-j", "ACCEPT"}
  112. if !iptable.Exists(filterChain.Table, filterChain.Name, rule2...) {
  113. t.Fatal("rule2 does not exist")
  114. }
  115. }
  116. func TestPrerouting(t *testing.T) {
  117. args := []string{
  118. "-i", "lo",
  119. "-d", "192.168.1.1"}
  120. iptable := GetIptable(IPv4)
  121. err := natChain.Prerouting(Insert, args...)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. if !iptable.Exists(natChain.Table, "PREROUTING", args...) {
  126. t.Fatal("rule does not exist")
  127. }
  128. delRule := append([]string{"-D", "PREROUTING", "-t", string(Nat)}, args...)
  129. if _, err = iptable.Raw(delRule...); err != nil {
  130. t.Fatal(err)
  131. }
  132. }
  133. func TestOutput(t *testing.T) {
  134. args := []string{
  135. "-o", "lo",
  136. "-d", "192.168.1.1"}
  137. iptable := GetIptable(IPv4)
  138. err := natChain.Output(Insert, args...)
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. if !iptable.Exists(natChain.Table, "OUTPUT", args...) {
  143. t.Fatal("rule does not exist")
  144. }
  145. delRule := append([]string{"-D", "OUTPUT", "-t",
  146. string(natChain.Table)}, args...)
  147. if _, err = iptable.Raw(delRule...); err != nil {
  148. t.Fatal(err)
  149. }
  150. }
  151. func TestConcurrencyWithWait(t *testing.T) {
  152. RunConcurrencyTest(t, true)
  153. }
  154. func TestConcurrencyNoWait(t *testing.T) {
  155. RunConcurrencyTest(t, false)
  156. }
  157. // Runs 10 concurrent rule additions. This will fail if iptables
  158. // is actually invoked simultaneously without --wait.
  159. // Note that if iptables does not support the xtable lock on this
  160. // system, then allowXlock has no effect -- it will always be off.
  161. func RunConcurrencyTest(t *testing.T, allowXlock bool) {
  162. var wg sync.WaitGroup
  163. if !allowXlock && supportsXlock {
  164. supportsXlock = false
  165. defer func() { supportsXlock = true }()
  166. }
  167. ip := net.ParseIP("192.168.1.1")
  168. port := 1234
  169. dstAddr := "172.17.0.1"
  170. dstPort := 4321
  171. proto := "tcp"
  172. for i := 0; i < 10; i++ {
  173. wg.Add(1)
  174. go func() {
  175. defer wg.Done()
  176. err := natChain.Forward(Append, ip, port, proto, dstAddr, dstPort, "lo")
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. }()
  181. }
  182. wg.Wait()
  183. }
  184. func TestCleanup(t *testing.T) {
  185. var err error
  186. var rules []byte
  187. // Cleanup filter/FORWARD first otherwise output of iptables-save is dirty
  188. link := []string{"-t", string(filterChain.Table),
  189. string(Delete), "FORWARD",
  190. "-o", bridgeName,
  191. "-j", filterChain.Name}
  192. iptable := GetIptable(IPv4)
  193. if _, err = iptable.Raw(link...); err != nil {
  194. t.Fatal(err)
  195. }
  196. filterChain.Remove()
  197. err = iptable.RemoveExistingChain(chainName, Nat)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. rules, err = exec.Command("iptables-save").Output()
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. if strings.Contains(string(rules), chainName) {
  206. t.Fatalf("Removing chain failed. %s found in iptables-save", chainName)
  207. }
  208. }
  209. func TestExistsRaw(t *testing.T) {
  210. testChain1 := "ABCD"
  211. testChain2 := "EFGH"
  212. iptable := GetIptable(IPv4)
  213. _, err := iptable.NewChain(testChain1, Filter, false)
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. defer func() {
  218. iptable.RemoveExistingChain(testChain1, Filter)
  219. }()
  220. _, err = iptable.NewChain(testChain2, Filter, false)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. defer func() {
  225. iptable.RemoveExistingChain(testChain2, Filter)
  226. }()
  227. // Test detection over full and truncated rule string
  228. input := []struct{ rule []string }{
  229. {[]string{"-s", "172.8.9.9/32", "-j", "ACCEPT"}},
  230. {[]string{"-d", "172.8.9.0/24", "-j", "DROP"}},
  231. {[]string{"-s", "172.0.3.0/24", "-d", "172.17.0.0/24", "-p", "tcp", "-m", "tcp", "--dport", "80", "-j", testChain2}},
  232. {[]string{"-j", "RETURN"}},
  233. }
  234. for i, r := range input {
  235. ruleAdd := append([]string{"-t", string(Filter), "-A", testChain1}, r.rule...)
  236. err = iptable.RawCombinedOutput(ruleAdd...)
  237. if err != nil {
  238. t.Fatalf("i=%d, err: %v", i, err)
  239. }
  240. if !iptable.existsRaw(Filter, testChain1, r.rule...) {
  241. t.Fatalf("Failed to detect rule. i=%d", i)
  242. }
  243. // Truncate the rule
  244. trg := r.rule[len(r.rule)-1]
  245. trg = trg[:len(trg)-2]
  246. r.rule[len(r.rule)-1] = trg
  247. if iptable.existsRaw(Filter, testChain1, r.rule...) {
  248. t.Fatalf("Invalid detection. i=%d", i)
  249. }
  250. }
  251. }
  252. func TestGetVersion(t *testing.T) {
  253. mj, mn, mc := parseVersionNumbers("iptables v1.4.19.1-alpha")
  254. if mj != 1 || mn != 4 || mc != 19 {
  255. t.Fatal("Failed to parse version numbers")
  256. }
  257. }
  258. func TestSupportsCOption(t *testing.T) {
  259. input := []struct {
  260. mj int
  261. mn int
  262. mc int
  263. ok bool
  264. }{
  265. {1, 4, 11, true},
  266. {1, 4, 12, true},
  267. {1, 5, 0, true},
  268. {0, 4, 11, false},
  269. {0, 5, 12, false},
  270. {1, 3, 12, false},
  271. {1, 4, 10, false},
  272. }
  273. for ind, inp := range input {
  274. if inp.ok != supportsCOption(inp.mj, inp.mn, inp.mc) {
  275. t.Fatalf("Incorrect check: %d", ind)
  276. }
  277. }
  278. }