Skip to content

Commit

Permalink
blockservice: add allowlist tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Aug 16, 2023
1 parent cfb43c1 commit 5f1f415
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions blockservice/blockservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import (
blockstore "github.com/ipfs/boxo/blockstore"
exchange "github.com/ipfs/boxo/exchange"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/verifcid"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
butil "github.com/ipfs/go-ipfs-blocksutil"
ipld "github.com/ipfs/go-ipld-format"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
)

func TestWriteThroughWorks(t *testing.T) {
t.Parallel()

bstore := &PutCountingBlockstore{
blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
0,
Expand Down Expand Up @@ -46,6 +51,8 @@ func TestWriteThroughWorks(t *testing.T) {
}

func TestExchangeWrite(t *testing.T) {
t.Parallel()

bstore := &PutCountingBlockstore{
blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
0,
Expand Down Expand Up @@ -117,6 +124,8 @@ func TestExchangeWrite(t *testing.T) {
}

func TestLazySessionInitialization(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down Expand Up @@ -215,6 +224,8 @@ func (fe *fakeSessionExchange) NewSession(ctx context.Context) exchange.Fetcher
}

func TestNilExchange(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand All @@ -241,3 +252,39 @@ func TestNilExchange(t *testing.T) {
t.Fatal("got the wrong block")
}
}

func TestAllowlist(t *testing.T) {
t.Parallel()
a := assert.New(t)

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

bgen := butil.NewBlockGenerator()
block := bgen.Next()

data := []byte("this is some blake3 block")
mh, err := multihash.Sum(data, multihash.BLAKE3, -1)
a.NoError(err)
blake3 := cid.NewCidV1(cid.Raw, mh)

bs := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
a.NoError(bs.Put(ctx, block))
b, err := blocks.NewBlockWithCid(data, blake3)
a.NoError(err)
a.NoError(bs.Put(ctx, b))

check := func(getBlock func(context.Context, cid.Cid) (blocks.Block, error)) {
_, err := getBlock(ctx, block.Cid())
a.Error(err)
a.ErrorIs(err, verifcid.ErrPossiblyInsecureHashFunction)

_, err = getBlock(ctx, blake3)
a.NoError(err)
}

blockservice := New(bs, nil, WithAllowlist(verifcid.NewAllowlist(map[uint64]bool{multihash.BLAKE3: true})))
check(blockservice.GetBlock)
check(NewSession(ctx, blockservice).GetBlock)
}

0 comments on commit 5f1f415

Please sign in to comment.