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

import (
4
	"bytes"
5 6 7 8 9 10 11 12 13
	"math/rand"
	"reflect"
	"testing"

	"github.com/ipfs/go-graphsync/testselector"
)

func TestAppendingRequests(t *testing.T) {
	selector := testselector.GenerateSelector()
14
	root := testselector.GenerateRootCid()
15 16 17 18
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())

	gsm := New()
19
	gsm.AddRequest(id, selector, root, priority)
20 21 22 23 24 25 26 27
	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 ||
28
		!reflect.DeepEqual(request.Root(), root) ||
29 30 31 32 33 34 35 36 37
		!reflect.DeepEqual(request.Selector(), selector) {
		t.Fatal("Did not properly add request to message")
	}

	pbMessage := gsm.ToProto()
	pbRequest := pbMessage.Reqlist[0]
	if pbRequest.Id != int32(id) ||
		pbRequest.Priority != int32(priority) ||
		pbRequest.Cancel != false ||
38
		!reflect.DeepEqual(pbRequest.Root, root.Bytes()) ||
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		!reflect.DeepEqual(pbRequest.Selector, selector.RawData()) {
		t.Fatal("Did not properly serialize message to protobuf")
	}

	deserialized, err := newMessageFromProto(*pbMessage,
		testselector.MockDecodeSelectorFunc,
		testselector.MockDecodeSelectionResponseFunc,
	)
	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 ||
58
		!reflect.DeepEqual(deserializedRequest.Root(), root) ||
59 60 61 62 63 64 65 66
		!reflect.DeepEqual(deserializedRequest.Selector(), selector) {
		t.Fatal("Did not properly deserialize protobuf messages so requests are equal")
	}
}

func TestAppendingResponses(t *testing.T) {
	selectionResponse := testselector.GenerateSelectionResponse()
	requestID := GraphSyncRequestID(rand.Int31())
67
	status := RequestAcknowledged
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

	gsm := New()
	gsm.AddResponse(requestID, status, selectionResponse)
	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 ||
		!reflect.DeepEqual(response.Response(), selectionResponse) {
		t.Fatal("Did not properly add response to message")
	}

	pbMessage := gsm.ToProto()
	pbResponse := pbMessage.Reslist[0]
	if pbResponse.Id != int32(requestID) ||
		pbResponse.Status != int32(status) ||
		!reflect.DeepEqual(pbResponse.Data, selectionResponse.RawData()) {
		t.Fatal("Did not properly serialize message to protobuf")
	}

	deserialized, err := newMessageFromProto(*pbMessage,
		testselector.MockDecodeSelectorFunc,
		testselector.MockDecodeSelectionResponseFunc,
	)
	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 ||
		!reflect.DeepEqual(deserializedResponse.Response(), selectionResponse) {
		t.Fatal("Did not properly deserialize protobuf messages so responses are equal")
	}
}

func TestRequestCancel(t *testing.T) {
	selector := testselector.GenerateSelector()
111
	root := testselector.GenerateRootCid()
112 113 114 115
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())

	gsm := New()
116
	gsm.AddRequest(id, selector, root, priority)
117 118 119 120 121 122 123 124 125 126 127 128 129

	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")
	}
}
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

func TestToNetFromNetEquivalency(t *testing.T) {
	selector := testselector.GenerateSelector()
	root := testselector.GenerateRootCid()
	selectionResponse := testselector.GenerateSelectionResponse()
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())
	status := RequestAcknowledged

	gsm := New()
	gsm.AddRequest(id, selector, root, priority)
	gsm.AddResponse(id, status, selectionResponse)

	buf := new(bytes.Buffer)
	err := gsm.ToNet(buf)
	if err != nil {
		t.Fatal("Unable to serialize GraphSyncMessage")
	}
	deserialized, err := FromNet(buf,
		testselector.MockDecodeSelectorFunc,
		testselector.MockDecodeSelectionResponseFunc,
	)
	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.Root(), request.Root()) ||
		!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() ||
		!reflect.DeepEqual(deserializedResponse.Response(), response.Response()) {
		t.Fatal("Did not keep responses when writing to stream and back")
	}
}