mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-13 07:11:19 +00:00
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
|
package keys
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
|
||
|
cmn "github.com/tendermint/tmlibs/common"
|
||
|
)
|
||
|
|
||
|
// TestECCPasses makes sure that the AddECC/CheckECC methods are symetric
|
||
|
func TestECCPasses(t *testing.T) {
|
||
|
assert := assert.New(t)
|
||
|
|
||
|
checks := []ECC{NoECC{}, CRC32{}}
|
||
|
|
||
|
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)
|
||
|
if assert.Nil(err, "%v: %+v", check, err) {
|
||
|
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)
|
||
|
|
||
|
checks := []ECC{CRC32{}}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
}
|