| 1 | /* |
| 2 | * This file is part of the go-nft project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Copyright 2021 Red Hat, Inc. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | package config_test |
| 21 | |
| 22 | import ( |
| 23 | "encoding/json" |
| 24 | "fmt" |
| 25 | "testing" |
| 26 | |
| 27 | assert "github.com/stretchr/testify/require" |
| 28 | |
| 29 | "github.com/networkplumbing/go-nft/nft" |
| 30 | "github.com/networkplumbing/go-nft/nft/schema" |
| 31 | ) |
| 32 | |
| 33 | type ruleAction string |
| 34 | |
| 35 | // Rule Actions |
| 36 | const ( |
| 37 | ruleADD ruleAction = "add" |
| 38 | ruleDELETE ruleAction = "delete" |
| 39 | ) |
| 40 | |
| 41 | func TestRule(t *testing.T) { |
| 42 | testAddRuleWithMatchAndVerdict(t) |
| 43 | testDeleteRule(t) |
| 44 | |
| 45 | testAddRuleWithRowExpression(t) |
| 46 | testAddRuleWithCounter(t) |
| 47 | testAddRuleWithNAT(t) |
| 48 | |
| 49 | testRuleLookup(t) |
| 50 | |
| 51 | testReadRuleWithNumericalExpression(t) |
| 52 | } |
| 53 | |
| 54 | func testAddRuleWithRowExpression(t *testing.T) { |
| 55 | const comment = "mycomment" |
| 56 | |
| 57 | table := nft.NewTable(tableName, nft.FamilyIP) |
| 58 | chain := nft.NewRegularChain(table, chainName) |
| 59 | |
| 60 | t.Run("Add rule with a row expression, check serialization", func(t *testing.T) { |
| 61 | statements, serializedStatements := matchWithRowExpression() |
| 62 | rule := nft.NewRule(table, chain, statements, nil, nil, comment) |
| 63 | |
| 64 | config := nft.NewConfig() |
| 65 | config.AddRule(rule) |
| 66 | |
| 67 | serializedConfig, err := config.ToJSON() |
| 68 | assert.NoError(t, err) |
| 69 | |
| 70 | expectedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 71 | assert.Equal(t, string(expectedConfig), string(serializedConfig)) |
| 72 | }) |
| 73 | |
| 74 | t.Run("Add rule with a row expression, check deserialization", func(t *testing.T) { |
| 75 | statements, serializedStatements := matchWithRowExpression() |
| 76 | |
| 77 | serializedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 78 | |
| 79 | var deserializedConfig nft.Config |
| 80 | assert.NoError(t, json.Unmarshal(serializedConfig, &deserializedConfig)) |
| 81 | |
| 82 | rule := nft.NewRule(table, chain, statements, nil, nil, comment) |
| 83 | expectedConfig := nft.NewConfig() |
| 84 | expectedConfig.AddRule(rule) |
| 85 | |
| 86 | assert.Equal(t, expectedConfig, &deserializedConfig) |
| 87 | }) |
| 88 | } |
| 89 | |
| 90 | func testAddRuleWithMatchAndVerdict(t *testing.T) { |
| 91 | const comment = "mycomment" |
| 92 | |
| 93 | table := nft.NewTable(tableName, nft.FamilyIP) |
| 94 | chain := nft.NewRegularChain(table, chainName) |
| 95 | |
| 96 | t.Run("Add rule with match and verdict, check serialization", func(t *testing.T) { |
| 97 | statements, serializedStatements := matchSrcIP4withReturnVerdict() |
| 98 | rule := nft.NewRule(table, chain, statements, nil, nil, comment) |
| 99 | |
| 100 | config := nft.NewConfig() |
| 101 | config.AddRule(rule) |
| 102 | |
| 103 | serializedConfig, err := config.ToJSON() |
| 104 | assert.NoError(t, err) |
| 105 | |
| 106 | expectedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 107 | assert.Equal(t, string(expectedConfig), string(serializedConfig)) |
| 108 | }) |
| 109 | |
| 110 | t.Run("Add rule with match and verdict, check deserialization", func(t *testing.T) { |
| 111 | statements, serializedStatements := matchSrcIP4withReturnVerdict() |
| 112 | |
| 113 | serializedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 114 | |
| 115 | var deserializedConfig nft.Config |
| 116 | assert.NoError(t, json.Unmarshal(serializedConfig, &deserializedConfig)) |
| 117 | |
| 118 | rule := nft.NewRule(table, chain, statements, nil, nil, comment) |
| 119 | expectedConfig := nft.NewConfig() |
| 120 | expectedConfig.AddRule(rule) |
| 121 | |
| 122 | assert.Equal(t, expectedConfig, &deserializedConfig) |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | func testDeleteRule(t *testing.T) { |
| 127 | table := nft.NewTable(tableName, nft.FamilyIP) |
| 128 | chain := nft.NewRegularChain(table, chainName) |
| 129 | |
| 130 | t.Run("Delete rule", func(t *testing.T) { |
| 131 | handleID := 100 |
| 132 | rule := nft.NewRule(table, chain, nil, &handleID, nil, "") |
| 133 | |
| 134 | config := nft.NewConfig() |
| 135 | config.DeleteRule(rule) |
| 136 | |
| 137 | serializedConfig, err := config.ToJSON() |
| 138 | assert.NoError(t, err) |
| 139 | |
| 140 | expectedConfig := buildSerializedConfig(ruleDELETE, "", &handleID, "") |
| 141 | assert.Equal(t, string(expectedConfig), string(serializedConfig)) |
| 142 | }) |
| 143 | } |
| 144 | |
| 145 | func buildSerializedConfig(action ruleAction, serializedStatements string, handle *int, comment string) []byte { |
| 146 | ruleArgs := fmt.Sprintf(`"family":%q,"table":%q,"chain":%q`, nft.FamilyIP, tableName, chainName) |
| 147 | if serializedStatements != "" { |
| 148 | ruleArgs += "," + serializedStatements |
| 149 | } |
| 150 | if handle != nil { |
| 151 | ruleArgs += fmt.Sprintf(`,"handle":%d`, *handle) |
| 152 | } |
| 153 | if comment != "" { |
| 154 | ruleArgs += fmt.Sprintf(`,"comment":%q`, comment) |
| 155 | } |
| 156 | |
| 157 | var config string |
| 158 | if action == ruleADD { |
| 159 | config = fmt.Sprintf(`{"nftables":[{"rule":{%s}}]}`, ruleArgs) |
| 160 | } else { |
| 161 | config = fmt.Sprintf(`{"nftables":[{%q:{"rule":{%s}}}]}`, action, ruleArgs) |
| 162 | } |
| 163 | return []byte(config) |
| 164 | } |
| 165 | |
| 166 | func matchSrcIP4withReturnVerdict() ([]schema.Statement, string) { |
| 167 | ipAddress := "10.10.10.10" |
| 168 | matchSrcIP4 := schema.Statement{ |
| 169 | Match: &schema.Match{ |
| 170 | Op: schema.OperEQ, |
| 171 | Left: schema.Expression{ |
| 172 | Payload: &schema.Payload{ |
| 173 | Protocol: schema.PayloadProtocolIP4, |
| 174 | Field: schema.PayloadFieldIPSAddr, |
| 175 | }, |
| 176 | }, |
| 177 | Right: schema.Expression{String: &ipAddress}, |
| 178 | }, |
| 179 | } |
| 180 | |
| 181 | verdict := schema.Statement{} |
| 182 | verdict.Return = true |
| 183 | |
| 184 | statements := []schema.Statement{matchSrcIP4, verdict} |
| 185 | |
| 186 | expectedMatch := fmt.Sprintf( |
| 187 | `"match":{"op":"==","left":{"payload":{"protocol":"ip","field":"saddr"}},"right":%q}`, ipAddress, |
| 188 | ) |
| 189 | expectedVerdict := `"return":null` |
| 190 | serializedStatements := fmt.Sprintf(`"expr":[{%s},{%s}]`, expectedMatch, expectedVerdict) |
| 191 | |
| 192 | return statements, serializedStatements |
| 193 | } |
| 194 | |
| 195 | func matchWithRowExpression() ([]schema.Statement, string) { |
| 196 | stringExpression := "string-expression" |
| 197 | rowExpression := `{"foo":"boo"}` |
| 198 | match := schema.Statement{ |
| 199 | Match: &schema.Match{ |
| 200 | Op: schema.OperEQ, |
| 201 | Left: schema.Expression{RowData: json.RawMessage(rowExpression)}, |
| 202 | Right: schema.Expression{String: &stringExpression}, |
| 203 | }, |
| 204 | } |
| 205 | |
| 206 | statements := []schema.Statement{match} |
| 207 | |
| 208 | expectedMatch := fmt.Sprintf(`"match":{"op":"==","left":%s,"right":%q}`, rowExpression, stringExpression) |
| 209 | serializedStatements := fmt.Sprintf(`"expr":[{%s}]`, expectedMatch) |
| 210 | |
| 211 | return statements, serializedStatements |
| 212 | } |
| 213 | |
| 214 | func testRuleLookup(t *testing.T) { |
| 215 | config := nft.NewConfig() |
| 216 | table_br := nft.NewTable("table-br", nft.FamilyBridge) |
| 217 | config.AddTable(table_br) |
| 218 | |
| 219 | chainRegular := nft.NewRegularChain(table_br, "chain-regular") |
| 220 | config.AddChain(chainRegular) |
| 221 | |
| 222 | ruleSimple := nft.NewRule(table_br, chainRegular, nil, nil, nil, "comment123") |
| 223 | config.AddRule(ruleSimple) |
| 224 | |
| 225 | ruleWithStatement := nft.NewRule(table_br, chainRegular, []schema.Statement{{}}, nil, nil, "comment456") |
| 226 | ruleWithStatement.Expr[0].Drop = true |
| 227 | config.AddRule(ruleWithStatement) |
| 228 | |
| 229 | handle := 10 |
| 230 | index := 1 |
| 231 | ruleWithAllParams := nft.NewRule(table_br, chainRegular, []schema.Statement{{}, {}}, &handle, &index, "comment789") |
| 232 | config.AddRule(ruleWithAllParams) |
| 233 | |
| 234 | t.Run("Lookup an existing rule by table, chain and comment", func(t *testing.T) { |
| 235 | rules := config.LookupRule(ruleSimple) |
| 236 | assert.Len(t, rules, 1) |
| 237 | assert.Equal(t, ruleSimple, rules[0]) |
| 238 | }) |
| 239 | |
| 240 | t.Run("Lookup an existing rule by table, chain, statement and comment", func(t *testing.T) { |
| 241 | rules := config.LookupRule(ruleWithStatement) |
| 242 | assert.Len(t, rules, 1) |
| 243 | assert.Equal(t, ruleWithStatement, rules[0]) |
| 244 | }) |
| 245 | |
| 246 | t.Run("Lookup an existing rule by all (root) parameters", func(t *testing.T) { |
| 247 | rules := config.LookupRule(ruleWithAllParams) |
| 248 | assert.Len(t, rules, 1) |
| 249 | assert.Equal(t, ruleWithAllParams, rules[0]) |
| 250 | }) |
| 251 | |
| 252 | t.Run("Lookup a missing rule (comment not matching)", func(t *testing.T) { |
| 253 | rule := nft.NewRule(table_br, chainRegular, nil, nil, nil, "comment-missing") |
| 254 | assert.Empty(t, config.LookupRule(rule)) |
| 255 | }) |
| 256 | |
| 257 | t.Run("Lookup a missing rule (statement content not matching)", func(t *testing.T) { |
| 258 | rule := nft.NewRule(table_br, chainRegular, []schema.Statement{{}}, nil, nil, "comment456") |
| 259 | rule.Expr[0].Drop = false |
| 260 | rule.Expr[0].Return = true |
| 261 | assert.Empty(t, config.LookupRule(rule)) |
| 262 | }) |
| 263 | |
| 264 | t.Run("Lookup a missing rule (statements count not matching)", func(t *testing.T) { |
| 265 | rule := nft.NewRule(table_br, chainRegular, []schema.Statement{{}, {}}, nil, nil, "comment456") |
| 266 | rule.Expr[0].Drop = true |
| 267 | assert.Empty(t, config.LookupRule(rule)) |
| 268 | }) |
| 269 | |
| 270 | t.Run("Lookup a missing rule (handle not matching)", func(t *testing.T) { |
| 271 | changedHandle := 99 |
| 272 | rule := nft.NewRule(table_br, chainRegular, []schema.Statement{{}, {}}, &changedHandle, &index, "comment789") |
| 273 | assert.Empty(t, config.LookupRule(rule)) |
| 274 | }) |
| 275 | } |
| 276 | |
| 277 | func testReadRuleWithNumericalExpression(t *testing.T) { |
| 278 | t.Run("Read rule with numerical expression", func(t *testing.T) { |
| 279 | c := nft.NewConfig() |
| 280 | assert.NoError(t, c.FromJSON([]byte(` |
| 281 | {"nftables":[{"rule":{ |
| 282 | "expr":[{"match":{"op":"==","left":"foo","right":12345}}] |
| 283 | }}]} |
| 284 | `))) |
| 285 | }) |
| 286 | } |
| 287 | |
| 288 | func testAddRuleWithCounter(t *testing.T) { |
| 289 | const comment = "mycomment" |
| 290 | |
| 291 | table := nft.NewTable(tableName, nft.FamilyIP) |
| 292 | chain := nft.NewRegularChain(table, chainName) |
| 293 | |
| 294 | statements, serializedStatements := counterStatements() |
| 295 | rule := nft.NewRule(table, chain, statements, nil, nil, comment) |
| 296 | |
| 297 | t.Run("Add rule with counter, check serialization", func(t *testing.T) { |
| 298 | config := nft.NewConfig() |
| 299 | config.AddRule(rule) |
| 300 | |
| 301 | serializedConfig, err := config.ToJSON() |
| 302 | assert.NoError(t, err) |
| 303 | |
| 304 | expectedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 305 | assert.JSONEq(t, string(expectedConfig), string(serializedConfig)) |
| 306 | }) |
| 307 | |
| 308 | t.Run("Add rule with counter, check deserialization", func(t *testing.T) { |
| 309 | serializedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 310 | |
| 311 | var deserializedConfig nft.Config |
| 312 | assert.NoError(t, json.Unmarshal(serializedConfig, &deserializedConfig)) |
| 313 | |
| 314 | expectedConfig := nft.NewConfig() |
| 315 | expectedConfig.AddRule(rule) |
| 316 | |
| 317 | assert.Equal(t, expectedConfig, &deserializedConfig) |
| 318 | }) |
| 319 | } |
| 320 | |
| 321 | func counterStatements() ([]schema.Statement, string) { |
| 322 | statements := []schema.Statement{{ |
| 323 | Counter: &schema.Counter{ |
| 324 | Packets: 0, |
| 325 | Bytes: 0, |
| 326 | }, |
| 327 | }} |
| 328 | |
| 329 | expectedCounter := `"counter":{"packets":0,"bytes":0}` |
| 330 | serializedStatements := fmt.Sprintf(`"expr":[{%s}]`, expectedCounter) |
| 331 | |
| 332 | return statements, serializedStatements |
| 333 | } |
| 334 | |
| 335 | func testAddRuleWithNAT(t *testing.T) { |
| 336 | tableTests := []struct { |
| 337 | typeName string |
| 338 | createStatements func() ([]schema.Statement, string) |
| 339 | }{ |
| 340 | {"dnat", dNATStatements}, |
| 341 | {"snat", sNATStatements}, |
| 342 | {"masquerade", masqueradeStatements}, |
| 343 | {"redirect", redirectStatements}, |
| 344 | } |
| 345 | for _, tt := range tableTests { |
| 346 | t.Run(fmt.Sprintf("Add rule with %s, check serialization", tt.typeName), func(t *testing.T) { |
| 347 | testSerializationWith(t, dNATStatements) |
| 348 | }) |
| 349 | t.Run(fmt.Sprintf("Add rule with %s, check deserialization", tt.typeName), func(t *testing.T) { |
| 350 | testDeserializationWith(t, dNATStatements) |
| 351 | }) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | func testSerializationWith(t *testing.T, createStatements func() ([]schema.Statement, string)) { |
| 356 | const comment = "mycomment" |
| 357 | |
| 358 | table := nft.NewTable(tableName, nft.FamilyIP) |
| 359 | chain := nft.NewRegularChain(table, chainName) |
| 360 | |
| 361 | statements, serializedStatements := createStatements() |
| 362 | rule := nft.NewRule(table, chain, statements, nil, nil, comment) |
| 363 | |
| 364 | config := nft.NewConfig() |
| 365 | config.AddRule(rule) |
| 366 | |
| 367 | serializedConfig, err := config.ToJSON() |
| 368 | assert.NoError(t, err) |
| 369 | |
| 370 | expectedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 371 | assert.Equal(t, string(expectedConfig), string(serializedConfig)) |
| 372 | } |
| 373 | |
| 374 | func testDeserializationWith(t *testing.T, createStatements func() ([]schema.Statement, string)) { |
| 375 | const comment = "mycomment" |
| 376 | |
| 377 | table := nft.NewTable(tableName, nft.FamilyIP) |
| 378 | chain := nft.NewRegularChain(table, chainName) |
| 379 | |
| 380 | statements, serializedStatements := createStatements() |
| 381 | |
| 382 | serializedConfig := buildSerializedConfig(ruleADD, serializedStatements, nil, comment) |
| 383 | |
| 384 | var deserializedConfig nft.Config |
| 385 | assert.NoError(t, json.Unmarshal(serializedConfig, &deserializedConfig)) |
| 386 | |
| 387 | rule := nft.NewRule(table, chain, statements, nil, nil, comment) |
| 388 | expectedConfig := nft.NewConfig() |
| 389 | expectedConfig.AddRule(rule) |
| 390 | |
| 391 | assert.Equal(t, expectedConfig, &deserializedConfig) |
| 392 | } |
| 393 | |
| 394 | func dNATStatements() ([]schema.Statement, string) { |
| 395 | address0 := "1.2.3.4" |
| 396 | addressWithFamily := schema.Statement{} |
| 397 | familyIP4 := schema.FamilyIP |
| 398 | addressWithFamily.Dnat = &schema.Dnat{ |
| 399 | Addr: &schema.Expression{String: &address0}, |
| 400 | Family: &familyIP4, |
| 401 | } |
| 402 | |
| 403 | portList := schema.Statement{} |
| 404 | portList.Dnat = &schema.Dnat{ |
| 405 | Port: &schema.Expression{RowData: json.RawMessage(`[80,8080]`)}, |
| 406 | } |
| 407 | |
| 408 | var port float64 = 12345 |
| 409 | address1 := "feed::c0fe" |
| 410 | fullHouse := schema.Statement{} |
| 411 | familyIP6 := schema.FamilyIP6 |
| 412 | fullHouse.Dnat = &schema.Dnat{ |
| 413 | Addr: &schema.Expression{String: &address1}, |
| 414 | Family: &familyIP6, |
| 415 | Port: &schema.Expression{Float64: &port}, |
| 416 | Flags: &schema.Flags{Flags: []string{schema.NATFlagRandom, schema.NATFlagPersistent}}, |
| 417 | } |
| 418 | |
| 419 | statements := []schema.Statement{addressWithFamily, portList, fullHouse} |
| 420 | |
| 421 | expectedDNATIP4 := `"dnat":{"addr":"1.2.3.4","family":"ip"}` |
| 422 | expectedDNATMultiPorts := `"dnat":{"port":[80,8080]}` |
| 423 | expectedDNATIP6PortAndFlags := `"dnat":{"addr":"feed::c0fe","family":"ip6","port":12345,"flags":["random","persistent"]}` |
| 424 | serializedStatements := fmt.Sprintf( |
| 425 | `"expr":[{%s},{%s},{%s}]`, expectedDNATIP4, expectedDNATMultiPorts, expectedDNATIP6PortAndFlags, |
| 426 | ) |
| 427 | |
| 428 | return statements, serializedStatements |
| 429 | } |
| 430 | |
| 431 | func sNATStatements() ([]schema.Statement, string) { |
| 432 | address0 := "1.2.3.4" |
| 433 | addressWithFamily := schema.Statement{} |
| 434 | familyIP4 := schema.FamilyIP |
| 435 | addressWithFamily.Snat = &schema.Snat{ |
| 436 | Addr: &schema.Expression{String: &address0}, |
| 437 | Family: &familyIP4, |
| 438 | } |
| 439 | |
| 440 | portList := schema.Statement{} |
| 441 | portList.Snat = &schema.Snat{ |
| 442 | Port: &schema.Expression{RowData: json.RawMessage(`[80,8080]`)}, |
| 443 | } |
| 444 | |
| 445 | var port float64 = 12345 |
| 446 | address1 := "feed::c0fe" |
| 447 | fullHouse := schema.Statement{} |
| 448 | familyIP6 := schema.FamilyIP6 |
| 449 | fullHouse.Snat = &schema.Snat{ |
| 450 | Addr: &schema.Expression{String: &address1}, |
| 451 | Family: &familyIP6, |
| 452 | Port: &schema.Expression{Float64: &port}, |
| 453 | Flags: &schema.Flags{Flags: []string{schema.NATFlagFullyRandom}}, |
| 454 | } |
| 455 | |
| 456 | statements := []schema.Statement{addressWithFamily, portList, fullHouse} |
| 457 | |
| 458 | expectedDNATIP4 := `"snat":{"addr":"1.2.3.4","family":"ip"}` |
| 459 | expectedDNATMultiPorts := `"snat":{"port":[80,8080]}` |
| 460 | expectedDNATIP6PortAndFlag := `"snat":{"addr":"feed::c0fe","family":"ip6","port":12345,"flags":"fully-random"}` |
| 461 | serializedStatements := fmt.Sprintf( |
| 462 | `"expr":[{%s},{%s},{%s}]`, expectedDNATIP4, expectedDNATMultiPorts, expectedDNATIP6PortAndFlag, |
| 463 | ) |
| 464 | |
| 465 | return statements, serializedStatements |
| 466 | } |
| 467 | |
| 468 | func masqueradeStatements() ([]schema.Statement, string) { |
| 469 | basic := schema.Statement{} |
| 470 | basic.Masquerade = &schema.Masquerade{Enabled: true} |
| 471 | |
| 472 | portList := schema.Statement{} |
| 473 | portList.Masquerade = &schema.Masquerade{ |
| 474 | Port: &schema.Expression{RowData: json.RawMessage(`[80,8080]`)}, |
| 475 | } |
| 476 | |
| 477 | var port float64 = 12345 |
| 478 | portAndFlags := schema.Statement{} |
| 479 | portAndFlags.Masquerade = &schema.Masquerade{ |
| 480 | Port: &schema.Expression{Float64: &port}, |
| 481 | Flags: &schema.Flags{Flags: []string{schema.NATFlagFullyRandom}}, |
| 482 | } |
| 483 | |
| 484 | statements := []schema.Statement{basic, portList, portAndFlags} |
| 485 | |
| 486 | expectedMasqueradeNoValues := `"masquerade":null` |
| 487 | expectedMasqueradeMultiPorts := `"masquerade":{"port":[80,8080]}` |
| 488 | expectedMasqueradePortAndFlag := `"masquerade":{"port":12345,"flags":"fully-random"}` |
| 489 | serializedStatements := fmt.Sprintf( |
| 490 | `"expr":[{%s},{%s},{%s}]`, |
| 491 | expectedMasqueradeNoValues, expectedMasqueradeMultiPorts, expectedMasqueradePortAndFlag, |
| 492 | ) |
| 493 | |
| 494 | return statements, serializedStatements |
| 495 | } |
| 496 | |
| 497 | func redirectStatements() ([]schema.Statement, string) { |
| 498 | basic := schema.Statement{} |
| 499 | basic.Redirect = &schema.Redirect{Enabled: true} |
| 500 | |
| 501 | portList := schema.Statement{} |
| 502 | portList.Redirect = &schema.Redirect{ |
| 503 | Port: &schema.Expression{RowData: json.RawMessage(`[80,8080]`)}, |
| 504 | } |
| 505 | |
| 506 | var port float64 = 12345 |
| 507 | portAndFlags := schema.Statement{} |
| 508 | portAndFlags.Redirect = &schema.Redirect{ |
| 509 | Port: &schema.Expression{Float64: &port}, |
| 510 | Flags: &schema.Flags{Flags: []string{schema.NATFlagFullyRandom}}, |
| 511 | } |
| 512 | |
| 513 | statements := []schema.Statement{basic, portList, portAndFlags} |
| 514 | |
| 515 | expectedRedirectNoValues := `"redirect":null` |
| 516 | expectedRedirectMultiPorts := `"redirect":{"port":[80,8080]}` |
| 517 | expectedRedirectPortAndFlag := `"redirect":{"port":12345,"flags":"fully-random"}` |
| 518 | serializedStatements := fmt.Sprintf( |
| 519 | `"expr":[{%s},{%s},{%s}]`, |
| 520 | expectedRedirectNoValues, expectedRedirectMultiPorts, expectedRedirectPortAndFlag, |
| 521 | ) |
| 522 | |
| 523 | return statements, serializedStatements |
| 524 | } |