add error checking for nil keys
Checks in: - blockstore - blockservice - dagservice - bitswap Do not anger the pokemans #2715 License: MIT Signed-off-by: Juan Benet <juan@benet.ai>
jbenet committed
May 16, 2016 at 22:39 UTC
0ddafe603a3ae788f22d933687018fe13f1d903f
8 files changed
+56
blocks/blockstore/blockstore.go
+4
@@ -74,6 +74,10 @@ type blockstore struct {
74
}
75
76
func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
77
+ if k == "" {
78
+ return nil, ErrNotFound
79
+ }
80
+
81
maybeData, err := bs.datastore.Get(k.DsKey())
82
if err == ds.ErrNotFound {
83
return nil, ErrNotFound
blocks/blockstore/blockstore_test.go
+8
@@ -27,6 +27,14 @@ func TestGetWhenKeyNotPresent(t *testing.T) {
27
t.Fail()
28
}
29
30
+func TestGetWhenKeyIsEmptyString(t *testing.T) {
31
+ bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
32
+ _, err := bs.Get(key.Key(""))
33
+ if err != ErrNotFound {
34
+ t.Fail()
35
+ }
36
+}
37
+
38
func TestPutThenGetBlock(t *testing.T) {
39
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
40
block := blocks.NewBlock([]byte("some data"))
blockservice/blockservice.go
+5
@@ -72,6 +72,11 @@ func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
72
// GetBlock retrieves a particular block from the service,
73
// Getting it from the datastore using the key (hash).
74
func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (blocks.Block, error) {
75
+ if k == "" {
76
+ log.Debug("BlockService GetBlock: Nil Key")
77
+ return nil, ErrNotFound
78
+ }
79
+
80
log.Debugf("BlockService GetBlock: '%s'", k)
81
block, err := s.Blockstore.Get(k)
82
if err == nil {
blockservice/test/blocks_test.go
+5
@@ -22,6 +22,11 @@ func TestBlocks(t *testing.T) {
22
bs := New(bstore, offline.Exchange(bstore))
23
defer bs.Close()
24
25
+ _, err := bs.GetBlock(context.Background(), key.Key(""))
26
+ if err != ErrNotFound {
27
+ t.Error("Empty String Key should error", err)
28
+ }
29
+
30
b := blocks.NewBlock([]byte("beep boop"))
31
h := u.Hash([]byte("beep boop"))
32
if !bytes.Equal(b.Multihash(), h) {
exchange/bitswap/bitswap.go
+3
@@ -155,6 +155,9 @@ type blockRequest struct {
155
// GetBlock attempts to retrieve a particular block from peers within the
156
// deadline enforced by the context.
157
func (bs *Bitswap) GetBlock(parent context.Context, k key.Key) (blocks.Block, error) {
158
+ if k == "" {
159
+ return nil, blockstore.ErrNotFound
160
+ }
161
162
// Any async work initiated by this function must end when this function
163
// returns. To ensure this, derive a new context. Note that it is okay to
exchange/bitswap/bitswap_test.go
+13
@@ -11,6 +11,7 @@ import (
11
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
12
13
blocks "github.com/ipfs/go-ipfs/blocks"
14
+ blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
15
blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
16
key "github.com/ipfs/go-ipfs/blocks/key"
17
tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
@@ -278,6 +279,18 @@ func TestSendToWantingPeer(t *testing.T) {
279
280
}
281
282
+func TestEmptyKey(t *testing.T) {
283
+ net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
284
+ sg := NewTestSessionGenerator(net)
285
+ defer sg.Close()
286
+ bs := sg.Instances(1)[0].Exchange
287
+
288
+ _, err := bs.GetBlock(context.Background(), key.Key(""))
289
+ if err != blockstore.ErrNotFound {
290
+ t.Error("empty str key should return ErrNotFound")
291
+ }
292
+}
293
+
294
func TestBasicBitswap(t *testing.T) {
295
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
296
sg := NewTestSessionGenerator(net)
merkledag/merkledag.go
+3
@@ -68,6 +68,9 @@ func (n *dagService) Batch() *Batch {
68
69
// Get retrieves a node from the dagService, fetching the block in the BlockService
70
func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
71
+ if k == "" {
72
+ return nil, ErrNotFound
73
+ }
74
if n == nil {
75
return nil, fmt.Errorf("dagService is nil")
76
}
merkledag/merkledag_test.go
+15
@@ -32,6 +32,13 @@ type dagservAndPinner struct {
32
mp pin.Pinner
33
}
34
35
+func getDagserv(t *testing.T) DAGService {
36
+ db := dssync.MutexWrap(ds.NewMapDatastore())
37
+ bs := bstore.NewBlockstore(db)
38
+ blockserv := bserv.New(bs, offline.Exchange(bs))
39
+ return NewDAGService(blockserv)
40
+}
41
+
42
func getDagservAndPinner(t *testing.T) dagservAndPinner {
43
db := dssync.MutexWrap(ds.NewMapDatastore())
44
bs := bstore.NewBlockstore(db)
@@ -245,6 +252,14 @@ func assertCanGet(t *testing.T, ds DAGService, n *Node) {
252
}
253
}
254
255
+func TestEmptyKey(t *testing.T) {
256
+ ds := getDagserv(t)
257
+ _, err := ds.Get(context.Background(), key.Key(""))
258
+ if err != ErrNotFound {
259
+ t.Error("dag service should error when key is nil", err)
260
+ }
261
+}
262
+
263
func TestCantGet(t *testing.T) {
264
dsp := getDagservAndPinner(t)
265
a := &Node{Data: []byte("A")}