2023-06-30 15:22:26 +01:00
|
|
|
package mjwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
2023-10-29 12:28:21 +00:00
|
|
|
"encoding/pem"
|
2023-06-30 15:22:26 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewMJwtSigner(t *testing.T) {
|
2023-10-29 12:28:21 +00:00
|
|
|
t.Parallel()
|
2023-06-30 15:22:26 +01:00
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
NewMJwtSigner("Test", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewMJwtSignerFromFile(t *testing.T) {
|
2023-10-29 12:28:21 +00:00
|
|
|
t.Parallel()
|
2023-06-30 15:22:26 +01:00
|
|
|
tempKey, err := os.CreateTemp("", "key-test-*.pem")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
2023-10-29 12:28:21 +00:00
|
|
|
b := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
|
|
|
|
_, err = tempKey.Write(b)
|
2023-06-30 15:22:26 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, tempKey.Close())
|
|
|
|
signer, err := NewMJwtSignerFromFile("Test", tempKey.Name())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, os.Remove(tempKey.Name()))
|
|
|
|
_, err = NewMJwtSignerFromFile("Test", tempKey.Name())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
assert.True(t, signer.(*defaultMJwtSigner).key.Equal(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewMJwtSignerFromFileOrCreate(t *testing.T) {
|
2023-10-29 12:28:21 +00:00
|
|
|
t.Parallel()
|
2023-06-30 15:22:26 +01:00
|
|
|
tempKey, err := os.CreateTemp("", "key-test-*.pem")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, tempKey.Close())
|
|
|
|
assert.NoError(t, os.Remove(tempKey.Name()))
|
|
|
|
signer, err := NewMJwtSignerFromFileOrCreate("Test", tempKey.Name(), rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
signer2, err := NewMJwtSignerFromFileOrCreate("Test", tempKey.Name(), rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
2023-10-29 12:28:21 +00:00
|
|
|
assert.True(t, signer.PrivateKey().Equal(signer2.PrivateKey()))
|
2023-06-30 15:22:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadOrCreatePrivateKey(t *testing.T) {
|
2023-10-29 12:28:21 +00:00
|
|
|
t.Parallel()
|
2023-06-30 15:22:26 +01:00
|
|
|
tempKey, err := os.CreateTemp("", "key-test-*.pem")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
2023-10-29 12:28:21 +00:00
|
|
|
b := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
|
|
|
|
_, err = tempKey.Write(b)
|
2023-06-30 15:22:26 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, tempKey.Close())
|
|
|
|
key2, err := readOrCreatePrivateKey(tempKey.Name(), rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, key.Equal(key2))
|
|
|
|
assert.NoError(t, os.Remove(tempKey.Name()))
|
|
|
|
key3, err := readOrCreatePrivateKey(tempKey.Name(), rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, key3.Validate())
|
|
|
|
}
|