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.

86 lines
2.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package blocks
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "testing"
  6. . "github.com/tendermint/tendermint/common"
  7. )
  8. func TestBasicPartSet(t *testing.T) {
  9. // Construct random data of size partSize * 100
  10. data := RandBytes(partSize * 100)
  11. partSet := NewPartSetFromData(data)
  12. if len(partSet.Hash()) == 0 {
  13. t.Error("Expected to get hash")
  14. }
  15. if partSet.Total() != 100 {
  16. t.Errorf("Expected to get 100 parts, but got %v", partSet.Total())
  17. }
  18. if !partSet.IsComplete() {
  19. t.Errorf("PartSet should be complete")
  20. }
  21. // Test adding parts to a new partSet.
  22. partSet2 := NewPartSetFromHeader(partSet.Header())
  23. for i := uint16(0); i < partSet.Total(); i++ {
  24. part := partSet.GetPart(i)
  25. //t.Logf("\n%v", part)
  26. added, err := partSet2.AddPart(part)
  27. if !added || err != nil {
  28. t.Errorf("Failed to add part %v, error: %v", i, err)
  29. }
  30. }
  31. if !bytes.Equal(partSet.Hash(), partSet2.Hash()) {
  32. t.Error("Expected to get same hash")
  33. }
  34. if partSet2.Total() != 100 {
  35. t.Errorf("Expected to get 100 parts, but got %v", partSet2.Total())
  36. }
  37. if !partSet2.IsComplete() {
  38. t.Errorf("Reconstructed PartSet should be complete")
  39. }
  40. // Reconstruct data, assert that they are equal.
  41. data2Reader := partSet2.GetReader()
  42. data2, err := ioutil.ReadAll(data2Reader)
  43. if err != nil {
  44. t.Errorf("Error reading data2Reader: %v", err)
  45. }
  46. if !bytes.Equal(data, data2) {
  47. t.Errorf("Got wrong data.")
  48. }
  49. }
  50. func TestWrongTrail(t *testing.T) {
  51. // Construct random data of size partSize * 100
  52. data := RandBytes(partSize * 100)
  53. partSet := NewPartSetFromData(data)
  54. // Test adding a part with wrong data.
  55. partSet2 := NewPartSetFromHeader(partSet.Header())
  56. // Test adding a part with wrong trail.
  57. part := partSet.GetPart(0)
  58. part.Trail[0][0] += byte(0x01)
  59. added, err := partSet2.AddPart(part)
  60. if added || err == nil {
  61. t.Errorf("Expected to fail adding a part with bad trail.")
  62. }
  63. // Test adding a part with wrong bytes.
  64. part = partSet.GetPart(1)
  65. part.Bytes[0] += byte(0x01)
  66. added, err = partSet2.AddPart(part)
  67. if added || err == nil {
  68. t.Errorf("Expected to fail adding a part with bad bytes.")
  69. }
  70. }