Skip to content

Commit

Permalink
add error checking for nil keys
Browse files Browse the repository at this point in the history
Checks in:
- blockstore
- blockservice
- dagservice
- bitswap

Do not anger the pokemans ipfs#2715

License: MIT
Signed-off-by: Juan Benet <juan@benet.ai>
  • Loading branch information
jbenet committed May 17, 2016
1 parent c5917bc commit b26887e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func (n *dagService) Batch() *Batch {

// 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) {
if k == "" {
return nil, ErrNotFound
}
if n == nil {
return nil, fmt.Errorf("dagService is nil")
}
Expand Down
15 changes: 15 additions & 0 deletions merkledag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ type dagservAndPinner struct {
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 {
db := dssync.MutexWrap(ds.NewMapDatastore())
bs := bstore.NewBlockstore(db)
Expand Down Expand Up @@ -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) {
dsp := getDagservAndPinner(t)
a := &Node{Data: []byte("A")}
Expand Down

0 comments on commit b26887e

Please sign in to comment.