From 8abadeacb086f3d6a1b2bd6b318206bb4405d4a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Valais?= Date: Thu, 4 Mar 2021 17:52:46 +0100 Subject: [PATCH] pki: add unit test around IPAddressesForCertificate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maƫl Valais --- pkg/util/pki/BUILD.bazel | 1 + pkg/util/pki/csr_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/pkg/util/pki/BUILD.bazel b/pkg/util/pki/BUILD.bazel index bccad9f45..e2c2a1e06 100644 --- a/pkg/util/pki/BUILD.bazel +++ b/pkg/util/pki/BUILD.bazel @@ -28,6 +28,7 @@ go_test( deps = [ "//pkg/apis/certmanager/v1:go_default_library", "//pkg/util:go_default_library", + "@com_github_stretchr_testify//assert:go_default_library", ], ) diff --git a/pkg/util/pki/csr_test.go b/pkg/util/pki/csr_test.go index f674a8e13..94103b303 100644 --- a/pkg/util/pki/csr_test.go +++ b/pkg/util/pki/csr_test.go @@ -20,9 +20,12 @@ import ( "crypto/x509" "crypto/x509/pkix" "encoding/asn1" + "net" "reflect" "testing" + "github.com/stretchr/testify/assert" + cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1" v1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1" "github.com/jetstack/cert-manager/pkg/util" @@ -553,3 +556,42 @@ func Test_buildKeyUsagesExtensionsForCertificate(t *testing.T) { }) } } + +func TestIPAddressesForCertificate(t *testing.T) { + tests := map[string]struct { + crt *v1.Certificate + want []net.IP + }{ + "valid ips should be returned": { + // Cannot use gen.Certificate because "gen" imports "pki". + crt: &v1.Certificate{Spec: v1.CertificateSpec{ + IPAddresses: []string{ + "127.0.0.1", + "127.0.0.2", + }, + }}, + want: []net.IP{ + net.ParseIP("127.0.0.1"), + net.ParseIP("127.0.0.2"), + }, + }, + "a malformed ip is silently skipped": { + // Cannot use gen.Certificate because "gen" imports "pki". + crt: &v1.Certificate{Spec: v1.CertificateSpec{ + IPAddresses: []string{ + "127.0.0.1", + "malformed", + }, + }}, + want: []net.IP{ + net.ParseIP("127.0.0.1"), + }, + }, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + got := IPAddressesForCertificate(tt.crt) + assert.Equalf(t, tt.want, got, "expected='%s', actual='%s'", tt.want, got) + }) + } +}