main_test.go 969 Bytes
Newer Older
Kevin Atkinson's avatar
Kevin Atkinson committed
1 2 3 4 5 6
package main

import (
	"fmt"
	"testing"

7
	c "gitlab.dms3.io/dms3/go-cid"
Kevin Atkinson's avatar
Kevin Atkinson committed
8 9
)

10 11
func TestCidConv(t *testing.T) {
	cidv0 := "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
Steven Allen's avatar
Steven Allen committed
12
	cidv1 := "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"
13
	cid, err := c.Decode(cidv0)
14 15 16 17 18 19 20
	if err != nil {
		t.Fatal(err)
	}
	cid, err = toCidV1(cid)
	if err != nil {
		t.Fatal(err)
	}
Steven Allen's avatar
Steven Allen committed
21
	if cid.String() != cidv1 {
Steven Allen's avatar
Steven Allen committed
22
		t.Fatalf("conversion failure: %s != %s", cid, cidv1)
23 24 25 26 27 28 29 30 31 32 33 34 35
	}
	cid, err = toCidV0(cid)
	if err != nil {
		t.Fatal(err)
	}
	cidStr := cid.String()
	if cidStr != cidv0 {
		t.Error(fmt.Sprintf("conversion failure, expected: %s; but got: %s", cidv0, cidStr))
	}
}

func TestBadCidConv(t *testing.T) {
	// this cid is a raw leaf and should not be able to convert to cidv0
Steven Allen's avatar
Steven Allen committed
36
	cidv1 := "bafkreifit7vvfkf2cwwzvyycdczm5znbdbqx54ab6shbesvwgkwthdf77y"
37
	cid, err := c.Decode(cidv1)
38 39 40 41 42 43 44 45
	if err != nil {
		t.Fatal(err)
	}
	cid, err = toCidV0(cid)
	if err == nil {
		t.Fatal("expected failure")
	}
}