Skip to content

Commit 308a94a

Browse files
committed
add more test
1 parent 7ebbd57 commit 308a94a

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

certify.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616
)
1717

18+
// Certificate hold certificate information
1819
type Certificate struct {
1920
Subject pkix.Name
2021
NotBefore time.Time
@@ -27,6 +28,7 @@ type Certificate struct {
2728
ExtentedKeyUsage []x509.ExtKeyUsage
2829
}
2930

31+
// Result hold created certificate in []byte format
3032
type Result struct {
3133
Certificate []byte
3234
}
@@ -110,6 +112,7 @@ func ParseCertificate(cert []byte) (*x509.Certificate, error) {
110112
return c, nil
111113
}
112114

115+
// CertInfo returns certificate information
113116
func CertInfo(cert *x509.Certificate) string {
114117
var buf bytes.Buffer
115118

@@ -163,7 +166,7 @@ func CertInfo(cert *x509.Certificate) string {
163166
}
164167
}
165168

166-
buf.WriteString(fmt.Sprintf("%8sSignature Algorithm: %v\n", "", cert.SignatureAlgorithm))
169+
buf.WriteString(fmt.Sprintf("%4sSignature Algorithm: %v\n", "", cert.SignatureAlgorithm))
167170

168171
return buf.String()
169172
}

cmd/certify/testdata/ca-cert.pem

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBmDCCAT2gAwIBAgIQUjIMhHGW4CreYEIQOnPDdDAKBggqhkjOPQQDAjAkMRAw
3+
DgYDVQQKEwdjZXJ0aWZ5MRAwDgYDVQQDEwdjZXJ0aWZ5MB4XDTIyMDMxNzA4NDQx
4+
MloXDTIzMDMxNzE0NDQxMlowJDEQMA4GA1UEChMHY2VydGlmeTEQMA4GA1UEAxMH
5+
Y2VydGlmeTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABIPmsrI8hCLHryeWc0wz
6+
zrrbAXhohqMfFnZS95qM83p/EHHUO4yoi4LSZhZnvPhPYG+St4KBZj2mqZYs6nf8
7+
sTSjUTBPMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8E
8+
BTADAQH/MB0GA1UdDgQWBBTuUKyfBpn78BTa2fodsucBYuApejAKBggqhkjOPQQD
9+
AgNJADBGAiEAlYCxixkXh6eI1nHBAhaUHajYF6ZWpK4tiDCWR5lHIA0CIQCpgqUp
10+
+R8a3HBTIcrpgdoI2g11HmV9+qOysbuWNpTnMw==
11+
-----END CERTIFICATE-----

cmd/certify/testdata/ca-key.pem

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHcCAQEEIOgIRqRHosbtIPpHON1XY8TSVg/U9K9tiw/xexfrGRJwoAoGCCqGSM49
3+
AwEHoUQDQgAEg+aysjyEIsevJ5ZzTDPOutsBeGiGox8WdlL3mozzen8QcdQ7jKiL
4+
gtJmFme8+E9gb5K3goFmPaaplizqd/yxNA==
5+
-----END EC PRIVATE KEY-----

