mediocre-go-lib/mcrypto/sig_test.go

45 lines
1.1 KiB
Go
Raw Permalink Normal View History

2018-03-12 12:29:51 +00:00
package mcrypto
import (
. "testing"
"time"
"github.com/ansel1/merry"
"github.com/mediocregopher/mediocre-go-lib/mtest"
"github.com/stretchr/testify/assert"
)
func TestSignerVerifier(t *T) {
secret := NewSecret(mtest.RandBytes(16))
var prevStr string
var prevSig Signature
2018-03-12 12:29:51 +00:00
for i := 0; i < 10000; i++ {
now := time.Now().Round(0)
secret.testNow = now
2018-03-12 12:29:51 +00:00
thisStr := mtest.RandHex(512)
thisSig := SignString(secret, thisStr)
thisSigStr := thisSig.String()
2018-03-12 12:29:51 +00:00
// sanity checks
assert.NotEmpty(t, thisSigStr)
assert.Equal(t, now, thisSig.Time())
assert.NoError(t, VerifyString(secret, thisSig, thisStr))
// marshaling/unmarshaling
var thisSig2 Signature
assert.NoError(t, thisSig2.UnmarshalText([]byte(thisSigStr)))
assert.Equal(t, thisSigStr, thisSig2.String())
assert.Equal(t, now, thisSig2.Time())
assert.NoError(t, VerifyString(secret, thisSig2, thisStr))
2018-03-12 12:29:51 +00:00
if prevStr != "" {
assert.NotEqual(t, prevSig.String(), thisSigStr)
err := VerifyString(secret, prevSig, thisStr)
2018-03-12 12:29:51 +00:00
assert.True(t, merry.Is(err, ErrInvalidSig))
}
prevStr = thisStr
prevSig = thisSig
}
}