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.

218 lines
5.3 KiB

  1. package proxy_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tendermint/tendermint/lite"
  7. "github.com/tendermint/tendermint/lite/proxy"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. var (
  11. deadBeefTxs = types.Txs{[]byte("DE"), []byte("AD"), []byte("BE"), []byte("EF")}
  12. deadBeefHash = deadBeefTxs.Hash()
  13. testTime1 = time.Date(2018, 1, 1, 1, 1, 1, 1, time.UTC)
  14. testTime2 = time.Date(2017, 1, 2, 1, 1, 1, 1, time.UTC)
  15. )
  16. var hdrHeight11 = &types.Header{
  17. Height: 11,
  18. Time: testTime1,
  19. ValidatorsHash: []byte("Tendermint"),
  20. }
  21. func TestValidateBlock(t *testing.T) {
  22. tests := []struct {
  23. block *types.Block
  24. commit lite.Commit
  25. wantErr string
  26. }{
  27. {
  28. block: nil, wantErr: "non-nil Block",
  29. },
  30. {
  31. block: &types.Block{}, wantErr: "nil Header",
  32. },
  33. {
  34. block: &types.Block{Header: new(types.Header)},
  35. },
  36. // Start Header.Height mismatch test
  37. {
  38. block: &types.Block{Header: &types.Header{Height: 10}},
  39. commit: lite.Commit{Header: &types.Header{Height: 11}},
  40. wantErr: "don't match - 10 vs 11",
  41. },
  42. {
  43. block: &types.Block{Header: &types.Header{Height: 11}},
  44. commit: lite.Commit{Header: &types.Header{Height: 11}},
  45. },
  46. // End Header.Height mismatch test
  47. // Start Header.Hash mismatch test
  48. {
  49. block: &types.Block{Header: hdrHeight11},
  50. commit: lite.Commit{Header: &types.Header{Height: 11}},
  51. wantErr: "Headers don't match",
  52. },
  53. {
  54. block: &types.Block{Header: hdrHeight11},
  55. commit: lite.Commit{Header: hdrHeight11},
  56. },
  57. // End Header.Hash mismatch test
  58. // Start Header.Data hash mismatch test
  59. {
  60. block: &types.Block{
  61. Header: &types.Header{Height: 11},
  62. Data: &types.Data{Txs: []types.Tx{[]byte("0xDE"), []byte("AD")}},
  63. },
  64. commit: lite.Commit{
  65. Header: &types.Header{Height: 11},
  66. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("0xDEADBEEF")}},
  67. },
  68. wantErr: "Data hash doesn't match header",
  69. },
  70. {
  71. block: &types.Block{
  72. Header: &types.Header{Height: 11, DataHash: deadBeefHash},
  73. Data: &types.Data{Txs: deadBeefTxs},
  74. },
  75. commit: lite.Commit{
  76. Header: &types.Header{Height: 11},
  77. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
  78. },
  79. },
  80. // End Header.Data hash mismatch test
  81. }
  82. for i, tt := range tests {
  83. err := proxy.ValidateBlock(tt.block, tt.commit)
  84. if tt.wantErr != "" {
  85. if err == nil {
  86. assert.FailNowf(t, "Unexpectedly passed", "#%d", i)
  87. } else {
  88. assert.Contains(t, err.Error(), tt.wantErr, "#%d should contain the substring\n\n", i)
  89. }
  90. continue
  91. }
  92. assert.Nil(t, err, "#%d: expecting a nil error", i)
  93. }
  94. }
  95. func TestValidateBlockMeta(t *testing.T) {
  96. tests := []struct {
  97. meta *types.BlockMeta
  98. commit lite.Commit
  99. wantErr string
  100. }{
  101. {
  102. meta: nil, wantErr: "non-nil BlockMeta",
  103. },
  104. {
  105. meta: &types.BlockMeta{}, wantErr: "non-nil Header",
  106. },
  107. {
  108. meta: &types.BlockMeta{Header: new(types.Header)},
  109. },
  110. // Start Header.Height mismatch test
  111. {
  112. meta: &types.BlockMeta{Header: &types.Header{Height: 10}},
  113. commit: lite.Commit{Header: &types.Header{Height: 11}},
  114. wantErr: "don't match - 10 vs 11",
  115. },
  116. {
  117. meta: &types.BlockMeta{Header: &types.Header{Height: 11}},
  118. commit: lite.Commit{Header: &types.Header{Height: 11}},
  119. },
  120. // End Header.Height mismatch test
  121. // Start Headers don't match test
  122. {
  123. meta: &types.BlockMeta{Header: hdrHeight11},
  124. commit: lite.Commit{Header: &types.Header{Height: 11}},
  125. wantErr: "Headers don't match",
  126. },
  127. {
  128. meta: &types.BlockMeta{Header: hdrHeight11},
  129. commit: lite.Commit{Header: hdrHeight11},
  130. },
  131. {
  132. meta: &types.BlockMeta{
  133. Header: &types.Header{
  134. Height: 11,
  135. ValidatorsHash: []byte("lite-test"),
  136. // TODO: should be able to use empty time after Amino upgrade
  137. Time: testTime1,
  138. },
  139. },
  140. commit: lite.Commit{
  141. Header: &types.Header{Height: 11, DataHash: deadBeefHash},
  142. },
  143. wantErr: "Headers don't match",
  144. },
  145. {
  146. meta: &types.BlockMeta{
  147. Header: &types.Header{
  148. Height: 11, DataHash: deadBeefHash,
  149. ValidatorsHash: []byte("Tendermint"),
  150. Time: testTime1,
  151. },
  152. },
  153. commit: lite.Commit{
  154. Header: &types.Header{
  155. Height: 11, DataHash: deadBeefHash,
  156. ValidatorsHash: []byte("Tendermint"),
  157. Time: testTime2,
  158. },
  159. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
  160. },
  161. wantErr: "Headers don't match",
  162. },
  163. {
  164. meta: &types.BlockMeta{
  165. Header: &types.Header{
  166. Height: 11, DataHash: deadBeefHash,
  167. ValidatorsHash: []byte("Tendermint"),
  168. Time: testTime2,
  169. },
  170. },
  171. commit: lite.Commit{
  172. Header: &types.Header{
  173. Height: 11, DataHash: deadBeefHash,
  174. ValidatorsHash: []byte("Tendermint-x"),
  175. Time: testTime2,
  176. },
  177. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
  178. },
  179. wantErr: "Headers don't match",
  180. },
  181. // End Headers don't match test
  182. }
  183. for i, tt := range tests {
  184. err := proxy.ValidateBlockMeta(tt.meta, tt.commit)
  185. if tt.wantErr != "" {
  186. if err == nil {
  187. assert.FailNowf(t, "Unexpectedly passed", "#%d: wanted error %q", i, tt.wantErr)
  188. } else {
  189. assert.Contains(t, err.Error(), tt.wantErr, "#%d should contain the substring\n\n", i)
  190. }
  191. continue
  192. }
  193. assert.Nil(t, err, "#%d: expecting a nil error", i)
  194. }
  195. }