iptables_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // +build linux
  2. package iptables
  3. import (
  4. "net"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "golang.org/x/sync/errgroup"
  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. if !allowXlock && supportsXlock {
  163. supportsXlock = false
  164. defer func() { supportsXlock = true }()
  165. }
  166. ip := net.ParseIP("192.168.1.1")
  167. port := 1234
  168. dstAddr := "172.17.0.1"
  169. dstPort := 4321
  170. proto := "tcp"
  171. group := new(errgroup.Group)
  172. for i := 0; i < 10; i++ {
  173. group.Go(func() error {
  174. return natChain.Forward(Append, ip, port, proto, dstAddr, dstPort, "lo")
  175. })
  176. }
  177. if err := group.Wait(); err != nil {
  178. t.Fatal(err)
  179. }
  180. }
  181. func TestCleanup(t *testing.T) {
  182. var err error
  183. var rules []byte
  184. // Cleanup filter/FORWARD first otherwise output of iptables-save is dirty
  185. link := []string{"-t", string(filterChain.Table),
  186. string(Delete), "FORWARD",
  187. "-o", bridgeName,
  188. "-j", filterChain.Name}
  189. iptable := GetIptable(IPv4)
  190. if _, err = iptable.Raw(link...); err != nil {
  191. t.Fatal(err)
  192. }
  193. filterChain.Remove()
  194. err = iptable.RemoveExistingChain(chainName, Nat)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. rules, err = exec.Command("iptables-save").Output()
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. if strings.Contains(string(rules), chainName) {
  203. t.Fatalf("Removing chain failed. %s found in iptables-save", chainName)
  204. }
  205. }
  206. func TestExistsRaw(t *testing.T) {
  207. testChain1 := "ABCD"
  208. testChain2 := "EFGH"
  209. iptable := GetIptable(IPv4)
  210. _, err := iptable.NewChain(testChain1, Filter, false)
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. defer func() {
  215. iptable.RemoveExistingChain(testChain1, Filter)
  216. }()
  217. _, err = iptable.NewChain(testChain2, Filter, false)
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. defer func() {
  222. iptable.RemoveExistingChain(testChain2, Filter)
  223. }()
  224. // Test detection over full and truncated rule string
  225. input := []struct{ rule []string }{
  226. {[]string{"-s", "172.8.9.9/32", "-j", "ACCEPT"}},
  227. {[]string{"-d", "172.8.9.0/24", "-j", "DROP"}},
  228. {[]string{"-s", "172.0.3.0/24", "-d", "172.17.0.0/24", "-p", "tcp", "-m", "tcp", "--dport", "80", "-j", testChain2}},
  229. {[]string{"-j", "RETURN"}},
  230. }
  231. for i, r := range input {
  232. ruleAdd := append([]string{"-t", string(Filter), "-A", testChain1}, r.rule...)
  233. err = iptable.RawCombinedOutput(ruleAdd...)
  234. if err != nil {
  235. t.Fatalf("i=%d, err: %v", i, err)
  236. }
  237. if !iptable.existsRaw(Filter, testChain1, r.rule...) {
  238. t.Fatalf("Failed to detect rule. i=%d", i)
  239. }
  240. // Truncate the rule
  241. trg := r.rule[len(r.rule)-1]
  242. trg = trg[:len(trg)-2]
  243. r.rule[len(r.rule)-1] = trg
  244. if iptable.existsRaw(Filter, testChain1, r.rule...) {
  245. t.Fatalf("Invalid detection. i=%d", i)
  246. }
  247. }
  248. }
  249. func TestGetVersion(t *testing.T) {
  250. mj, mn, mc := parseVersionNumbers("iptables v1.4.19.1-alpha")
  251. if mj != 1 || mn != 4 || mc != 19 {
  252. t.Fatal("Failed to parse version numbers")
  253. }
  254. }
  255. func TestSupportsCOption(t *testing.T) {
  256. input := []struct {
  257. mj int
  258. mn int
  259. mc int
  260. ok bool
  261. }{
  262. {1, 4, 11, true},
  263. {1, 4, 12, true},
  264. {1, 5, 0, true},
  265. {0, 4, 11, false},
  266. {0, 5, 12, false},
  267. {1, 3, 12, false},
  268. {1, 4, 10, false},
  269. }
  270. for ind, inp := range input {
  271. if inp.ok != supportsCOption(inp.mj, inp.mn, inp.mc) {
  272. t.Fatalf("Incorrect check: %d", ind)
  273. }
  274. }
  275. }