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

import (
	"bytes"
	"testing"

7
	proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
Jeromy's avatar
Jeromy committed
8

9
	blocks "github.com/ipfs/go-ipfs/blocks"
10
	key "github.com/ipfs/go-ipfs/blocks/key"
11
	pb "github.com/ipfs/go-ipfs/exchange/bitswap/message/internal/pb"
12 13 14 15
)

func TestAppendWanted(t *testing.T) {
	const str = "foo"
16
	m := New(true)
17
	m.AddEntry(key.Key(str), 1)
18

Jeromy's avatar
Jeromy committed
19
	if !wantlistContains(m.ToProto().GetWantlist(), str) {
20 21
		t.Fail()
	}
Jeromy's avatar
Jeromy committed
22
	m.ToProto().GetWantlist().GetEntries()
23 24 25 26
}

func TestNewMessageFromProto(t *testing.T) {
	const str = "a_key"
27
	protoMessage := new(pb.Message)
Jeromy's avatar
Jeromy committed
28 29
	protoMessage.Wantlist = new(pb.Message_Wantlist)
	protoMessage.Wantlist.Entries = []*pb.Message_Wantlist_Entry{
rht's avatar
rht committed
30
		{Block: proto.String(str)},
Jeromy's avatar
Jeromy committed
31 32
	}
	if !wantlistContains(protoMessage.Wantlist, str) {
33 34
		t.Fail()
	}
35
	m := newMessageFromProto(*protoMessage)
Jeromy's avatar
Jeromy committed
36
	if !wantlistContains(m.ToProto().GetWantlist(), str) {
37 38 39 40 41 42 43 44 45 46
		t.Fail()
	}
}

func TestAppendBlock(t *testing.T) {

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

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

	// assert strings are in proto message
	for _, blockbytes := range m.ToProto().GetBlocks() {
		s := bytes.NewBuffer(blockbytes).String()
		if !contains(strs, s) {
			t.Fail()
		}
	}
}

62 63
func TestWantlist(t *testing.T) {
	keystrs := []string{"foo", "bar", "baz", "bat"}
64
	m := New(true)
65
	for _, s := range keystrs {
66
		m.AddEntry(key.Key(s), 1)
67 68 69 70 71 72 73
	}
	exported := m.Wantlist()

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

Jeromy's avatar
Jeromy committed
74
			if s == string(k.Key) {
75 76 77 78
				present = true
			}
		}
		if !present {
Jeromy's avatar
Jeromy committed
79
			t.Logf("%v isn't in original list", k.Key)
80 81 82 83 84
			t.Fail()
		}
	}
}

85 86
func TestCopyProtoByValue(t *testing.T) {
	const str = "foo"
87
	m := New(true)
88
	protoBeforeAppend := m.ToProto()
89
	m.AddEntry(key.Key(str), 1)
Jeromy's avatar
Jeromy committed
90
	if wantlistContains(protoBeforeAppend.GetWantlist(), str) {
91 92 93 94
		t.Fail()
	}
}

95
func TestToNetFromNetPreservesWantList(t *testing.T) {
96
	original := New(true)
97 98 99 100 101
	original.AddEntry(key.Key("M"), 1)
	original.AddEntry(key.Key("B"), 1)
	original.AddEntry(key.Key("D"), 1)
	original.AddEntry(key.Key("T"), 1)
	original.AddEntry(key.Key("F"), 1)
102

103 104
	buf := new(bytes.Buffer)
	if err := original.ToNet(buf); err != nil {
105 106 107
		t.Fatal(err)
	}

108
	copied, err := FromNet(buf)
109 110 111 112
	if err != nil {
		t.Fatal(err)
	}

113
	keys := make(map[key.Key]bool)
114
	for _, k := range copied.Wantlist() {
Jeromy's avatar
Jeromy committed
115
		keys[k.Key] = true
116 117 118
	}

	for _, k := range original.Wantlist() {
Jeromy's avatar
Jeromy committed
119
		if _, ok := keys[k.Key]; !ok {
120 121 122 123 124 125 126
			t.Fatalf("Key Missing: \"%v\"", k)
		}
	}
}

func TestToAndFromNetMessage(t *testing.T) {

127
	original := New(true)
Jeromy's avatar
Jeromy committed
128 129 130 131
	original.AddBlock(blocks.NewBlock([]byte("W")))
	original.AddBlock(blocks.NewBlock([]byte("E")))
	original.AddBlock(blocks.NewBlock([]byte("F")))
	original.AddBlock(blocks.NewBlock([]byte("M")))
132

133 134
	buf := new(bytes.Buffer)
	if err := original.ToNet(buf); err != nil {
135 136 137
		t.Fatal(err)
	}

138
	m2, err := FromNet(buf)
139 140 141 142
	if err != nil {
		t.Fatal(err)
	}

143
	keys := make(map[key.Key]bool)
144 145 146 147 148 149 150 151 152 153 154
	for _, b := range m2.Blocks() {
		keys[b.Key()] = true
	}

	for _, b := range original.Blocks() {
		if _, ok := keys[b.Key()]; !ok {
			t.Fail()
		}
	}
}

Jeromy's avatar
Jeromy committed
155 156 157 158 159 160 161 162 163 164 165 166
func wantlistContains(wantlist *pb.Message_Wantlist, x string) bool {
	for _, e := range wantlist.GetEntries() {
		if e.GetBlock() == x {
			return true
		}
	}
	return false
}

func contains(strs []string, x string) bool {
	for _, s := range strs {
		if s == x {
167 168 169 170 171
			return true
		}
	}
	return false
}
172 173 174

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

Brian Tiger Chow's avatar
Brian Tiger Chow committed
177 178
	msg.AddEntry(b.Key(), 1)
	msg.AddEntry(b.Key(), 1)
179 180 181 182
	if len(msg.Wantlist()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}

Jeromy's avatar
Jeromy committed
183 184
	msg.AddBlock(b)
	msg.AddBlock(b)
185 186 187 188
	if len(msg.Blocks()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}
}