helper_test.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package certify
2+
3+
import (
4+
"crypto/x509"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestGetPublicKey(t *testing.T) {
10+
expectedPubKey := `-----BEGIN PUBLIC KEY-----
11+
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEg+aysjyEIsevJ5ZzTDPOutsBeGiG
12+
ox8WdlL3mozzen8QcdQ7jKiLgtJmFme8+E9gb5K3goFmPaaplizqd/yxNA==
13+
-----END PUBLIC KEY-----
14+
`
15+
16+
cert, err := readCertificateFile("./cmd/certify/testdata/ca-cert.pem")
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
pubkey, err := GetPublicKey(cert.PublicKey)
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
26+
if pubkey != expectedPubKey {
27+
t.Fatalf("got %v, want %v", pubkey, expectedPubKey)
28+
}
29+
}
30+
31+
func TestParseExtKeyUsage(t *testing.T) {
32+
t.Run("Test single eku", func(t *testing.T) {
33+
result := parseExtKeyUsage([]x509.ExtKeyUsage{
34+
x509.ExtKeyUsageServerAuth,
35+
})
36+
37+
expectedResult := "TLS Web Server Authentication"
38+
39+
if result != expectedResult {
40+
t.Fatalf("got %v, eant %v", result, expectedResult)
41+
}
42+
})
43+
44+
t.Run("Test multiple eku", func(t *testing.T) {
45+
result := parseExtKeyUsage([]x509.ExtKeyUsage{
46+
x509.ExtKeyUsageServerAuth,
47+
x509.ExtKeyUsageClientAuth,
48+
})
49+
50+
expectedResult := "TLS Web Server Authentication, TLS Web Client Authentication"
51+
52+
if result != expectedResult {
53+
t.Fatalf("got %v, eant %v", result, expectedResult)
54+
}
55+
})
56+
57+
t.Run("Test all Eku", func(t *testing.T) {
58+
result := parseExtKeyUsage([]x509.ExtKeyUsage{
59+
x509.ExtKeyUsageServerAuth,
60+
x509.ExtKeyUsageClientAuth,
61+
x509.ExtKeyUsageAny,
62+
x509.ExtKeyUsageCodeSigning,
63+
x509.ExtKeyUsageEmailProtection,
64+
x509.ExtKeyUsageIPSECEndSystem,
65+
x509.ExtKeyUsageIPSECTunnel,
66+
x509.ExtKeyUsageIPSECUser,
67+
x509.ExtKeyUsageTimeStamping,
68+
x509.ExtKeyUsageOCSPSigning,
69+
x509.ExtKeyUsageMicrosoftServerGatedCrypto,
70+
x509.ExtKeyUsageNetscapeServerGatedCrypto,
71+
x509.ExtKeyUsageMicrosoftCommercialCodeSigning,
72+
x509.ExtKeyUsageMicrosoftKernelCodeSigning,
73+
})
74+
75+
expectedResult := "TLS Web Server Authentication, TLS Web Client Authentication, Any Extended Key Usage, Code Signing, E-mail Protection, IPSec End System, IPSec Tunnel, IPSec User, Time Stamping, OCSP Signing, Microsoft Server Gated Crypto, Netscape Server Gated Crypto, Microsoft Commercial Code Signing, 1.3.6.1.4.1.311.61.1.1"
76+
77+
if result != expectedResult {
78+
t.Fatalf("got %v, eant %v", result, expectedResult)
79+
}
80+
})
81+
}
82+
83+
func TestFormatKeyIDWithColon(t *testing.T) {
84+
result := formatKeyIDWithColon([]byte{36, 44, 106, 165, 22, 233, 173, 100, 28, 6, 69, 211, 74, 214, 212, 162})
85+
expectedResult := "24:2c:6a:a5:16:e9:ad:64:1c:06:45:d3:4a:d6:d4:a2"
86+
87+
if result != expectedResult {
88+
t.Fatalf("got %v, want %v", result, expectedResult)
89+
}
90+
}
91+
92+
func readCertificateFile(path string) (*x509.Certificate, error) {
93+
f, err := os.ReadFile(path)
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
c, err := ParseCertificate(f)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
return c, nil
104+
}

key.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
"fmt"
1111
)
1212

13+
// PrivateKey hold private key
1314
type PrivateKey struct {
1415
*ecdsa.PrivateKey
1516
}
1617

17-
// getPrivateKey returns struct PrivateKey containing the private key
18+
// GetPrivateKey returns struct PrivateKey containing the private key
1819
func GetPrivateKey() (*PrivateKey, error) {
1920
pkey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
2021
if err != nil {

0 commit comments

Comments
 (0)