diff options
author | Michael Muré <batolettre@gmail.com> | 2021-04-04 15:49:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-04 15:49:16 +0200 |
commit | fc04af34f5d6dff930003cc1f27ead972d424324 (patch) | |
tree | 8b9750d46182fff88d72fcc891e89d5a79ed83b0 /identity/key_test.go | |
parent | 2055bd5d0afb534cc98cd11bd9802d66acbb0c8f (diff) | |
parent | cb9b06551ddc1fae33046733f79ede20f8d09f9a (diff) | |
download | git-bug-fc04af34f5d6dff930003cc1f27ead972d424324.tar.gz |
Merge pull request #532 from MichaelMure/dag-entity
Work towards a reusable entity datastructure + commit signature
Diffstat (limited to 'identity/key_test.go')
-rw-r--r-- | identity/key_test.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/identity/key_test.go b/identity/key_test.go new file mode 100644 index 00000000..6e320dc2 --- /dev/null +++ b/identity/key_test.go @@ -0,0 +1,60 @@ +package identity + +import ( + "crypto/rsa" + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/MichaelMure/git-bug/repository" +) + +func TestPublicKeyJSON(t *testing.T) { + k := generatePublicKey() + + dataJSON, err := json.Marshal(k) + require.NoError(t, err) + + var read Key + 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)) +} |