message_test.go 3.72 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 10 11
	blocks "github.com/ipfs/go-ipfs/blocks"
	pb "github.com/ipfs/go-ipfs/exchange/bitswap/message/internal/pb"
	u "github.com/ipfs/go-ipfs/util"
12 13 14 15 16
)

func TestAppendWanted(t *testing.T) {
	const str = "foo"
	m := New()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
17
	m.AddEntry(u.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 30 31 32
	protoMessage.Wantlist = new(pb.Message_Wantlist)
	protoMessage.Wantlist.Entries = []*pb.Message_Wantlist_Entry{
		&pb.Message_Wantlist_Entry{Block: proto.String(str)},
	}
	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 47 48
		t.Fail()
	}
}

func TestAppendBlock(t *testing.T) {

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

	m := New()
	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 64 65
func TestWantlist(t *testing.T) {
	keystrs := []string{"foo", "bar", "baz", "bat"}
	m := New()
	for _, s := range keystrs {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
66
		m.AddEntry(u.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 87 88
func TestCopyProtoByValue(t *testing.T) {
	const str = "foo"
	m := New()
	protoBeforeAppend := m.ToProto()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
89
	m.AddEntry(u.Key(str), 1)
Jeromy's avatar
Jeromy committed
90
	if wantlistContains(protoBeforeAppend.GetWantlist(), str) {
91 92 93 94
		t.Fail()
	}
}

95 96
func TestToNetFromNetPreservesWantList(t *testing.T) {
	original := New()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
97 98 99 100 101
	original.AddEntry(u.Key("M"), 1)
	original.AddEntry(u.Key("B"), 1)
	original.AddEntry(u.Key("D"), 1)
	original.AddEntry(u.Key("T"), 1)
	original.AddEntry(u.Key("F"), 1)
102

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

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

	keys := make(map[u.Key]bool)
	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 127
			t.Fatalf("Key Missing: \"%v\"", k)
		}
	}
}

func TestToAndFromNetMessage(t *testing.T) {

	original := New()
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
	var buf bytes.Buffer
	if err := original.ToNet(&buf); err != nil {
135 136 137
		t.Fatal(err)
	}

138
	m2, err := FromNet(&buf)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	if err != nil {
		t.Fatal(err)
	}

	keys := make(map[u.Key]bool)
	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 175 176

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

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")
	}
}