| 1 | |
| 2 | |
| 3 | |
| 4 | package portfwd |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | |
| 9 | "gotest.tools/v3/assert" |
| 10 | |
| 11 | "github.com/lima-vm/lima/v2/pkg/guestagent/api" |
| 12 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 13 | "github.com/lima-vm/lima/v2/pkg/limayaml" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | |
| 18 | func testForwarder() *Forwarder { |
| 19 | rules := []limatype.PortForward{ |
| 20 | {GuestPortRange: [2]int{1, 65535}, Proto: limatype.ProtoTCP, Ignore: true}, |
| 21 | {}, |
| 22 | } |
| 23 | for i := range rules { |
| 24 | limayaml.FillPortForwardDefaults(&rules[i], "", limatype.User{}, nil) |
| 25 | } |
| 26 | return &Forwarder{rules: rules} |
| 27 | } |
| 28 | |
| 29 | func TestForwardingAddressesRejectsUnknownProto(t *testing.T) { |
| 30 | fw := testForwarder() |
| 31 | for _, proto := range []string{"tcp6", "TCP", "any", ""} { |
| 32 | hostAddr, _ := fw.forwardingAddresses(&api.IPPort{Ip: "127.0.0.1", Port: 8080, Protocol: proto}) |
| 33 | assert.Equal(t, hostAddr, "", "proto %#q", proto) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestForwardingAddressesHonorsIgnoredProto(t *testing.T) { |
| 38 | fw := testForwarder() |
| 39 | tcpAddr, _ := fw.forwardingAddresses(&api.IPPort{Ip: "127.0.0.1", Port: 8080, Protocol: limatype.ProtoTCP}) |
| 40 | assert.Equal(t, tcpAddr, "") |
| 41 | udpAddr, _ := fw.forwardingAddresses(&api.IPPort{Ip: "127.0.0.1", Port: 8080, Protocol: limatype.ProtoUDP}) |
| 42 | assert.Equal(t, udpAddr, "127.0.0.1:8080") |
| 43 | } |