test: improve test to ensure ipv6 compatability (#5113)

This commit is contained in:
Damian Debkowski authored and GitHub committed 2024-09-19 08:55:27 -07:00
1 parent 61c1544446
commit 9576ed40dc
12 files changed
+116 -45

No files matched your search

+7 -1
View File
@@ -23,11 +23,17 @@ func TestConfigSetAddress(t *testing.T) {
tests := []test{
{
"bare",
"ipv4",
"http://127.0.0.1:9200",
"http://127.0.0.1:9200",
"",
},
{
"ipv6",
"http://[::1]:9200",
"http://[::1]:9200",
"",
},
{
"bare with version",
"http://127.0.0.1:9200/v1",
+3
View File
@@ -169,6 +169,9 @@ func testSessionAuth(t *testing.T) *targets.SessionAuthorizationData {
{
Address: "localhost:9202",
},
{
Address: "[::1]:9201",
},
},
}
@@ -724,7 +724,7 @@ func Test_ValidateDiscoveryInfo(t *testing.T) {
// do not run these tests with t.Parallel()
ctx := context.Background()
tp := oidc.StartTestProvider(t)
tp := oidc.StartTestProvider(t, oidc.WithTestHost("::1"))
tpClientId := "alice-rp"
tpClientSecret := "her-dog's-name"
tp.SetClientCreds(tpClientId, tpClientSecret)
@@ -744,7 +744,7 @@ func Test_ValidateDiscoveryInfo(t *testing.T) {
databaseWrapper, err := kmsCache.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
port := testutil.TestFreePort(t)
testAuthMethodCallback, err := url.Parse(fmt.Sprintf("http://localhost:%d/callback", port))
testAuthMethodCallback, err := url.Parse(fmt.Sprintf("http://[::1]:%d/callback", port))
require.NoError(t, err)
testAuthMethod := TestAuthMethod(t,
conn, databaseWrapper,
@@ -793,7 +793,7 @@ func Test_ValidateDiscoveryInfo(t *testing.T) {
authMethod: func() *AuthMethod {
cp := testAuthMethod.Clone()
port := testutil.TestFreePort(t)
cp.Issuer = fmt.Sprintf("http://localhost:%d", port)
cp.Issuer = fmt.Sprintf("http://[::1]:%d", port)
return cp
}(),
withAuthMethod: true,
@@ -1327,7 +1327,7 @@ func Test_pingEndpoint(t *testing.T) {
}, nil
},
}
return client, http.MethodGet, "http://localhost/get"
return client, http.MethodGet, "http://[::1]/get"
},
wantStatus: 200,
},
@@ -1341,7 +1341,7 @@ func Test_pingEndpoint(t *testing.T) {
}, nil
},
}
return client, http.MethodGet, "http://localhost/get"
return client, http.MethodGet, "http://[::1]/get"
},
wantStatus: 500,
},
@@ -1353,7 +1353,7 @@ func Test_pingEndpoint(t *testing.T) {
return nil, fmt.Errorf("invalid request")
},
}
return client, http.MethodGet, "http://localhost/get"
return client, http.MethodGet, "http://[::1]/get"
},
wantErr: true,
},
@@ -26,10 +26,10 @@ func Test_GetOpts(t *testing.T) {
assert.Equal(t, opts, testOpts)
})
t.Run("WithUrl", func(t *testing.T) {
opts, err := getOpts(WithUrl(ctx, "http://localhost:9200"))
opts, err := getOpts(WithUrl(ctx, "http://[::1]:9200"))
require.NoError(t, err)
testOpts := getDefaultOptions()
testOpts.withUrl = "http://localhost:9200"
testOpts.withUrl = "http://[::1]:9200"
assert.Equal(t, opts, testOpts)
})
t.Run("WithLogger", func(t *testing.T) {
@@ -20,7 +20,7 @@ import (
func Test_newCloudEventsFormatterFilter(t *testing.T) {
t.Parallel()
testSource, err := url.Parse("https://localhost:9200")
testSource, err := url.Parse("https://[::1]:9200")
require.NoError(t, err)
tests := []struct {
name string
@@ -152,7 +152,7 @@ func Test_newCloudEventsFormatterFilter(t *testing.T) {
func TestNode_Process(t *testing.T) {
t.Parallel()
ctx := context.Background()
testUrl, err := url.Parse("https://localhost")
testUrl, err := url.Parse("https://[::1]")
require.NoError(t, err)
now := time.Now()
@@ -294,12 +294,12 @@ func TestNode_Process(t *testing.T) {
},
wantText: `{
"id": "%s",
"source": "https://localhost",
"source": "https://[::1]",
"specversion": "1.0",
"type": "test",
"data": "test-string",
"datacontentype": "text/plain",
"dataschema": "https://localhost",
"dataschema": "https://[::1]",
"time": %s
}
`,
+16 -16
View File
@@ -67,7 +67,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusOK,
http.Header{
@@ -109,7 +109,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusOK,
http.Header{
@@ -151,7 +151,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusOK,
http.Header{
@@ -193,7 +193,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusOK,
http.Header{
@@ -235,7 +235,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusOK,
http.Header{
@@ -277,7 +277,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusOK,
http.Header{
@@ -319,7 +319,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusOK,
http.Header{
@@ -369,7 +369,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusTooManyRequests,
http.Header{
@@ -436,7 +436,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusServiceUnavailable,
http.Header{
@@ -477,7 +477,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusInternalServerError,
http.Header{},
@@ -516,7 +516,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusNotFound,
http.Header{},
@@ -555,7 +555,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusBadRequest,
http.Header{},
@@ -594,7 +594,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusMethodNotAllowed,
http.Header{},
@@ -633,7 +633,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusMethodNotAllowed,
http.Header{},
@@ -672,7 +672,7 @@ func TestHandler(t *testing.T) {
require.NoError(t, err)
return r
},
"127.0.0.1",
"[::1]",
"authtoken",
http.StatusMethodNotAllowed,
http.Header{},
@@ -756,7 +756,7 @@ func TestHandlerErrors(t *testing.T) {
ctx, err = event.NewRequestInfoContext(ctx, &event.RequestInfo{
Id: id,
EventId: common.GeneratedTraceId(ctx),
ClientIp: "127.0.0.1",
ClientIp: "[::1]",
})
require.NoError(t, err)
return ctx
+18 -2
View File
@@ -54,13 +54,29 @@ func TestRepository_UpsertController(t *testing.T) {
wantErr: true,
},
{
name: "valid-controller",
name: "valid-ipv4-controller",
controller: &store.Controller{
PrivateId: "test-controller",
PrivateId: "test-ipv4-controller",
Address: "127.0.0.1",
},
wantCount: 1,
},
{
name: "valid-ipv6-controller",
controller: &store.Controller{
PrivateId: "test-ipv6-controller",
Address: "[2001:4860:4860:0:0:0:0:8888]",
},
wantCount: 1,
},
{
name: "valid-abbreviated-ipv6-controller",
controller: &store.Controller{
PrivateId: "test-abbreviated-ipv6-controller",
Address: "[2001:4860:4860::8888]",
},
wantCount: 1,
},
{
name: "valid-controller-short-name",
controller: &store.Controller{
+12 -1
View File
@@ -29,7 +29,7 @@ func TestConnectWith_validate(t *testing.T) {
wantErr bool
}{
{
name: "valid",
name: "valid-ipv4",
fields: fields{
SessionId: id,
ClientTcpAddress: "0.0.0.1",
@@ -39,6 +39,17 @@ func TestConnectWith_validate(t *testing.T) {
UserClientIp: "127.0.0.1",
},
},
{
name: "valid-ipv6",
fields: fields{
SessionId: id,
ClientTcpAddress: "[::1]",
ClientTcpPort: 22,
EndpointTcpAddress: "[::1]",
EndpointTcpPort: 2222,
UserClientIp: "[::2]",
},
},
{
name: "missing-SessionId",
fields: fields{
+29 -2
View File
@@ -33,7 +33,7 @@ func TestSession_Create(t *testing.T) {
composedOf := testSessionCredentialParams(t, conn, wrapper, iamRepo)
exp := &timestamp.Timestamp{Timestamp: timestamppb.New(time.Now().Add(time.Hour))}
defaultAddresses := []string{"1.2.3.4", "a.b.c.d"}
defaultAddresses := []string{"1.2.3.4", "a.b.c.d", "[2001:4860:4860::8888]", "[2001:4860:4860:0:0:0:0:8888]"}
type args struct {
composedOf ComposedOf
addresses []string
@@ -50,7 +50,7 @@ func TestSession_Create(t *testing.T) {
wantCreateErr bool
}{
{
name: "valid-hostset-host",
name: "valid-hostset-host-ipv4",
args: args{
composedOf: composedOf,
opt: []Option{WithExpirationTime(exp)},
@@ -72,6 +72,33 @@ func TestSession_Create(t *testing.T) {
},
create: true,
},
{
name: "valid-hostset-host-ipv6",
args: args{
composedOf: func() ComposedOf {
c := composedOf
c.Endpoint = "tcp://[::1]:22"
return c
}(),
opt: []Option{WithExpirationTime(exp)},
addresses: defaultAddresses,
},
want: &Session{
UserId: composedOf.UserId,
HostId: composedOf.HostId,
TargetId: composedOf.TargetId,
HostSetId: composedOf.HostSetId,
AuthTokenId: composedOf.AuthTokenId,
ProjectId: composedOf.ProjectId,
Endpoint: "tcp://[::1]:22",
ExpirationTime: composedOf.ExpirationTime,
ConnectionLimit: composedOf.ConnectionLimit,
DynamicCredentials: composedOf.DynamicCredentials,
StaticCredentials: composedOf.StaticCredentials,
CorrelationId: composedOf.CorrelationId,
},
create: true,
},
{
name: "valid-target-address",
args: args{
+15 -7
View File
@@ -347,10 +347,10 @@ func TestTarget_AddressMutualExclusiveRelationship(t *testing.T) {
// Create target with a network address association
targetResp, err := tClient.Create(tc.Context(), "tcp", proj.GetPublicId(),
targets.WithName("test-address"), targets.WithAddress("localhost"), targets.WithTcpTargetDefaultPort(22))
targets.WithName("test-address"), targets.WithAddress("[::1]"), targets.WithTcpTargetDefaultPort(22))
require.NoError(t, err)
require.NotNil(t, targetResp)
require.Equal(t, "localhost", targetResp.GetItem().Address)
require.Equal(t, "[::1]", targetResp.GetItem().Address)
// Setup host catalog, host set, & host resources
hc, err := hostcatalogs.NewClient(client).Create(tc.Context(), "static", proj.GetPublicId())
@@ -359,7 +359,7 @@ func TestTarget_AddressMutualExclusiveRelationship(t *testing.T) {
hs, err := hostsets.NewClient(client).Create(tc.Context(), hc.Item.Id)
require.NoError(t, err)
require.NotNil(t, hs)
h, err := hosts.NewClient(client).Create(tc.Context(), hc.Item.Id, hosts.WithStaticHostAddress("localhost"))
h, err := hosts.NewClient(client).Create(tc.Context(), hc.Item.Id, hosts.WithStaticHostAddress("[::1]"))
require.NoError(t, err)
require.NotNil(t, h)
hUpdate, err := hostsets.NewClient(client).AddHosts(tc.Context(), hs.Item.Id, hs.Item.Version, []string{h.GetItem().Id})
@@ -405,7 +405,7 @@ func TestTarget_HostSourceMutualExclusiveRelationship(t *testing.T) {
hs, err := hostsets.NewClient(client).Create(tc.Context(), hc.Item.Id)
require.NoError(t, err)
require.NotNil(t, hs)
h, err := hosts.NewClient(client).Create(tc.Context(), hc.Item.Id, hosts.WithStaticHostAddress("localhost"))
h, err := hosts.NewClient(client).Create(tc.Context(), hc.Item.Id, hosts.WithStaticHostAddress("[::1]"))
require.NoError(t, err)
require.NotNil(t, h)
hUpdate, err := hostsets.NewClient(client).AddHosts(tc.Context(), hs.Item.Id, hs.Item.Version, []string{h.GetItem().Id})
@@ -428,7 +428,7 @@ func TestTarget_HostSourceMutualExclusiveRelationship(t *testing.T) {
require.Empty(t, updateResp.GetItem().Address)
require.Equal(t, []string{hs.Item.Id}, updateResp.GetItem().HostSourceIds)
version = updateResp.GetItem().Version
updateResp, err = tClient.Update(tc.Context(), targetId, version, targets.WithAddress("localhost"))
updateResp, err = tClient.Update(tc.Context(), targetId, version, targets.WithAddress("[::1]"))
require.Error(t, err)
require.Nil(t, updateResp)
apiErr := api.AsServerError(err)
@@ -441,10 +441,10 @@ func TestTarget_HostSourceMutualExclusiveRelationship(t *testing.T) {
require.NotNil(t, updateResp)
require.Empty(t, updateResp.GetItem().HostSourceIds)
version = updateResp.GetItem().Version
updateResp, err = tClient.Update(tc.Context(), targetId, version, targets.WithAddress("localhost"))
updateResp, err = tClient.Update(tc.Context(), targetId, version, targets.WithAddress("[::1]"))
require.NoError(t, err)
require.NotNil(t, updateResp)
require.Equal(t, "localhost", updateResp.GetItem().Address)
require.Equal(t, "[::1]", updateResp.GetItem().Address)
require.Empty(t, updateResp.GetItem().HostSourceIds)
}
@@ -465,6 +465,14 @@ func TestCreateTarget_DirectlyAttachedAddress(t *testing.T) {
name: "target-ipv4-address",
address: "127.0.0.1",
},
{
name: "target-ipv6-address",
address: "[2001:4860:4860:0:0:0:0:8888]",
},
{
name: "target-abbreviated-ipv6-address",
address: "[2001:4860:4860::8888]",
},
{
name: "target-dns-address",
address: "null",
@@ -86,7 +86,7 @@ func testWorkerSessionCleanupSingle(burdenCase timeoutBurdenType) func(t *testin
conf, err := config.DevController()
require.NoError(err)
pl, err := net.Listen("tcp", "localhost:0")
pl, err := net.Listen("tcp", "[::1]:0")
require.NoError(err)
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
Config: conf,
@@ -217,7 +217,7 @@ func testWorkerSessionCleanupMulti(burdenCase timeoutBurdenType) func(t *testing
conf1, err := config.DevController()
require.NoError(err)
pl1, err := net.Listen("tcp", "localhost:0")
pl1, err := net.Listen("tcp", "[::1]:0")
require.NoError(err)
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
Config: conf1,
@@ -230,7 +230,7 @@ func testWorkerSessionCleanupMulti(burdenCase timeoutBurdenType) func(t *testing
// ******************
// ** Controller 2 **
// ******************
pl2, err := net.Listen("tcp", "localhost:0")
pl2, err := net.Listen("tcp", "[::1]:0")
require.NoError(err)
c2 := c1.AddClusterControllerMember(t, &controller.TestControllerOpts{
Logger: logger.Named("c2"),
+1 -1
View File
@@ -19,7 +19,7 @@ func Test_TestFreePort(t *testing.T) {
p := TestFreePort(t)
assert.NotEmpty(p)
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", p))
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("[::1]:%d", p))
require.NoError(err)
l, err := net.ListenTCP("tcp", addr)
require.NoError(err)