-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnutls_test.go
156 lines (132 loc) · 3.51 KB
/
gnutls_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package dtls_gnutls_test
import (
"net"
"sync"
"testing"
"time"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
dtls_gnutls "github.com/nttdots/go-dtls"
)
func TestNewDTLSClientContext(t *testing.T) {
log.Print("-- TestNewDTLSClientContext")
ctx, err := dtls_gnutls.NewDTLSClientContext("ca-cert.pem", "client-cert.pem", "client-key.pem")
if err != nil {
t.Error(err)
return
}
log.Infof("ctx: %+v", ctx)
defer ctx.Close()
session, err := ctx.Connect("localhost:11112")
if err != nil {
t.Error(err)
return
}
defer session.Close()
send_string := "client abcde"
expected := len(send_string)
n, err := session.Write([]byte(send_string))
if err != nil {
t.Errorf("send data error. %s", err)
}
if n != expected {
t.Errorf("got %v, want %v", n, expected)
}
buf := make([]byte, 1500)
n, err = session.Read(buf)
if err != nil {
t.Errorf("receive data error. %s", err)
}
if n != expected {
t.Errorf("got %v, want %v", n, expected)
}
if string(buf[:n]) != send_string {
t.Errorf("got %v, want %v", string(buf[:n]), send_string)
}
}
func TestNewDTLSServerContext(t *testing.T) {
log.Print("-- TestNewDTLSServerContext")
ctx, err := dtls_gnutls.NewDTLSServerContext("ca-cert.pem", "crl.pem", "server-cert.pem", "server-key.pem")
if err != nil {
t.Error(err)
}
dataCh := make(chan net.Conn, 1)
errCh := make(chan error, 1)
sctx, err := ctx.Listen("localhost:5557", dataCh, errCh)
if err != nil {
t.Error(err)
}
go func() {
for {
select {
case conn := <-dataCh:
go func() {
log.Infof("TestNewDTLSServerContext -- connected. client: %s", conn.RemoteAddr().String())
dtlsConn, ok := conn.(dtls_gnutls.DTLSServerConn)
if !ok {
t.Errorf("connection type error, %T", conn)
}
if ok && dtlsConn.GetClientCN() != "client.sample.example.com" {
t.Errorf("CN read error, cn:%s", dtlsConn.GetClientCN())
}
data := make([]byte, 1500)
n, err := conn.Read(data)
if err != nil {
t.Errorf("connection read error, %s", err.Error())
}
if n != len("test-data") {
t.Errorf("receive data len error. want: %d, got: %d", len("test-data"), n)
}
if string(data[:n]) != "test-data" {
t.Errorf("receive data error. want: %s, got: %s", "test-data", string(data[:n]))
}
conn.Write([]byte("server-send"))
conn.Close()
}()
case err := <-errCh:
t.Errorf("errch: %s", err)
}
}
}()
defer sctx.Close()
// Waiting for the server to complete the boot sequence.
time.Sleep(100 * time.Millisecond)
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(wg_ref *sync.WaitGroup, counter int) {
var clientctx *dtls_gnutls.DTLSCTX
var clientSession *dtls_gnutls.DTLS_CLIENT_SESSION
var n int
var buffer []byte
var err error
clientctx, err = dtls_gnutls.NewDTLSClientContext("ca-cert.pem", "client-cert.pem", "client-key.pem")
if err != nil {
goto Error
}
defer clientctx.Close()
clientSession, err = clientctx.Connect("localhost:5557")
if err != nil {
goto Error
}
defer clientSession.Close()
clientSession.Write([]byte("test-data"))
buffer = make([]byte, 1500)
n, _ = clientSession.Read(buffer)
if string(buffer[:n]) != "server-send" {
err = errors.New(fmt.Sprintf("receive data error. want: %s, got: %s", "server-send", string(buffer[:n])))
goto Error
}
wg_ref.Done()
log.Infof("client %d finish.", counter)
return
Error:
wg_ref.Done()
t.Error(err)
}(&wg, i)
}
log.Infof("wait.")
wg.Wait()
log.Infof("done.")
}