From dcc4da0b37109d7073590731794b4891f6bd4b69 Mon Sep 17 00:00:00 2001 From: Travis Person Date: Wed, 20 May 2015 20:38:57 -0700 Subject: [PATCH] Replaced old logic to check for valid path Added the original logic to check for a invalid path and a simple test. --- core/pathresolver.go | 6 ++++-- core/pathresolver_test.go | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 core/pathresolver_test.go diff --git a/core/pathresolver.go b/core/pathresolver.go index 08bbac0e3..1ca605250 100644 --- a/core/pathresolver.go +++ b/core/pathresolver.go @@ -3,6 +3,7 @@ package core import ( "errors" "strings" + "fmt" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" @@ -29,8 +30,9 @@ func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (*merkledag.Node, er } seg := p.Segments() - if len(seg) < 2 { - return nil, errors.New("No path given") + + if len(seg) < 2 || seg[1] == "" { // just "/" without further segments + return nil, fmt.Errorf("invalid path: %s", string(p)) } extensions := seg[2:] diff --git a/core/pathresolver_test.go b/core/pathresolver_test.go new file mode 100644 index 000000000..b458a98e8 --- /dev/null +++ b/core/pathresolver_test.go @@ -0,0 +1,41 @@ +package core + +import ( + "testing" + + context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" + config "github.com/ipfs/go-ipfs/repo/config" + "github.com/ipfs/go-ipfs/util/testutil" + "github.com/ipfs/go-ipfs/repo" + path "github.com/ipfs/go-ipfs/path" +) + +func TestResolveInvalidPath(t *testing.T) { + ctx := context.TODO() + id := testIdentity + + r := &repo.Mock{ + C: config.Config{ + Identity: id, + Datastore: config.Datastore{ + Type: "memory", + }, + Addresses: config.Addresses{ + Swarm: []string{"/ip4/0.0.0.0/tcp/4001"}, + API: "/ip4/127.0.0.1/tcp/8000", + }, + }, + D: testutil.ThreadSafeCloserMapDatastore(), + } + + n, err := NewIPFSNode(ctx, Standard(r, false)) + if n == nil || err != nil { + t.Error("Should have constructed.", err) + } + + _, err = Resolve(ctx, n, path.Path("/ipfs/")) + if err == nil { + t.Error("Should get invalid path") + } + +}