Skip to content

Commit

Permalink
http: add tests for reading TLS private keys
Browse files Browse the repository at this point in the history
This commit adds some test cases to verify that
private keys are read, decrypted and parsed
correctly.

Signed-off-by: Andreas Auernhammer <[email protected]>
  • Loading branch information
aead committed Oct 13, 2021
1 parent 79aa750 commit f874de8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/http/testdata/privatekeys/encrypted.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,E1CF19C5B05C92E1D8D88867779455F8

hX5Pwa91m/+DGSsE4ON34/fsFyXOakpjnYK7IrbVWHagbMcDgDGTgwkeCqlEX7U/
BQiX8oGFR/ff+R+TcLRI6tGfmns9B4QS3RVMeY965F1zHysMn/jjIgzQyfmo8YNI
K/11h1SW8SEmbMzmCIhnagYmt/JQrjJVbgGlQgxHV0w=
-----END EC PRIVATE KEY-----
5 changes: 5 additions & 0 deletions internal/http/testdata/privatekeys/plaintext.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIB4u/+f//HoqSAdVd9DN91JRhQoHLml+/gbvK91SYbK5oAoGCCqGSM49
AwEHoUQDQgAEZwWOkBVrkeLKYE5QFwDzDDHwkBjiVFJ+BgXOaXHTkRcjclh7k85r
wx4zTr/x27oWtlDusD/JTa8dSqJADEF3HA==
-----END EC PRIVATE KEY-----
33 changes: 33 additions & 0 deletions internal/http/tls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.

package http

import (
"testing"
)

var readPrivateKeyTests = []struct {
FilePath string
Password string
ShouldFail bool
}{
{FilePath: "testdata/privatekeys/plaintext.pem", Password: ""}, // 0
{FilePath: "testdata/privatekeys/plaintext.pem", Password: "ignored_password"}, // 1
{FilePath: "testdata/privatekeys/encrypted.pem", Password: "correct_password"}, // 2
{FilePath: "testdata/privatekeys/encrypted.pem", Password: "", ShouldFail: true}, // 3
{FilePath: "testdata/privatekeys/encrypted.pem", Password: "incorrect_password", ShouldFail: true}, // 4
}

func TestReadPrivateKey(t *testing.T) {
for i, test := range readPrivateKeyTests {
_, err := readPrivateKey(test.FilePath, test.Password)
if err != nil && !test.ShouldFail {
t.Fatalf("Test %d: failed to read private key %q: %v", i, test.FilePath, err)
}
if err == nil && test.ShouldFail {
t.Fatalf("Test %d: reading private key %q should have failed", i, test.FilePath)
}
}
}

0 comments on commit f874de8

Please sign in to comment.