direct_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2019 Yunion
  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 firewalld
  15. import (
  16. "testing"
  17. )
  18. func TestDirect(t *testing.T) {
  19. d := NewDirect(
  20. NewIP4Rule(0, "nat", "PREROUTING", "-j DNAT --to-destination 10.168.222.99:1099"),
  21. NewIP4Rule(0, "nat", "POSTROUTING", "-j SNAT --to-source :80"),
  22. NewIP4Rule(100, "nat", "POSTROUTING", "-j wg0 -j MASQUERADE"),
  23. )
  24. ruleWant := []string{
  25. `<rule priority="0" table="nat" ipv="ipv4" chain="PREROUTING">-j DNAT --to-destination 10.168.222.99:1099</rule>`,
  26. `<rule priority="0" table="nat" ipv="ipv4" chain="POSTROUTING">-j SNAT --to-source :80</rule>`,
  27. `<rule priority="100" table="nat" ipv="ipv4" chain="POSTROUTING">-j wg0 -j MASQUERADE</rule>`,
  28. }
  29. for i, r := range d.Rules {
  30. got := r.String()
  31. want := ruleWant[i]
  32. if got != want {
  33. t.Errorf("rule %d\n got\n %s\n want\n %s", i, got, want)
  34. }
  35. }
  36. want := `<direct>
  37. <rule priority="0" table="nat" ipv="ipv4" chain="PREROUTING">-j DNAT --to-destination 10.168.222.99:1099</rule>
  38. <rule priority="0" table="nat" ipv="ipv4" chain="POSTROUTING">-j SNAT --to-source :80</rule>
  39. <rule priority="100" table="nat" ipv="ipv4" chain="POSTROUTING">-j wg0 -j MASQUERADE</rule>
  40. </direct>`
  41. got := d.String()
  42. if got != want {
  43. t.Errorf("direct:\n got:\n %s\n want:\n %s", got, want)
  44. }
  45. }