2021-01-04 01:59:25 +03:00
|
|
|
package identity
|
|
|
|
|
|
|
|
import (
|
2021-01-24 21:45:21 +03:00
|
|
|
"crypto/rsa"
|
2021-01-04 01:59:25 +03:00
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-24 21:45:21 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2021-01-04 01:59:25 +03:00
|
|
|
)
|
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
func TestPublicKeyJSON(t *testing.T) {
|
2021-01-04 01:59:25 +03:00
|
|
|
k := generatePublicKey()
|
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
dataJSON, err := json.Marshal(k)
|
2021-01-04 01:59:25 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var read Key
|
2021-01-24 21:45:21 +03:00
|
|
|
err = json.Unmarshal(dataJSON, &read)
|
2021-01-04 01:59:25 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, k, &read)
|
|
|
|
}
|
2021-01-24 21:45:21 +03:00
|
|
|
|
|
|
|
func TestStoreLoad(t *testing.T) {
|
|
|
|
repo := repository.NewMockRepoKeyring()
|
|
|
|
|
|
|
|
// public + private
|
|
|
|
k := GenerateKey()
|
|
|
|
|
|
|
|
// Store
|
|
|
|
|
|
|
|
dataJSON, err := json.Marshal(k)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = k.storePrivate(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Load
|
|
|
|
|
|
|
|
var read Key
|
|
|
|
err = json.Unmarshal(dataJSON, &read)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = read.ensurePrivateKey(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, k.public, read.public)
|
|
|
|
|
|
|
|
require.IsType(t, (*rsa.PrivateKey)(nil), k.private.PrivateKey)
|
|
|
|
|
|
|
|
// See https://github.com/golang/crypto/pull/175
|
|
|
|
rsaPriv := read.private.PrivateKey.(*rsa.PrivateKey)
|
|
|
|
back := rsaPriv.Primes[0]
|
|
|
|
rsaPriv.Primes[0] = rsaPriv.Primes[1]
|
|
|
|
rsaPriv.Primes[1] = back
|
|
|
|
|
|
|
|
require.True(t, k.private.PrivateKey.(*rsa.PrivateKey).Equal(read.private.PrivateKey))
|
|
|
|
}
|