From 4e807fb17ca1bc1eb7875da219928633f0ae93ed Mon Sep 17 00:00:00 2001 From: Oliver O'Mahony Date: Thu, 28 Nov 2024 15:09:00 +0000 Subject: [PATCH] cert test --- internal/file/file_manager_service_test.go | 30 ++++++++++++++-------- test/helpers/cert_utils.go | 14 +++++++--- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/internal/file/file_manager_service_test.go b/internal/file/file_manager_service_test.go index c91daf69b..b0f7fba87 100644 --- a/internal/file/file_manager_service_test.go +++ b/internal/file/file_manager_service_test.go @@ -626,7 +626,15 @@ X/vYrzgKRoKSUWUt1ejKTntrVuaJK4NMxANOTTjIXgxyoV3YcgEmL9KzribCqILi p79Nno9d+kovtX5VKsJ5FCcPw9mEATgZDOQ4nLTk/HHG6bwtpubp6Zb7H1AjzBkz rQHX6DP4w6IwZY8JB8LS -----END CERTIFICATE-----`, - expectedSerial: []byte{0x1, 0xe0, 0xf3}, + expectedSerial: []byte{ + 0x47, 0xe6, 0x6, + 0x81, 0x11, 0xe1, + 0x63, 0xa, 0x2d, + 0x17, 0x20, 0x4e, + 0xbd, 0x27, 0x35, + 0x28, 0x3f, 0x5d, + 0xe3, 0x99, + }, }, } @@ -635,21 +643,23 @@ rQHX6DP4w6IwZY8JB8LS for _, test := range tests { t.Run(test.name, func(t *testing.T) { var certBytes []byte + var certPath string if test.certContent == "" { _, certBytes = helpers.GenerateSelfSignedCert(t) + certContents := helpers.Cert{ + Name: fmt.Sprintf("%s.pem", test.certName), + Type: "CERTIFICATE", + Contents: certBytes, + } + certPath = helpers.WriteCertFiles(t, tempDir, certContents) } else { - certBytes = []byte(test.certContent) - } - - certContents := helpers.Cert{ - Name: fmt.Sprintf("%s.pem", test.certName), - Type: "CERTIFICATE", - Contents: certBytes, + certPath = fmt.Sprintf("%s%c%s", tempDir, os.PathSeparator, test.certName) + err := os.WriteFile(certPath, []byte(test.certContent), 0o600) + require.NoError(t, err) } - certFile := helpers.WriteCertFiles(t, tempDir, certContents) - certFileMeta, certFileMetaErr := files.FileMetaWithCertificate(certFile) + certFileMeta, certFileMetaErr := files.FileMetaWithCertificate(certPath) require.NoError(t, certFileMetaErr) assert.Equal(t, test.expectedSerial, certFileMeta.GetCertificateMeta().GetSerialNumber()) diff --git a/test/helpers/cert_utils.go b/test/helpers/cert_utils.go index 45dcac28d..f221ec932 100644 --- a/test/helpers/cert_utils.go +++ b/test/helpers/cert_utils.go @@ -68,10 +68,16 @@ func GenerateSelfSignedCert(t testing.TB) (keyBytes, certBytes []byte) { func WriteCertFiles(t *testing.T, location string, cert Cert) string { t.Helper() - pemContents := pem.EncodeToMemory(&pem.Block{ - Type: cert.Type, - Bytes: cert.Contents, - }) + var pemContents []byte + + if strings.HasPrefix(string(cert.Contents), "-----") { + pemContents = cert.Contents + } else { + pemContents = pem.EncodeToMemory(&pem.Block{ + Type: cert.Type, + Bytes: cert.Contents, + }) + } var certFile string if strings.HasSuffix(location, string(os.PathSeparator)) {