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

import (
	"bytes"
	"testing"

7
	"github.com/jbenet/go-ipfs/blocks"
8
	peer "github.com/jbenet/go-ipfs/peer"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	u "github.com/jbenet/go-ipfs/util"
)

func TestAppendWanted(t *testing.T) {
	const str = "foo"
	m := New()
	m.AppendWanted(u.Key(str))

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

func TestNewMessageFromProto(t *testing.T) {
	const str = "a_key"
	protoMessage := new(PBMessage)
	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 44
		block := blocks.NewBlock([]byte(str))
		m.AppendBlock(*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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
func TestWantlist(t *testing.T) {
	keystrs := []string{"foo", "bar", "baz", "bat"}
	m := New()
	for _, s := range keystrs {
		m.AppendWanted(u.Key(s))
	}
	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 83 84 85 86 87 88
func TestCopyProtoByValue(t *testing.T) {
	const str = "foo"
	m := New()
	protoBeforeAppend := m.ToProto()
	m.AppendWanted(u.Key(str))
	if contains(protoBeforeAppend.GetWantlist(), str) {
		t.Fail()
	}
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
func TestToNetMethodSetsPeer(t *testing.T) {
	m := New()
	p := &peer.Peer{ID: []byte("X")}
	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()
	original.AppendWanted(u.Key("M"))
	original.AppendWanted(u.Key("B"))
	original.AppendWanted(u.Key("D"))
	original.AppendWanted(u.Key("T"))
	original.AppendWanted(u.Key("F"))

	netmsg, err := original.ToNet(&peer.Peer{ID: []byte("X")})
	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()
134 135 136 137
	original.AppendBlock(*blocks.NewBlock([]byte("W")))
	original.AppendBlock(*blocks.NewBlock([]byte("E")))
	original.AppendBlock(*blocks.NewBlock([]byte("F")))
	original.AppendBlock(*blocks.NewBlock([]byte("M")))
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

	p := &peer.Peer{ID: []byte("X")}
	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()
		}
	}
}

162 163 164 165 166 167 168 169
func contains(s []string, x string) bool {
	for _, a := range s {
		if a == x {
			return true
		}
	}
	return false
}