message_test.go 5.99 KB
Newer Older
1 2 3
package message

import (
4
	"bytes"
5 6 7 8
	"math/rand"
	"reflect"
	"testing"

9 10 11
	blocks "github.com/ipfs/go-block-format"

	cid "github.com/ipfs/go-cid"
12
	"github.com/ipfs/go-graphsync/testutil"
13 14 15
)

func TestAppendingRequests(t *testing.T) {
16
	selector := testutil.RandomBytes(100)
17 18 19 20
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())

	gsm := New()
21
	gsm.AddRequest(id, selector, priority)
22 23 24 25 26 27 28 29 30 31 32 33 34
	requests := gsm.Requests()
	if len(requests) != 1 {
		t.Fatal("Did not add request to message")
	}
	request := requests[0]
	if request.ID() != id ||
		request.IsCancel() != false ||
		request.Priority() != priority ||
		!reflect.DeepEqual(request.Selector(), selector) {
		t.Fatal("Did not properly add request to message")
	}

	pbMessage := gsm.ToProto()
35
	pbRequest := pbMessage.Requests[0]
36 37 38
	if pbRequest.Id != int32(id) ||
		pbRequest.Priority != int32(priority) ||
		pbRequest.Cancel != false ||
39
		!reflect.DeepEqual(pbRequest.Selector, selector) {
40 41 42
		t.Fatal("Did not properly serialize message to protobuf")
	}

43
	deserialized, err := newMessageFromProto(*pbMessage)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	if err != nil {
		t.Fatal("Error deserializing protobuf message")
	}
	deserializedRequests := deserialized.Requests()
	if len(deserializedRequests) != 1 {
		t.Fatal("Did not add request to deserialized message")
	}
	deserializedRequest := deserializedRequests[0]
	if deserializedRequest.ID() != id ||
		deserializedRequest.IsCancel() != false ||
		deserializedRequest.Priority() != priority ||
		!reflect.DeepEqual(deserializedRequest.Selector(), selector) {
		t.Fatal("Did not properly deserialize protobuf messages so requests are equal")
	}
}

func TestAppendingResponses(t *testing.T) {
61
	extra := testutil.RandomBytes(100)
62
	requestID := GraphSyncRequestID(rand.Int31())
63
	status := RequestAcknowledged
64 65

	gsm := New()
66
	gsm.AddResponse(requestID, status, extra)
67 68 69 70 71 72 73
	responses := gsm.Responses()
	if len(responses) != 1 {
		t.Fatal("Did not add response to message")
	}
	response := responses[0]
	if response.RequestID() != requestID ||
		response.Status() != status ||
74
		!reflect.DeepEqual(response.Extra(), extra) {
75 76 77 78
		t.Fatal("Did not properly add response to message")
	}

	pbMessage := gsm.ToProto()
79
	pbResponse := pbMessage.Responses[0]
80 81
	if pbResponse.Id != int32(requestID) ||
		pbResponse.Status != int32(status) ||
82
		!reflect.DeepEqual(pbResponse.Extra, extra) {
83 84 85
		t.Fatal("Did not properly serialize message to protobuf")
	}

86
	deserialized, err := newMessageFromProto(*pbMessage)
87 88 89 90 91 92 93 94 95 96
	if err != nil {
		t.Fatal("Error deserializing protobuf message")
	}
	deserializedResponses := deserialized.Responses()
	if len(deserializedResponses) != 1 {
		t.Fatal("Did not add response to message")
	}
	deserializedResponse := deserializedResponses[0]
	if deserializedResponse.RequestID() != requestID ||
		deserializedResponse.Status() != status ||
97
		!reflect.DeepEqual(deserializedResponse.Extra(), extra) {
98 99 100 101
		t.Fatal("Did not properly deserialize protobuf messages so responses are equal")
	}
}

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
func TestAppendBlock(t *testing.T) {

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

	m := New()
	for _, str := range strs {
		block := blocks.NewBlock([]byte(str))
		m.AddBlock(block)
	}

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

func contains(strs []string, x string) bool {
	for _, s := range strs {
		if s == x {
			return true
		}
	}
	return false
}

132
func TestRequestCancel(t *testing.T) {
133
	selector := testutil.RandomBytes(100)
134 135 136 137
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())

	gsm := New()
138
	gsm.AddRequest(id, selector, priority)
139 140 141 142 143 144 145 146 147 148 149 150 151

	gsm.Cancel(id)

	requests := gsm.Requests()
	if len(requests) != 1 {
		t.Fatal("Did not properly cancel request")
	}
	request := requests[0]
	if request.ID() != id ||
		request.IsCancel() != true {
		t.Fatal("Did not properly add cancel request to message")
	}
}
152 153

func TestToNetFromNetEquivalency(t *testing.T) {
154 155
	selector := testutil.RandomBytes(100)
	extra := testutil.RandomBytes(100)
156 157 158 159 160
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())
	status := RequestAcknowledged

	gsm := New()
161 162 163 164 165 166 167
	gsm.AddRequest(id, selector, priority)
	gsm.AddResponse(id, status, extra)

	gsm.AddBlock(blocks.NewBlock([]byte("W")))
	gsm.AddBlock(blocks.NewBlock([]byte("E")))
	gsm.AddBlock(blocks.NewBlock([]byte("F")))
	gsm.AddBlock(blocks.NewBlock([]byte("M")))
168 169 170 171 172 173

	buf := new(bytes.Buffer)
	err := gsm.ToNet(buf)
	if err != nil {
		t.Fatal("Unable to serialize GraphSyncMessage")
	}
174
	deserialized, err := FromNet(buf)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	if err != nil {
		t.Fatal("Error deserializing protobuf message")
	}

	requests := gsm.Requests()
	if len(requests) != 1 {
		t.Fatal("Did not add request to message")
	}
	request := requests[0]
	deserializedRequests := deserialized.Requests()
	if len(deserializedRequests) != 1 {
		t.Fatal("Did not add request to deserialized message")
	}
	deserializedRequest := deserializedRequests[0]
	if deserializedRequest.ID() != request.ID() ||
		deserializedRequest.IsCancel() != request.IsCancel() ||
		deserializedRequest.Priority() != request.Priority() ||
		!reflect.DeepEqual(deserializedRequest.Selector(), request.Selector()) {
		t.Fatal("Did not keep requests when writing to stream and back")
	}

	responses := gsm.Responses()
	if len(responses) != 1 {
		t.Fatal("Did not add response to message")
	}
	response := responses[0]
	deserializedResponses := deserialized.Responses()
	if len(deserializedResponses) != 1 {
		t.Fatal("Did not add response to message")
	}
	deserializedResponse := deserializedResponses[0]
	if deserializedResponse.RequestID() != response.RequestID() ||
		deserializedResponse.Status() != response.Status() ||
208
		!reflect.DeepEqual(deserializedResponse.Extra(), response.Extra()) {
209 210
		t.Fatal("Did not keep responses when writing to stream and back")
	}
211 212 213 214 215 216 217 218 219 220 221

	keys := make(map[cid.Cid]bool)
	for _, b := range deserialized.Blocks() {
		keys[b.Cid()] = true
	}

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