Skip to content

Commit

Permalink
Add test for loading public keys and fix bug in loading ed25519 publi…
Browse files Browse the repository at this point in the history
…c key (#19)
  • Loading branch information
jaztec authored Feb 18, 2023
1 parent 2133efb commit f067866
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
4 changes: 2 additions & 2 deletions certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func loadPublicKey(pemBytes []byte) (crypto.PublicKey, error) {
return key.(*ecdsa.PublicKey), err
case *rsa.PublicKey:
return key.(*rsa.PublicKey), err
case *ed25519.PublicKey:
return key.(*ed25519.PublicKey), err
case ed25519.PublicKey:
return key.(ed25519.PublicKey), err
}
return nil, fmt.Errorf("cert type %T is not a valid type", key)
}
Expand Down
79 changes: 79 additions & 0 deletions certs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package simplcert

import (
"os"
"testing"
"time"
)

func createTestDirectory(name string, t *testing.T) string {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("%s: Cannot get work dir: %+v", name, err)
}
outPath := cwd + string(os.PathSeparator) + "tmp"
if err := os.Mkdir(outPath, 0777); err != nil {
t.Fatalf("%s: Cannot create work dir: %+v", name, err)
}
return outPath
}

func certConfig(certType CertType, isServer bool) CertConfig {
return CertConfig{
Name: "Test",
Host: "test.org, , 127.0.0.1, ::1,",
IsServer: isServer,
CertType: certType,
NotAfter: time.Now().AddDate(0, 0, 1),
}
}

func TestLoadPublicKey(t *testing.T) {
tests := []struct {
name string
certType CertType
}{
{"Test load ECDSA public key", TypeECDSA},
{"Test load RSA public key", TypeRSA},
{"Test load ED25519 public key", TypeED25519},
}

for _, test := range tests {
outPath := createTestDirectory(test.name, t)
if err := CreateRootCAFiles(test.certType, outPath); err != nil {
t.Errorf("%s: Creating roto CA files returned an error: %+v", test.name, err)
}
m, err := NewManager(outPath)
if err != nil {
t.Fatalf("%s: Error creating manager: %+v", test.name, err)
}

// create the certificate
_, priv, pubBytes, err := m.CreateNamedCert(certConfig(test.certType, true))
if err != nil {
t.Fatalf("%s: Error creating named certificate: %+v", test.name, err)
}
privRaw, err := m.MarshalPrivateKey(priv)
if err != nil {
t.Fatalf("%s: Error marshaling private key: %+v", test.name, err)
}
privBytes := EncodePrivateKey(privRaw)

// load the private and public keys
loadedPriv, err := loadPrivateKey(privBytes)
if err != nil {
t.Errorf("%s: Loading the private key file failed: %+v", test.name, err)
}
loadedPub, err := loadPublicKey(pubBytes)
if err != nil {
t.Errorf("%s: Loading the public key file failed: %+v", test.name, err)
}
if !publicKeyEquals(loadedPriv.Public(), loadedPub) {
t.Errorf("%s: The public keys do not match", test.name)
}

if err := os.RemoveAll(outPath); err != nil {
t.Fatalf("%s: Cannot remove work dir: %+v", test.name, err)
}
}
}
4 changes: 2 additions & 2 deletions root_ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func publicKeyEquals(public crypto.PublicKey, other crypto.PublicKey) bool {
case *ecdsa.PublicKey:
p := public.(*ecdsa.PublicKey)
return p.Equal(other)
case *ed25519.PublicKey:
p := public.(*ed25519.PublicKey)
case ed25519.PublicKey:
p := public.(ed25519.PublicKey)
return p.Equal(other)
}
return false
Expand Down

0 comments on commit f067866

Please sign in to comment.