~whereswaldon/sprout-go

ref: relay sprout-go/sprout_test.go -rw-r--r-- 14.2 KiB
82082f28Chris Waldon Implement list verb in example relay 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package sprout_test

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io"
	"math/rand"
	"net"
	"testing"
	"time"

	forest "git.sr.ht/~whereswaldon/forest-go"
	"git.sr.ht/~whereswaldon/forest-go/fields"
	"git.sr.ht/~whereswaldon/forest-go/testkeys"
	sprout "git.sr.ht/~whereswaldon/sprout-go"
)

type LoopbackConn struct {
	bytes.Buffer
}

func (l LoopbackConn) Close() error {
	return nil
}

func (l LoopbackConn) LocalAddr() net.Addr {
	return &net.IPAddr{}
}

func (l LoopbackConn) RemoteAddr() net.Addr {
	return &net.IPAddr{}
}

func (l LoopbackConn) SetDeadline(t time.Time) error {
	if err := l.SetReadDeadline(t); err != nil {
		return err
	}
	return l.SetWriteDeadline(t)
}
func (l LoopbackConn) SetReadDeadline(t time.Time) error {
	return nil
}

func (l LoopbackConn) SetWriteDeadline(t time.Time) error {
	return nil
}

// TeeConn is a debugging tool that can be used in place of LoopbackConn
// to log all of the protocol text to an io.Writer for analysis
type TeeConn struct {
	LoopbackConn
	Out io.Writer
}

func (t *TeeConn) Write(b []byte) (int, error) {
	n, err := t.LoopbackConn.Write(b)
	if err != nil {
		return n, err
	}
	return t.Out.Write(b)
}

var _ net.Conn = &TeeConn{}

