Skip to content

Commit

Permalink
update testing to follow the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-forbes committed Mar 31, 2021
1 parent 753c54a commit 529b265
Showing 1 changed file with 51 additions and 14 deletions.
65 changes: 51 additions & 14 deletions types/shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
"math/rand"
"reflect"
"testing"

Expand Down Expand Up @@ -61,8 +62,8 @@ func TestMakeShares(t *testing.T) {
ID: reservedEvidenceNamespaceID,
}, NamespacedShare{
Share: append(
append(reservedEvidenceNamespaceID, byte(0)),
zeroPadIfNecessary(testEvidenceBytes[TxShareSize:], TxShareSize)...,
reservedEvidenceNamespaceID,
zeroPadIfNecessary(testEvidenceBytes[TxShareSize:], MsgShareSize)...,
),
ID: reservedEvidenceNamespaceID,
}},
Expand All @@ -74,8 +75,8 @@ func TestMakeShares(t *testing.T) {
NamespacedShares{
NamespacedShare{
Share: append(
append(reservedTxNamespaceID, byte(0)),
zeroPadIfNecessary(smolTxLenDelimited, TxShareSize)...,
reservedTxNamespaceID,
zeroPadIfNecessary(smolTxLenDelimited, MsgShareSize)...,
),
ID: reservedTxNamespaceID,
},
Expand All @@ -88,15 +89,15 @@ func TestMakeShares(t *testing.T) {
NamespacedShares{
NamespacedShare{
Share: append(
append(reservedTxNamespaceID, byte(0)),
largeTxLenDelimited[:TxShareSize]...,
reservedTxNamespaceID,
largeTxLenDelimited[:MsgShareSize]...,
),
ID: reservedTxNamespaceID,
},
NamespacedShare{
Share: append(
append(reservedTxNamespaceID, byte(0)),
zeroPadIfNecessary(largeTxLenDelimited[TxShareSize:], TxShareSize)...,
reservedTxNamespaceID,
zeroPadIfNecessary(largeTxLenDelimited[MsgShareSize:], MsgShareSize)...,
),
ID: reservedTxNamespaceID,
},
Expand All @@ -109,17 +110,17 @@ func TestMakeShares(t *testing.T) {
NamespacedShares{
NamespacedShare{
Share: append(
append(reservedTxNamespaceID, byte(0)),
largeTxLenDelimited[:TxShareSize]...,
reservedTxNamespaceID,
largeTxLenDelimited[:MsgShareSize]...,
),
ID: reservedTxNamespaceID,
},
NamespacedShare{
Share: append(
append(reservedTxNamespaceID, byte(len(largeTxLenDelimited)-TxShareSize+NamespaceSize+ShareReservedBytes)),
reservedTxNamespaceID,
zeroPadIfNecessary(
append(largeTxLenDelimited[TxShareSize:], smolTxLenDelimited...),
TxShareSize,
append(largeTxLenDelimited[MsgShareSize:], smolTxLenDelimited...),
MsgShareSize,
)...,
),
ID: reservedTxNamespaceID,
Expand Down Expand Up @@ -194,7 +195,43 @@ func Test_appendToSharesOverwrite(t *testing.T) {
assert.Equal(t, extraCopy, []byte(newShare.Share[:MsgShareSize]))
}

func generateRandomNamespacedShares(count, leafSize int) []NamespacedShare {
func Test_parseShares(t *testing.T) {
type test struct {
name string
count int
maxSize uint8
}

// generateRandNamespacedRawData(2, NamespaceSize, MsgShareSize)

tests := []test{
{"basic", 5, 88},
}
for _, tt := range tests {
tt := tt
txs, namedShares := generateRandomContiguousShares(tt.count, tt.maxSize)
res := parseShares(namedShares)
assert.Equal(t, len(res), len(txs))
for i, got := range res {
assert.Equal(t, []byte(txs[i]), got)
}
}
}

func generateRandomContiguousShares(count int, maxSize uint8) (Txs, [][]byte) {
txs := make(Txs, count)
for i := 0; i < count; i++ {
tx := make([]byte, (randInt(50, int(maxSize))))
_, err := rand.Read(tx)
if err != nil {
panic(err)
}
txs[i] = Tx(tx)
}
return txs, txs.splitIntoShares().RawShares()
}

func generateRandomNamespacedShares(count, leafSize int) NamespacedShares {
shares := generateRandNamespacedRawData(count, NamespaceSize, leafSize)
nsShares := make(NamespacedShares, count)
for i, s := range shares {
Expand Down

0 comments on commit 529b265

Please sign in to comment.