Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: enable square tests #12

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions internal/test/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package test

import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand"

"github.com/celestiaorg/go-square/pkg/blob"
"github.com/celestiaorg/go-square/pkg/namespace"
"github.com/celestiaorg/go-square/pkg/shares"
)

var DefaultTestNamespace = namespace.MustNewV0([]byte("test"))

func GenerateTxs(minSize, maxSize, numTxs int) [][]byte {
txs := make([][]byte, numTxs)
for i := 0; i < numTxs; i++ {
txs[i] = GenerateRandomTx(minSize, maxSize)
}
return txs
}

func GenerateRandomTx(minSize, maxSize int) []byte {
size := minSize
if maxSize > minSize {
size = rand.Intn(maxSize-minSize) + minSize
}
return RandomBytes(size)
}

func RandomBytes(size int) []byte {
b := make([]byte, size)
_, err := crand.Read(b)
if err != nil {
panic(err)
}
return b
}

func GenerateBlobTxWithNamespace(namespaces []namespace.Namespace, blobSizes []int) []byte {
blobs := make([]*blob.Blob, len(blobSizes))
if len(namespaces) != len(blobSizes) {
panic("number of namespaces should match number of blob sizes")
}
for i, size := range blobSizes {
blobs[i] = blob.New(namespaces[i], RandomBytes(size), shares.DefaultShareVersion)
}
blobTx, err := blob.MarshalBlobTx(MockPFB(toUint32(blobSizes)), blobs...)
if err != nil {
panic(err)
}
return blobTx
}

func GenerateBlobTx(blobSizes []int) []byte {
return GenerateBlobTxWithNamespace(Repeat(DefaultTestNamespace, len(blobSizes)), blobSizes)
}

func GenerateBlobTxs(numTxs, blobsPerPfb, blobSize int) [][]byte {
blobSizes := make([]int, blobsPerPfb)
for i := range blobSizes {
blobSizes[i] = blobSize
}
txs := make([][]byte, numTxs)
for i := 0; i < numTxs; i++ {
txs[i] = GenerateBlobTx(blobSizes)
}
return txs
}

const mockPFBExtraBytes = 329

func MockPFB(blobSizes []uint32) []byte {
if len(blobSizes) == 0 {
panic("must have at least one blob")
}
tx := make([]byte, len(blobSizes)*4)
for i, size := range blobSizes {
binary.BigEndian.PutUint32(tx[i*4:], uint32(size))
}

return append(RandomBytes(mockPFBExtraBytes), tx...)
}

func DecodeMockPFB(pfb []byte) ([]uint32, error) {
if len(pfb) < mockPFBExtraBytes+4 {
return nil, fmt.Errorf("must have a length of at least %d bytes, got %d", mockPFBExtraBytes+4, len(pfb))
}
pfb = pfb[mockPFBExtraBytes:]
blobSizes := make([]uint32, len(pfb)/4)
for i := 0; i < len(blobSizes); i++ {
blobSizes[i] = binary.BigEndian.Uint32(pfb[i*4 : (i+1)*4])
}
return blobSizes, nil
}

func toUint32(arr []int) []uint32 {
output := make([]uint32, len(arr))
for i, value := range arr {
output[i] = uint32(value)
}
return output
}

func Repeat[T any](s T, count int) []T {
ss := make([]T, count)
for i := 0; i < count; i++ {
ss[i] = s
}
return ss
}
21 changes: 21 additions & 0 deletions internal/test/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test_test

import (
"testing"

"github.com/celestiaorg/go-square/internal/test"
"github.com/stretchr/testify/require"
)

