OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-crypto / keys / ecc_test.go
1 package keys
2
3 import (
4         "testing"
5
6         "github.com/stretchr/testify/assert"
7
8         cmn "github.com/tendermint/tmlibs/common"
9 )
10
11 var codecs = []ECC{
12         NewIBMCRC16(),
13         NewSCSICRC16(),
14         NewCCITTCRC16(),
15         NewIEEECRC32(),
16         NewCastagnoliCRC32(),
17         NewKoopmanCRC32(),
18         NewISOCRC64(),
19         NewECMACRC64(),
20 }
21
22 // TestECCPasses makes sure that the AddECC/CheckECC methods are symetric
23 func TestECCPasses(t *testing.T) {
24         assert := assert.New(t)
25
26         checks := append(codecs, NoECC{})
27
28         for _, check := range checks {
29                 for i := 0; i < 2000; i++ {
30                         numBytes := cmn.RandInt()%60 + 1
31                         data := cmn.RandBytes(numBytes)
32
33                         checked := check.AddECC(data)
34                         res, err := check.CheckECC(checked)
35                         if assert.Nil(err, "%#v: %+v", check, err) {
36                                 assert.Equal(data, res, "%v", check)
37                         }
38                 }
39         }
40 }
41
42 // TestECCFails makes sure random data will (usually) fail the checksum
43 func TestECCFails(t *testing.T) {
44         assert := assert.New(t)
45
46         checks := codecs
47         attempts := 2000
48
49         for _, check := range checks {
50                 failed := 0
51                 for i := 0; i < attempts; i++ {
52                         numBytes := cmn.RandInt()%60 + 1
53                         data := cmn.RandBytes(numBytes)
54                         _, err := check.CheckECC(data)
55                         if err != nil {
56                                 failed += 1
57                         }
58                 }
59                 // we allow up to 1 falsely accepted checksums, as there are random matches
60                 assert.InDelta(attempts, failed, 1, "%v", check)
61         }
62 }