forked from cloudfoundry-attic/gonats
/
handshake_test.go
70 lines (53 loc) · 1.09 KB
/
handshake_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
package nats
import (
"fmt"
"github.com/cloudfoundry/gonats/test"
"net"
"sync"
"testing"
)
func testHandshake(t *testing.T, user, pass string, ssl bool) {
c, s := net.Pipe()
srv := test.NewTestServer(t, s)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
h := Handshake{
Username: user,
Password: pass,
}
_, e := h.Handshake(c)
if e != nil {
t.Error(e)
}
wg.Done()
}()
var p string
if ssl {
p = "true"
} else {
p = "false"
}
p = fmt.Sprintf("INFO {\"ssl_required\":%s}\r\n", p)
srv.AssertWrite(p)
if ssl {
srv.StartTLS()
}
p = fmt.Sprintf("CONNECT {\"verbose\":true,\"pedantic\":true,\"user\":\"%s\",\"pass\":\"%s\"}\r\n", user, pass)
srv.AssertRead(p)
p = fmt.Sprintf("+OK\r\n")
srv.AssertWrite(p)
wg.Wait()
}
func TestHandshakeWithoutAuth(t *testing.T) {
testHandshake(t, "", "", false)
}
func TestHandshakeWithAuth(t *testing.T) {
testHandshake(t, "john", "doe", false)
}
func TestHandshakeWithoutAuthWithSsl(t *testing.T) {
testHandshake(t, "", "", true)
}
func TestHandshakeWithAuthWithSsl(t *testing.T) {
testHandshake(t, "john", "doe", true)
}