message_test.go 6.42 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
	root := testutil.GenerateCids(1)[0]
17
	selector := testutil.RandomBytes(100)
18 19 20 21
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())

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

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

46
	deserialized, err := newMessageFromProto(*pbMessage)
47 48 49 50 51 52 53 54 55 56 57
	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
		deserializedRequest.Root().String() != root.String() ||
59 60 61 62 63 64
		!reflect.DeepEqual(deserializedRequest.Selector(), selector) {
		t.Fatal("Did not properly deserialize protobuf messages so requests are equal")
	}
}

func TestAppendingResponses(t *testing.T) {
65
	extra := testutil.RandomBytes(100)
66
	requestID := GraphSyncRequestID(rand.Int31())
67
	status := RequestAcknowledged
68 69

	gsm := New()
70
	gsm.AddResponse(NewResponse(requestID, status, extra))
71 72 73 74 75 76 77
	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 ||
78
		!reflect.DeepEqual(response.Extra(), extra) {
79 80 81 82
		t.Fatal("Did not properly add response to message")
	}

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

90
	deserialized, err := newMessageFromProto(*pbMessage)
91 92 93 94 95 96 97 98 99 100
	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 ||
101
		!reflect.DeepEqual(deserializedResponse.Extra(), extra) {
102 103 104 105
		t.Fatal("Did not properly deserialize protobuf messages so responses are equal")
	}
}

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 134 135
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
}

136
func TestRequestCancel(t *testing.T) {
137
	selector := testutil.RandomBytes(100)
138 139
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())
140
	root := testutil.GenerateCids(1)[0]
141 142

	gsm := New()
143
	gsm.AddRequest(NewRequest(id, root, selector, priority))
144

145
	gsm.AddRequest(CancelRequest(id))
146 147 148 149 150 151 152 153 154 155 156

	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")
	}
}
157 158

func TestToNetFromNetEquivalency(t *testing.T) {
159
	root := testutil.GenerateCids(1)[0]
160 161
	selector := testutil.RandomBytes(100)
	extra := testutil.RandomBytes(100)
162 163 164 165 166
	id := GraphSyncRequestID(rand.Int31())
	priority := GraphSyncPriority(rand.Int31())
	status := RequestAcknowledged

	gsm := New()
167
	gsm.AddRequest(NewRequest(id, root, selector, priority))
168
	gsm.AddResponse(NewResponse(id, status, extra))
169 170 171 172 173

	gsm.AddBlock(blocks.NewBlock([]byte("W")))
	gsm.AddBlock(blocks.NewBlock([]byte("E")))
	gsm.AddBlock(blocks.NewBlock([]byte("F")))
	gsm.AddBlock(blocks.NewBlock([]byte("M")))
174 175 176 177 178 179

	buf := new(bytes.Buffer)
	err := gsm.ToNet(buf)
	if err != nil {
		t.Fatal("Unable to serialize GraphSyncMessage")
	}
180
	deserialized, err := FromNet(buf)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	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() ||
198
		deserializedRequest.Root().String() != request.Root().String() ||
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		!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() ||
215
		!reflect.DeepEqual(deserializedResponse.Extra(), response.Extra()) {
216 217
		t.Fatal("Did not keep responses when writing to stream and back")
	}
218 219 220 221 222 223 224 225 226 227 228

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