message_test.go 3.94 KB
Newer Older
1 2 3 4 5 6
package message

import (
	"bytes"
	"testing"

Jeromy's avatar
Jeromy committed
7
	pb "github.com/ipfs/go-bitswap/message/pb"
Jan Winkelmann's avatar
Jan Winkelmann committed
8

Jeromy's avatar
Jeromy committed
9 10 11
	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"
	u "github.com/ipfs/go-ipfs-util"
12 13
)

14
func mkFakeCid(s string) cid.Cid {
15 16 17
	return cid.NewCidV0(u.Hash([]byte(s)))
}

18
func TestAppendWanted(t *testing.T) {
19
	str := mkFakeCid("foo")
20
	m := New(true)
21
	m.AddEntry(str, 1)
22

23
	if !wantlistContains(m.ToProtoV0().GetWantlist(), str) {
24 25 26 27 28
		t.Fail()
	}
}

func TestNewMessageFromProto(t *testing.T) {
29
	str := mkFakeCid("a_key")
30
	protoMessage := new(pb.Message)
Jeromy's avatar
Jeromy committed
31 32
	protoMessage.Wantlist = new(pb.Message_Wantlist)
	protoMessage.Wantlist.Entries = []*pb.Message_Wantlist_Entry{
Steven Allen's avatar
Steven Allen committed
33
		{Block: str.Bytes()},
Jeromy's avatar
Jeromy committed
34 35
	}
	if !wantlistContains(protoMessage.Wantlist, str) {
36 37
		t.Fail()
	}
38 39 40 41 42
	m, err := newMessageFromProto(*protoMessage)
	if err != nil {
		t.Fatal(err)
	}

43
	if !wantlistContains(m.ToProtoV0().GetWantlist(), str) {
44 45 46 47 48 49 50 51 52 53
		t.Fail()
	}
}

func TestAppendBlock(t *testing.T) {

	strs := make([]string, 2)
	strs = append(strs, "Celeritas")
	strs = append(strs, "Incendia")

54
	m := New(true)
55
	for _, str := range strs {
56
		block := blocks.NewBlock([]byte(str))
Jeromy's avatar
Jeromy committed
57
		m.AddBlock(block)
58 59 60
	}

	// assert strings are in proto message
61
	for _, blockbytes := range m.ToProtoV0().GetBlocks() {
62 63 64 65 66 67 68
		s := bytes.NewBuffer(blockbytes).String()
		if !contains(strs, s) {
			t.Fail()
		}
	}
}

69
func TestWantlist(t *testing.T) {
70
	keystrs := []cid.Cid{mkFakeCid("foo"), mkFakeCid("bar"), mkFakeCid("baz"), mkFakeCid("bat")}
71
	m := New(true)
72
	for _, s := range keystrs {
73
		m.AddEntry(s, 1)
74 75 76 77 78 79 80
	}
	exported := m.Wantlist()

	for _, k := range exported {
		present := false
		for _, s := range keystrs {

81
			if s.Equals(k.Cid) {
82 83 84 85
				present = true
			}
		}
		if !present {
86
			t.Logf("%v isn't in original list", k.Cid)
87 88 89 90 91
			t.Fail()
		}
	}
}

92
func TestCopyProtoByValue(t *testing.T) {
93
	str := mkFakeCid("foo")
94
	m := New(true)
95
	protoBeforeAppend := m.ToProtoV0()
96
	m.AddEntry(str, 1)
Jeromy's avatar
Jeromy committed
97
	if wantlistContains(protoBeforeAppend.GetWantlist(), str) {
98 99 100 101
		t.Fail()
	}
}

102
func TestToNetFromNetPreservesWantList(t *testing.T) {
103
	original := New(true)
104 105 106 107 108
	original.AddEntry(mkFakeCid("M"), 1)
	original.AddEntry(mkFakeCid("B"), 1)
	original.AddEntry(mkFakeCid("D"), 1)
	original.AddEntry(mkFakeCid("T"), 1)
	original.AddEntry(mkFakeCid("F"), 1)
109

110
	buf := new(bytes.Buffer)
111
	if err := original.ToNetV1(buf); err != nil {
112 113 114
		t.Fatal(err)
	}

115
	copied, err := FromNet(buf)
116 117 118 119
	if err != nil {
		t.Fatal(err)
	}

120 121 122 123
	if !copied.Full() {
		t.Fatal("fullness attribute got dropped on marshal")
	}

124
	keys := make(map[string]bool)
125
	for _, k := range copied.Wantlist() {
126
		keys[k.Cid.KeyString()] = true
127 128 129
	}

	for _, k := range original.Wantlist() {
130
		if _, ok := keys[k.Cid.KeyString()]; !ok {
131 132 133 134 135 136 137
			t.Fatalf("Key Missing: \"%v\"", k)
		}
	}
}

func TestToAndFromNetMessage(t *testing.T) {

138
	original := New(true)
Jeromy's avatar
Jeromy committed
139 140 141 142
	original.AddBlock(blocks.NewBlock([]byte("W")))
	original.AddBlock(blocks.NewBlock([]byte("E")))
	original.AddBlock(blocks.NewBlock([]byte("F")))
	original.AddBlock(blocks.NewBlock([]byte("M")))
143

144
	buf := new(bytes.Buffer)
145
	if err := original.ToNetV1(buf); err != nil {
146 147 148
		t.Fatal(err)
	}

149
	m2, err := FromNet(buf)
150 151 152 153
	if err != nil {
		t.Fatal(err)
	}

154
	keys := make(map[string]bool)
155
	for _, b := range m2.Blocks() {
156
		keys[b.Cid().KeyString()] = true
157 158 159
	}

	for _, b := range original.Blocks() {
160
		if _, ok := keys[b.Cid().KeyString()]; !ok {
161 162 163 164 165
			t.Fail()
		}
	}
}

166
func wantlistContains(wantlist *pb.Message_Wantlist, c cid.Cid) bool {
Jeromy's avatar
Jeromy committed
167
	for _, e := range wantlist.GetEntries() {
Steven Allen's avatar
Steven Allen committed
168
		if bytes.Equal(e.GetBlock(), c.Bytes()) {
Jeromy's avatar
Jeromy committed
169 170 171 172 173 174 175 176 177
			return true
		}
	}
	return false
}

func contains(strs []string, x string) bool {
	for _, s := range strs {
		if s == x {
178 179 180 181 182
			return true
		}
	}
	return false
}
183 184 185

func TestDuplicates(t *testing.T) {
	b := blocks.NewBlock([]byte("foo"))
186
	msg := New(true)
187

188 189
	msg.AddEntry(b.Cid(), 1)
	msg.AddEntry(b.Cid(), 1)
190 191 192 193
	if len(msg.Wantlist()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}

Jeromy's avatar
Jeromy committed
194 195
	msg.AddBlock(b)
	msg.AddBlock(b)
196 197 198 199
	if len(msg.Blocks()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}
}