message_test.go 3.25 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 10 11 12 13 14
	u "github.com/jbenet/go-ipfs/util"
)

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

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

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

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

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

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

89 90
func TestToNetFromNetPreservesWantList(t *testing.T) {
	original := New()
91 92 93 94 95
	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"))
96

97 98
	var buf bytes.Buffer
	if err := original.ToNet(&buf); err != nil {
99 100 101
		t.Fatal(err)
	}

102
	copied, err := FromNet(&buf)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	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
122 123 124 125
	original.AddBlock(blocks.NewBlock([]byte("W")))
	original.AddBlock(blocks.NewBlock([]byte("E")))
	original.AddBlock(blocks.NewBlock([]byte("F")))
	original.AddBlock(blocks.NewBlock([]byte("M")))
126

127 128
	var buf bytes.Buffer
	if err := original.ToNet(&buf); err != nil {
129 130 131
		t.Fatal(err)
	}

132
	m2, err := FromNet(&buf)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	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()
		}
	}
}

149 150 151 152 153 154 155 156
func contains(s []string, x string) bool {
	for _, a := range s {
		if a == x {
			return true
		}
	}
	return false
}
157 158 159 160 161 162 163 164 165 166 167

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
168 169
	msg.AddBlock(b)
	msg.AddBlock(b)
170 171 172 173
	if len(msg.Blocks()) != 1 {
		t.Fatal("Duplicate in BitSwapMessage")
	}
}