github.com/sunrise-zone/sunrise-node@v0.13.1-sr2/share/ipld/corrupted_data_test.go (about)

     1  package ipld_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/sunrise-zone/sunrise-node/header/headertest"
    11  	"github.com/sunrise-zone/sunrise-node/share"
    12  	"github.com/sunrise-zone/sunrise-node/share/availability/full"
    13  	availability_test "github.com/sunrise-zone/sunrise-node/share/availability/test"
    14  	"github.com/sunrise-zone/sunrise-node/share/getters"
    15  )
    16  
    17  // sharesAvailableTimeout is an arbitrarily picked interval of time in which a TestNode is expected
    18  // to be able to complete a SharesAvailable request from a connected peer in a TestDagNet.
    19  const sharesAvailableTimeout = 2 * time.Second
    20  
    21  // TestNamespaceHasher_CorruptedData is an integration test that verifies that the NamespaceHasher
    22  // of a recipient of corrupted data will not panic, and will throw away the corrupted data.
    23  func TestNamespaceHasher_CorruptedData(t *testing.T) {
    24  	ctx, cancel := context.WithCancel(context.Background())
    25  	t.Cleanup(cancel)
    26  	net := availability_test.NewTestDAGNet(ctx, t)
    27  
    28  	requester := full.Node(net)
    29  	provider, mockBS := availability_test.MockNode(t, net)
    30  	provider.Availability = full.TestAvailability(t, getters.NewIPLDGetter(provider.BlockService))
    31  	net.ConnectAll()
    32  
    33  	// before the provider starts attacking, we should be able to retrieve successfully. We pass a size
    34  	// 16 block, but this is not important to the test and any valid block size behaves the same.
    35  	root := availability_test.RandFillBS(t, 16, provider.BlockService)
    36  
    37  	eh := headertest.RandExtendedHeaderWithRoot(t, root)
    38  	getCtx, cancelGet := context.WithTimeout(ctx, sharesAvailableTimeout)
    39  	t.Cleanup(cancelGet)
    40  	err := requester.SharesAvailable(getCtx, eh)
    41  	require.NoError(t, err)
    42  
    43  	// clear the storage of the requester so that it must retrieve again, then start attacking
    44  	// we reinitialize the node to clear the eds store
    45  	requester = full.Node(net)
    46  	mockBS.Attacking = true
    47  	getCtx, cancelGet = context.WithTimeout(ctx, sharesAvailableTimeout)
    48  	t.Cleanup(cancelGet)
    49  	err = requester.SharesAvailable(getCtx, eh)
    50  	require.ErrorIs(t, err, share.ErrNotAvailable)
    51  }