cidbase_test.go 1.61 KB
Newer Older
1 2 3 4
package cmdenv

import (
	"testing"
5 6 7

	cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
	mbase "gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase"
8 9
)

10 11 12 13 14 15 16 17
func TestEncoderFromPath(t *testing.T) {
	test := func(path string, expected cidenc.Encoder) {
		actual, err := CidEncoderFromPath(path)
		if err != nil {
			t.Error(err)
		}
		if actual != expected {
			t.Errorf("CidEncoderFromPath(%s) failed: expected %#v but got %#v", path, expected, actual)
18 19 20
		}
	}
	p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu"
21 22 23 24 25 26 27 28
	enc := cidenc.Default()
	test(p, enc)
	test(p+"/a", enc)
	test(p+"/a/b", enc)
	test(p+"/a/b/", enc)
	test(p+"/a/b/c", enc)
	test("/ipfs/"+p, enc)
	test("/ipfs/"+p+"/b", enc)
29 30

	p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk"
31 32 33 34 35 36 37 38 39 40 41 42 43
	enc = cidenc.Encoder{
		Base:    mbase.MustNewEncoder(mbase.Base58BTC),
		Upgrade: true,
	}
	test(p, enc)
	test(p+"/a", enc)
	test(p+"/a/b", enc)
	test(p+"/a/b/", enc)
	test(p+"/a/b/c", enc)
	test("/ipfs/"+p, enc)
	test("/ipfs/"+p+"/b", enc)
	test("/ipld/"+p, enc)
	test("/ipns/"+p, enc) // even IPNS should work.
44

45
	p = "bafyreifrcnyjokuw4i4ggkzg534tjlc25lqgt3ttznflmyv5fftdgu52hm"
46 47 48 49 50 51 52
	enc = cidenc.Encoder{
		Base:    mbase.MustNewEncoder(mbase.Base32),
		Upgrade: true,
	}
	test(p, enc)
	test("/ipfs/"+p, enc)
	test("/ipld/"+p, enc)
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	for _, badPath := range []string{
		"/ipld/",
		"/ipld",
		"/ipld//",
		"ipld//",
		"ipld",
		"",
		"ipns",
		"/ipfs/asdf",
		"/ipfs/...",
		"...",
		"abcdefg",
		"boo",
	} {
		_, err := CidEncoderFromPath(badPath)
		if err == nil {
			t.Errorf("expected error extracting encoder from bad path: %s", badPath)
		}
	}
73
}