File: keyring_test.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (47 lines) | stat: -rw-r--r-- 960 bytes parent folder | download
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
package proton

import (
	"testing"

	"github.com/ProtonMail/gopenpgp/v2/crypto"
	"github.com/ProtonMail/gopenpgp/v2/helper"
	"github.com/stretchr/testify/require"
)

func TestKeyring_Unlock(t *testing.T) {
	r := require.New(t)

	newKey := func(id, passphrase string) Key {
		arm, err := helper.GenerateKey(id, id+"@email.com", []byte(passphrase), "rsa", 2048)
		r.NoError(err)

		privKey, err := crypto.NewKeyFromArmored(arm)
		r.NoError(err)

		serial, err := privKey.Serialize()
		r.NoError(err)

		return Key{
			ID:         id,
			PrivateKey: serial,
			Active:     true,
		}
	}

	keys := Keys{
		newKey("1", "good_phrase"),
		newKey("2", "good_phrase"),
		newKey("3", "bad_phrase"),
	}

	_, err := keys.Unlock([]byte("ugly_phrase"), nil)
	r.Error(err)

	kr, err := keys.Unlock([]byte("bad_phrase"), nil)
	r.NoError(err)
	r.Equal(1, kr.CountEntities())

	kr, err = keys.Unlock([]byte("good_phrase"), nil)
	r.NoError(err)
	r.Equal(2, kr.CountEntities())
}