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"
|
|
|
|
)
|
|
|
|
|
2018-03-23 15:10:53 +00:00
|
|
|
func TestSignerVerifier(t *T) {
|
2018-03-12 12:29:51 +00:00
|
|
|
secret := mtest.RandBytes(16)
|
2018-03-23 15:10:53 +00:00
|
|
|
sigI, ver := NewSignerVerifier(secret)
|
|
|
|
sig := sigI.(signVerifier)
|
|
|
|
weakSigI, weakVer := NewWeakSignerVerifier(secret)
|
|
|
|
weakSig := weakSigI.(signVerifier)
|
|
|
|
var prevStr string
|
|
|
|
var prevSig, prevWeakSig Signature
|
2018-03-12 12:29:51 +00:00
|
|
|
for i := 0; i < 10000; i++ {
|
2018-03-23 15:10:53 +00:00
|
|
|
now := time.Now().Round(0)
|
|
|
|
sig.testNow = now
|
|
|
|
weakSig.testNow = now
|
|
|
|
|
2018-03-12 12:29:51 +00:00
|
|
|
thisStr := mtest.RandHex(512)
|
2018-03-23 15:10:53 +00:00
|
|
|
thisSig := SignString(sig, thisStr)
|
|
|
|
thisWeakSig := SignString(weakSig, thisStr)
|
|
|
|
thisSigStr, thisWeakSigStr := thisSig.String(), thisWeakSig.String()
|
|
|
|
|
|
|
|
// checking the times made it
|
|
|
|
assert.Equal(t, now, thisSig.Time())
|
|
|
|
assert.Equal(t, now, thisWeakSig.Time())
|
2018-03-12 12:29:51 +00:00
|
|
|
|
|
|
|
// sanity checks
|
2018-03-23 15:10:53 +00:00
|
|
|
assert.NotEmpty(t, thisSigStr)
|
|
|
|
assert.NotEmpty(t, thisWeakSigStr)
|
|
|
|
assert.NotEqual(t, thisSigStr, thisWeakSigStr)
|
|
|
|
assert.True(t, len(thisSigStr) > len(thisWeakSigStr))
|
|
|
|
|
|
|
|
// marshaling/unmarshaling
|
|
|
|
var thisSig2, thisWeakSig2 Signature
|
|
|
|
assert.NoError(t, thisSig2.UnmarshalText([]byte(thisSigStr)))
|
|
|
|
assert.Equal(t, thisSigStr, thisSig2.String())
|
|
|
|
assert.NoError(t, thisWeakSig2.UnmarshalText([]byte(thisWeakSigStr)))
|
|
|
|
assert.Equal(t, thisWeakSigStr, thisWeakSig2.String())
|
|
|
|
assert.Equal(t, now, thisSig2.Time())
|
|
|
|
assert.Equal(t, now, thisWeakSig2.Time())
|
2018-03-12 12:29:51 +00:00
|
|
|
|
2018-03-23 15:10:53 +00:00
|
|
|
// Either sigVer should be able to verify either signature
|
|
|
|
assert.NoError(t, VerifyString(ver, thisSig, thisStr))
|
|
|
|
assert.NoError(t, VerifyString(weakVer, thisWeakSig, thisStr))
|
|
|
|
assert.NoError(t, VerifyString(ver, thisWeakSig, thisStr))
|
|
|
|
assert.NoError(t, VerifyString(weakVer, thisSig, thisStr))
|
2018-03-12 12:29:51 +00:00
|
|
|
|
|
|
|
if prevStr != "" {
|
2018-03-23 15:10:53 +00:00
|
|
|
assert.NotEqual(t, prevSig.String(), thisSigStr)
|
|
|
|
assert.NotEqual(t, prevWeakSig.String(), thisWeakSigStr)
|
|
|
|
err := VerifyString(ver, prevSig, thisStr)
|
2018-03-12 12:29:51 +00:00
|
|
|
assert.True(t, merry.Is(err, ErrInvalidSig))
|
2018-03-23 15:10:53 +00:00
|
|
|
err = VerifyString(ver, prevWeakSig, thisStr)
|
2018-03-12 12:29:51 +00:00
|
|
|
assert.True(t, merry.Is(err, ErrInvalidSig))
|
|
|
|
}
|
|
|
|
prevStr = thisStr
|
|
|
|
prevSig = thisSig
|
|
|
|
prevWeakSig = thisWeakSig
|
|
|
|
}
|
|
|
|
}
|