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

import (
	"bytes"
	"testing"

7 8
	blocks "github.com/jbenet/go-ipfs/blocks"
	pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
9
	u "github.com/jbenet/go-ipfs/util"
10
	testutil "github.com/jbenet/go-ipfs/util/testutil"
11 12 13 14 15
)

func TestAppendWanted(t *testing.T) {
	const str = "foo"
	m := New()
16
	m.AddWanted(u.Key(str))
17 18 19 20 21 22 23 24

	if !contains(m.ToProto().GetWantlist(), str) {
		t.Fail()
	}
}

func TestNewMessageFromProto(t *testing.T) {
	const str = "a_key"
25
	protoMessage := new(pb.Message)
26 27 28 29
	protoMessage.Wantlist = []string{string(str)}
	if !contains(protoMessage.Wantlist, str) {
		t.Fail()
	}
30
	m := newMessageFromProto(*protoMessage)
31 32 33 34 35 36 37 38 39 40 41 42 43
	if !contains(m.ToProto().GetWantlist(), str) {
		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 {
44
		block := blocks.NewBlock([]byte(str))
Jeromy's avatar
Jeromy committed
45
		m.AddBlock(block)
46 47 48 49 50 51 52 53 54 55 56
	}

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

57 58 59 60
func TestWantlist(t *testing.T) {
	keystrs := []string{"foo", "bar", "baz", "bat"}
	m := New()
	for _, s := range keystrs {
61
		m.AddWanted(u.Key(s))
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	}
	exported := m.Wantlist()

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

			if s == string(k) {
				present = true
			}
		}
		if !present {
			t.Logf("%v isn't in original list", string(k))
			t.Fail()
		}
	}
}

80 81 82 83
func TestCopyProtoByValue(t *testing.T) {
	const str = "foo"
	m := New()
	protoBeforeAppend := m.ToProto()
84
	m.AddWanted(u.Key(str))
85 86 87 88 89
	if contains(protoBeforeAppend.GetWantlist(), str) {
		t.Fail()
	}
}

90 91
func TestToNetMethodSetsPeer(t *testing.T) {
	m := New()
92
	p := testutil.NewPeerWithIDString("X")
93 94 95 96 97 98 99 100 101 102 103
	netmsg, err := m.ToNet(p)
	if err != nil {
		t.Fatal(err)
	}
	if !(netmsg.Peer().Key() == p.Key()) {
		t.Fatal("Peer key is different")
	}
}

func TestToNetFromNetPreservesWantList(t *testing.T) {
	original := New()
104 105 106 107 108
	original.AddWanted(u.Key("M"))
	original.AddWanted(u.Key("B"))
	original.AddWanted(u.Key("D"))
	original.AddWanted(u.Key("T"))
	original.AddWanted(u.Key("F"))
109

110
	p := testutil.NewPeerWithIDString("X")
111
	netmsg, err := original.ToNet(p)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	if err != nil {
		t.Fatal(err)
	}

	copied, err := FromNet(netmsg)
	if err != nil {
		t.Fatal(err)
	}

	keys := make(map[u.Key]bool)
	for _, k := range copied.Wantlist() {
		keys[k] = true
	}

	for _, k := range original.Wantlist() {
		if _, ok := keys[k]; !ok {
			t.Fatalf("Key Missing: \"%v\"", k)
		}
	}
}

func TestToAndFromNetMessage(t *testing.T) {

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

141
	p := testutil.NewPeerWithIDString("X")
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	netmsg, err := original.ToNet(p)
	if err != nil {
		t.Fatal(err)
	}

	m2, err := FromNet(netmsg)
	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()
		}
	}
}

164 165 166 167 168 169 170 171
func contains(s []string, x string) bool {
	for _, a := range s {
		if a == x {
			return true
		}
	}
	return false
}
172 173 174 175 176 177 178 179 180 181 182

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

	msg.AddWanted(b.Key())
	msg.AddWanted(b.Key())
	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")
	}
}