-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog_client_test.go
118 lines (92 loc) · 2.38 KB
/
syslog_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
package main
import (
"testing"
"github.com/AndyBowskill/syslog_client/message"
)
func Test_setupProtocol(t *testing.T) {
var tests = []struct {
protocol string
validProtocol string
}{
{"udp", message.UDP},
{"UDP", message.UDP},
{"tcp", message.TCP},
{"", message.UDP},
{"123 Test", message.UDP},
}
for _, tt := range tests {
protocol := setupProtocol(tt.protocol)
if protocol != tt.validProtocol {
t.Errorf("setupProtocol function with a %s didn't return with %s", tt.protocol, tt.validProtocol)
}
}
}
func Test_setupAddressPort(t *testing.T) {
var tests = []struct {
address string
validAddressPort string
}{
{"172.16.20.3", "172.16.20.3:514"},
{"", ":514"},
}
for _, tt := range tests {
addressPort := setupAddressPort(tt.address)
if addressPort != tt.validAddressPort {
t.Errorf("addressPort function with a valid %s didn't return with valid %s", tt.address, tt.validAddressPort)
}
}
}
func Test_calculatePrioty(t *testing.T) {
var tests = []struct {
severity uint
validPriority uint
}{
{3, 11},
{20, 15},
{0, 8},
}
for _, tt := range tests {
priority := calculatePriority(tt.severity)
if priority != tt.validPriority {
t.Errorf("calculatePriority function with a %d severity didn't return with %d priority", tt.severity, tt.validPriority)
}
}
}
func Test_setupClientValidArgs(t *testing.T) {
sm := message.NewSyslogMessage("udp", "192.168.48.10:514", "", 0)
conn, err := setupClient(sm)
defer closeClient(conn)
if err != nil {
t.Errorf("setupClient function did error with valid args.")
}
}
func Test_setupClientUnknownAddress(t *testing.T) {
sm := message.NewSyslogMessage("udp", ":514", "", 0)
conn, err := setupClient(sm)
defer closeClient(conn)
if err != nil {
t.Errorf("setupClient function didn't error with unknown address.")
}
}
func Test_setupClientInvalidArgs(t *testing.T) {
sm := message.NewSyslogMessage("", "", "", 0)
_, err := setupClient(sm)
if err == nil {
t.Errorf("setupClient function didn't error with invalid args.")
}
}
type TestWriter struct {
}
func (tw TestWriter) Write(p []byte) (n int, err error) {
n = 19
err = nil
return n, err
}
func Test_send(t *testing.T) {
tw := TestWriter{}
sm := message.NewSyslogMessage("udp", "192.168.48.10:514", "Error - Testing", 3)
err := send(sm, tw)
if err != nil {
t.Errorf("send function did error with valid args.")
}
}