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

import (
	"fmt"
	"testing"

7
	c "github.com/ipfs/go-cid"
Kevin Atkinson's avatar
Kevin Atkinson committed
8 9
)

10 11 12
func TestCidConv(t *testing.T) {
	cidv0 := "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
	cidv1 := "zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi"
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 {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
		t.Fatal("conversion failure")
	}
	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
	cidv1 := "zb2rhhzX7uSKrtQ2ZZXFAabKiKFYZrJqKY2KE1cJ8yre2GSWZ"
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")
	}
}