You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.3 KiB

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