iptables_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. "net"
  20. "os"
  21. "reflect"
  22. "testing"
  23. )
  24. func TestProto(t *testing.T) {
  25. ipt, err := New()
  26. if err != nil {
  27. t.Fatalf("New failed: %v", err)
  28. }
  29. if ipt.Proto() != ProtocolIPv4 {
  30. t.Fatalf("Expected default protocol IPv4, got %v", ipt.Proto())
  31. }
  32. ip4t, err := NewWithProtocol(ProtocolIPv4)
  33. if err != nil {
  34. t.Fatalf("NewWithProtocol(ProtocolIPv4) failed: %v", err)
  35. }
  36. if ip4t.Proto() != ProtocolIPv4 {
  37. t.Fatalf("Expected protocol IPv4, got %v", ip4t.Proto())
  38. }
  39. ip6t, err := NewWithProtocol(ProtocolIPv6)
  40. if err != nil {
  41. t.Fatalf("NewWithProtocol(ProtocolIPv6) failed: %v", err)
  42. }
  43. if ip6t.Proto() != ProtocolIPv6 {
  44. t.Fatalf("Expected protocol IPv6, got %v", ip6t.Proto())
  45. }
  46. }
  47. func TestTimeout(t *testing.T) {
  48. ipt, err := New()
  49. if err != nil {
  50. t.Fatalf("New failed: %v", err)
  51. }
  52. if ipt.timeout != 0 {
  53. t.Fatalf("Expected timeout 0 (wait forever), got %v", ipt.timeout)
  54. }
  55. ipt2, err := New(Timeout(5))
  56. if err != nil {
  57. t.Fatalf("New failed: %v", err)
  58. }
  59. if ipt2.timeout != 5 {
  60. t.Fatalf("Expected timeout 5, got %v", ipt.timeout)
  61. }
  62. }
  63. func randChain(t *testing.T) string {
  64. n, err := rand.Int(rand.Reader, big.NewInt(1000000))
  65. if err != nil {
  66. t.Fatalf("Failed to generate random chain name: %v", err)
  67. }
  68. return "TEST-" + n.String()
  69. }
  70. func contains(list []string, value string) bool {
  71. for _, val := range list {
  72. if val == value {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. // mustTestableIptables returns a list of ip(6)tables handles with various
  79. // features enabled & disabled, to test compatibility.
  80. // We used to test noWait as well, but that was removed as of iptables v1.6.0
  81. func mustTestableIptables() []*IPTables {
  82. ipt, err := New()
  83. if err != nil {
  84. panic(fmt.Sprintf("New failed: %v", err))
  85. }
  86. ip6t, err := NewWithProtocol(ProtocolIPv6)
  87. if err != nil {
  88. panic(fmt.Sprintf("NewWithProtocol(ProtocolIPv6) failed: %v", err))
  89. }
  90. ipts := []*IPTables{ipt, ip6t}
  91. // ensure we check one variant without built-in checking
  92. if ipt.hasCheck {
  93. i := *ipt
  94. i.hasCheck = false
  95. ipts = append(ipts, &i)
  96. i6 := *ip6t
  97. i6.hasCheck = false
  98. ipts = append(ipts, &i6)
  99. } else {
  100. panic("iptables on this machine is too old -- missing -C")
  101. }
  102. return ipts
  103. }
  104. func TestChain(t *testing.T) {
  105. for i, ipt := range mustTestableIptables() {
  106. t.Run(fmt.Sprint(i), func(t *testing.T) {
  107. runChainTests(t, ipt)
  108. })
  109. }
  110. }
  111. func runChainTests(t *testing.T, ipt *IPTables) {
  112. t.Logf("testing %s (hasWait=%t, hasCheck=%t)", ipt.path, ipt.hasWait, ipt.hasCheck)
  113. chain := randChain(t)
  114. // Saving the list of chains before executing tests
  115. originaListChain, err := ipt.ListChains("filter")
  116. if err != nil {
  117. t.Fatalf("ListChains of Initial failed: %v", err)
  118. }
  119. // chain shouldn't exist, this will create new
  120. err = ipt.ClearChain("filter", chain)
  121. if err != nil {
  122. t.Fatalf("ClearChain (of missing) failed: %v", err)
  123. }
  124. // chain should be in listChain
  125. listChain, err := ipt.ListChains("filter")
  126. if err != nil {
  127. t.Fatalf("ListChains failed: %v", err)
  128. }
  129. if !contains(listChain, chain) {
  130. t.Fatalf("ListChains doesn't contain the new chain %v", chain)
  131. }
  132. // ChainExists should find it, too
  133. exists, err := ipt.ChainExists("filter", chain)
  134. if err != nil {
  135. t.Fatalf("ChainExists for existing chain failed: %v", err)
  136. } else if !exists {
  137. t.Fatalf("ChainExists doesn't find existing chain")
  138. }
  139. // chain now exists
  140. err = ipt.ClearChain("filter", chain)
  141. if err != nil {
  142. t.Fatalf("ClearChain (of empty) failed: %v", err)
  143. }
  144. // put a simple rule in
  145. err = ipt.Append("filter", chain, "-s", "0/0", "-j", "ACCEPT")
  146. if err != nil {
  147. t.Fatalf("Append failed: %v", err)
  148. }
  149. // can't delete non-empty chain
  150. err = ipt.DeleteChain("filter", chain)
  151. if err == nil {
  152. t.Fatalf("DeleteChain of non-empty chain did not fail")
  153. }
  154. e, ok := err.(*Error)
  155. if ok && e.IsNotExist() {
  156. t.Fatal("DeleteChain of non-empty chain returned IsNotExist")
  157. }
  158. err = ipt.ClearChain("filter", chain)
  159. if err != nil {
  160. t.Fatalf("ClearChain (of non-empty) failed: %v", err)
  161. }
  162. // rename the chain
  163. newChain := randChain(t)
  164. err = ipt.RenameChain("filter", chain, newChain)
  165. if err != nil {
  166. t.Fatalf("RenameChain failed: %v", err)
  167. }
  168. // chain empty, should be ok
  169. err = ipt.DeleteChain("filter", newChain)
  170. if err != nil {
  171. t.Fatalf("DeleteChain of empty chain failed: %v", err)
  172. }
  173. // check that chain is fully gone and that state similar to initial one
  174. listChain, err = ipt.ListChains("filter")
  175. if err != nil {
  176. t.Fatalf("ListChains failed: %v", err)
  177. }
  178. if !reflect.DeepEqual(originaListChain, listChain) {
  179. t.Fatalf("ListChains mismatch: \ngot %#v \nneed %#v", originaListChain, listChain)
  180. }
  181. // ChainExists must not find it anymore
  182. exists, err = ipt.ChainExists("filter", chain)
  183. if err != nil {
  184. t.Fatalf("ChainExists for non-existing chain failed: %v", err)
  185. } else if exists {
  186. t.Fatalf("ChainExists finds non-existing chain")
  187. }
  188. // test ClearAndDelete
  189. err = ipt.NewChain("filter", chain)
  190. if err != nil {
  191. t.Fatalf("NewChain failed: %v", err)
  192. }
  193. err = ipt.Append("filter", chain, "-j", "ACCEPT")
  194. if err != nil {
  195. t.Fatalf("Append failed: %v", err)
  196. }
  197. err = ipt.ClearAndDeleteChain("filter", chain)
  198. if err != nil {
  199. t.Fatalf("ClearAndDelete failed: %v", err)
  200. }
  201. exists, err = ipt.ChainExists("filter", chain)
  202. if err != nil {
  203. t.Fatalf("ChainExists failed: %v", err)
  204. }
  205. if exists {
  206. t.Fatalf("ClearAndDelete didn't delete the chain")
  207. }
  208. err = ipt.ClearAndDeleteChain("filter", chain)
  209. if err != nil {
  210. t.Fatalf("ClearAndDelete failed for non-existing chain: %v", err)
  211. }
  212. }
  213. func TestRules(t *testing.T) {
  214. for i, ipt := range mustTestableIptables() {
  215. t.Run(fmt.Sprint(i), func(t *testing.T) {
  216. runRulesTests(t, ipt)
  217. })
  218. }
  219. }
  220. func runRulesTests(t *testing.T, ipt *IPTables) {
  221. t.Logf("testing %s (hasWait=%t, hasCheck=%t)", getIptablesCommand(ipt.Proto()), ipt.hasWait, ipt.hasCheck)
  222. var address1, address2, subnet1, subnet2 string
  223. if ipt.Proto() == ProtocolIPv6 {
  224. address1 = "2001:db8::1/128"
  225. address2 = "2001:db8::2/128"
  226. subnet1 = "2001:db8:a::/48"
  227. subnet2 = "2001:db8:b::/48"
  228. } else {
  229. address1 = "203.0.113.1/32"
  230. address2 = "203.0.113.2/32"
  231. subnet1 = "192.0.2.0/24"
  232. subnet2 = "198.51.100.0/24"
  233. }
  234. chain := randChain(t)
  235. // chain shouldn't exist, this will create new
  236. err := ipt.ClearChain("filter", chain)
  237. if err != nil {
  238. t.Fatalf("ClearChain (of missing) failed: %v", err)
  239. }
  240. err = ipt.Append("filter", chain, "-s", subnet1, "-d", address1, "-j", "ACCEPT")
  241. if err != nil {
  242. t.Fatalf("Append failed: %v", err)
  243. }
  244. err = ipt.AppendUnique("filter", chain, "-s", subnet1, "-d", address1, "-j", "ACCEPT")
  245. if err != nil {
  246. t.Fatalf("AppendUnique failed: %v", err)
  247. }
  248. err = ipt.Append("filter", chain, "-s", subnet2, "-d", address1, "-j", "ACCEPT")
  249. if err != nil {
  250. t.Fatalf("Append failed: %v", err)
  251. }
  252. err = ipt.Insert("filter", chain, 2, "-s", subnet2, "-d", address2, "-j", "ACCEPT")
  253. if err != nil {
  254. t.Fatalf("Insert failed: %v", err)
  255. }
  256. err = ipt.InsertUnique("filter", chain, 2, "-s", subnet2, "-d", address2, "-j", "ACCEPT")
  257. if err != nil {
  258. t.Fatalf("Insert failed: %v", err)
  259. }
  260. err = ipt.Insert("filter", chain, 1, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  261. if err != nil {
  262. t.Fatalf("Insert failed: %v", err)
  263. }
  264. err = ipt.Delete("filter", chain, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  265. if err != nil {
  266. t.Fatalf("Delete failed: %v", err)
  267. }
  268. err = ipt.Append("filter", chain, "-s", address1, "-d", subnet2, "-j", "ACCEPT")
  269. if err != nil {
  270. t.Fatalf("Append failed: %v", err)
  271. }
  272. rules, err := ipt.List("filter", chain)
  273. if err != nil {
  274. t.Fatalf("List failed: %v", err)
  275. }
  276. expected := []string{
  277. "-N " + chain,
  278. "-A " + chain + " -s " + subnet1 + " -d " + address1 + " -j ACCEPT",
  279. "-A " + chain + " -s " + subnet2 + " -d " + address2 + " -j ACCEPT",
  280. "-A " + chain + " -s " + subnet2 + " -d " + address1 + " -j ACCEPT",
  281. "-A " + chain + " -s " + address1 + " -d " + subnet2 + " -j ACCEPT",
  282. }
  283. if !reflect.DeepEqual(rules, expected) {
  284. t.Fatalf("List mismatch: \ngot %#v \nneed %#v", rules, expected)
  285. }
  286. rules, err = ipt.ListWithCounters("filter", chain)
  287. if err != nil {
  288. t.Fatalf("ListWithCounters failed: %v", err)
  289. }
  290. suffix := " -c 0 0 -j ACCEPT"
  291. if ipt.mode == "nf_tables" {
  292. suffix = " -j ACCEPT -c 0 0"
  293. }
  294. expected = []string{
  295. "-N " + chain,
  296. "-A " + chain + " -s " + subnet1 + " -d " + address1 + suffix,
  297. "-A " + chain + " -s " + subnet2 + " -d " + address2 + suffix,
  298. "-A " + chain + " -s " + subnet2 + " -d " + address1 + suffix,
  299. "-A " + chain + " -s " + address1 + " -d " + subnet2 + suffix,
  300. }
  301. if !reflect.DeepEqual(rules, expected) {
  302. t.Fatalf("ListWithCounters mismatch: \ngot %#v \nneed %#v", rules, expected)
  303. }
  304. stats, err := ipt.Stats("filter", chain)
  305. if err != nil {
  306. t.Fatalf("Stats failed: %v", err)
  307. }
  308. opt := "--"
  309. if ipt.proto == ProtocolIPv6 {
  310. opt = " "
  311. }
  312. expectedStats := [][]string{
  313. {"0", "0", "ACCEPT", "all", opt, "*", "*", subnet1, address1, ""},
  314. {"0", "0", "ACCEPT", "all", opt, "*", "*", subnet2, address2, ""},
  315. {"0", "0", "ACCEPT", "all", opt, "*", "*", subnet2, address1, ""},
  316. {"0", "0", "ACCEPT", "all", opt, "*", "*", address1, subnet2, ""},
  317. }
  318. if !reflect.DeepEqual(stats, expectedStats) {
  319. t.Fatalf("Stats mismatch: \ngot %#v \nneed %#v", stats, expectedStats)
  320. }
  321. structStats, err := ipt.StructuredStats("filter", chain)
  322. if err != nil {
  323. t.Fatalf("StructuredStats failed: %v", err)
  324. }
  325. // It's okay to not check the following errors as they will be evaluated
  326. // in the subsequent usage
  327. _, address1CIDR, _ := net.ParseCIDR(address1)
  328. _, address2CIDR, _ := net.ParseCIDR(address2)
  329. _, subnet1CIDR, _ := net.ParseCIDR(subnet1)
  330. _, subnet2CIDR, _ := net.ParseCIDR(subnet2)
  331. expectedStructStats := []Stat{
  332. {0, 0, "ACCEPT", "all", opt, "*", "*", subnet1CIDR, address1CIDR, ""},
  333. {0, 0, "ACCEPT", "all", opt, "*", "*", subnet2CIDR, address2CIDR, ""},
  334. {0, 0, "ACCEPT", "all", opt, "*", "*", subnet2CIDR, address1CIDR, ""},
  335. {0, 0, "ACCEPT", "all", opt, "*", "*", address1CIDR, subnet2CIDR, ""},
  336. }
  337. if !reflect.DeepEqual(structStats, expectedStructStats) {
  338. t.Fatalf("StructuredStats mismatch: \ngot %#v \nneed %#v",
  339. structStats, expectedStructStats)
  340. }
  341. for i, stat := range expectedStats {
  342. stat, err := ipt.ParseStat(stat)
  343. if err != nil {
  344. t.Fatalf("ParseStat failed: %v", err)
  345. }
  346. if !reflect.DeepEqual(stat, expectedStructStats[i]) {
  347. t.Fatalf("ParseStat mismatch: \ngot %#v \nneed %#v",
  348. stat, expectedStructStats[i])
  349. }
  350. }
  351. err = ipt.DeleteIfExists("filter", chain, "-s", address1, "-d", subnet2, "-j", "ACCEPT")
  352. if err != nil {
  353. t.Fatalf("DeleteIfExists failed for existing rule: %v", err)
  354. }
  355. err = ipt.DeleteIfExists("filter", chain, "-s", address1, "-d", subnet2, "-j", "ACCEPT")
  356. if err != nil {
  357. t.Fatalf("DeleteIfExists failed for non-existing rule: %v", err)
  358. }
  359. // Clear the chain that was created.
  360. err = ipt.ClearChain("filter", chain)
  361. if err != nil {
  362. t.Fatalf("Failed to clear test chain: %v", err)
  363. }
  364. // Delete the chain that was created
  365. err = ipt.DeleteChain("filter", chain)
  366. if err != nil {
  367. t.Fatalf("Failed to delete test chain: %v", err)
  368. }
  369. }
  370. // TestError checks that we're OK when iptables fails to execute
  371. func TestError(t *testing.T) {
  372. ipt, err := New()
  373. if err != nil {
  374. t.Fatalf("failed to init: %v", err)
  375. }
  376. chain := randChain(t)
  377. _, err = ipt.List("filter", chain)
  378. if err == nil {
  379. t.Fatalf("no error with invalid params")
  380. }
  381. switch e := err.(type) {
  382. case *Error:
  383. // OK
  384. default:
  385. t.Fatalf("expected type iptables.Error, got %t", e)
  386. }
  387. // Now set an invalid binary path
  388. ipt.path = "/does-not-exist"
  389. _, err = ipt.ListChains("filter")
  390. if err == nil {
  391. t.Fatalf("no error with invalid ipt binary")
  392. }
  393. switch e := err.(type) {
  394. case *os.PathError:
  395. // OK
  396. default:
  397. t.Fatalf("expected type os.PathError, got %t", e)
  398. }
  399. }
  400. func TestIsNotExist(t *testing.T) {
  401. ipt, err := New()
  402. if err != nil {
  403. t.Fatalf("failed to init: %v", err)
  404. }
  405. // Create a chain, add a rule
  406. chainName := randChain(t)
  407. err = ipt.NewChain("filter", chainName)
  408. if err != nil {
  409. t.Fatal(err)
  410. }
  411. defer func() {
  412. if err := ipt.ClearChain("filter", chainName); err != nil {
  413. t.Fatal(err)
  414. }
  415. if err := ipt.DeleteChain("filter", chainName); err != nil {
  416. t.Fatal(err)
  417. }
  418. }()
  419. err = ipt.Append("filter", chainName, "-p", "tcp", "-j", "DROP")
  420. if err != nil {
  421. t.Fatal(err)
  422. }
  423. // Delete rule twice
  424. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  425. if err != nil {
  426. t.Fatal(err)
  427. }
  428. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  429. if err == nil {
  430. t.Fatal("delete twice got no error...")
  431. }
  432. e, ok := err.(*Error)
  433. if !ok {
  434. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  435. }
  436. if !e.IsNotExist() {
  437. t.Fatal("IsNotExist returned false, expected true")
  438. }
  439. // Delete chain
  440. err = ipt.DeleteChain("filter", chainName)
  441. if err != nil {
  442. t.Fatal(err)
  443. }
  444. err = ipt.DeleteChain("filter", chainName)
  445. if err == nil {
  446. t.Fatal("deletechain twice got no error...")
  447. }
  448. e, ok = err.(*Error)
  449. if !ok {
  450. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  451. }
  452. if !e.IsNotExist() {
  453. t.Fatal("IsNotExist returned false, expected true")
  454. }
  455. // iptables may add more logs to the errors msgs
  456. e.msg = "Another app is currently holding the xtables lock; waiting (1s) for it to exit..." + e.msg
  457. if !e.IsNotExist() {
  458. t.Fatal("IsNotExist returned false, expected true")
  459. }
  460. }
  461. func TestIsNotExistForIPv6(t *testing.T) {
  462. ipt, err := NewWithProtocol(ProtocolIPv6)
  463. if err != nil {
  464. t.Fatalf("failed to init: %v", err)
  465. }
  466. // Create a chain, add a rule
  467. chainName := randChain(t)
  468. err = ipt.NewChain("filter", chainName)
  469. if err != nil {
  470. t.Fatal(err)
  471. }
  472. defer func() {
  473. if err := ipt.ClearChain("filter", chainName); err != nil {
  474. t.Fatal(err)
  475. }
  476. if err := ipt.DeleteChain("filter", chainName); err != nil {
  477. t.Fatal(err)
  478. }
  479. }()
  480. err = ipt.Append("filter", chainName, "-p", "tcp", "-j", "DROP")
  481. if err != nil {
  482. t.Fatal(err)
  483. }
  484. // Delete rule twice
  485. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  486. if err != nil {
  487. t.Fatal(err)
  488. }
  489. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  490. if err == nil {
  491. t.Fatal("delete twice got no error...")
  492. }
  493. e, ok := err.(*Error)
  494. if !ok {
  495. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  496. }
  497. if !e.IsNotExist() {
  498. t.Fatal("IsNotExist returned false, expected true")
  499. }
  500. // Delete chain
  501. err = ipt.DeleteChain("filter", chainName)
  502. if err != nil {
  503. t.Fatal(err)
  504. }
  505. err = ipt.DeleteChain("filter", chainName)
  506. if err == nil {
  507. t.Fatal("deletechain twice got no error...")
  508. }
  509. e, ok = err.(*Error)
  510. if !ok {
  511. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  512. }
  513. if !e.IsNotExist() {
  514. t.Fatal("IsNotExist returned false, expected true")
  515. }
  516. // iptables may add more logs to the errors msgs
  517. e.msg = "Another app is currently holding the xtables lock; waiting (1s) for it to exit..." + e.msg
  518. if !e.IsNotExist() {
  519. t.Fatal("IsNotExist returned false, expected true")
  520. }
  521. }
  522. func TestFilterRuleOutput(t *testing.T) {
  523. testCases := []struct {
  524. name string
  525. in string
  526. out string
  527. }{
  528. {
  529. "legacy output",
  530. "-A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT",
  531. "-A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT",
  532. },
  533. {
  534. "nft output",
  535. "[99:42] -A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT",
  536. "-A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT -c 99 42",
  537. },
  538. }
  539. for _, tt := range testCases {
  540. t.Run(tt.name, func(t *testing.T) {
  541. actual := filterRuleOutput(tt.in)
  542. if actual != tt.out {
  543. t.Fatalf("expect %s actual %s", tt.out, actual)
  544. }
  545. })
  546. }
  547. }
  548. func TestExtractIptablesVersion(t *testing.T) {
  549. testCases := []struct {
  550. in string
  551. v1, v2, v3 int
  552. mode string
  553. err bool
  554. }{
  555. {
  556. "iptables v1.8.0 (nf_tables)",
  557. 1, 8, 0,
  558. "nf_tables",
  559. false,
  560. },
  561. {
  562. "iptables v1.8.0 (legacy)",
  563. 1, 8, 0,
  564. "legacy",
  565. false,
  566. },
  567. {
  568. "iptables v1.6.2",
  569. 1, 6, 2,
  570. "legacy",
  571. false,
  572. },
  573. }
  574. for i, tt := range testCases {
  575. t.Run(fmt.Sprint(i), func(t *testing.T) {
  576. v1, v2, v3, mode, err := extractIptablesVersion(tt.in)
  577. if err == nil && tt.err {
  578. t.Fatal("expected err, got none")
  579. } else if err != nil && !tt.err {
  580. t.Fatalf("unexpected err %s", err)
  581. }
  582. if v1 != tt.v1 || v2 != tt.v2 || v3 != tt.v3 || mode != tt.mode {
  583. t.Fatalf("expected %d %d %d %s, got %d %d %d %s",
  584. tt.v1, tt.v2, tt.v3, tt.mode,
  585. v1, v2, v3, mode)
  586. }
  587. })
  588. }
  589. }