func TestVersionMessage(t *testing.T) {
	var (
		outMajor    int
		outMinor    int
		inID, outID sprout.MessageID
		err         error
		sconn       *sprout.Conn
	)
	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnVersion = func(s *sprout.Conn, m sprout.MessageID, major, minor int) error {
		outID = m
		outMajor = major
		outMinor = minor
		return nil
	}
	inID, err = sconn.SendVersion()
	if err != nil {
		t.Fatalf("failed to send version: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to send version: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if sconn.Major != outMajor {
		t.Fatalf("major version mismatch, expected %d, got %d", sconn.Major, outMajor)
	} else if sconn.Minor != outMinor {
		t.Fatalf("minor version mismatch, expected %d, got %d", sconn.Minor, outMinor)
	}
}

func TestQueryAnyMessage(t *testing.T) {
	var (
		inID, outID             sprout.MessageID
		inNodeType, outNodeType fields.NodeType
		inQuantity, outQuantity int
		err                     error
		sconn                   *sprout.Conn
	)
	inNodeType = fields.NodeTypeCommunity
	inQuantity = 5
	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnList = func(s *sprout.Conn, m sprout.MessageID, nodeType fields.NodeType, quantity int) error {
		outID = m
		outNodeType = nodeType
		outQuantity = quantity
		return nil
	}
	inID, err = sconn.SendList(inNodeType, inQuantity)
	if err != nil {
		t.Fatalf("failed to send query_any: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read query_any: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if inNodeType != outNodeType {
		t.Fatalf("node type mismatch, expected %d, got %d", inNodeType, outNodeType)
	} else if inQuantity != outQuantity {
		t.Fatalf("quantity mismatch, expected %d, got %d", inQuantity, outQuantity)
	}
}

func randomQualifiedHash() *fields.QualifiedHash {
	length := 32
	return &fields.QualifiedHash{
		Descriptor: fields.HashDescriptor{
			Type:   fields.HashTypeSHA512,
			Length: fields.ContentLength(length),
		},
		Blob: fields.Blob(randomBytes(length)),
	}
}

func randomQualifiedHashSlice(count int) []*fields.QualifiedHash {
	out := make([]*fields.QualifiedHash, count)
	for i := 0; i < count; i++ {
		out[i] = randomQualifiedHash()
	}
	return out
}

func randomBytes(length int) []byte {
	b := make([]byte, length)
	_, _ = rand.Read(b)
	return b
}

func randomString(length int) string {
	return string(base64.StdEncoding.EncodeToString(randomBytes(length)))
}

func randomIdentity(t *testing.T) *forest.Identity {
	signer := testkeys.Signer(t, testkeys.PrivKey1)
	name := randomString(12)
	id, err := forest.NewIdentity(signer, name, "")
	if err != nil {
		t.Errorf("Failed to generate test identity: %v", err)
		return nil
	}
	return id
}

func TestQueryMessage(t *testing.T) {
	var (
		inID, outID           sprout.MessageID
		inNodeIDs, outNodeIDs []*fields.QualifiedHash
		err                   error
		sconn                 *sprout.Conn
	)
	inNodeIDs = randomQualifiedHashSlice(10)

	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnQuery = func(s *sprout.Conn, m sprout.MessageID, nodeIDs []*fields.QualifiedHash) error {
		outID = m
		outNodeIDs = nodeIDs
		return nil
	}
	inID, err = sconn.SendQuery(inNodeIDs...)
	if err != nil {
		t.Fatalf("failed to send query: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read query: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if len(inNodeIDs) != len(outNodeIDs) {
		t.Fatalf("node id list length mismatch, expected %d, got %d", len(inNodeIDs), len(outNodeIDs))
	}
	for i, n := range inNodeIDs {
		if !n.Equals(outNodeIDs[i]) {
			inString, _ := n.MarshalText()
			outString, _ := outNodeIDs[i].MarshalText()
			t.Fatalf("node id mismatch, expected %s got %s", inString, outString)
		}
	}
}

func TestAncestryMessage(t *testing.T) {
	var (
		inID, outID         sprout.MessageID
		inNodeID, outNodeID *fields.QualifiedHash
		inLevels, outLevels int
		err                 error
		sconn               *sprout.Conn
	)
	inNodeID = randomQualifiedHash()
	inLevels = 5

	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnAncestry = func(s *sprout.Conn, m sprout.MessageID, nodeID *fields.QualifiedHash, levels int) error {
		outID = m
		outNodeID = nodeID
		outLevels = levels
		return nil
	}
	inID, err = sconn.SendAncestry(inNodeID, inLevels)
	if err != nil {
		t.Fatalf("failed to send ancestry: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read ancestry: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if inLevels != outLevels {
		t.Fatalf("levels mismatch, expected %d, got %d", inLevels, outLevels)
	}
	if !inNodeID.Equals(outNodeID) {
		inString, _ := inNodeID.MarshalText()
		outString, _ := outNodeID.MarshalText()
		t.Fatalf("node id mismatch, expected %s got %s", inString, outString)
	}
}

func TestLeavesOfMessage(t *testing.T) {
	var (
		inID, outID             sprout.MessageID
		inNodeID, outNodeID     *fields.QualifiedHash
		inQuantity, outQuantity int
		err                     error
		sconn                   *sprout.Conn
	)
	inNodeID = randomQualifiedHash()
	inQuantity = 5
	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnLeavesOf = func(s *sprout.Conn, m sprout.MessageID, nodeID *fields.QualifiedHash, quantity int) error {
		outID = m
		outNodeID = nodeID
		outQuantity = quantity
		return nil
	}
	inID, err = sconn.SendLeavesOf(inNodeID, inQuantity)
	if err != nil {
		t.Fatalf("failed to send query_any: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read query_any: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if !inNodeID.Equals(outNodeID) {
		inString, _ := inNodeID.MarshalText()
		outString, _ := outNodeID.MarshalText()
		t.Fatalf("node id mismatch, expected %s, got %s", inString, outString)
	} else if inQuantity != outQuantity {
		t.Fatalf("quantity mismatch, expected %d, got %d", inQuantity, outQuantity)
	}
}

func TestSubscribeMessage(t *testing.T) {
	var (
		inID, outID         sprout.MessageID
		inNodeID, outNodeID *fields.QualifiedHash
		err                 error
		sconn               *sprout.Conn
	)
	inNodeID = randomQualifiedHash()

	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnSubscribe = func(s *sprout.Conn, m sprout.MessageID, nodeID *fields.QualifiedHash) error {
		outID = m
		outNodeID = nodeID
		return nil
	}
	inID, err = sconn.SendSubscribeByID(inNodeID)
	if err != nil {
		t.Fatalf("failed to send subscribe: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read subscribe: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	}
	if !inNodeID.Equals(outNodeID) {
		inString, _ := inNodeID.MarshalText()
		outString, _ := outNodeID.MarshalText()
		t.Fatalf("node id mismatch, expected %s got %s", inString, outString)
	}
}

func TestUnsubscribeMessage(t *testing.T) {
	var (
		inID, outID         sprout.MessageID
		inNodeID, outNodeID *fields.QualifiedHash
		err                 error
		sconn               *sprout.Conn
	)
	inNodeID = randomQualifiedHash()

	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnUnsubscribe = func(s *sprout.Conn, m sprout.MessageID, nodeID *fields.QualifiedHash) error {
		outID = m
		outNodeID = nodeID
		return nil
	}
	inID, err = sconn.SendUnsubscribeByID(inNodeID)
	if err != nil {
		t.Fatalf("failed to send unsubscribe: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read unsubscribe: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	}
	if !inNodeID.Equals(outNodeID) {
		inString, _ := inNodeID.MarshalText()
		outString, _ := outNodeID.MarshalText()
		t.Fatalf("node id mismatch, expected %s got %s", inString, outString)
	}
}

func TestStatusMessage(t *testing.T) {
	var (
		inID, outID     sprout.MessageID
		inCode, outCode sprout.StatusCode
		err             error
		sconn           *sprout.Conn
	)
	inID = 5
	inCode = sprout.ErrorMalformed
	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnStatus = func(s *sprout.Conn, target sprout.MessageID, code sprout.StatusCode) error {
		outID = target
		outCode = code
		return nil
	}
	err = sconn.SendStatus(inID, inCode)
	if err != nil {
		t.Fatalf("failed to send error: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read error: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if inCode != outCode {
		t.Fatalf("error code mismatch, expected %d, got %d", inCode, outCode)
	}
}

func TestResponseMessage(t *testing.T) {
	var (
		inID, outID       sprout.MessageID
		inNodes, outNodes []forest.Node
		err               error
		sconn             *sprout.Conn
	)
	const count = 3
	inNodes = make([]forest.Node, count)
	for i := range inNodes {
		inNodes[i] = randomIdentity(t)
	}
	conn := new(LoopbackConn)
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnResponse = func(s *sprout.Conn, m sprout.MessageID, nodes []forest.Node) error {
		outID = m
		outNodes = nodes
		return nil
	}
	err = sconn.SendResponse(inID, inNodes)
	if err != nil {
		t.Fatalf("failed to send response: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read response: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if len(inNodes) != len(outNodes) {
		t.Fatalf("node list length mismatch, expected %d, got %d", len(inNodes), len(outNodes))
	}
	for i, node := range inNodes {
		if !node.Equals(outNodes[i]) {
			t.Fatalf("Node mismatch at index %d,\nin: %v\nout: %v", i, node, outNodes[i])
		}
	}
}

func TestAnnounceMessage(t *testing.T) {
	var (
		inID, outID       sprout.MessageID
		inNodes, outNodes []forest.Node
		err               error
		sconn             *sprout.Conn
	)
	const count = 3
	inNodes = make([]forest.Node, count)
	for i := range inNodes {
		inNodes[i] = randomIdentity(t)
	}
	conn := &LoopbackConn{}
	sconn, err = sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("failed to construct sprout.Conn: %v", err)
	}
	sconn.OnAnnounce = func(s *sprout.Conn, m sprout.MessageID, nodes []forest.Node) error {
		outID = m
		outNodes = nodes
		return nil
	}
	inID, err = sconn.SendAnnounce(inNodes)
	if err != nil {
		t.Fatalf("failed to send response: %v", err)
	}
	err = sconn.ReadMessage()
	if err != nil {
		t.Fatalf("failed to read response: %v", err)
	}
	if inID != outID {
		t.Fatalf("id mismatch, got %d, expected %d", outID, inID)
	} else if len(inNodes) != len(outNodes) {
		t.Fatalf("node list length mismatch, expected %d, got %d", len(inNodes), len(outNodes))
	}
	for i, node := range inNodes {
		if !node.Equals(outNodes[i]) {
			t.Fatalf("Node mismatch at index %d,\nin: %v\nout: %v", i, node, outNodes[i])
		}
	}
}

func TestWithRealNetConn(t *testing.T) {
	var (
		address  string
		listener net.Listener
		err      error
	)
	for i := 7000; i < 8000; i++ {
		address = fmt.Sprintf("localhost:%d", i)
		listener, err = net.Listen("tcp", address)
		if err == nil {
			break
		}
	}
	if err != nil {
		t.Fatalf("Failed to create test listener: %v", err)
	}
	go func() {
		conn, err := net.Dial("tcp", address)
		if err != nil {
			t.Fatalf("Failed to dial test listener: %v", err)
		}
		defer conn.Close()
		sconn, err := sprout.NewConn(conn)
		if err != nil {
			t.Fatalf("Failed to make sprout.Conn from net.Conn: %v", err)
		}
		if _, err := sconn.SendVersion(); err != nil {
			t.Fatalf("Failed to send version: %v", err)
		}
	}()
	conn, err := listener.Accept()
	if err != nil {
		t.Fatalf("Failed to accept test connection: %v", err)
	}
	sconn, err := sprout.NewConn(conn)
	if err != nil {
		t.Fatalf("Failed to make sprout.Conn from net.Conn: %v", err)
	}
	out := make(chan struct {
		id           sprout.MessageID
		major, minor int
	}, 1)
	sconn.OnVersion = func(s *sprout.Conn, id sprout.MessageID, major, minor int) error {
		out <- struct {
			id           sprout.MessageID
			major, minor int
		}{id: id, major: major, minor: minor}
		return nil
	}
	if err := sconn.ReadMessage(); err != nil {
		t.Fatalf("Failed to read message from test connection: %v", err)
	}
	select {
	case <-out:
	// handler was invoked, we're all good
	case <-time.NewTicker(time.Second).C:
		t.Fatalf("Handler wasn't invoked within 1 second")
	}
}