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.

223 lines
5.9 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. deadBeefRipEmd160Hash = deadBeefTxs.Hash()
  13. )
  14. var hdrHeight11Tendermint = &types.Header{
  15. Height: 11,
  16. Time: time.Date(2018, 1, 1, 1, 1, 1, 1, time.UTC),
  17. ValidatorsHash: []byte("Tendermint"),
  18. }
  19. func TestValidateBlock(t *testing.T) {
  20. tests := []struct {
  21. block *types.Block
  22. commit lite.Commit
  23. wantErr string
  24. }{
  25. {
  26. block: nil, wantErr: "non-nil Block",
  27. },
  28. {
  29. block: &types.Block{}, wantErr: "nil Header",
  30. },
  31. {
  32. block: &types.Block{Header: new(types.Header)},
  33. },
  34. // Start Header.Height mismatch test
  35. {
  36. block: &types.Block{Header: &types.Header{Height: 10}},
  37. commit: lite.Commit{Header: &types.Header{Height: 11}},
  38. wantErr: "don't match - 10 vs 11",
  39. },
  40. {
  41. block: &types.Block{Header: &types.Header{Height: 11}},
  42. commit: lite.Commit{Header: &types.Header{Height: 11}},
  43. },
  44. // End Header.Height mismatch test
  45. // Start Header.Hash mismatch test
  46. {
  47. block: &types.Block{Header: hdrHeight11Tendermint},
  48. commit: lite.Commit{Header: &types.Header{Height: 11}},
  49. wantErr: "Headers don't match",
  50. },
  51. {
  52. block: &types.Block{Header: hdrHeight11Tendermint},
  53. commit: lite.Commit{Header: hdrHeight11Tendermint},
  54. },
  55. // End Header.Hash mismatch test
  56. // Start Header.Data hash mismatch test
  57. {
  58. block: &types.Block{
  59. Header: &types.Header{Height: 11},
  60. Data: &types.Data{Txs: []types.Tx{[]byte("0xDE"), []byte("AD")}},
  61. },
  62. commit: lite.Commit{
  63. Header: &types.Header{Height: 11},
  64. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("0xDEADBEEF")}},
  65. },
  66. wantErr: "Data hash doesn't match header",
  67. },
  68. {
  69. block: &types.Block{
  70. Header: &types.Header{Height: 11, DataHash: deadBeefRipEmd160Hash},
  71. Data: &types.Data{Txs: deadBeefTxs},
  72. },
  73. commit: lite.Commit{
  74. Header: &types.Header{Height: 11},
  75. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
  76. },
  77. },
  78. // End Header.Data hash mismatch test
  79. }
  80. for i, tt := range tests {
  81. err := proxy.ValidateBlock(tt.block, tt.commit)
  82. if tt.wantErr != "" {
  83. if err == nil {
  84. assert.FailNowf(t, "Unexpectedly passed", "#%d", i)
  85. } else {
  86. assert.Contains(t, err.Error(), tt.wantErr, "#%d should contain the substring\n\n", i)
  87. }
  88. continue
  89. }
  90. assert.Nil(t, err, "#%d: expecting a nil error", i)
  91. }
  92. }
  93. func TestValidateBlockMeta(t *testing.T) {
  94. tests := []struct {
  95. meta *types.BlockMeta
  96. commit lite.Commit
  97. wantErr string
  98. }{
  99. {
  100. meta: nil, wantErr: "non-nil BlockMeta",
  101. },
  102. {
  103. meta: &types.BlockMeta{}, wantErr: "non-nil Header",
  104. },
  105. {
  106. meta: &types.BlockMeta{Header: new(types.Header)},
  107. },
  108. // Start Header.Height mismatch test
  109. {
  110. meta: &types.BlockMeta{Header: &types.Header{Height: 10}},
  111. commit: lite.Commit{Header: &types.Header{Height: 11}},
  112. wantErr: "don't match - 10 vs 11",
  113. },
  114. {
  115. meta: &types.BlockMeta{Header: &types.Header{Height: 11}},
  116. commit: lite.Commit{Header: &types.Header{Height: 11}},
  117. },
  118. // End Header.Height mismatch test
  119. // Start Headers don't match test
  120. {
  121. meta: &types.BlockMeta{Header: hdrHeight11Tendermint},
  122. commit: lite.Commit{Header: &types.Header{Height: 11}},
  123. wantErr: "Headers don't match",
  124. },
  125. {
  126. meta: &types.BlockMeta{Header: hdrHeight11Tendermint},
  127. commit: lite.Commit{Header: hdrHeight11Tendermint},
  128. },
  129. {
  130. meta: &types.BlockMeta{
  131. Header: &types.Header{
  132. Height: 11,
  133. // TODO: (@odeke-em) inquire why ValidatorsHash has to be non-blank
  134. // for the Header to be hashed. Perhaps this is a security hole because
  135. // an aggressor could perhaps pass in headers that don't have
  136. // ValidatorsHash set and we won't be able to validate blocks.
  137. ValidatorsHash: []byte("lite-test"),
  138. // TODO: (@odeke-em) file an issue with Tendermint to get them to update
  139. // to the latest go-wire, then no more need for this value fill to avoid
  140. // the time zero value of less than 1970.
  141. Time: time.Date(2018, 1, 1, 1, 1, 1, 1, time.UTC),
  142. },
  143. },
  144. commit: lite.Commit{
  145. Header: &types.Header{Height: 11, DataHash: deadBeefRipEmd160Hash},
  146. },
  147. wantErr: "Headers don't match",
  148. },
  149. {
  150. meta: &types.BlockMeta{
  151. Header: &types.Header{
  152. Height: 11, DataHash: deadBeefRipEmd160Hash,
  153. ValidatorsHash: []byte("Tendermint"),
  154. Time: time.Date(2017, 1, 2, 1, 1, 1, 1, time.UTC),
  155. },
  156. },
  157. commit: lite.Commit{
  158. Header: &types.Header{
  159. Height: 11, DataHash: deadBeefRipEmd160Hash,
  160. ValidatorsHash: []byte("Tendermint"),
  161. Time: time.Date(2018, 1, 2, 1, 1, 1, 1, time.UTC),
  162. },
  163. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
  164. },
  165. wantErr: "Headers don't match",
  166. },
  167. {
  168. meta: &types.BlockMeta{
  169. Header: &types.Header{
  170. Height: 11, DataHash: deadBeefRipEmd160Hash,
  171. ValidatorsHash: []byte("Tendermint"),
  172. Time: time.Date(2017, 1, 2, 1, 1, 1, 1, time.UTC),
  173. },
  174. },
  175. commit: lite.Commit{
  176. Header: &types.Header{
  177. Height: 11, DataHash: deadBeefRipEmd160Hash,
  178. ValidatorsHash: []byte("Tendermint-x"),
  179. Time: time.Date(2017, 1, 2, 1, 1, 1, 1, time.UTC),
  180. },
  181. Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
  182. },
  183. wantErr: "Headers don't match",
  184. },
  185. // End Headers don't match test
  186. }
  187. for i, tt := range tests {
  188. err := proxy.ValidateBlockMeta(tt.meta, tt.commit)
  189. if tt.wantErr != "" {
  190. if err == nil {
  191. assert.FailNowf(t, "Unexpectedly passed", "#%d: wanted error %q", i, tt.wantErr)
  192. } else {
  193. assert.Contains(t, err.Error(), tt.wantErr, "#%d should contain the substring\n\n", i)
  194. }
  195. continue
  196. }
  197. assert.Nil(t, err, "#%d: expecting a nil error", i)
  198. }
  199. }