-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstash_test.go
161 lines (138 loc) · 8.37 KB
/
stash_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
157
158
159
160
161
package stash
import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"sync"
"testing"
"time"
)
type Message struct {
Data string `json:"data"`
}
type Log struct {
Action string `json:"action"`
Time time.Time `json:"time"`
Message Message `json:"message"`
}
func testConnection(t *testing.T, host string, port uint64, opts ...Option) {
_, err := Connect(host, port, opts...)
if err == nil {
t.Fatal(err)
}
return
}
func testWriteData(t *testing.T, host string, port uint64, opts ...Option) {
s, _ := Connect(host, port, opts...)
defer s.Close()
data := Log{
Action: "get_me",
Time: time.Now(),
Message: Message{
Data: "get me for me",
},
}
dataJSON, _ := json.Marshal(data)
_, err := s.Write(dataJSON)
if err != nil {
t.Fatal("Cannot write message to host")
}
return
}
func testWriteInvalidData(t *testing.T, host string, port uint64, opts ...Option) {
s, _ := Connect(host, port, opts...)
// early close connection before write data
s.Close()
_, err := s.Write(make([]byte, 0))
if err == nil {
t.Fatal("Cannot write message to host")
}
return
}
func testHandleConnection(conn net.Conn) {
defer conn.Close()
return
}
func testHandleSecureConnection(conn net.Conn) {
defer conn.Close()
return
}
func TestStash(t *testing.T) {
const host string = "localhost"
const listenPort uint64 = 5000
const secureListenPort = 5433
const invalidHost string = "localhostnet"
const invalidListenPort uint64 = 6000
// TCP handler
var tcpWg sync.WaitGroup
go func() {
defer tcpWg.Done()
l, err := net.Listen("tcp", fmt.Sprintf(":%d", listenPort))
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Test invalid host
testConnection(t, invalidHost, listenPort)
// Test invalid port
testConnection(t, host, invalidListenPort)
// Test write
opts := []Option{
SetKeepAlive(time.Minute * 1),
SetReadTimeout(time.Minute * 1),
SetWriteTimeout(time.Minute * 1),
}
testWriteData(t, host, listenPort, opts...)
testWriteInvalidData(t, host, listenPort, opts...)
for {
conn, err := l.Accept()
if err != nil {
t.Fatal(err)
return
}
testHandleConnection(conn)
return
}
}()
// Secure TCP handler
tcpWg.Add(1)
go func() {
defer tcpWg.Done()
cer, err := tls.X509KeyPair([]byte("-----BEGIN CERTIFICATE-----\nMIIFszCCA5ugAwIBAgIUBZNBpLJtsBRZMJ5pOE/5Rnm2454wDQYJKoZIhvcNAQEL\nBQAwaTELMAkGA1UEBhMCSUQxEzARBgNVBAgMCkphd2EgQmFyYXQxEDAOBgNVBAcM\nB0pha2FydGExJTAjBgNVBAoMHFBULiBUZWxla29tdW5pa2FzaSBJbmRvbmVzaWEx\nDDAKBgNVBAsMA0VXWjAeFw0yMjA4MDkwODA1MTVaFw0zMjA4MDYwODA1MTVaMGkx\nCzAJBgNVBAYTAklEMRMwEQYDVQQIDApKYXdhIEJhcmF0MRAwDgYDVQQHDAdKYWth\ncnRhMSUwIwYDVQQKDBxQVC4gVGVsZWtvbXVuaWthc2kgSW5kb25lc2lhMQwwCgYD\nVQQLDANFV1owggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDOdSF/7N11\nCYo/ZeBRfiKCd7rNzAOsU7CM2G+ruCfXxkmHfhIX3dHFG4H8OXipSGC1pF8NcQfB\naSNzM7bxew2NMTXhvOG/TqgNM6Kxgbt6URTWQRbiv3+3w5dG+2hCRuXyFh45ygGo\nZvNs5gYsI6sJ5lw2fNsQipgvoi1yAJVqRkINN6I0B7mTJfZ5hzrBUwu1PGPXs1nz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C3kCggEAWFnvs3r/MkQVycGU6d5ctMY2iFjDni91NYHCIC2lPanaVocZj0odCUaE\nOxivuP7sfRKsS9f6SNw2Hi3ZhHWpAWp65KUP0U2FG+3HLMz1b6geN8tFWchFV22Y\n7AMBwu/0g8zEx/N50/iKQO5wl2bS5TR8ScwZvC77wa2+2QqfQkpLSiDxYixbLoGa\n7J/qj5z+Crag1UMsefZ2Q7YWG9+/DYn69RTPnXXXm5KKoDQEQCtxptaRB3QePrlE\n8E906gS3Cua8MVMQX8Lh8Kgts8kP463ABmez7piufyc4oKWZJQTmN002J7YHRrWr\nRldjXOjCE69qcxZzyWxPCb5gedmuDA==\n-----END PRIVATE KEY-----\n"))
if err != nil {
t.Fatal(err)
}
tlsConfig := &tls.Config{Certificates: []tls.Certificate{cer}}
ln, err := tls.Listen("tcp", fmt.Sprintf(":%d", secureListenPort), tlsConfig)
if err != nil {
t.Fatal(err)
}
defer ln.Close()
// Test write data
tlsOpts := []Option{
SetTLS(true),
SetSkipVerify(true),
SetTLSConfig(tlsConfig),
}
tlsStrictOpts := []Option{
SetTLS(true),
SetSkipVerify(false),
SetTLSConfig(tlsConfig),
}
testConnection(t, host, secureListenPort, tlsOpts...)
testConnection(t, host, secureListenPort, tlsStrictOpts...)
testWriteData(t, host, secureListenPort, tlsOpts...)
testWriteData(t, host, secureListenPort, tlsStrictOpts...)
for {
conn, err := ln.Accept()
if err != nil {
t.Fatal(err)
return
}
testHandleSecureConnection(conn)
return
}
}()
tcpWg.Wait()
}