Skip to content

Commit

Permalink
test(host): validate ipv6 address for hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebko committed Sep 5, 2024
1 parent 5575919 commit e36fe7d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
10 changes: 9 additions & 1 deletion internal/host/plugin/job_set_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package plugin
import (
"context"
"fmt"
"net"
"testing"
"time"

Expand Down Expand Up @@ -198,7 +199,7 @@ func TestSetSyncJob_Run(t *testing.T) {
Hosts: []*plgpb.ListHostsResponseHost{
{
ExternalId: "first",
IpAddresses: []string{fmt.Sprintf("10.0.0.%d", *counter)},
IpAddresses: []string{fmt.Sprintf("10.0.0.%d", *counter), testGetIpv6Address(t)},
DnsNames: []string{"foo.com"},
SetIds: setIds,
},
Expand All @@ -225,6 +226,13 @@ func TestSetSyncJob_Run(t *testing.T) {
assert.Len(hosts, 1)
for _, host := range hosts {
assert.Equal(uint32(1), host.Version)
require.Len(host.IpAddresses, 2)

Check failure on line 229 in internal/host/plugin/job_set_sync_test.go

View workflow job for this annotation

GitHub Actions / Run Linter

host.IpAddresses undefined (type *Host has no field or method IpAddresses) (typecheck)
ipv4 := net.ParseIP(host.IpAddresses[0])

Check failure on line 230 in internal/host/plugin/job_set_sync_test.go

View workflow job for this annotation

GitHub Actions / Run Linter

host.IpAddresses undefined (type *Host has no field or method IpAddresses) (typecheck)
require.NotNil(ipv4)
require.NotNil(ipv4.To4())
ipv6 := net.ParseIP(host.IpAddresses[1])

Check failure on line 233 in internal/host/plugin/job_set_sync_test.go

View workflow job for this annotation

GitHub Actions / Run Linter

host.IpAddresses undefined (type *Host has no field or method IpAddresses) (typecheck)
require.NotNil(ipv6)
require.NotNil(ipv6.To16())
}

require.NoError(rw.LookupByPublicId(ctx, hsa))
Expand Down
2 changes: 1 addition & 1 deletion internal/host/plugin/repository_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestJob_UpsertHosts(t *testing.T) {
in: func() *input {
ph := phs[1]
e := exp[1]
newIp := testGetIpAddress(t)
newIp := testGetIpv4Address(t)
newName := testGetDnsName(t)
ph.IpAddresses = append(ph.IpAddresses, newIp)
e.IpAddresses = append(e.IpAddresses, newIp)
Expand Down
24 changes: 20 additions & 4 deletions internal/host/plugin/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,16 @@ func TestExternalHosts(t testing.TB, catalog *HostCatalog, setIds []string, coun
externalId, err := base62.Random(10)
require.NoError(err)

ipStr := testGetIpAddress(t)
ipv4Str := testGetIpv4Address(t)
ipv6Str := testGetIpv6Address(t)
dnsName := testGetDnsName(t)

rh := &plgpb.ListHostsResponseHost{
ExternalId: externalId,
Name: base62.MustRandom(10),
Description: base62.MustRandom(10),
SetIds: setIds[0 : i+1],
IpAddresses: []string{ipStr},
IpAddresses: []string{ipv4Str, ipv6Str},
DnsNames: []string{dnsName},
}
retRH = append(retRH, rh)
Expand All @@ -190,7 +191,7 @@ func TestExternalHosts(t testing.TB, catalog *HostCatalog, setIds []string, coun
CatalogId: catalog.PublicId,
PublicId: publicId,
ExternalId: externalId,
IpAddresses: []string{ipStr},
IpAddresses: []string{ipv4Str, ipv6Str},
DnsNames: []string{dnsName},
Version: 1,
},
Expand All @@ -217,7 +218,7 @@ func testGetDnsName(t testing.TB) string {
return fmt.Sprintf("%s.example.com", dnsName)
}

func testGetIpAddress(t testing.TB) string {
func testGetIpv4Address(t testing.TB) string {
ipBytes := make([]byte, 4)
for {
lr := io.LimitReader(rand.Reader, 4)
Expand All @@ -231,3 +232,18 @@ func testGetIpAddress(t testing.TB) string {
}
}
}

func testGetIpv6Address(t testing.TB) string {
ipBytes := make([]byte, 16)
for {
lr := io.LimitReader(rand.Reader, 16)
n, err := lr.Read(ipBytes)
require.NoError(t, err)
require.Equal(t, n, 16)
ip := net.IP(ipBytes)
v6 := ip.To16()
if v6 != nil {
return v6.String()
}
}
}
66 changes: 64 additions & 2 deletions internal/host/static/repository_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestRepository_CreateHost(t *testing.T) {
wantIsErr: errors.InvalidParameter,
},
{
name: "valid-no-options",
name: "valid-ipv4-address",
in: &Host{
Host: &store.Host{
CatalogId: catalog.PublicId,
Expand All @@ -84,6 +84,36 @@ func TestRepository_CreateHost(t *testing.T) {
},
},
},
{
name: "valid-abbreviated-ipv6-address",
in: &Host{
Host: &store.Host{
CatalogId: catalog.PublicId,

Check failure on line 91 in internal/host/static/repository_host_test.go

View workflow job for this annotation

GitHub Actions / Run Linter

catalog.PublicId undefined (type *HostCatalog has no field or method PublicId) (typecheck)
Address: "2001:4860:4860::8888",
},
},
want: &Host{
Host: &store.Host{
CatalogId: catalog.PublicId,

Check failure on line 97 in internal/host/static/repository_host_test.go

View workflow job for this annotation

GitHub Actions / Run Linter

catalog.PublicId undefined (type *HostCatalog has no field or method PublicId) (typecheck)
Address: "2001:4860:4860::8888",
},
},
},
{
name: "valid-ipv6-address",
in: &Host{
Host: &store.Host{
CatalogId: catalog.PublicId,

Check failure on line 106 in internal/host/static/repository_host_test.go

View workflow job for this annotation

GitHub Actions / Run Linter

catalog.PublicId undefined (type *HostCatalog has no field or method PublicId) (typecheck)
Address: "2001:4860:4860:0:0:0:0:8888",
},
},
want: &Host{
Host: &store.Host{
CatalogId: catalog.PublicId,
Address: "2001:4860:4860:0:0:0:0:8888",
},
},
},
{
name: "valid-with-name",
in: &Host{
Expand Down Expand Up @@ -545,7 +575,7 @@ func TestRepository_UpdateHost(t *testing.T) {
wantCount: 1,
},
{
name: "change-address",
name: "change-ipv4-address",
orig: &Host{
Host: &store.Host{
Address: "127.0.0.1",
Expand All @@ -560,6 +590,38 @@ func TestRepository_UpdateHost(t *testing.T) {
},
wantCount: 1,
},
{
name: "change-abbreviated-ipv6-address",
orig: &Host{
Host: &store.Host{
Address: "127.0.0.1",
},
},
chgFn: changeAddress("2001:4860:4860::8888"),
masks: []string{"Address"},
want: &Host{
Host: &store.Host{
Address: "2001:4860:4860::8888",
},
},
wantCount: 1,
},
{
name: "change-ipv6-address",
orig: &Host{
Host: &store.Host{
Address: "127.0.0.1",
},
},
chgFn: changeAddress("2001:4860:4860:0:0:0:0:8888"),
masks: []string{"Address"},
want: &Host{
Host: &store.Host{
Address: "2001:4860:4860:0:0:0:0:8888",
},
},
wantCount: 1,
},
{
name: "change-short-address",
orig: &Host{
Expand Down

0 comments on commit e36fe7d

Please sign in to comment.