2015-06-13 15:53:17 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
2015-06-14 00:33:21 +00:00
|
|
|
"bytes"
|
2016-01-27 01:01:58 +00:00
|
|
|
"crypto/rand"
|
2015-06-13 15:53:17 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"testing"
|
2015-10-16 19:05:16 +00:00
|
|
|
"time"
|
2018-09-15 17:16:35 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-06-13 15:53:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGeneratePrivateKey(t *testing.T) {
|
2016-01-27 01:01:58 +00:00
|
|
|
key, err := generatePrivateKey(RSA2048)
|
2018-09-15 17:16:35 +00:00
|
|
|
require.NoError(t, err, "Error generating private key")
|
|
|
|
|
|
|
|
assert.NotNil(t, key)
|
2015-06-13 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateCSR(t *testing.T) {
|
2016-01-27 01:01:58 +00:00
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 512)
|
2018-09-15 17:16:35 +00:00
|
|
|
require.NoError(t, err, "Error generating private key")
|
2015-06-13 15:53:17 +00:00
|
|
|
|
2016-10-27 09:22:10 +00:00
|
|
|
csr, err := generateCsr(key, "fizz.buzz", nil, true)
|
2018-09-15 17:16:35 +00:00
|
|
|
require.NoError(t, err, "Error generating CSR")
|
|
|
|
|
|
|
|
assert.NotEmpty(t, csr)
|
2015-06-13 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPEMEncode(t *testing.T) {
|
2015-06-14 00:33:21 +00:00
|
|
|
buf := bytes.NewBufferString("TestingRSAIsSoMuchFun")
|
|
|
|
|
|
|
|
reader := MockRandReader{b: buf}
|
|
|
|
key, err := rsa.GenerateKey(reader, 32)
|
2018-09-15 17:16:35 +00:00
|
|
|
require.NoError(t, err, "Error generating private key")
|
2015-06-13 15:53:17 +00:00
|
|
|
|
|
|
|
data := pemEncode(key)
|
2018-09-15 17:16:35 +00:00
|
|
|
require.NotNil(t, data)
|
|
|
|
assert.Len(t, data, 127)
|
2015-06-13 15:53:17 +00:00
|
|
|
}
|
2015-06-14 00:33:21 +00:00
|
|
|
|
2015-10-18 19:18:36 +00:00
|
|
|
func TestPEMCertExpiration(t *testing.T) {
|
2016-01-27 01:01:58 +00:00
|
|
|
privKey, err := generatePrivateKey(RSA2048)
|
2018-09-15 17:16:35 +00:00
|
|
|
require.NoError(t, err, "Error generating private key")
|
2015-10-16 19:05:16 +00:00
|
|
|
|
|
|
|
expiration := time.Now().Add(365)
|
|
|
|
expiration = expiration.Round(time.Second)
|
2018-06-13 23:20:56 +00:00
|
|
|
certBytes, err := generateDerCert(privKey.(*rsa.PrivateKey), expiration, "test.com", nil)
|
2018-09-15 17:16:35 +00:00
|
|
|
require.NoError(t, err, "Error generating cert")
|
2015-10-16 19:05:16 +00:00
|
|
|
|
|
|
|
buf := bytes.NewBufferString("TestingRSAIsSoMuchFun")
|
|
|
|
|
2015-10-18 19:18:36 +00:00
|
|
|
// Some random string should return an error.
|
2018-09-15 17:16:35 +00:00
|
|
|
ctime, err := GetPEMCertExpiration(buf.Bytes())
|
2018-10-02 22:02:01 +00:00
|
|
|
require.Errorf(t, err, "Expected getCertExpiration to return an error for garbage string but returned %v", ctime)
|
2015-10-16 19:05:16 +00:00
|
|
|
|
2015-10-18 19:18:36 +00:00
|
|
|
// A DER encoded certificate should return an error.
|
2018-09-15 17:16:35 +00:00
|
|
|
_, err = GetPEMCertExpiration(certBytes)
|
|
|
|
require.Error(t, err, "Expected getCertExpiration to return an error for DER certificates")
|
2015-10-18 19:18:36 +00:00
|
|
|
|
|
|
|
// A PEM encoded certificate should work ok.
|
|
|
|
pemCert := pemEncode(derCertificateBytes(certBytes))
|
2018-09-15 17:16:35 +00:00
|
|
|
ctime, err = GetPEMCertExpiration(pemCert)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, expiration.UTC(), ctime)
|
2015-10-16 19:05:16 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 00:33:21 +00:00
|
|
|
type MockRandReader struct {
|
|
|
|
b *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r MockRandReader) Read(p []byte) (int, error) {
|
|
|
|
return r.b.Read(p)
|
|
|
|
}
|