Commit c6d39019 authored by pcw109550's avatar pcw109550 Committed by protolambda

Add tests for span batch field size check

parent 177b78c5
......@@ -2,6 +2,7 @@ package derive
import (
"bytes"
"math"
"math/big"
"math/rand"
"testing"
......@@ -542,9 +543,68 @@ func TestSpanBatchMaxTxData(t *testing.T) {
func TestSpanBatchMaxOriginBitsLength(t *testing.T) {
var sb RawSpanBatch
sb.blockCount = 0xFFFFFFFFFFFFFFFF
sb.blockCount = math.MaxUint64
r := bytes.NewReader([]byte{})
err := sb.decodeOriginBits(r)
assert.ErrorIs(t, err, ErrTooBigSpanBatchFieldSize)
}
func TestSpanBatchMaxBlockCount(t *testing.T) {
rng := rand.New(rand.NewSource(0x77556691))
chainID := big.NewInt(rng.Int63n(1000))
rawSpanBatch := RandomRawSpanBatch(rng, chainID)
rawSpanBatch.blockCount = math.MaxUint64
var buf bytes.Buffer
err := rawSpanBatch.encodeBlockCount(&buf)
require.NoError(t, err)
result := buf.Bytes()
r := bytes.NewReader(result)
var sb RawSpanBatch
err = sb.decodeBlockCount(r)
require.ErrorIs(t, err, ErrTooBigSpanBatchFieldSize)
}
func TestSpanBatchMaxBlockTxCount(t *testing.T) {
rng := rand.New(rand.NewSource(0x77556692))
chainID := big.NewInt(rng.Int63n(1000))
rawSpanBatch := RandomRawSpanBatch(rng, chainID)
rawSpanBatch.blockTxCounts[0] = math.MaxUint64
var buf bytes.Buffer
err := rawSpanBatch.encodeBlockTxCounts(&buf)
require.NoError(t, err)
result := buf.Bytes()
r := bytes.NewReader(result)
var sb RawSpanBatch
sb.blockCount = rawSpanBatch.blockCount
err = sb.decodeBlockTxCounts(r)
require.ErrorIs(t, err, ErrTooBigSpanBatchFieldSize)
}
func TestSpanBatchTotalBlockTxCountNotOverflow(t *testing.T) {
rng := rand.New(rand.NewSource(0x77556693))
chainID := big.NewInt(rng.Int63n(1000))
rawSpanBatch := RandomRawSpanBatch(rng, chainID)
rawSpanBatch.blockTxCounts[0] = MaxSpanBatchFieldSize - 1
rawSpanBatch.blockTxCounts[1] = MaxSpanBatchFieldSize - 1
// we are sure that totalBlockTxCount will overflow on uint64
var buf bytes.Buffer
err := rawSpanBatch.encodeBlockTxCounts(&buf)
require.NoError(t, err)
result := buf.Bytes()
r := bytes.NewReader(result)
var sb RawSpanBatch
sb.blockTxCounts = rawSpanBatch.blockTxCounts
err = sb.decodeTxs(r)
require.ErrorIs(t, err, ErrTooBigSpanBatchFieldSize)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment