2020-04-11 23:10:18 +00:00
|
|
|
package sigcred
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSelfVerifyingCredentials(t *testing.T) {
|
|
|
|
seed := time.Now().UnixNano()
|
|
|
|
t.Logf("seed: %d", seed)
|
|
|
|
rand := rand.New(rand.NewSource(seed))
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
descr string
|
|
|
|
mkCred func(toSign []byte) (Credential, error)
|
|
|
|
expErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
descr: "pgp sig no body",
|
|
|
|
mkCred: func(toSign []byte) (Credential, error) {
|
2020-04-12 17:02:05 +00:00
|
|
|
privKey, _ := TestSignifierPGP("", rand)
|
2020-04-11 23:10:18 +00:00
|
|
|
return privKey.Sign(nil, toSign)
|
|
|
|
},
|
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pgp sig with body",
|
|
|
|
mkCred: func(toSign []byte) (Credential, error) {
|
2020-04-12 17:02:05 +00:00
|
|
|
privKey, pubKeyBody := TestSignifierPGP("", rand)
|
2020-04-11 23:10:18 +00:00
|
|
|
cred, err := privKey.Sign(nil, toSign)
|
|
|
|
cred.PGPSignature.PubKeyBody = string(pubKeyBody)
|
|
|
|
return cred, err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.descr, func(t *testing.T) {
|
|
|
|
data := make([]byte, rand.Intn(1024))
|
|
|
|
if _, err := rand.Read(data); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cred, err := test.mkCred(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cred.SelfVerify(data)
|
|
|
|
isNotSelfVerifying := errors.As(err, new(ErrNotSelfVerifying))
|
|
|
|
if test.expErr && !isNotSelfVerifying {
|
|
|
|
t.Fatalf("expected ErrNotSelfVerifying but got: %v", err)
|
|
|
|
} else if !test.expErr && err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|