forked from Wondertan/go-ipfs-recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_test.go
67 lines (53 loc) · 1.67 KB
/
node_test.go
1
2
3
4
5
6
7
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
62
63
64
65
66
67
package restore
import (
"testing"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNodeRedundant(t *testing.T) {
nd := NewNode(merkledag.NodeWithData([]byte("1234567890")))
r := merkledag.NewRawNode([]byte("12345"))
nd.AddRedundantNode(r)
assert.Len(t, nd.Redundant(), 1)
nd.RemoveRedundantNode(r.Cid())
assert.Len(t, nd.Redundant(), 0)
}
func TestNodeMarshalUnmarshal(t *testing.T) {
in := NewNode(merkledag.NodeWithData([]byte("1234567890")))
in.AddRedundantNode(merkledag.NewRawNode([]byte("12345")))
data, err := MarshalNode(in)
require.NoError(t, err)
out, err := UnmarshalNode(data)
require.NoError(t, err)
out.SetCidBuilder(in.CidBuilder())
assert.True(t, in.Cid().Equals(out.Cid()))
}
func TestNodeDecode(t *testing.T) {
in := NewNode(merkledag.NodeWithData([]byte("1234567890")))
out, err := format.Decode(in)
require.NoError(t, err)
assert.True(t, in.Cid().Equals(out.Cid()))
}
func TestNodeCache(t *testing.T) {
nd := NewNode(merkledag.NodeWithData([]byte("1234567890")))
nd.AddRedundantNode(merkledag.NewRawNode([]byte("12345")))
assert.Zero(t, nd.cid)
assert.Nil(t, nd.cache)
nd.Cid()
assert.NotZero(t, nd.cid)
assert.NotNil(t, nd.cache)
nd.AddNodeLink("", merkledag.NewRawNode([]byte("12345")))
assert.Nil(t, nd.cache)
}
func TestNode_Copy(t *testing.T) {
nd := NewNode(merkledag.NodeWithData([]byte("1234567890")))
r := merkledag.NewRawNode([]byte("12345"))
nd.AddRedundantNode(r)
cp := nd.Copy()
nd.SetData([]byte{})
nd.RemoveRedundantNode(r.Cid())
assert.NotNil(t, cp.(*Node).Redundant())
assert.NotNil(t, cp.(*Node).Data())
}