aboutsummaryrefslogtreecommitdiffstats
path: root/entities/identity/key_test.go
diff options
context:
space:
mode:
authorSteve Moyer <smoyer1@selesy.com>2022-08-23 10:33:19 -0400
committerSteve Moyer <smoyer1@selesy.com>2022-08-23 10:33:19 -0400
commit7ba98bfadc2c4c7fd3e777074976b78d6171b163 (patch)
tree4865a84837a016e9a18b566046a2aa7a1992acc7 /entities/identity/key_test.go
parent2c2c449187557384fd1e5b9333e808451be86041 (diff)
parent5a70e8b3a2e0fe3d1a1dcd4c24bb6bf64633cb7f (diff)
downloadgit-bug-7ba98bfadc2c4c7fd3e777074976b78d6171b163.tar.gz
Merge branch 'master' into fix-850-ineffective-comment-edit
Diffstat (limited to 'entities/identity/key_test.go')
-rw-r--r--entities/identity/key_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/entities/identity/key_test.go b/entities/identity/key_test.go
new file mode 100644
index 00000000..6e320dc2
--- /dev/null
+++ b/entities/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))
+}