-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
132 lines (119 loc) · 3.18 KB
/
client_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
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
package sparoid
import (
//"bytes"
"net"
"testing"
//"golang.org/x/crypto/ssh"
"errors"
"regexp"
)
var (
key = "0000000000000000000000000000000000000000000000000000000000000000"
hmacKey = "0000000000000000000000000000000000000000000000000000000000000000"
client, _ = NewClient(key, hmacKey)
)
func TestClientCreatesNewClient(t *testing.T) {
client, err := NewClient(key, hmacKey)
if err != nil {
t.Errorf("Expected no error %s", err)
}
if client == nil {
t.Errorf("Expected client to be created")
}
}
func TestClientGuardsAgainstShortKeys(t *testing.T) {
_, err1 := NewClient("short", hmacKey)
_, err2 := NewClient(key, "short")
if errors.Is(err1, ErrKeyLength) == false && errors.Is(err2, ErrKeyLength) == false {
t.Errorf("Expected ErrKeyLength")
}
}
func TestClientResolvesPublicIP(t *testing.T) {
ip, err := client.publicIP()
if err != nil {
t.Errorf("Expected no error %s", err)
}
match, err := regexp.MatchString(`^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$`, ip.String())
if err != nil {
t.Errorf("Expected no error %s", err)
}
if !match {
t.Errorf("Expected IP address to match pattern")
}
}
func TestClientCreatesAMessage(t *testing.T) {
message := client.message()
if len(message) != 32 {
t.Errorf("Expected message length to be 32, got %d", len(message))
}
}
func TestClientEncryptsMessages(t *testing.T) {
message := client.message()
encrypted, err := client.encrypt(message)
if err != nil {
t.Errorf("Expected no error %s", err)
t.FailNow()
}
t.Logf("Encrypted message: %x", encrypted)
if len(encrypted) != 64 {
t.Errorf("Expected encrypted message length to be 64, got %d", len(encrypted))
}
}
func TestClientAddsHMAC(t *testing.T) {
message := client.message()
encrypted, _ := client.encrypt(message)
prefixed := client.prefixHMAC(encrypted)
if len(prefixed) != 96 {
t.Errorf("Expected prefixed message length to be 96, got %d", len(prefixed))
}
}
func TestClientSendsMessage(t *testing.T) {
server, _ := net.ListenPacket("udp", "127.0.0.1:0")
defer server.Close()
port := server.LocalAddr().(*net.UDPAddr).Port
go func() {
err := client.Auth("127.0.0.1", port)
if err != nil {
t.Errorf("Expected no error %s", err)
}
}()
buf := make([]byte, 512)
n, _, err := server.ReadFrom(buf)
if err != nil {
t.Errorf("Expected no error %s", err)
}
if n != 96 {
t.Errorf("Expected received message length to be 96 got %d", n)
}
}
func TestClientOpensPortForPassedInIPArgument(t *testing.T) {
ip := "127.0.0.1"
server, _ := net.ListenPacket("udp", "127.0.0.1:0")
defer server.Close()
port := server.LocalAddr().(*net.UDPAddr).Port
go func() {
err := client.Auth(ip, port)
if err != nil {
t.Errorf("Expected no error %s", err)
}
}()
buf := make([]byte, 512)
n, _, _ := server.ReadFrom(buf)
if n != 96 {
t.Errorf("Expected received message length to be 96, got %d", n)
}
}
func TestPad(t *testing.T) {
message := []byte("hello")
padded := pad(message)
if len(padded) != 16 {
t.Errorf("Expected padded message length to be 16, got %d", len(padded))
}
}
func TestCanConnectToSparoidServer(t *testing.T) {
clusterName := "localhost"
err := client.Auth(clusterName, 8484)
if err != nil {
t.Fatal("Failed to dial: ", err)
}
}