cidbase_test.go
1 package cmdenv 2 3 import ( 4 "testing" 5 6 cidenc "github.com/ipfs/go-cidutil/cidenc" 7 mbase "github.com/multiformats/go-multibase" 8 ) 9 10 func TestEncoderFromPath(t *testing.T) { 11 test := func(path string, expected cidenc.Encoder) { 12 actual, err := CidEncoderFromPath(path) 13 if err != nil { 14 t.Error(err) 15 } 16 if actual != expected { 17 t.Errorf("CidEncoderFromPath(%s) failed: expected %#v but got %#v", path, expected, actual) 18 } 19 } 20 p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu" 21 enc := cidenc.Default() 22 test(p, enc) 23 test(p+"/a", enc) 24 test(p+"/a/b", enc) 25 test(p+"/a/b/", enc) 26 test(p+"/a/b/c", enc) 27 test("/ipfs/"+p, enc) 28 test("/ipfs/"+p+"/b", enc) 29 30 p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk" 31 enc = cidenc.Encoder{ 32 Base: mbase.MustNewEncoder(mbase.Base58BTC), 33 Upgrade: true, 34 } 35 test(p, enc) 36 test(p+"/a", enc) 37 test(p+"/a/b", enc) 38 test(p+"/a/b/", enc) 39 test(p+"/a/b/c", enc) 40 test("/ipfs/"+p, enc) 41 test("/ipfs/"+p+"/b", enc) 42 test("/ipld/"+p, enc) 43 test("/ipns/"+p, enc) // even IPNS should work. 44 45 p = "bafyreifrcnyjokuw4i4ggkzg534tjlc25lqgt3ttznflmyv5fftdgu52hm" 46 enc = cidenc.Encoder{ 47 Base: mbase.MustNewEncoder(mbase.Base32), 48 Upgrade: true, 49 } 50 test(p, enc) 51 test("/ipfs/"+p, enc) 52 test("/ipld/"+p, enc) 53 54 for _, badPath := range []string{ 55 "/ipld/", 56 "/ipld", 57 "/ipld//", 58 "ipld//", 59 "ipld", 60 "", 61 "ipns", 62 "/ipfs/asdf", 63 "/ipfs/...", 64 "...", 65 "abcdefg", 66 "boo", 67 } { 68 _, err := CidEncoderFromPath(badPath) 69 if err == nil { 70 t.Errorf("expected error extracting encoder from bad path: %s", badPath) 71 } 72 } 73 }