Commit b26887e5 authored by jbenet's avatar jbenet Committed by Juan Benet

add error checking for nil keys

Checks in:
- blockstore
- blockservice
- dagservice
- bitswap

Do not anger the pokemans #2715

License: MIT
Signed-off-by: default avatarJuan Benet <juan@benet.ai>
parent c5917bc1
...@@ -68,6 +68,9 @@ func (n *dagService) Batch() *Batch { ...@@ -68,6 +68,9 @@ func (n *dagService) Batch() *Batch {
// Get retrieves a node from the dagService, fetching the block in the BlockService // Get retrieves a node from the dagService, fetching the block in the BlockService
func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) { func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
if k == "" {
return nil, ErrNotFound
}
if n == nil { if n == nil {
return nil, fmt.Errorf("dagService is nil") return nil, fmt.Errorf("dagService is nil")
} }
......
...@@ -32,6 +32,13 @@ type dagservAndPinner struct { ...@@ -32,6 +32,13 @@ type dagservAndPinner struct {
mp pin.Pinner mp pin.Pinner
} }
func getDagserv(t *testing.T) DAGService {
db := dssync.MutexWrap(ds.NewMapDatastore())
bs := bstore.NewBlockstore(db)
blockserv := bserv.New(bs, offline.Exchange(bs))
return NewDAGService(blockserv)
}
func getDagservAndPinner(t *testing.T) dagservAndPinner { func getDagservAndPinner(t *testing.T) dagservAndPinner {
db := dssync.MutexWrap(ds.NewMapDatastore()) db := dssync.MutexWrap(ds.NewMapDatastore())
bs := bstore.NewBlockstore(db) bs := bstore.NewBlockstore(db)
...@@ -245,6 +252,14 @@ func assertCanGet(t *testing.T, ds DAGService, n *Node) { ...@@ -245,6 +252,14 @@ func assertCanGet(t *testing.T, ds DAGService, n *Node) {
} }
} }
func TestEmptyKey(t *testing.T) {
ds := getDagserv(t)
_, err := ds.Get(context.Background(), key.Key(""))
if err != ErrNotFound {
t.Error("dag service should error when key is nil", err)
}
}
func TestCantGet(t *testing.T) { func TestCantGet(t *testing.T) {
dsp := getDagservAndPinner(t) dsp := getDagservAndPinner(t)
a := &Node{Data: []byte("A")} a := &Node{Data: []byte("A")}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment