-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtls_test.go
83 lines (72 loc) · 2.07 KB
/
tls_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
package ircx
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"testing"
"time"
)
func TestTLSConnect(t *testing.T) {
// Generate self-signed certificate for mock server
ca := &x509.Certificate{
SerialNumber: big.NewInt(1234),
Subject: pkix.Name{
Country: []string{"USA"},
Organization: []string{"ircxtest"},
OrganizationalUnit: []string{"test"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
SubjectKeyId: []byte{1, 2, 3, 4, 5},
BasicConstraintsValid: true,
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
}
priv, _ := rsa.GenerateKey(rand.Reader, 1024)
pub := &priv.PublicKey
cab, err := x509.CreateCertificate(rand.Reader, ca, ca, pub, priv)
if err != nil {
t.Fatalf("error generating self-cert: %v", err)
}
cert := tls.Certificate{
Certificate: [][]byte{cab},
PrivateKey: priv,
}
// prep config for mock server
serverConfig := tls.Config{Certificates: []tls.Certificate{cert}}
l, err := tls.Listen("tcp", "127.0.0.1:0", &serverConfig)
if err != nil {
t.Fatalf("Wanted listener, got err: %v", err)
}
// prep bot/bot config
botConfig := &tls.Config{InsecureSkipVerify: true}
b := WithLoginTLS(l.Addr().String(), "test-bot", "test-user", "test-password", botConfig)
not := make(chan string)
go echoHelper(l, not)
err = b.Connect()
if err != nil {
t.Fatalf("error connecting to mock TLS server: %v", err)
}
// We should get back the connect info. If 500ms has happened and we haven't gotten anything
// we're either not connected right, or all of the data has been sent.
data := []string{}
for {
select {
case d := <-not:
data = append(data, d)
case <-time.After(250 * time.Millisecond):
goto DONE
}
}
DONE:
d := b.connectMessages()
for k, v := range d {
if v.String() != data[k] {
t.Fatalf("Should have recieved %s, got %s", d[k], v)
}
}
}