Files
go-proton-api/keyring_test.go
2023-09-11 09:27:54 +02:00

49 lines
1.0 KiB
Go

package proton_test
import (
"testing"
"github.com/ProtonMail/go-proton-api"
"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) proton.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 proton.Key{
ID: id,
PrivateKey: serial,
Active: true,
}
}
keys := proton.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())
}