mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
41 lines
957 B
Go
41 lines
957 B
Go
|
package letsencrypt
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/rand"
|
||
|
"crypto/rsa"
|
||
|
"crypto/x509"
|
||
|
"encoding/pem"
|
||
|
"os"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestSaveAndLoadRSAPrivateKey(t *testing.T) {
|
||
|
keyFile := "test.key"
|
||
|
defer os.Remove(keyFile)
|
||
|
|
||
|
privateKey, err := rsa.GenerateKey(rand.Reader, 256) // small key size is OK for testing
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
privateKeyPEM := pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
|
||
|
|
||
|
// test save
|
||
|
err = saveRSAPrivateKey(privateKey, keyFile)
|
||
|
if err != nil {
|
||
|
t.Fatal("error saving private key:", err)
|
||
|
}
|
||
|
|
||
|
// test load
|
||
|
loadedKey, err := loadRSAPrivateKey(keyFile)
|
||
|
if err != nil {
|
||
|
t.Error("error loading private key:", err)
|
||
|
}
|
||
|
loadedKeyPEM := pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(loadedKey)}
|
||
|
|
||
|
// very loaded key is correct
|
||
|
if !bytes.Equal(loadedKeyPEM.Bytes, privateKeyPEM.Bytes) {
|
||
|
t.Error("Expected key bytes to be the same, but they weren't")
|
||
|
}
|
||
|
}
|