aboutsummaryrefslogtreecommitdiffstats
path: root/identity/key_test.go
blob: 6e320dc2056dc35dcc0e4f8de2806a1d277520e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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))
}