File: network_test.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (92 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package util_test

import (
	"fmt"
	"net"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/lxc/incus/v6/internal/ports"
	internalUtil "github.com/lxc/incus/v6/internal/util"
)

func TestCanonicalNetworkAddress(t *testing.T) {
	cases := map[string]string{
		"127.0.0.1":                             "127.0.0.1:8443",
		"127.0.0.1:":                            "127.0.0.1:8443",
		"foo.bar":                               "foo.bar:8443",
		"foo.bar:":                              "foo.bar:8443",
		"foo.bar:8444":                          "foo.bar:8444",
		"192.168.1.1:443":                       "192.168.1.1:443",
		"f921:7358:4510:3fce:ac2e:844:2a35:54e": "[f921:7358:4510:3fce:ac2e:844:2a35:54e]:8443",
		"[f921:7358:4510:3fce:ac2e:844:2a35:54e]":      "[f921:7358:4510:3fce:ac2e:844:2a35:54e]:8443",
		"[f921:7358:4510:3fce:ac2e:844:2a35:54e]:":     "[f921:7358:4510:3fce:ac2e:844:2a35:54e]:8443",
		"[f921:7358:4510:3fce:ac2e:844:2a35:54e]:8444": "[f921:7358:4510:3fce:ac2e:844:2a35:54e]:8444",
	}

	for in, out := range cases {
		t.Run(in, func(t *testing.T) {
			assert.Equal(t, out, internalUtil.CanonicalNetworkAddress(in, ports.HTTPSDefaultPort))
		})
	}
}

func TestIsAddressCovered(t *testing.T) {
	type testCase struct {
		address1 string
		address2 string
		covered  bool
	}

	cases := []testCase{
		{"127.0.0.1:8443", "127.0.0.1:8443", true},
		{"garbage", "127.0.0.1:8443", false},
		{"127.0.0.1:8444", "garbage", false},
		{"127.0.0.1:8444", "127.0.0.1:8443", false},
		{"127.0.0.1:8443", "0.0.0.0:8443", true},
		{"[::1]:8443", "0.0.0.0:8443", false},
		{":8443", "0.0.0.0:8443", false},
		{"127.0.0.1:8443", "[::]:8443", true},
		{"[::1]:8443", "[::]:8443", true},
		{"[::1]:8443", ":8443", true},
		{":8443", "[::]:8443", true},
		{"0.0.0.0:8443", "[::]:8443", true},
		{"10.30.0.8:8443", "[::]", true},
		{"localhost:8443", "127.0.0.1:8443", true},
		{"localhost:8443", "[::]:8443", true},
	}

	// Test some localhost cases too
	ips, err := net.LookupHost("localhost")
	if err == nil && len(ips) > 0 && ips[0] == "127.0.0.1" {
		cases = append(cases, testCase{"127.0.0.1:8443", "localhost:8443", true})
	}

	ips, err = net.LookupHost("ip6-localhost")
	if err == nil && len(ips) > 0 && ips[0] == "::1" {
		cases = append(cases, testCase{"[::1]:8443", "ip6-localhost:8443", true})
	}

	for _, c := range cases {
		t.Run(fmt.Sprintf("%s-%s", c.address1, c.address2), func(t *testing.T) {
			covered := internalUtil.IsAddressCovered(c.address1, c.address2)
			if c.covered {
				assert.True(t, covered)
			} else {
				assert.False(t, covered)
			}
		})
	}
}

// This is a check against Go's stdlib to make sure that when listening to a port without specifying an address,
// then an IPv6 wildcard is assumed.
func TestListenImplicitIPv6Wildcard(t *testing.T) {
	listener, err := net.Listen("tcp", ":9999")
	require.NoError(t, err)
	defer func() { _ = listener.Close() }()

	assert.Equal(t, "[::]:9999", listener.Addr().String())
}