File: addr_test.go

package info (click to toggle)
golang-github-spiffe-go-spiffe 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,116 kB
  • sloc: makefile: 157
file content (92 lines) | stat: -rw-r--r-- 2,039 bytes parent folder | download | duplicates (2)
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 workloadapi

import (
	"os"
	"testing"

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

type validateAddressCase struct {
	addr string
	err  string
}

func TestGetDefaultAddress(t *testing.T) {
	if orig, ok := os.LookupEnv(SocketEnv); ok {
		defer os.Setenv(SocketEnv, orig)
	} else {
		defer os.Unsetenv(SocketEnv)
	}

	os.Unsetenv(SocketEnv)
	addr, ok := GetDefaultAddress()
	assert.False(t, ok)
	assert.Equal(t, "", addr)

	os.Setenv(SocketEnv, "ADDRESS")
	addr, ok = GetDefaultAddress()
	assert.True(t, ok)
	assert.Equal(t, "ADDRESS", addr)
}

func TestValidateAddress(t *testing.T) {
	testCases := []validateAddressCase{
		{
			addr: "\t",
			err:  "net/url: invalid control character in URL",
		},
		{
			addr: "blah",
			err:  ErrInvalidEndpointScheme.Error(),
		},
		{
			addr: "tcp:opaque",
			err:  "workload endpoint tcp socket URI must not be opaque",
		},
		{
			addr: "tcp://",
			err:  "workload endpoint tcp socket URI must include a host",
		},
		{
			addr: "tcp://1.2.3.4:5?whatever",
			err:  "workload endpoint tcp socket URI must not include query values",
		},
		{
			addr: "tcp://1.2.3.4:5#whatever",
			err:  "workload endpoint tcp socket URI must not include a fragment",
		},
		{
			addr: "tcp://john:doe@1.2.3.4:5/path",
			err:  "workload endpoint tcp socket URI must not include user info",
		},
		{
			addr: "tcp://1.2.3.4:5/path",
			err:  "workload endpoint tcp socket URI must not include a path",
		},
		{
			addr: "tcp://foo",
			err:  "workload endpoint tcp socket URI host component must be an IP:port",
		},
		{
			addr: "tcp://1.2.3.4",
			err:  "workload endpoint tcp socket URI host component must include a port",
		},
		{
			addr: "tcp://1.2.3.4:5",
			err:  "",
		},
	}
	testCases = append(testCases, validateAddressCasesOS()...)

	for _, testCase := range testCases {
		err := ValidateAddress(testCase.addr)
		if testCase.err != "" {
			require.Error(t, err)
			assert.Contains(t, err.Error(), testCase.err)
			continue
		}
		assert.NoError(t, err)
	}
}