github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/cmd/rootlessport/wsl_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 6 "github.com/containers/common/pkg/machine" 7 "github.com/rootless-containers/rootlesskit/pkg/port" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 type SpecData struct { 12 mach string 13 sourceProto string 14 sourceIP string 15 expectCount int 16 expectProto string 17 expectIP string 18 secondProto string 19 secondIP string 20 } 21 22 func TestDualStackSplit(t *testing.T) { 23 //nolint 24 const ( 25 IP4_ALL = "0.0.0.0" 26 IP4__LO = "127.0.0.1" 27 IP6_ALL = "::" 28 IP6__LO = "::1" 29 TCP_ = "tcp" 30 TCP4 = "tcp4" 31 TCP6 = "tcp6" 32 WSL = "wsl" 33 ___ = "" 34 IP6_REG = "2001:0db8:85a3:0000:0000:8a2e:0370:7334" 35 IP4_REG = "10.0.0.1" 36 ) 37 38 tests := []SpecData{ 39 // Split cases 40 {WSL, TCP_, IP4_ALL, 2, TCP4, IP4_ALL, TCP6, IP6_ALL}, 41 {WSL, TCP_, IP6_ALL, 2, TCP4, IP4_ALL, TCP6, IP6_ALL}, 42 {WSL, TCP_, IP6__LO, 2, TCP4, IP4__LO, TCP6, IP6__LO}, 43 44 // Non-Split 45 {WSL, TCP_, IP4__LO, 1, TCP_, IP4__LO, "", ""}, 46 {WSL, TCP4, IP4_ALL, 1, TCP4, IP4_ALL, "", ""}, 47 {WSL, TCP6, IP6__LO, 1, TCP6, IP6__LO, "", ""}, 48 {WSL, TCP_, IP4_REG, 1, TCP_, IP4_REG, "", ""}, 49 {WSL, TCP_, IP6_REG, 1, TCP_, IP6_REG, "", ""}, 50 {___, TCP_, IP4_ALL, 1, TCP_, IP4_ALL, "", ""}, 51 {___, TCP_, IP6_ALL, 1, TCP_, IP6_ALL, "", ""}, 52 {___, TCP_, IP4__LO, 1, TCP_, IP4__LO, "", ""}, 53 {___, TCP_, IP6__LO, 1, TCP_, IP6__LO, "", ""}, 54 } 55 56 for _, data := range tests { 57 verifySplit(t, data) 58 } 59 } 60 61 func verifySplit(t *testing.T, data SpecData) { 62 machine := machine.GetMachineMarker() 63 oldEnable, oldType := machine.Enabled, machine.Type 64 machine.Enabled, machine.Type = len(data.mach) > 0, data.mach 65 66 source := port.Spec{ 67 Proto: data.sourceProto, 68 ParentIP: data.sourceIP, 69 ParentPort: 100, 70 ChildIP: "1.1.1.1", 71 ChildPort: 200, 72 } 73 expect, second := source, source 74 specs := splitDualStackSpecIfWsl(source) 75 76 assert.Equal(t, data.expectCount, len(specs)) 77 78 expect.Proto = data.expectProto 79 expect.ParentIP = data.expectIP 80 assert.Equal(t, expect, specs[0]) 81 82 if data.expectCount > 1 { 83 second.Proto = data.secondProto 84 second.ParentIP = data.secondIP 85 assert.Equal(t, second, specs[1]) 86 } 87 88 machine.Enabled, machine.Type = oldEnable, oldType 89 }