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/mediocregopher/mediocre-go-lib/merr"
"github.com/mediocregopher/mediocre-go-lib/mrand"
2018-03-12 12:29:51 +00:00
"github.com/stretchr/testify/assert"
)
func TestSignerVerifier(t *T) {
secret := NewSecret(mrand.Bytes(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
thisStr := mrand.Hex(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)
assert.True(t, merr.Equal(err, ErrInvalidSig))
2018-03-12 12:29:51 +00:00
}
prevStr = thisStr
prevSig = thisSig
}
}