node_test.go 3.07 KB
Newer Older
1
package merkledag_test
2 3

import (
Jeromy's avatar
Jeromy committed
4
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"context"
6
	"testing"
7

Jeromy's avatar
Jeromy committed
8 9
	. "github.com/ipfs/go-merkledag"
	mdtest "github.com/ipfs/go-merkledag/test"
10

Jeromy's avatar
Jeromy committed
11
	ipld "github.com/ipfs/go-ipld-format"
Steven Allen's avatar
Steven Allen committed
12
	cid "github.com/ipfs/go-cid"
13 14
)

Steven Allen's avatar
Steven Allen committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
func TestStableCID(t *testing.T) {
	nd := &ProtoNode{}
	nd.SetData([]byte("foobar"))
	nd.SetLinks([]*ipld.Link{
		{Name: "a"},
		{Name: "b"},
		{Name: "c"},
	})
	expected, err := cid.Decode("QmSN3WED2xPLbYvBbfvew2ZLtui8EbFYYcbfkpKH5jwG9C")
	if err != nil {
		t.Fatal(err)
	}
	if !nd.Cid().Equals(expected) {
		t.Fatalf("Got CID %s, expected CID %s", nd.Cid(), expected)
	}
}

32
func TestRemoveLink(t *testing.T) {
33
	nd := &ProtoNode{}
34
	nd.SetLinks([]*ipld.Link{
Jeromy's avatar
Jeromy committed
35 36 37 38 39 40
		{Name: "a"},
		{Name: "b"},
		{Name: "a"},
		{Name: "a"},
		{Name: "c"},
		{Name: "a"},
41
	})
42 43 44 45 46 47

	err := nd.RemoveNodeLink("a")
	if err != nil {
		t.Fatal(err)
	}

48
	if len(nd.Links()) != 2 {
49 50 51
		t.Fatal("number of links incorrect")
	}

52
	if nd.Links()[0].Name != "b" {
53 54 55
		t.Fatal("link order wrong")
	}

56
	if nd.Links()[1].Name != "c" {
57 58 59 60 61
		t.Fatal("link order wrong")
	}

	// should fail
	err = nd.RemoveNodeLink("a")
62
	if err != ipld.ErrNotFound {
63 64 65 66
		t.Fatal("should have failed to remove link")
	}

	// ensure nothing else got touched
67
	if len(nd.Links()) != 2 {
68 69 70
		t.Fatal("number of links incorrect")
	}

71
	if nd.Links()[0].Name != "b" {
72 73 74
		t.Fatal("link order wrong")
	}

75
	if nd.Links()[1].Name != "c" {
76 77 78
		t.Fatal("link order wrong")
	}
}
79 80

func TestFindLink(t *testing.T) {
81 82
	ctx := context.Background()

83
	ds := mdtest.Mock()
84 85
	ndEmpty := new(ProtoNode)
	err := ds.Add(ctx, ndEmpty)
86 87 88 89
	if err != nil {
		t.Fatal(err)
	}

90 91
	kEmpty := ndEmpty.Cid()

92
	nd := &ProtoNode{}
93
	nd.SetLinks([]*ipld.Link{
94 95 96
		{Name: "a", Cid: kEmpty},
		{Name: "c", Cid: kEmpty},
		{Name: "b", Cid: kEmpty},
97
	})
98

99
	err = ds.Add(ctx, nd)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	if err != nil {
		t.Fatal(err)
	}

	lnk, err := nd.GetNodeLink("b")
	if err != nil {
		t.Fatal(err)
	}

	if lnk.Name != "b" {
		t.Fatal("got wrong link back")
	}

	_, err = nd.GetNodeLink("f")
	if err != ErrLinkNotFound {
		t.Fatal("shouldnt have found link")
	}

	_, err = nd.GetLinkedNode(context.Background(), ds, "b")
	if err != nil {
		t.Fatal(err)
	}

	outnd, err := nd.UpdateNodeLink("b", nd)
	if err != nil {
		t.Fatal(err)
	}

	olnk, err := outnd.GetNodeLink("b")
	if err != nil {
		t.Fatal(err)
	}

133
	if olnk.Cid.String() == kEmpty.String() {
134 135 136 137 138
		t.Fatal("new link should have different hash")
	}
}

func TestNodeCopy(t *testing.T) {
139
	nd := &ProtoNode{}
140
	nd.SetLinks([]*ipld.Link{
Jeromy's avatar
Jeromy committed
141 142 143
		{Name: "a"},
		{Name: "c"},
		{Name: "b"},
144 145
	})

146 147
	nd.SetData([]byte("testing"))

148
	ond := nd.Copy().(*ProtoNode)
149 150 151 152 153 154
	ond.SetData(nil)

	if nd.Data() == nil {
		t.Fatal("should be different objects")
	}
}
Jeromy's avatar
Jeromy committed
155 156 157

func TestJsonRoundtrip(t *testing.T) {
	nd := new(ProtoNode)
158
	nd.SetLinks([]*ipld.Link{
Jeromy's avatar
Jeromy committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		{Name: "a"},
		{Name: "c"},
		{Name: "b"},
	})
	nd.SetData([]byte("testing"))

	jb, err := nd.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}

	nn := new(ProtoNode)
	err = nn.UnmarshalJSON(jb)
	if err != nil {
		t.Fatal(err)
	}

	if !bytes.Equal(nn.Data(), nd.Data()) {
		t.Fatal("data wasnt the same")
	}

	if !nn.Cid().Equals(nd.Cid()) {
		t.Fatal("objects differed after marshaling")
	}
}