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.

212 lines
5.1 KiB

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