encoder_test.go 2.1 KB
Newer Older
1 2 3 4 5 6
package cidenc

import (
	"testing"

	mbase "github.com/multiformats/go-multibase"
7
	cid "gitlab.dms3.io/dms3/go-cid"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
)

func TestCidEncoder(t *testing.T) {
	cidv0str := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
	cidv1str := "zdj7Wkkhxcu2rsiN6GUyHCLsSLL47kdUNfjbFqBUUhMFTZKBi"
	cidb32str := "bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku"
	cidv0, _ := cid.Decode(cidv0str)
	cidv1, _ := cid.Decode(cidv1str)

	testEncode := func(enc Encoder, cid cid.Cid, expect string) {
		actual := enc.Encode(cid)
		if actual != expect {
			t.Errorf("%+v.Encode(%s): expected %s but got %s", enc, cid, expect, actual)
		}
	}

	testRecode := func(enc Encoder, cid string, expect string) {
		actual, err := enc.Recode(cid)
		if err != nil {
			t.Errorf("%+v.Recode(%s): %s", enc, cid, err)
			return
		}
		if actual != expect {
			t.Errorf("%+v.Recode(%s): expected %s but got %s", enc, cid, expect, actual)
		}
	}

	enc := Encoder{Base: mbase.MustNewEncoder(mbase.Base58BTC), Upgrade: false}
	testEncode(enc, cidv0, cidv0str)
	testEncode(enc, cidv1, cidv1str)
	testRecode(enc, cidv0str, cidv0str)
	testRecode(enc, cidv1str, cidv1str)
	testRecode(enc, cidb32str, cidv1str)

	enc = Encoder{Base: mbase.MustNewEncoder(mbase.Base58BTC), Upgrade: true}
	testEncode(enc, cidv0, cidv1str)
	testEncode(enc, cidv1, cidv1str)
	testRecode(enc, cidv0str, cidv1str)
	testRecode(enc, cidv1str, cidv1str)
	testRecode(enc, cidb32str, cidv1str)

	enc = Encoder{Base: mbase.MustNewEncoder(mbase.Base32), Upgrade: false}
	testEncode(enc, cidv0, cidv0str)
	testEncode(enc, cidv1, cidb32str)
	testRecode(enc, cidv0str, cidv0str)
	testRecode(enc, cidv1str, cidb32str)
	testRecode(enc, cidb32str, cidb32str)

	enc = Encoder{Base: mbase.MustNewEncoder(mbase.Base32), Upgrade: true}
	testEncode(enc, cidv0, cidb32str)
	testEncode(enc, cidv1, cidb32str)
	testRecode(enc, cidv0str, cidb32str)
	testRecode(enc, cidv1str, cidb32str)
	testRecode(enc, cidb32str, cidb32str)
Steven Allen's avatar
Steven Allen committed
62 63 64 65 66 67 68 69

	enc = Default()
	testEncode(enc, cidv0, cidv0str)
	testEncode(enc, cidv1, cidb32str)
	testRecode(enc, cidv0str, cidv0str)
	testRecode(enc, cidv1str, cidb32str)
	testRecode(enc, cidb32str, cidb32str)

70
}