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
|
2020-04-26 20:23:03 +00:00
|
|
|
mkCred func(toSign []byte) (CredentialUnion, error)
|
2020-04-11 23:10:18 +00:00
|
|
|
expErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
descr: "pgp sig no body",
|
2020-04-26 20:23:03 +00:00
|
|
|
mkCred: func(toSign []byte) (CredentialUnion, error) {
|
2020-04-12 17:59:23 +00:00
|
|
|
privKey, _ := TestSignifierPGP("", false, rand)
|
2020-04-11 23:10:18 +00:00
|
|
|
return privKey.Sign(nil, toSign)
|
|
|
|
},
|
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pgp sig with body",
|
2020-04-26 20:23:03 +00:00
|
|
|
mkCred: func(toSign []byte) (CredentialUnion, error) {
|
2020-04-12 17:59:23 +00:00
|
|
|
privKey, _ := TestSignifierPGP("", true, rand)
|
|
|
|
return privKey.Sign(nil, toSign)
|
2020-04-11 23:10:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|