61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
|
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) {
|
||
|
privKey, _ := SignifierPGPTmp("", rand)
|
||
|
return privKey.Sign(nil, toSign)
|
||
|
},
|
||
|
expErr: true,
|
||
|
},
|
||
|
{
|
||
|
descr: "pgp sig with body",
|
||
|
mkCred: func(toSign []byte) (Credential, error) {
|
||
|
privKey, pubKeyBody := SignifierPGPTmp("", rand)
|
||
|
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)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|