message_test.go 7.62 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 17 18 19 20
	extensionName := GraphSyncExtensionName("graphsync/awesome")
	extension := GraphSyncExtension{
		Name: extensionName,
		Data: testutil.RandomBytes(100),
	}
21
	root := testutil.GenerateCids(1)[0]
22
	selector := testutil.RandomBytes(100)
23 24 25 26
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())

	gsm := New()
27
	gsm.AddRequest(NewRequest(id, root, selector, priority, extension))
28 29 30 31 32
	requests := gsm.Requests()
	if len(requests) != 1 {
		t.Fatal("Did not add request to message")
	}
	request := requests[0]
33
	extensionData, err := request.Extension(extensionName)
34 35 36
	if request.ID() != id ||
		request.IsCancel() != false ||
		request.Priority() != priority ||
37
		request.Root().String() != root.String() ||
38 39 40
		!reflect.DeepEqual(request.Selector(), selector) ||
		err != nil ||
		!reflect.DeepEqual(extension.Data, extensionData) {
41 42 43 44
		t.Fatal("Did not properly add request to message")
	}

	pbMessage := gsm.ToProto()
45
	pbRequest := pbMessage.Requests[0]
46 47 48
	if pbRequest.Id != int32(id) ||
		pbRequest.Priority != int32(priority) ||
		pbRequest.Cancel != false ||
49
		!reflect.DeepEqual(pbRequest.Root, root.Bytes()) ||
50 51
		!reflect.DeepEqual(pbRequest.Selector, selector) ||
		!reflect.DeepEqual(pbRequest.Extensions, map[string][]byte{"graphsync/awesome": extension.Data}) {
52 53 54
		t.Fatal("Did not properly serialize message to protobuf")
	}

55
	deserialized, err := newMessageFromProto(*pbMessage)
56 57 58 59 60 61 62 63
	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]
64
	extensionData, err = deserializedRequest.Extension(extensionName)
65 66 67
	if deserializedRequest.ID() != id ||
		deserializedRequest.IsCancel() != false ||
		deserializedRequest.Priority() != priority ||
68
		deserializedRequest.Root().String() != root.String() ||
69 70 71
		!reflect.DeepEqual(deserializedRequest.Selector(), selector) ||
		err != nil ||
		!reflect.DeepEqual(extension.Data, extensionData) {
72 73 74 75 76
		t.Fatal("Did not properly deserialize protobuf messages so requests are equal")
	}
}

func TestAppendingResponses(t *testing.T) {
77 78 79 80 81
	extensionName := GraphSyncExtensionName("graphsync/awesome")
	extension := GraphSyncExtension{
		Name: extensionName,
		Data: testutil.RandomBytes(100),
	}
82
	requestID := GraphSyncRequestID(rand.Int31())
83
	status := RequestAcknowledged
84 85

	gsm := New()
86
	gsm.AddResponse(NewResponse(requestID, status, extension))
87 88 89 90 91
	responses := gsm.Responses()
	if len(responses) != 1 {
		t.Fatal("Did not add response to message")
	}
	response := responses[0]
92
	extensionData, err := response.Extension(extensionName)
93 94
	if response.RequestID() != requestID ||
		response.Status() != status ||
95 96
		err != nil ||
		!reflect.DeepEqual(extension.Data, extensionData) {
97 98 99 100
		t.Fatal("Did not properly add response to message")
	}

	pbMessage := gsm.ToProto()
101
	pbResponse := pbMessage.Responses[0]
102 103
	if pbResponse.Id != int32(requestID) ||
		pbResponse.Status != int32(status) ||
104
		!reflect.DeepEqual(pbResponse.Extensions, map[string][]byte{"graphsync/awesome": extension.Data}) {
105 106 107
		t.Fatal("Did not properly serialize message to protobuf")
	}

108
	deserialized, err := newMessageFromProto(*pbMessage)
109 110 111 112 113 114 115 116
	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]
117 118 119 120 121
	extensionData, err = deserializedResponse.Extension(extensionName)
	if deserializedResponse.RequestID() != response.RequestID() ||
		deserializedResponse.Status() != response.Status() ||
		err != nil ||
		!reflect.DeepEqual(extensionData, extension.Data) {
122 123 124 125
		t.Fatal("Did not properly deserialize protobuf messages so responses are equal")
	}
}

126 127 128 129 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
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
}

156
func TestRequestCancel(t *testing.T) {
157
	selector := testutil.RandomBytes(100)
158 159
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())
160
	root := testutil.GenerateCids(1)[0]
161 162

	gsm := New()
163
	gsm.AddRequest(NewRequest(id, root, selector, priority))
164

165
	gsm.AddRequest(CancelRequest(id))
166 167 168 169 170 171 172 173 174 175 176

	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")
	}
}
177 178

func TestToNetFromNetEquivalency(t *testing.T) {
179
	root := testutil.GenerateCids(1)[0]
180
	selector := testutil.RandomBytes(100)
181 182 183 184 185
	extensionName := GraphSyncExtensionName("graphsync/awesome")
	extension := GraphSyncExtension{
		Name: extensionName,
		Data: testutil.RandomBytes(100),
	}
186 187 188 189 190
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())
	status := RequestAcknowledged

	gsm := New()
191 192
	gsm.AddRequest(NewRequest(id, root, selector, priority, extension))
	gsm.AddResponse(NewResponse(id, status, extension))
193 194 195 196 197

	gsm.AddBlock(blocks.NewBlock([]byte("W")))
	gsm.AddBlock(blocks.NewBlock([]byte("E")))
	gsm.AddBlock(blocks.NewBlock([]byte("F")))
	gsm.AddBlock(blocks.NewBlock([]byte("M")))
198 199 200 201 202 203

	buf := new(bytes.Buffer)
	err := gsm.ToNet(buf)
	if err != nil {
		t.Fatal("Unable to serialize GraphSyncMessage")
	}
204
	deserialized, err := FromNet(buf)
205 206 207 208 209 210 211 212 213 214 215 216 217 218
	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]
219
	extensionData, err := deserializedRequest.Extension(extensionName)
220 221 222
	if deserializedRequest.ID() != request.ID() ||
		deserializedRequest.IsCancel() != request.IsCancel() ||
		deserializedRequest.Priority() != request.Priority() ||
223
		deserializedRequest.Root().String() != request.Root().String() ||
224 225 226
		!reflect.DeepEqual(deserializedRequest.Selector(), request.Selector()) ||
		err != nil ||
		!reflect.DeepEqual(extensionData, extension.Data) {
227 228 229 230 231 232 233 234 235 236 237 238 239
		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]
240
	extensionData, err = deserializedResponse.Extension(extensionName)
241 242
	if deserializedResponse.RequestID() != response.RequestID() ||
		deserializedResponse.Status() != response.Status() ||
243 244
		err != nil ||
		!reflect.DeepEqual(extensionData, extension.Data) {
245 246
		t.Fatal("Did not keep responses when writing to stream and back")
	}
247 248 249 250 251 252 253 254 255 256 257

	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()
		}
	}
258
}