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

import (
	"bytes"
	"testing"

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

9
	cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
10
	u "gx/ipfs/QmSU6eubNdhXjFBJBSksTp8kv8YRub8mGAPv8tVJHmL2EU/go-ipfs-util"
11
	blocks "gx/ipfs/QmSn9Td7xgxm9EV7iEjTckpUWmWApggzPxu7eFGWkkpwin/go-block-format"
Jan Winkelmann's avatar
Jan Winkelmann committed
12
	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
13 14
)

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

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

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

func TestNewMessageFromProto(t *testing.T) {
30
	str := mkFakeCid("a_key")
31
	protoMessage := new(pb.Message)
Jeromy's avatar
Jeromy committed
32 33
	protoMessage.Wantlist = new(pb.Message_Wantlist)
	protoMessage.Wantlist.Entries = []*pb.Message_Wantlist_Entry{
34
		{Block: proto.String(str.KeyString())},
Jeromy's avatar
Jeromy committed
35 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)
	}

44
	if !wantlistContains(m.ToProtoV0().GetWantlist(), 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 {
74
		m.AddEntry(s, 1)
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()
97
	m.AddEntry(str, 1)
Jeromy's avatar
Jeromy committed
98
	if wantlistContains(protoBeforeAppend.GetWantlist(), str) {
99 100 101 102
		t.Fail()
	}
}

103
func TestToNetFromNetPreservesWantList(t *testing.T) {
104
	original := New(true)
105 106 107 108 109
	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)
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")
	}

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

	for _, k := range original.Wantlist() {
131
		if _, ok := keys[k.Cid.KeyString()]; !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)
	}

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

	for _, b := range original.Blocks() {
161
		if _, ok := keys[b.Cid().KeyString()]; !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() {
169
		if e.GetBlock() == c.KeyString() {
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

189 190
	msg.AddEntry(b.Cid(), 1)
	msg.AddEntry(b.Cid(), 1)
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 200
	if len(msg.Blocks()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}
}