tendermint/keys/ecc_test.go

63 lines
1.3 KiB
Go
Raw Normal View History

2017-06-20 15:49:17 +02:00
package keys
import (
"testing"
"github.com/stretchr/testify/assert"
cmn "github.com/tendermint/tmlibs/common"
)
2017-07-27 15:59:59 -04:00
var codecs = []ECC{
NewIBMCRC16(),
NewSCSICRC16(),
NewCCITTCRC16(),
NewIEEECRC32(),
NewCastagnoliCRC32(),
NewKoopmanCRC32(),
NewISOCRC64(),
NewECMACRC64(),
}
2017-06-20 15:49:17 +02:00
// TestECCPasses makes sure that the AddECC/CheckECC methods are symetric
func TestECCPasses(t *testing.T) {
assert := assert.New(t)
2017-07-27 15:59:59 -04:00
checks := append(codecs, NoECC{})
2017-06-20 15:49:17 +02:00
for _, check := range checks {
for i := 0; i < 2000; i++ {
numBytes := cmn.RandInt()%60 + 1
data := cmn.RandBytes(numBytes)
checked := check.AddECC(data)
res, err := check.CheckECC(checked)
2017-06-20 16:02:47 +02:00
if assert.Nil(err, "%#v: %+v", check, err) {
2017-06-20 15:49:17 +02:00
assert.Equal(data, res, "%v", check)
}
}
}
}
// TestECCFails makes sure random data will (usually) fail the checksum
func TestECCFails(t *testing.T) {
assert := assert.New(t)
2017-07-27 15:59:59 -04:00
checks := codecs
2017-06-20 15:49:17 +02:00
attempts := 2000
for _, check := range checks {
failed := 0
for i := 0; i < attempts; i++ {
numBytes := cmn.RandInt()%60 + 1
data := cmn.RandBytes(numBytes)
_, err := check.CheckECC(data)
if err != nil {
failed += 1
}
}
// we allow up to 1 falsely accepted checksums, as there are random matches
assert.InDelta(attempts, failed, 1, "%v", check)
}
}