mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-27 13:27:50 +08:00
Merge pull request #1475 from ipfs/fix/path-parse
fix parsing for paths of format <hash>/path
This commit is contained in:
commit
88ec46ee15
@ -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
|
||||
|
||||
30
path/path_test.go
Normal file
30
path/path_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
'
|
||||
|
||||
Loading…
Reference in New Issue
Block a user