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.

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