Skip to content

Commit

Permalink
tests: check errors in test
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Oct 25, 2023
1 parent 4836c06 commit 79d349e
Showing 1 changed file with 51 additions and 31 deletions.
82 changes: 51 additions & 31 deletions share/eds/byzantine/bad_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,86 +41,109 @@ func TestBEFP_Validate(t *testing.T) {

var test = []struct {
name string
doFn func() error
prepareFn func() error
expectedResult func(error)
}{
{
name: "valid BEFP",
doFn: func() error {
prepareFn: func() error {
return befp.Validate(&header.ExtendedHeader{DAH: &dah})
},
expectedResult: func(err error) {
require.NoError(t, err)
},
},
{
name: "heights mismatch",
doFn: func() error {
return befp.Validate(&header.ExtendedHeader{
RawHeader: core.Header{
Height: 42,
name: "invalid BEFP for valid header",
prepareFn: func() error {
validSquare := edstest.RandEDS(t, 2)
validDah, err := da.NewDataAvailabilityHeader(validSquare)
require.NoError(t, err)
err = ipld.ImportEDS(ctx, validSquare, bServ)
require.NoError(t, err)
validShares := validSquare.Flattened()
errInvalidByz := NewErrByzantine(ctx, bServ, &validDah,
&rsmt2d.ErrByzantineData{
Axis: rsmt2d.Row,
Index: 0,
Shares: validShares[0:4],
},
DAH: &dah,
})
)
invalidBefp := CreateBadEncodingProof([]byte("hash"), 0, errInvalidByz)
return invalidBefp.Validate(&header.ExtendedHeader{DAH: &validDah})
},
expectedResult: func(err error) {
require.Error(t, err)
require.ErrorIs(t, err, errNMTTreeRootsMatch)
},
},
{
name: "index out of bounds",
doFn: func() error {
name: "incorrect share with Proof",
prepareFn: func() error {
befp, ok := befp.(*BadEncodingProof)
require.True(t, ok)
befpCopy := *befp
befpCopy.Index = 100
return befpCopy.Validate(&header.ExtendedHeader{DAH: &dah})
befp.Shares[0].Share = befp.Shares[1].Share
return befp.Validate(&header.ExtendedHeader{DAH: &dah})
},
expectedResult: func(err error) {
require.Error(t, err)
require.ErrorIs(t, err, errIncorrectShare)
},
},
{
name: "incorrect share with Proof",
doFn: func() error {
name: "invalid amount of shares",
prepareFn: func() error {
befp, ok := befp.(*BadEncodingProof)
require.True(t, ok)
befp.Shares[0].Share = befp.Shares[1].Share
befp.Shares = befp.Shares[0 : len(befp.Shares)/2]
return befp.Validate(&header.ExtendedHeader{DAH: &dah})
},
expectedResult: func(err error) {
require.Error(t, err)
require.ErrorIs(t, err, errIncorrectAmountOfShares)
},
},
{
name: "not enough shares to recompute the root",
doFn: func() error {
prepareFn: func() error {
befp, ok := befp.(*BadEncodingProof)
require.True(t, ok)
befp.Shares[0] = nil
return befp.Validate(&header.ExtendedHeader{DAH: &dah})
},
expectedResult: func(err error) {
require.Error(t, err)
require.ErrorIs(t, err, errIncorrectAmountOfShares)
},
},
{
name: "invalid amount of shares",
doFn: func() error {
name: "index out of bounds",
prepareFn: func() error {
befp, ok := befp.(*BadEncodingProof)
require.True(t, ok)
befp.Shares = befp.Shares[0 : len(befp.Shares)/2]
return befp.Validate(&header.ExtendedHeader{DAH: &dah})
befpCopy := *befp
befpCopy.Index = 100
return befpCopy.Validate(&header.ExtendedHeader{DAH: &dah})
},
expectedResult: func(err error) {
require.ErrorIs(t, err, errIncorrectIndex)
},
},
{
name: "heights mismatch",
prepareFn: func() error {
return befp.Validate(&header.ExtendedHeader{
RawHeader: core.Header{
Height: 42,
},
DAH: &dah,
})
},
expectedResult: func(err error) {
require.Error(t, err)
require.ErrorIs(t, err, errHeightMismatch)
},
},
}

for _, tt := range test {
t.Run(tt.name, func(t *testing.T) {
err = tt.doFn()
err = tt.prepareFn()
tt.expectedResult(err)
})
}
Expand Down Expand Up @@ -203,9 +226,6 @@ func TestBEFP_ValidateOutOfOrderShares(t *testing.T) {
errByz := NewErrByzantine(context.Background(), bServ, &dah, errRsmt2d)

befp := CreateBadEncodingProof([]byte("hash"), 0, errByz)
err = befp.Validate(&header.ExtendedHeader{
DAH: &dah,
})
err = befp.Validate(&header.ExtendedHeader{DAH: &dah})
require.Error(t, err)
}

0 comments on commit 79d349e

Please sign in to comment.