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

import (
	"bytes"
	"testing"

7 8 9
	pb "gitlab.dms3.io/dms3/go-bitswap/message/pb"
	"gitlab.dms3.io/dms3/go-bitswap/wantlist"
	blocksutil "gitlab.dms3.io/dms3/go-dms3-blocksutil"
Jan Winkelmann's avatar
Jan Winkelmann committed
10

11 12 13
	blocks "gitlab.dms3.io/dms3/go-block-format"
	cid "gitlab.dms3.io/dms3/go-cid"
	u "gitlab.dms3.io/dms3/go-dms3-util"
14 15
)

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

20
func TestAppendWanted(t *testing.T) {
21
	str := mkFakeCid("foo")
22
	m := New(true)
dirkmc's avatar
dirkmc committed
23
	m.AddEntry(str, 1, pb.Message_Wantlist_Block, true)
24

Steven Allen's avatar
Steven Allen committed
25
	if !wantlistContains(&m.ToProtoV0().Wantlist, str) {
26 27 28 29 30
		t.Fail()
	}
}

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

Steven Allen's avatar
Steven Allen committed
44
	if !wantlistContains(&m.ToProtoV0().Wantlist, str) {
45 46 47 48 49 50 51 52 53 54
		t.Fail()
	}
}

func TestAppendBlock(t *testing.T) {

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

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

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

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

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

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

93
func TestCopyProtoByValue(t *testing.T) {
94
	str := mkFakeCid("foo")
95
	m := New(true)
96
	protoBeforeAppend := m.ToProtoV0()
dirkmc's avatar
dirkmc committed
97
	m.AddEntry(str, 1, pb.Message_Wantlist_Block, true)
Steven Allen's avatar
Steven Allen committed
98
	if wantlistContains(&protoBeforeAppend.Wantlist, str) {
99 100 101 102
		t.Fail()
	}
}

103
func TestToNetFromNetPreservesWantList(t *testing.T) {
104
	original := New(true)
dirkmc's avatar
dirkmc committed
105 106 107 108 109
	original.AddEntry(mkFakeCid("M"), 1, pb.Message_Wantlist_Block, true)
	original.AddEntry(mkFakeCid("B"), 1, pb.Message_Wantlist_Block, true)
	original.AddEntry(mkFakeCid("D"), 1, pb.Message_Wantlist_Block, true)
	original.AddEntry(mkFakeCid("T"), 1, pb.Message_Wantlist_Block, true)
	original.AddEntry(mkFakeCid("F"), 1, pb.Message_Wantlist_Block, true)
110

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

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

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

Steven Allen's avatar
Steven Allen committed
125
	keys := make(map[cid.Cid]bool)
126
	for _, k := range copied.Wantlist() {
Steven Allen's avatar
Steven Allen committed
127
		keys[k.Cid] = true
128 129 130
	}

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

func TestToAndFromNetMessage(t *testing.T) {

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

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

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

Steven Allen's avatar
Steven Allen committed
155
	keys := make(map[cid.Cid]bool)
156
	for _, b := range m2.Blocks() {
Steven Allen's avatar
Steven Allen committed
157
		keys[b.Cid()] = true
158 159 160
	}

	for _, b := range original.Blocks() {
Steven Allen's avatar
Steven Allen committed
161
		if _, ok := keys[b.Cid()]; !ok {
162 163 164 165 166
			t.Fail()
		}
	}
}

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

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

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

dirkmc's avatar
dirkmc committed
189 190
	msg.AddEntry(b.Cid(), 1, pb.Message_Wantlist_Block, true)
	msg.AddEntry(b.Cid(), 1, pb.Message_Wantlist_Block, true)
191 192 193 194
	if len(msg.Wantlist()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}

Jeromy's avatar
Jeromy committed
195 196
	msg.AddBlock(b)
	msg.AddBlock(b)
197 198 199
	if len(msg.Blocks()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}
dirkmc's avatar
dirkmc committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

	b2 := blocks.NewBlock([]byte("bar"))
	msg.AddBlockPresence(b2.Cid(), pb.Message_Have)
	msg.AddBlockPresence(b2.Cid(), pb.Message_Have)
	if len(msg.Haves()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}
}

func TestBlockPresences(t *testing.T) {
	b1 := blocks.NewBlock([]byte("foo"))
	b2 := blocks.NewBlock([]byte("bar"))
	msg := New(true)

	msg.AddBlockPresence(b1.Cid(), pb.Message_Have)
	msg.AddBlockPresence(b2.Cid(), pb.Message_DontHave)
	if len(msg.Haves()) != 1 || !msg.Haves()[0].Equals(b1.Cid()) {
		t.Fatal("Expected HAVE")
	}
	if len(msg.DontHaves()) != 1 || !msg.DontHaves()[0].Equals(b2.Cid()) {
		t.Fatal("Expected HAVE")
	}

	msg.AddBlock(b1)
	if len(msg.Haves()) != 0 {
		t.Fatal("Expected block to overwrite HAVE")
	}

	msg.AddBlock(b2)
	if len(msg.DontHaves()) != 0 {
		t.Fatal("Expected block to overwrite DONT_HAVE")
	}

	msg.AddBlockPresence(b1.Cid(), pb.Message_Have)
	if len(msg.Haves()) != 0 {
		t.Fatal("Expected HAVE not to overwrite block")
	}

	msg.AddBlockPresence(b2.Cid(), pb.Message_DontHave)
	if len(msg.DontHaves()) != 0 {
		t.Fatal("Expected DONT_HAVE not to overwrite block")
	}
}

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

	msg.AddEntry(b.Cid(), 1, pb.Message_Wantlist_Have, false)
	msg.AddEntry(b.Cid(), 2, pb.Message_Wantlist_Block, true)
	entries := msg.Wantlist()
	if len(entries) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}
	e := entries[0]
	if e.WantType != pb.Message_Wantlist_Block {
		t.Fatal("want-block should override want-have")
	}
	if e.SendDontHave != true {
		t.Fatal("true SendDontHave should override false SendDontHave")
	}
	if e.Priority != 1 {
		t.Fatal("priority should only be overridden if wants are of same type")
	}

	msg.AddEntry(b.Cid(), 2, pb.Message_Wantlist_Block, true)
	e = msg.Wantlist()[0]
	if e.Priority != 2 {
		t.Fatal("priority should be overridden if wants are of same type")
	}

	msg.AddEntry(b.Cid(), 3, pb.Message_Wantlist_Have, false)
	e = msg.Wantlist()[0]
	if e.WantType != pb.Message_Wantlist_Block {
		t.Fatal("want-have should not override want-block")
	}
	if e.SendDontHave != true {
		t.Fatal("false SendDontHave should not override true SendDontHave")
	}
	if e.Priority != 2 {
		t.Fatal("priority should only be overridden if wants are of same type")
	}

	msg.Cancel(b.Cid())
	e = msg.Wantlist()[0]
	if !e.Cancel {
		t.Fatal("cancel should override want")
	}

	msg.AddEntry(b.Cid(), 10, pb.Message_Wantlist_Block, true)
	if !e.Cancel {
		t.Fatal("want should not override cancel")
	}
293
}
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

func TestEntrySize(t *testing.T) {
	blockGenerator := blocksutil.NewBlockGenerator()
	c := blockGenerator.Next().Cid()
	e := Entry{
		Entry: wantlist.Entry{
			Cid:      c,
			Priority: 10,
			WantType: pb.Message_Wantlist_Have,
		},
		SendDontHave: true,
		Cancel:       false,
	}
	epb := e.ToPB()
	if e.Size() != epb.Size() {
		t.Fatal("entry size calculation incorrect", e.Size(), epb.Size())
	}
}