-
Notifications
You must be signed in to change notification settings - Fork 14
/
client_test.go
172 lines (162 loc) · 5.06 KB
/
client_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
162
163
164
165
166
167
168
169
170
171
172
package simplesyslog
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"net"
"regexp"
"strconv"
"testing"
"time"
)
const host = "127.0.0.1:15140"
const tlsCRT = `-----BEGIN CERTIFICATE-----
MIICJzCCAZACCQCPGY+4vjNV0TANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJE
RTEPMA0GA1UECAwGSGVzc2VuMRIwEAYDVQQHDAlGcmFua2Z1cnQxEjAQBgNVBAoM
CUNvZGVoYXJkdDEQMA4GA1UEAwwHVGVzdGluZzAeFw0xODA0MjAwNzUwMzZaFw0x
OTA0MjEwNzUwMzZaMFgxCzAJBgNVBAYTAkRFMQ8wDQYDVQQIDAZIZXNzZW4xEjAQ
BgNVBAcMCUZyYW5rZnVydDESMBAGA1UECgwJQ29kZWhhcmR0MRAwDgYDVQQDDAdU
ZXN0aW5nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcHWCPUdd4VfxorTjk
5g/97HiAdcVn2kbDEVv2aI9HdSbcC9DC209DoaX4/7+3cEf3TwE5RKu9acf8tDue
W8tAWvKH4wW7hIHiipfhFisuQeLe5NgXGqY+bs+B5+A0C/rKTrGkHu8hpXjFPY2y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kL6rEOthAfh9nnPJp41zMk7jHz5zmRe6g==
-----END RSA PRIVATE KEY-----`
var tlsConfig *tls.Config
var messageRegex = regexp.MustCompile(`<133>[A-Z][a-z]{2} (([0-9]{2})|( [0-9])) [0-9]{2}:[0-9]{2}:[0-9]{2} testing\/127\.0\.0\.1 foo bar baz`)
func TestNewClient(t *testing.T) {
testNewClientTCP(t, false)
testNewClientTCP(t, true)
testNewClientUDP(t)
}
func testNewClientUDP(t *testing.T) {
t.Logf("testing udp")
serverAddr, err := net.ResolveUDPAddr("udp", host)
if err != nil {
t.Fatalf("could not resolve udp addr: %s", err)
}
conn, err := net.ListenUDP("udp", serverAddr)
if err != nil {
t.Fatalf("could not listen udp: %s", err)
}
if err := conn.SetReadDeadline(time.Now().Add(time.Second * 5)); err != nil {
t.Fatalf("could not set read deadline: %s", err)
}
defer conn.Close()
go func() {
/*
* Send the message 'foo bar baz' to the syslog server
*/
client, err := NewClient(ConnectionUDP, host, nil)
if err != nil {
t.Fatalf("could not initialize server: %s", err)
}
client.Hostname = "testing" // overwrite hostname for testing
defer client.Close()
if err := client.Send("foo bar baz", LOG_LOCAL0|LOG_NOTICE); err != nil {
t.Fatalf("could not send message: %s", err)
}
}()
buf := make([]byte, 1024)
n, _, err := conn.ReadFrom(buf)
if err != nil {
t.Fatalf("could not read udp: %s", err)
}
b := buf[:n]
if !messageRegex.MatchString(string(b)) {
t.Fatalf("wrong message: %s", string(b))
} else {
t.Logf("correct message: '%s'", string(b))
}
}
func testNewClientTCP(t *testing.T, useTLS bool) {
t.Logf("testing tcp with tls '%s'", strconv.FormatBool(useTLS))
var (
listener net.Listener
err error
)
if useTLS {
listener, err = tls.Listen("tcp", host, tlsConfig)
} else {
listener, err = net.Listen("tcp", host)
}
if err != nil {
t.Fatalf("could not listen: %s", err)
}
defer listener.Close()
go func() {
/*
* Send the message 'foo bar baz' to the syslog server
*/
connectionType := ConnectionTCP
if useTLS {
connectionType = ConnectionTLS
}
client, err := NewClient(connectionType, host, &tls.Config{InsecureSkipVerify: true})
if err != nil {
t.Fatalf("could not initialize server: %s", err)
}
client.Hostname = "testing" // overwrite hostname for testing
defer client.Close()
if err := client.Send("foo bar baz", LOG_LOCAL0|LOG_NOTICE); err != nil {
t.Fatalf("could not send message: %s", err)
}
}()
for {
conn, err := listener.Accept()
if err != nil {
t.Fatalf("could not accept connection: %s", err)
}
defer conn.Close()
b, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatalf("could not read all: %s", err)
}
if !messageRegex.MatchString(string(b)) {
t.Fatalf("wrong message: %s", string(b))
} else {
t.Logf("correct message: '%s'", string(b))
}
return
}
}
func init() {
pemCert, _ := pem.Decode([]byte(tlsCRT))
if pemCert == nil {
panic("no test tls certificate")
}
pemKey, _ := pem.Decode([]byte(tlsKEY))
if pemKey == nil {
panic("no test tls key")
}
privateKey, err := x509.ParsePKCS1PrivateKey(pemKey.Bytes)
if err != nil {
panic("invalid test tls key")
}
cert := &tls.Certificate{
Certificate: [][]byte{pemCert.Bytes},
PrivateKey: privateKey,
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{*cert},
MinVersion: tls.VersionTLS11,
}
}