-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver_test.go
105 lines (88 loc) · 2.39 KB
/
server_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
package epp
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"math/big"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestServer(t *testing.T) {
cert := generateCertificate()
didStart := make(chan struct{})
srv := Server{
Addr: ":9889",
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
SessionConfig: SessionConfig{
IdleTimeout: 10 * time.Minute,
SessionTimeout: 10 * time.Minute,
Handler: func(s *Session, in []byte) ([]byte, error) {
data := fmt.Sprintf("%s", string(in))
return []byte(data), nil
},
Greeting: func(s *Session) ([]byte, error) {
return []byte("hello"), nil
},
},
OnStarteds: []func(){
func() {
didStart <- struct{}{}
},
},
}
defer srv.Stop()
go func() {
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}()
<-didStart
client := &Client{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
greeting, err := client.Connect(":9889")
require.Nil(t, err)
assert.Equal(t, string(greeting), "hello")
for i := 0; i < 5; i++ {
data := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
<hello>message %d</hello>
</epp>`, i)
response, err := client.Send([]byte(data))
assert.Nil(t, err)
assert.Equal(t, fmt.Sprintf("%s", data), string(response))
}
}
func generateCertificate() tls.Certificate {
key, _ := rsa.GenerateKey(rand.Reader, 2048)
cert := &x509.Certificate{
SerialNumber: big.NewInt(1653),
Subject: pkix.Name{
CommonName: "epp.example.test",
Organization: []string{"Simple Server Test"},
Country: []string{"SE"},
Locality: []string{"Stockholm"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(0, 0, 1),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
certificate, _ := x509.CreateCertificate(rand.Reader, cert, cert, key.Public(), key)
return tls.Certificate{
Certificate: [][]byte{certificate},
PrivateKey: key,
}
}