forked from spacemonkeygo/openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls_ext_test.go
120 lines (107 loc) · 2.78 KB
/
tls_ext_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
// Copyright (C) 2014 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openssl
import (
"bytes"
"io"
"sync"
"testing"
"unsafe"
)
var gFoundServerName bool = false
var gServerName string
var gCallbackData string = "some callback data"
func passThroughServername() func(ssl Conn, ad int, arg unsafe.Pointer) int {
x := func(ssl Conn, ad int, arg unsafe.Pointer) int {
cbData := (*string)(arg)
if *cbData != gCallbackData { //we should getthe callback data we set on the CTX
return 1
}
name := ssl.GetServerName()
if name == gServerName {
gFoundServerName = true
//here we'd normally do soemthing like get a CTX for the specific server name and
//set it on the conn.
} else {
gFoundServerName = false
}
return 0
}
return x
}
func TestTLSExtSNI(t *testing.T) {
//setup SNI On the CTX
server_conn, client_conn := NetPipe(t)
defer server_conn.Close()
defer client_conn.Close()
server, client := OpenSSLConstructor(t, server_conn, client_conn)
cconn := client.(*Conn)
sconn := server.(*Conn)
ctx := (*sconn).ctx
//setup SNI On the CTX
rc := ctx.SetTlsExtServerNameCallback(passThroughServername(), unsafe.Pointer(&gCallbackData))
if rc != 0 {
t.Fatal("Expected 0 from ctx.SetTlsExtServerNameCallback, but got %d", rc)
}
data := "first test string\n"
host := "test-host"
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
gServerName = host
err := cconn.SetTlsExtHostName(host)
if err != nil {
t.Fatal(err)
}
err = client.Handshake()
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(client, bytes.NewReader([]byte(data)))
if err != nil {
t.Fatal(err)
}
err = client.Close()
if err != nil {
t.Fatal(err)
}
}()
go func() {
defer wg.Done()
err := server.Handshake()
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(make([]byte, 0, len(data)))
_, err = io.CopyN(buf, server, int64(len(data)))
if err != nil {
t.Fatal(err)
}
if string(buf.Bytes()) != data {
t.Fatal("mismatched data")
}
err = server.Close()
if err != nil {
t.Fatal(err)
}
}()
wg.Wait()
if gFoundServerName == false {
t.Fatal("Expected gFoundServerName to be set to true")
}
if gServerName != host {
t.Fatal("Expected gServerName to be '%s', but it was '%s'", host, gServerName)
}
}