-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyggquic_test.go
87 lines (75 loc) · 1.88 KB
/
yggquic_test.go
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
/*
* Copyright (c) 2023 Neil Alexander
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package yggquic
import (
"encoding/hex"
"io"
"net"
"testing"
"github.com/yggdrasil-network/yggdrasil-go/src/config"
"github.com/yggdrasil-network/yggdrasil-go/src/core"
)
func TestQUICOverYggdrasil(t *testing.T) {
cfg1 := config.GenerateConfig()
cfg2 := config.GenerateConfig()
// Create the Yggdrasil nodes.
node1, err := core.New(cfg1.Certificate, nil)
if err != nil {
t.Fatal(err)
}
node2, err := core.New(cfg2.Certificate, nil)
if err != nil {
t.Fatal(err)
}
// Peer the Yggdrasil nodes to each other.
l, r := net.Pipe()
go node1.HandleConn(node2.PublicKey(), l, 0) // nolint:errcheck
go node2.HandleConn(node1.PublicKey(), r, 0) // nolint:errcheck
// Create QUIC over Yggdrasil endpoints.
quic1, err := New(node1, *cfg1.Certificate, nil)
if err != nil {
t.Fatal(err)
}
quic2, err := New(node2, *cfg2.Certificate, nil)
if err != nil {
t.Fatal(err)
}
// Dial node 1 from node 2.
t.Run("Dial", func(t *testing.T) {
t.Parallel()
destination := hex.EncodeToString(node1.PublicKey())
for i := 0; i < 5; i++ {
c, err := quic2.Dial("yggdrasil", destination)
if err != nil {
t.Fatal(err)
}
t.Logf("Opened connection to %q", c.RemoteAddr().String())
if _, err = c.Write([]byte("Hello!")); err != nil {
t.Fatal(err)
}
if err = c.Close(); err != nil {
t.Fatal(err)
}
}
})
t.Run("Listen", func(t *testing.T) {
t.Parallel()
for i := 0; i < 5; i++ {
c, err := quic1.Accept()
if err != nil {
t.Fatal(err)
}
t.Logf("Accepted connection from %q", c.RemoteAddr())
b, err := io.ReadAll(c)
if err != nil {
t.Fatal(err)
}
t.Logf("Received: %s", b[:])
}
})
}