iptables_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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.Insert("filter", chain, 1, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  257. if err != nil {
  258. t.Fatalf("Insert failed: %v", err)
  259. }
  260. err = ipt.Delete("filter", chain, "-s", subnet1, "-d", address2, "-j", "ACCEPT")
  261. if err != nil {
  262. t.Fatalf("Delete failed: %v", err)
  263. }
  264. err = ipt.Append("filter", chain, "-s", address1, "-d", subnet2, "-j", "ACCEPT")
  265. if err != nil {
  266. t.Fatalf("Append failed: %v", err)
  267. }
  268. rules, err := ipt.List("filter", chain)
  269. if err != nil {
  270. t.Fatalf("List failed: %v", err)
  271. }
  272. expected := []string{
  273. "-N " + chain,
  274. "-A " + chain + " -s " + subnet1 + " -d " + address1 + " -j ACCEPT",
  275. "-A " + chain + " -s " + subnet2 + " -d " + address2 + " -j ACCEPT",
  276. "-A " + chain + " -s " + subnet2 + " -d " + address1 + " -j ACCEPT",
  277. "-A " + chain + " -s " + address1 + " -d " + subnet2 + " -j ACCEPT",
  278. }
  279. if !reflect.DeepEqual(rules, expected) {
  280. t.Fatalf("List mismatch: \ngot %#v \nneed %#v", rules, expected)
  281. }
  282. rules, err = ipt.ListWithCounters("filter", chain)
  283. if err != nil {
  284. t.Fatalf("ListWithCounters failed: %v", err)
  285. }
  286. suffix := " -c 0 0 -j ACCEPT"
  287. if ipt.mode == "nf_tables" {
  288. suffix = " -j ACCEPT -c 0 0"
  289. }
  290. expected = []string{
  291. "-N " + chain,
  292. "-A " + chain + " -s " + subnet1 + " -d " + address1 + suffix,
  293. "-A " + chain + " -s " + subnet2 + " -d " + address2 + suffix,
  294. "-A " + chain + " -s " + subnet2 + " -d " + address1 + suffix,
  295. "-A " + chain + " -s " + address1 + " -d " + subnet2 + suffix,
  296. }
  297. if !reflect.DeepEqual(rules, expected) {
  298. t.Fatalf("ListWithCounters mismatch: \ngot %#v \nneed %#v", rules, expected)
  299. }
  300. stats, err := ipt.Stats("filter", chain)
  301. if err != nil {
  302. t.Fatalf("Stats failed: %v", err)
  303. }
  304. opt := "--"
  305. if ipt.proto == ProtocolIPv6 {
  306. opt = " "
  307. }
  308. expectedStats := [][]string{
  309. {"0", "0", "ACCEPT", "all", opt, "*", "*", subnet1, address1, ""},
  310. {"0", "0", "ACCEPT", "all", opt, "*", "*", subnet2, address2, ""},
  311. {"0", "0", "ACCEPT", "all", opt, "*", "*", subnet2, address1, ""},
  312. {"0", "0", "ACCEPT", "all", opt, "*", "*", address1, subnet2, ""},
  313. }
  314. if !reflect.DeepEqual(stats, expectedStats) {
  315. t.Fatalf("Stats mismatch: \ngot %#v \nneed %#v", stats, expectedStats)
  316. }
  317. structStats, err := ipt.StructuredStats("filter", chain)
  318. if err != nil {
  319. t.Fatalf("StructuredStats failed: %v", err)
  320. }
  321. // It's okay to not check the following errors as they will be evaluated
  322. // in the subsequent usage
  323. _, address1CIDR, _ := net.ParseCIDR(address1)
  324. _, address2CIDR, _ := net.ParseCIDR(address2)
  325. _, subnet1CIDR, _ := net.ParseCIDR(subnet1)
  326. _, subnet2CIDR, _ := net.ParseCIDR(subnet2)
  327. expectedStructStats := []Stat{
  328. {0, 0, "ACCEPT", "all", opt, "*", "*", subnet1CIDR, address1CIDR, ""},
  329. {0, 0, "ACCEPT", "all", opt, "*", "*", subnet2CIDR, address2CIDR, ""},
  330. {0, 0, "ACCEPT", "all", opt, "*", "*", subnet2CIDR, address1CIDR, ""},
  331. {0, 0, "ACCEPT", "all", opt, "*", "*", address1CIDR, subnet2CIDR, ""},
  332. }
  333. if !reflect.DeepEqual(structStats, expectedStructStats) {
  334. t.Fatalf("StructuredStats mismatch: \ngot %#v \nneed %#v",
  335. structStats, expectedStructStats)
  336. }
  337. for i, stat := range expectedStats {
  338. stat, err := ipt.ParseStat(stat)
  339. if err != nil {
  340. t.Fatalf("ParseStat failed: %v", err)
  341. }
  342. if !reflect.DeepEqual(stat, expectedStructStats[i]) {
  343. t.Fatalf("ParseStat mismatch: \ngot %#v \nneed %#v",
  344. stat, expectedStructStats[i])
  345. }
  346. }
  347. err = ipt.DeleteIfExists("filter", chain, "-s", address1, "-d", subnet2, "-j", "ACCEPT")
  348. if err != nil {
  349. t.Fatalf("DeleteIfExists failed for existing rule: %v", err)
  350. }
  351. err = ipt.DeleteIfExists("filter", chain, "-s", address1, "-d", subnet2, "-j", "ACCEPT")
  352. if err != nil {
  353. t.Fatalf("DeleteIfExists failed for non-existing rule: %v", err)
  354. }
  355. // Clear the chain that was created.
  356. err = ipt.ClearChain("filter", chain)
  357. if err != nil {
  358. t.Fatalf("Failed to clear test chain: %v", err)
  359. }
  360. // Delete the chain that was created
  361. err = ipt.DeleteChain("filter", chain)
  362. if err != nil {
  363. t.Fatalf("Failed to delete test chain: %v", err)
  364. }
  365. }
  366. // TestError checks that we're OK when iptables fails to execute
  367. func TestError(t *testing.T) {
  368. ipt, err := New()
  369. if err != nil {
  370. t.Fatalf("failed to init: %v", err)
  371. }
  372. chain := randChain(t)
  373. _, err = ipt.List("filter", chain)
  374. if err == nil {
  375. t.Fatalf("no error with invalid params")
  376. }
  377. switch e := err.(type) {
  378. case *Error:
  379. // OK
  380. default:
  381. t.Fatalf("expected type iptables.Error, got %t", e)
  382. }
  383. // Now set an invalid binary path
  384. ipt.path = "/does-not-exist"
  385. _, err = ipt.ListChains("filter")
  386. if err == nil {
  387. t.Fatalf("no error with invalid ipt binary")
  388. }
  389. switch e := err.(type) {
  390. case *os.PathError:
  391. // OK
  392. default:
  393. t.Fatalf("expected type os.PathError, got %t", e)
  394. }
  395. }
  396. func TestIsNotExist(t *testing.T) {
  397. ipt, err := New()
  398. if err != nil {
  399. t.Fatalf("failed to init: %v", err)
  400. }
  401. // Create a chain, add a rule
  402. chainName := randChain(t)
  403. err = ipt.NewChain("filter", chainName)
  404. if err != nil {
  405. t.Fatal(err)
  406. }
  407. defer func() {
  408. ipt.ClearChain("filter", chainName)
  409. ipt.DeleteChain("filter", chainName)
  410. }()
  411. err = ipt.Append("filter", chainName, "-p", "tcp", "-j", "DROP")
  412. if err != nil {
  413. t.Fatal(err)
  414. }
  415. // Delete rule twice
  416. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  417. if err != nil {
  418. t.Fatal(err)
  419. }
  420. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  421. if err == nil {
  422. t.Fatal("delete twice got no error...")
  423. }
  424. e, ok := err.(*Error)
  425. if !ok {
  426. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  427. }
  428. if !e.IsNotExist() {
  429. t.Fatal("IsNotExist returned false, expected true")
  430. }
  431. // Delete chain
  432. err = ipt.DeleteChain("filter", chainName)
  433. if err != nil {
  434. t.Fatal(err)
  435. }
  436. err = ipt.DeleteChain("filter", chainName)
  437. if err == nil {
  438. t.Fatal("deletechain twice got no error...")
  439. }
  440. e, ok = err.(*Error)
  441. if !ok {
  442. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  443. }
  444. if !e.IsNotExist() {
  445. t.Fatal("IsNotExist returned false, expected true")
  446. }
  447. // iptables may add more logs to the errors msgs
  448. e.msg = "Another app is currently holding the xtables lock; waiting (1s) for it to exit..." + e.msg
  449. if !e.IsNotExist() {
  450. t.Fatal("IsNotExist returned false, expected true")
  451. }
  452. }
  453. func TestIsNotExistForIPv6(t *testing.T) {
  454. ipt, err := NewWithProtocol(ProtocolIPv6)
  455. if err != nil {
  456. t.Fatalf("failed to init: %v", err)
  457. }
  458. // Create a chain, add a rule
  459. chainName := randChain(t)
  460. err = ipt.NewChain("filter", chainName)
  461. if err != nil {
  462. t.Fatal(err)
  463. }
  464. defer func() {
  465. ipt.ClearChain("filter", chainName)
  466. ipt.DeleteChain("filter", chainName)
  467. }()
  468. err = ipt.Append("filter", chainName, "-p", "tcp", "-j", "DROP")
  469. if err != nil {
  470. t.Fatal(err)
  471. }
  472. // Delete rule twice
  473. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. err = ipt.Delete("filter", chainName, "-p", "tcp", "-j", "DROP")
  478. if err == nil {
  479. t.Fatal("delete twice got no error...")
  480. }
  481. e, ok := err.(*Error)
  482. if !ok {
  483. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  484. }
  485. if !e.IsNotExist() {
  486. t.Fatal("IsNotExist returned false, expected true")
  487. }
  488. // Delete chain
  489. err = ipt.DeleteChain("filter", chainName)
  490. if err != nil {
  491. t.Fatal(err)
  492. }
  493. err = ipt.DeleteChain("filter", chainName)
  494. if err == nil {
  495. t.Fatal("deletechain twice got no error...")
  496. }
  497. e, ok = err.(*Error)
  498. if !ok {
  499. t.Fatalf("Got wrong error type, expected iptables.Error, got %T", err)
  500. }
  501. if !e.IsNotExist() {
  502. t.Fatal("IsNotExist returned false, expected true")
  503. }
  504. // iptables may add more logs to the errors msgs
  505. e.msg = "Another app is currently holding the xtables lock; waiting (1s) for it to exit..." + e.msg
  506. if !e.IsNotExist() {
  507. t.Fatal("IsNotExist returned false, expected true")
  508. }
  509. }
  510. func TestFilterRuleOutput(t *testing.T) {
  511. testCases := []struct {
  512. name string
  513. in string
  514. out string
  515. }{
  516. {
  517. "legacy output",
  518. "-A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT",
  519. "-A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT",
  520. },
  521. {
  522. "nft output",
  523. "[99:42] -A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT",
  524. "-A foo1 -p tcp -m tcp --dport 1337 -j ACCEPT -c 99 42",
  525. },
  526. }
  527. for _, tt := range testCases {
  528. t.Run(tt.name, func(t *testing.T) {
  529. actual := filterRuleOutput(tt.in)
  530. if actual != tt.out {
  531. t.Fatalf("expect %s actual %s", tt.out, actual)
  532. }
  533. })
  534. }
  535. }
  536. func TestExtractIptablesVersion(t *testing.T) {
  537. testCases := []struct {
  538. in string
  539. v1, v2, v3 int
  540. mode string
  541. err bool
  542. }{
  543. {
  544. "iptables v1.8.0 (nf_tables)",
  545. 1, 8, 0,
  546. "nf_tables",
  547. false,
  548. },
  549. {
  550. "iptables v1.8.0 (legacy)",
  551. 1, 8, 0,
  552. "legacy",
  553. false,
  554. },
  555. {
  556. "iptables v1.6.2",
  557. 1, 6, 2,
  558. "legacy",
  559. false,
  560. },
  561. }
  562. for i, tt := range testCases {
  563. t.Run(fmt.Sprint(i), func(t *testing.T) {
  564. v1, v2, v3, mode, err := extractIptablesVersion(tt.in)
  565. if err == nil && tt.err {
  566. t.Fatal("expected err, got none")
  567. } else if err != nil && !tt.err {
  568. t.Fatalf("unexpected err %s", err)
  569. }
  570. if v1 != tt.v1 || v2 != tt.v2 || v3 != tt.v3 || mode != tt.mode {
  571. t.Fatalf("expected %d %d %d %s, got %d %d %d %s",
  572. tt.v1, tt.v2, tt.v3, tt.mode,
  573. v1, v2, v3, mode)
  574. }
  575. })
  576. }
  577. }