Skip to content

Commit

Permalink
cert test
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Nov 28, 2024
1 parent 9bb2f71 commit 4e807fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
30 changes: 20 additions & 10 deletions internal/file/file_manager_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}

Expand All @@ -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())
Expand Down
14 changes: 10 additions & 4 deletions test/helpers/cert_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 4e807fb

Please sign in to comment.