diff --git a/path/path.go b/path/path.go index 6fe75adbe..e865ba287 100644 --- a/path/path.go +++ b/path/path.go @@ -56,10 +56,9 @@ func ParsePath(txt string) (Path, error) { return kp, nil } } - if len(parts) < 3 { - return "", ErrBadPath - } + // if the path doesnt being with a '/' + // we expect this to start with a hash, and be an 'ipfs' path if parts[0] != "" { if _, err := ParseKeyToPath(parts[0]); err != nil { return "", ErrBadPath @@ -68,6 +67,10 @@ func ParsePath(txt string) (Path, error) { return Path("/ipfs/" + txt), nil } + if len(parts) < 3 { + return "", ErrBadPath + } + if parts[1] == "ipfs" { if _, err := ParseKeyToPath(parts[2]); err != nil { return "", err diff --git a/path/path_test.go b/path/path_test.go new file mode 100644 index 000000000..f800e19e7 --- /dev/null +++ b/path/path_test.go @@ -0,0 +1,30 @@ +package path + +import ( + "testing" +) + +func TestPathParsing(t *testing.T) { + cases := map[string]bool{ + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true, + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a": true, + "/ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a/b/c/d/e/f": true, + "/ipns/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a/b/c/d/e/f": true, + "/ipns/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true, + "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a/b/c/d/e/f": true, + "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true, + "/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": false, + "/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a": false, + "/ipfs/": false, + "ipfs/": false, + "ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": false, + } + + for p, expected := range cases { + _, err := ParsePath(p) + valid := (err == nil) + if valid != expected { + t.Fatalf("expected %s to have valid == %s", p, expected) + } + } +} diff --git a/test/sharness/t0051-object.sh b/test/sharness/t0051-object.sh index c41dd621f..7ee088a5a 100755 --- a/test/sharness/t0051-object.sh +++ b/test/sharness/t0051-object.sh @@ -114,6 +114,20 @@ test_object_cmd() { test_cmp hwfile hwfile_out ' + test_expect_success "ipfs object stat path succeeds" ' + ipfs object stat $(cat multi_patch)/a > obj_stat_out + ' + + test_expect_success "ipfs object stat output looks good" ' + echo NumLinks: 1 > obj_stat_exp && + echo BlockSize: 47 >> obj_stat_exp && + echo LinksSize: 45 >> obj_stat_exp && + echo DataSize: 2 >> obj_stat_exp && + echo CumulativeSize: 114 >> obj_stat_exp && + + test_cmp obj_stat_out obj_stat_exp + ' + test_expect_success "should have created dir within a dir" ' ipfs ls $OUTPUT > patched_output '