iptables_test.go 7.0 KB

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