func TestPFBParity(t *testing.T) {
blobSizes := []uint32{20, 30, 10}
pfb := test.MockPFB(blobSizes)
output, err := test.DecodeMockPFB(pfb)
require.NoError(t, err)
require.Equal(t, blobSizes, output)

require.Panics(t, func() { test.MockPFB(nil) })

_, err = test.DecodeMockPFB(test.RandomBytes(20))
require.Error(t, err)
}
20 changes: 13 additions & 7 deletions merkle/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,18 @@ func TestProofValidateBasic(t *testing.T) {
{"Good", func(sp *Proof) {}, ""},
{"Negative Total", func(sp *Proof) { sp.Total = -1 }, "negative Total"},
{"Negative Index", func(sp *Proof) { sp.Index = -1 }, "negative Index"},
{"Invalid LeafHash", func(sp *Proof) { sp.LeafHash = make([]byte, 10) },
"expected LeafHash size to be 32, got 10"},
{"Too many Aunts", func(sp *Proof) { sp.Aunts = make([][]byte, MaxAunts+1) },
"expected no more than 100 aunts, got 101"},
{"Invalid Aunt", func(sp *Proof) { sp.Aunts[0] = make([]byte, 10) },
"expected Aunts#0 size to be 32, got 10"},
{
"Invalid LeafHash", func(sp *Proof) { sp.LeafHash = make([]byte, 10) },
"expected LeafHash size to be 32, got 10",
},
{
"Too many Aunts", func(sp *Proof) { sp.Aunts = make([][]byte, MaxAunts+1) },
"expected no more than 100 aunts, got 101",
},
{
"Invalid Aunt", func(sp *Proof) { sp.Aunts[0] = make([]byte, 10) },
"expected Aunts#0 size to be 32, got 10",
},
}

for _, tc := range testCases {
Expand All @@ -235,8 +241,8 @@ func TestProofValidateBasic(t *testing.T) {
})
}
}
func TestVoteProtobuf(t *testing.T) {

func TestVoteProtobuf(t *testing.T) {
_, proofs := ProofsFromByteSlices([][]byte{
[]byte("apple"),
[]byte("watermelon"),
Expand Down
2 changes: 0 additions & 2 deletions merkle/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func TestHashFromByteSlices(t *testing.T) {
}

func TestProof(t *testing.T) {

// Try an empty proof first
rootHash, proofs := ProofsFromByteSlices([][]byte{})
require.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(rootHash))
Expand Down Expand Up @@ -100,7 +99,6 @@ func TestProof(t *testing.T) {
}

func TestHashAlternatives(t *testing.T) {

total := 100

items := make([][]byte, total)
Expand Down
6 changes: 2 additions & 4 deletions pkg/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"google.golang.org/protobuf/proto"
)

var (
// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
SupportedBlobNamespaceVersions = []uint8{namespace.NamespaceVersionZero}
)
// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
var SupportedBlobNamespaceVersions = []uint8{namespace.NamespaceVersionZero}

// ProtoBlobTxTypeID is included in each encoded BlobTx to help prevent
// decoding binaries that are not actually BlobTxs.
Expand Down
6 changes: 2 additions & 4 deletions pkg/shares/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,5 @@ const (
MaxShareVersion = 127
)

var (
// SupportedShareVersions is a list of supported share versions.
SupportedShareVersions = []uint8{ShareVersionZero}
)
// SupportedShareVersions is a list of supported share versions.
var SupportedShareVersions = []uint8{ShareVersionZero}
4 changes: 2 additions & 2 deletions pkg/shares/split_compact_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCount(t *testing.T) {
}
testCases := []testCase{
{transactions: [][]byte{}, wantShareCount: 0},
{transactions: [][]byte{[]byte{0}}, wantShareCount: 1},
{transactions: [][]byte{{0}}, wantShareCount: 1},
{transactions: [][]byte{bytes.Repeat([]byte{1}, 100)}, wantShareCount: 1},
// Test with 1 byte over 1 share
{transactions: [][]byte{bytes.Repeat([]byte{1}, RawTxSize(FirstCompactShareContentSize+1))}, wantShareCount: 2},
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestWriteAndExportIdempotence(t *testing.T) {
bytes.Repeat([]byte{0xf}, RawTxSize(FirstCompactShareContentSize)),
bytes.Repeat([]byte{0xf}, RawTxSize(ContinuationCompactShareContentSize)),
bytes.Repeat([]byte{0xf}, RawTxSize(ContinuationCompactShareContentSize)),
[]byte{0xf},
{0xf},
},
wantLen: 4,
},
Expand Down
Loading
Loading