Skip to content

Commit 7ea1dc7

Browse files
authored
Wrote tests for the rsa.go file (#1386)
What changed? Wrote tests for rsa.go (now 85.7% according to goland) Some of the error cases in loadRSAKey are not reachable without manipulating the key bytes to trigger them. I didn't do those Why? Improve code coverage How did you test it? Potential risks
1 parent baa7d46 commit 7ea1dc7

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

internal/common/util/rsa_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) 2017-2021 Uber Technologies Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package util
22+
23+
import (
24+
"os"
25+
"testing"
26+
27+
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
func loadTestKeyFile(t *testing.T, path string) []byte {
32+
t.Helper()
33+
key, err := os.ReadFile(path)
34+
require.NoError(t, err)
35+
return key
36+
}
37+
38+
func TestLoadRSAPublicKey(t *testing.T) {
39+
publicKeyPEM := loadTestKeyFile(t, "./../auth/credentials/keytest.pub")
40+
41+
t.Run("valid public key", func(t *testing.T) {
42+
key, err := LoadRSAPublicKey(publicKeyPEM)
43+
assert.NoError(t, err)
44+
assert.NotNil(t, key)
45+
assert.Equal(t, 2048, key.Size()*8)
46+
})
47+
48+
t.Run("invalid public key", func(t *testing.T) {
49+
_, err := LoadRSAPublicKey([]byte("invalid PEM data"))
50+
assert.ErrorContains(t, err, "failed to parse PEM block containing the public key")
51+
})
52+
53+
t.Run("incorrect PEM type for public key", func(t *testing.T) {
54+
privateKeyPEM := loadTestKeyFile(t, "./../auth/credentials/keytest")
55+
_, err := LoadRSAPublicKey(privateKeyPEM)
56+
assert.ErrorContains(t, err, "failed to parse PEM block containing the public key")
57+
})
58+
}
59+
60+
func TestLoadRSAPrivateKey(t *testing.T) {
61+
privateKeyPEM := loadTestKeyFile(t, "./../auth/credentials/keytest")
62+
63+
t.Run("valid private key", func(t *testing.T) {
64+
key, err := LoadRSAPrivateKey(privateKeyPEM)
65+
assert.NoError(t, err)
66+
assert.NotNil(t, key)
67+
assert.Equal(t, 2048, key.Size()*8)
68+
})
69+
70+
t.Run("invalid private key", func(t *testing.T) {
71+
_, err := LoadRSAPrivateKey([]byte("invalid PEM data"))
72+
assert.ErrorContains(t, err, "failed to parse PEM block containing the private key")
73+
})
74+
75+
t.Run("incorrect PEM type for private key", func(t *testing.T) {
76+
publicKeyPEM := loadTestKeyFile(t, "./../auth/credentials/keytest.pub")
77+
_, err := LoadRSAPrivateKey(publicKeyPEM)
78+
assert.ErrorContains(t, err, "failed to parse PEM block containing the private key")
79+
})
80+
}

0 commit comments

Comments
 (0)