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.

71 lines
1.4 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. NewIBMCRC16(),
  13. NewSCSICRC16(),
  14. NewCCITTCRC16(),
  15. NewIEEECRC32(),
  16. NewCastagnoliCRC32(),
  17. NewKoopmanCRC32(),
  18. NewISOCRC64(),
  19. NewECMACRC64(),
  20. }
  21. for _, check := range checks {
  22. for i := 0; i < 2000; i++ {
  23. numBytes := cmn.RandInt()%60 + 1
  24. data := cmn.RandBytes(numBytes)
  25. checked := check.AddECC(data)
  26. res, err := check.CheckECC(checked)
  27. if assert.Nil(err, "%#v: %+v", check, err) {
  28. assert.Equal(data, res, "%v", check)
  29. }
  30. }
  31. }
  32. }
  33. // TestECCFails makes sure random data will (usually) fail the checksum
  34. func TestECCFails(t *testing.T) {
  35. assert := assert.New(t)
  36. checks := []ECC{
  37. NewIBMCRC16(),
  38. NewSCSICRC16(),
  39. NewCCITTCRC16(),
  40. NewIEEECRC32(),
  41. NewCastagnoliCRC32(),
  42. NewKoopmanCRC32(),
  43. NewISOCRC64(),
  44. NewECMACRC64(),
  45. }
  46. attempts := 2000
  47. for _, check := range checks {
  48. failed := 0
  49. for i := 0; i < attempts; i++ {
  50. numBytes := cmn.RandInt()%60 + 1
  51. data := cmn.RandBytes(numBytes)
  52. _, err := check.CheckECC(data)
  53. if err != nil {
  54. failed += 1
  55. }
  56. }
  57. // we allow up to 1 falsely accepted checksums, as there are random matches
  58. assert.InDelta(attempts, failed, 1, "%v", check)
  59. }
  60. }