mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 11:31:54 +08:00
78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package offline
|
|
|
|
import (
|
|
"testing"
|
|
|
|
blocks "github.com/ipfs/go-ipfs/blocks"
|
|
"github.com/ipfs/go-ipfs/blocks/blockstore"
|
|
"github.com/ipfs/go-ipfs/blocks/blocksutil"
|
|
key "github.com/ipfs/go-ipfs/blocks/key"
|
|
ds "gx/ipfs/QmNgqJarToRiq2GBaPJhkmW4B5BxS5B74E1rkGvv2JoaTp/go-datastore"
|
|
ds_sync "gx/ipfs/QmNgqJarToRiq2GBaPJhkmW4B5BxS5B74E1rkGvv2JoaTp/go-datastore/sync"
|
|
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
|
|
)
|
|
|
|
func TestBlockReturnsErr(t *testing.T) {
|
|
off := Exchange(bstore())
|
|
_, err := off.GetBlock(context.Background(), key.Key("foo"))
|
|
if err != nil {
|
|
return // as desired
|
|
}
|
|
t.Fail()
|
|
}
|
|
|
|
func TestHasBlockReturnsNil(t *testing.T) {
|
|
store := bstore()
|
|
ex := Exchange(store)
|
|
block := blocks.NewBlock([]byte("data"))
|
|
|
|
err := ex.HasBlock(block)
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
|
|
if _, err := store.Get(block.Key()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestGetBlocks(t *testing.T) {
|
|
store := bstore()
|
|
ex := Exchange(store)
|
|
g := blocksutil.NewBlockGenerator()
|
|
|
|
expected := g.Blocks(2)
|
|
|
|
for _, b := range expected {
|
|
if err := ex.HasBlock(b); err != nil {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
request := func() []key.Key {
|
|
var ks []key.Key
|
|
|
|
for _, b := range expected {
|
|
ks = append(ks, b.Key())
|
|
}
|
|
return ks
|
|
}()
|
|
|
|
received, err := ex.GetBlocks(context.Background(), request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var count int
|
|
for _ = range received {
|
|
count++
|
|
}
|
|
if len(expected) != count {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func bstore() blockstore.Blockstore {
|
|
return blockstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
|
}
|