| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 7 |
"github.com/ProtonMail/gopenpgp/v2/helper" |
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
func TestKeyring_Unlock(t *testing.T) { |
| 12 |
r := require.New(t) |
| 13 |
|
| 14 |
newKey := func(id, passphrase string) Key { |
| 15 |
arm, err := helper.GenerateKey(id, id+"@email.com", []byte(passphrase), "rsa", 2048) |
| 16 |
r.NoError(err) |
| 17 |
|
| 18 |
privKey, err := crypto.NewKeyFromArmored(arm) |
| 19 |
r.NoError(err) |
| 20 |
|
| 21 |
serial, err := privKey.Serialize() |
| 22 |
r.NoError(err) |
| 23 |
|
| 24 |
return Key{ |
| 25 |
ID: id, |
| 26 |
PrivateKey: serial, |
| 27 |
Active: true, |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
keys := Keys{ |
| 32 |
newKey("1", "good_phrase"), |
| 33 |
newKey("2", "good_phrase"), |
| 34 |
newKey("3", "bad_phrase"), |
| 35 |
} |
| 36 |
|
| 37 |
_, err := keys.Unlock([]byte("ugly_phrase"), nil) |
| 38 |
r.Error(err) |
| 39 |
|
| 40 |
kr, err := keys.Unlock([]byte("bad_phrase"), nil) |
| 41 |
r.NoError(err) |
| 42 |
r.Equal(1, kr.CountEntities()) |
| 43 |
|
| 44 |
kr, err = keys.Unlock([]byte("good_phrase"), nil) |
| 45 |
r.NoError(err) |
| 46 |
r.Equal(2, kr.CountEntities()) |
| 47 |
} |