From 8d63c983c982f93cc48d3996d6bd097ddeeb327f Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 3 Jan 2021 23:59:25 +0100 Subject: WIP --- identity/key_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 identity/key_test.go (limited to 'identity/key_test.go') diff --git a/identity/key_test.go b/identity/key_test.go new file mode 100644 index 00000000..3206c34e --- /dev/null +++ b/identity/key_test.go @@ -0,0 +1,21 @@ +package identity + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestKeyJSON(t *testing.T) { + k := generatePublicKey() + + data, err := json.Marshal(k) + require.NoError(t, err) + + var read Key + err = json.Unmarshal(data, &read) + require.NoError(t, err) + + require.Equal(t, k, &read) +} -- cgit From dc5059bc3372941e2908739831188768335ac50b Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 24 Jan 2021 19:45:21 +0100 Subject: entity: more progress on merging and signing --- identity/key_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'identity/key_test.go') diff --git a/identity/key_test.go b/identity/key_test.go index 3206c34e..6e320dc2 100644 --- a/identity/key_test.go +++ b/identity/key_test.go @@ -1,21 +1,60 @@ package identity import ( + "crypto/rsa" "encoding/json" "testing" "github.com/stretchr/testify/require" + + "github.com/MichaelMure/git-bug/repository" ) -func TestKeyJSON(t *testing.T) { +func TestPublicKeyJSON(t *testing.T) { k := generatePublicKey() - data, err := json.Marshal(k) + dataJSON, err := json.Marshal(k) require.NoError(t, err) var read Key - err = json.Unmarshal(data, &read) + err = json.Unmarshal(dataJSON, &read) require.NoError(t, err) require.Equal(t, k, &read) } + +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)) +} -- cgit