|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "google.golang.org/protobuf/proto" |
| 9 | +) |
| 10 | + |
| 11 | +// TestValidateBasic tests the ValidateBasic method of Signature. |
| 12 | +func TestValidateBasic(t *testing.T) { |
| 13 | + sig := Signature([]byte("signature")) |
| 14 | + assert.NoError(t, sig.ValidateBasic()) |
| 15 | + |
| 16 | + emptySig := Signature([]byte{}) |
| 17 | + assert.ErrorIs(t, emptySig.ValidateBasic(), ErrSignatureEmpty) |
| 18 | + |
| 19 | + nilSig := Signature(nil) |
| 20 | + assert.ErrorIs(t, nilSig.ValidateBasic(), ErrSignatureEmpty) |
| 21 | +} |
| 22 | + |
| 23 | +// TestValidate tests the Validate function for Data against a SignedHeader. |
| 24 | +func TestValidate(t *testing.T) { |
| 25 | + header, data := GetRandomBlock(1, 10, "testchain") |
| 26 | + |
| 27 | + // Case 1: Valid header and data |
| 28 | + t.Run("valid header and data", func(t *testing.T) { |
| 29 | + err := Validate(header, data) |
| 30 | + assert.NoError(t, err) |
| 31 | + }) |
| 32 | + |
| 33 | + // Case 2: Mismatched ChainID |
| 34 | + t.Run("mismatched chainID", func(t *testing.T) { |
| 35 | + invalidData := data.New() |
| 36 | + *invalidData = *data |
| 37 | + invalidData.Metadata = &Metadata{} |
| 38 | + *invalidData.Metadata = *data.Metadata |
| 39 | + invalidData.Metadata.ChainID = "wrongchain" |
| 40 | + err := Validate(header, invalidData) |
| 41 | + assert.Error(t, err) |
| 42 | + assert.Contains(t, err.Error(), "header and data do not match") |
| 43 | + }) |
| 44 | + |
| 45 | + // Case 3: Mismatched Height |
| 46 | + t.Run("mismatched height", func(t *testing.T) { |
| 47 | + invalidData := data.New() |
| 48 | + *invalidData = *data |
| 49 | + invalidData.Metadata = &Metadata{} |
| 50 | + *invalidData.Metadata = *data.Metadata |
| 51 | + invalidData.Metadata.Height = header.Height() + 1 |
| 52 | + err := Validate(header, invalidData) |
| 53 | + assert.Error(t, err) |
| 54 | + assert.Contains(t, err.Error(), "header and data do not match") |
| 55 | + }) |
| 56 | + |
| 57 | + // Case 4: Mismatched Time |
| 58 | + t.Run("mismatched time", func(t *testing.T) { |
| 59 | + invalidData := data.New() |
| 60 | + *invalidData = *data |
| 61 | + invalidData.Metadata = &Metadata{} |
| 62 | + *invalidData.Metadata = *data.Metadata |
| 63 | + invalidData.Metadata.Time = uint64(header.Time().Unix() + 1) |
| 64 | + err := Validate(header, invalidData) |
| 65 | + assert.Error(t, err) |
| 66 | + assert.Contains(t, err.Error(), "header and data do not match") |
| 67 | + }) |
| 68 | + |
| 69 | + // Case 5: Mismatched DataHash (due to different Txs) |
| 70 | + t.Run("mismatched data hash", func(t *testing.T) { |
| 71 | + invalidData := data.New() |
| 72 | + *invalidData = *data |
| 73 | + invalidData.Txs = Txs{Tx("different tx")} |
| 74 | + err := Validate(header, invalidData) |
| 75 | + assert.Error(t, err) |
| 76 | + assert.Contains(t, err.Error(), "dataHash from the header does not match with hash of the block's data") |
| 77 | + }) |
| 78 | + |
| 79 | + // Case 6: Data with nil Metadata (should still validate DataHash) |
| 80 | + t.Run("nil metadata", func(t *testing.T) { |
| 81 | + dataWithNilMeta := &Data{ |
| 82 | + Txs: data.Txs, |
| 83 | + } |
| 84 | + err := Validate(header, dataWithNilMeta) |
| 85 | + assert.NoError(t, err) |
| 86 | + |
| 87 | + // Now test nil metadata with wrong Txs |
| 88 | + dataWithNilMetaWrongHash := &Data{ |
| 89 | + Txs: Txs{Tx("different tx")}, |
| 90 | + } |
| 91 | + err = Validate(header, dataWithNilMetaWrongHash) |
| 92 | + assert.Error(t, err) |
| 93 | + assert.Contains(t, err.Error(), "dataHash from the header does not match with hash of the block's data") |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +// TestDataGetters tests the getter methods on the Data struct. |
| 98 | +func TestDataGetters(t *testing.T) { |
| 99 | + _, data := GetRandomBlock(5, 3, "getter-test") |
| 100 | + |
| 101 | + assert.Equal(t, "getter-test", data.ChainID()) |
| 102 | + assert.Equal(t, uint64(5), data.Height()) |
| 103 | + assert.Equal(t, data.LastDataHash, data.LastHeader()) |
| 104 | + assert.Equal(t, time.Unix(0, int64(data.Metadata.Time)), data.Time()) |
| 105 | + |
| 106 | + nilMetaData := &Data{Txs: data.Txs} |
| 107 | + assert.Panics(t, func() { nilMetaData.ChainID() }) |
| 108 | + assert.Panics(t, func() { nilMetaData.Height() }) |
| 109 | + assert.Panics(t, func() { nilMetaData.LastHeader() }) |
| 110 | + assert.Panics(t, func() { nilMetaData.Time() }) |
| 111 | +} |
| 112 | + |
| 113 | +// TestVerify tests the Verify method for comparing two Data objects. |
| 114 | +func TestVerify(t *testing.T) { |
| 115 | + _, trustedData := GetRandomBlock(1, 5, "verify-test") |
| 116 | + _, untrustedData := GetRandomBlock(2, 5, "verify-test") |
| 117 | + |
| 118 | + trustedDataHash := trustedData.Hash() |
| 119 | + untrustedData.LastDataHash = trustedDataHash |
| 120 | + |
| 121 | + // Case 1: Valid verification |
| 122 | + t.Run("valid verification", func(t *testing.T) { |
| 123 | + err := trustedData.Verify(untrustedData) |
| 124 | + assert.NoError(t, err) |
| 125 | + }) |
| 126 | + |
| 127 | + // Case 2: Invalid verification (wrong LastDataHash) |
| 128 | + t.Run("invalid last data hash", func(t *testing.T) { |
| 129 | + invalidUntrustedData := untrustedData.New() |
| 130 | + *invalidUntrustedData = *untrustedData |
| 131 | + invalidUntrustedData.Metadata = &Metadata{} |
| 132 | + *invalidUntrustedData.Metadata = *untrustedData.Metadata |
| 133 | + invalidUntrustedData.LastDataHash = []byte("clearly wrong hash") |
| 134 | + err := trustedData.Verify(invalidUntrustedData) |
| 135 | + assert.Error(t, err) |
| 136 | + assert.Contains(t, err.Error(), "data hash of the trusted data does not match with last data hash of the untrusted data") |
| 137 | + }) |
| 138 | + |
| 139 | + // Case 3: Untrusted data is nil |
| 140 | + t.Run("nil untrusted data", func(t *testing.T) { |
| 141 | + err := trustedData.Verify(nil) |
| 142 | + assert.Error(t, err) |
| 143 | + assert.Contains(t, err.Error(), "untrusted block cannot be nil") |
| 144 | + }) |
| 145 | + |
| 146 | + _, data3 := GetRandomBlock(3, 2, "another-chain") |
| 147 | + data3.LastDataHash = trustedDataHash |
| 148 | + err := trustedData.Verify(data3) |
| 149 | + assert.NoError(t, err, "Verify should only compare trusted.Hash() and untrusted.LastDataHash") |
| 150 | +} |
| 151 | + |
| 152 | +// TestDataSize tests the Size method of Data. |
| 153 | +func TestDataSize(t *testing.T) { |
| 154 | + _, data := GetRandomBlock(1, 5, "size-test") |
| 155 | + protoData := data.ToProto() |
| 156 | + expectedSize := proto.Size(protoData) |
| 157 | + assert.Equal(t, expectedSize, data.Size()) |
| 158 | + assert.True(t, data.Size() > 0) |
| 159 | + |
| 160 | + emptyData := &Data{} |
| 161 | + protoEmpty := emptyData.ToProto() |
| 162 | + expectedEmptySize := proto.Size(protoEmpty) |
| 163 | + assert.Equal(t, expectedEmptySize, emptyData.Size()) |
| 164 | +} |
| 165 | + |
| 166 | +// TestIsZero tests the IsZero method of Data. |
| 167 | +func TestIsZero(t *testing.T) { |
| 168 | + var nilData *Data |
| 169 | + assert.True(t, nilData.IsZero()) |
| 170 | + |
| 171 | + data := &Data{} |
| 172 | + assert.False(t, data.IsZero()) |
| 173 | + |
| 174 | + _, data = GetRandomBlock(1, 1, "not-zero") |
| 175 | + assert.False(t, data.IsZero()) |
| 176 | +} |
| 177 | + |
| 178 | +// TestDataValidate tests the Validate method of Data returns nil |
| 179 | +func TestDataValidate(t *testing.T) { |
| 180 | + _, data := GetRandomBlock(1, 5, "validate-test") |
| 181 | + assert.NoError(t, data.Validate()) |
| 182 | +} |
0 commit comments