mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
Replaced old logic to check for valid path
Added the original logic to check for a invalid path and a simple test.
This commit is contained in:
@ -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 "/<protocol/>" without further segments
|
||||
return nil, fmt.Errorf("invalid path: %s", string(p))
|
||||
}
|
||||
|
||||
extensions := seg[2:]
|
||||
|
41
core/pathresolver_test.go
Normal file
41
core/pathresolver_test.go
Normal file
@ -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")
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user