-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener_test.go
203 lines (155 loc) · 5.45 KB
/
listener_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package proxyprotocol_test
import (
"errors"
"net"
"reflect"
"testing"
"github.com/c0va23/go-proxyprotocol"
"github.com/golang/mock/gomock"
)
func TestNewListener(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
rawListener := NewMockListener(mockCtrl)
builder := NewMockHeaderParserBuilder(mockCtrl)
listener := proxyprotocol.NewListener(rawListener, builder)
expectedListener := proxyprotocol.Listener{
Listener: rawListener,
HeaderParserBuilder: builder,
}
if !reflect.DeepEqual(expectedListener, listener) {
t.Errorf("NewListener return unexpected result. Expected %#v, got %#v.", expectedListener, listener)
}
}
func TestListener_WithLogger(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
rawListener := NewMockListener(mockCtrl)
builder := NewMockHeaderParserBuilder(mockCtrl)
listener := proxyprotocol.NewListener(rawListener, builder)
logger := proxyprotocol.LoggerFunc(t.Logf)
withLogger := listener.WithLogger(logger)
if reflect.ValueOf(withLogger.Logger).Pointer() != reflect.ValueOf(logger).Pointer() {
t.Errorf("Unexpected LoggerFn")
}
}
func TestListener_WithSourceChecker(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
rawListener := NewMockListener(mockCtrl)
builder := NewMockHeaderParserBuilder(mockCtrl)
listener := proxyprotocol.NewListener(rawListener, builder)
sourceChecker := func(net.Addr) (bool, error) {
return false, nil
}
withSourceChecker := listener.WithSourceChecker(sourceChecker)
if reflect.ValueOf(withSourceChecker.SourceChecker).Pointer() != reflect.ValueOf(sourceChecker).Pointer() {
t.Errorf("Unexpected SourceChecker")
}
}
func TestListener_WithHeaderParserBuilder(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
rawListener := NewMockListener(mockCtrl)
builder := NewMockHeaderParserBuilder(mockCtrl)
listener := proxyprotocol.NewListener(rawListener, builder)
otherBuilder := NewMockHeaderParserBuilder(mockCtrl)
withHeaderParserBuilder := listener.WithHeaderParserBuilder(otherBuilder)
if withHeaderParserBuilder.HeaderParserBuilder != otherBuilder {
t.Errorf(
"Unexpected HeaderParserBuilder. Expect %v, got %v.",
otherBuilder,
withHeaderParserBuilder.HeaderParserBuilder,
)
}
}
func TestListener_Accept(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
rawListener := NewMockListener(mockCtrl)
builder := NewMockHeaderParserBuilder(mockCtrl)
listener := proxyprotocol.NewListener(rawListener, builder)
t.Run("when raw listener accept return error", func(t *testing.T) {
acceptErr := errors.New("accept error")
rawListener.EXPECT().Accept().Return(nil, acceptErr)
conn, err := listener.Accept()
if conn != nil {
t.Errorf("Expect nil conn, but got %s", conn)
}
if err != acceptErr {
t.Errorf("Unexpected error. Expect %s, got %s", acceptErr, err)
}
})
t.Run("when raw listener accept return connection", func(t *testing.T) {
rawConn := NewMockConn(mockCtrl)
rawListener.EXPECT().Accept().Return(rawConn, nil).AnyTimes()
remoteAddr := net.TCPAddr{
IP: net.IPv4(1, 2, 3, 4),
Port: 12345,
}
rawConn.EXPECT().RemoteAddr().Return(&remoteAddr).AnyTimes()
logger := proxyprotocol.FallbackLogger{Logger: listener.Logger}
headerParser := NewMockHeaderParser(mockCtrl)
t.Run("listener not have SourceChecker", func(t *testing.T) {
builder.EXPECT().Build(logger).Return(headerParser)
conn, err := listener.Accept()
if err != nil {
t.Errorf("Expect nil error, but got %s", err)
}
trustedAddr := true
expectedConn := proxyprotocol.NewConn(rawConn, logger, headerParser, trustedAddr)
if !reflect.DeepEqual(expectedConn, conn) {
t.Errorf("Unexpected connection %s", conn)
}
})
t.Run("listener have SourceChecker", func(t *testing.T) {
var sourceCheckErr error
var sourceCheckResult bool
sourceChecker := func(net.Addr) (bool, error) {
return sourceCheckResult, sourceCheckErr
}
listener = listener.WithSourceChecker(sourceChecker)
t.Run("source checker return error", func(t *testing.T) {
sourceCheckErr = errors.New("source check err")
sourceCheckResult = false
conn, err := listener.Accept()
if err != sourceCheckErr {
t.Errorf("Unexpected error %s", err)
}
if conn != nil {
t.Errorf("Unexpeced connection %s", conn)
}
})
t.Run("source checker return false", func(t *testing.T) {
sourceCheckErr = nil
sourceCheckResult = false
builder.EXPECT().Build(logger).Return(headerParser)
conn, err := listener.Accept()
if err != nil {
t.Errorf("Unexpected error %s", err)
}
trustedAddr := false
expectedConn := proxyprotocol.NewConn(rawConn, logger, headerParser, trustedAddr)
if !reflect.DeepEqual(expectedConn, conn) {
t.Errorf("Unexpected connection %s", conn)
}
})
t.Run("source checker return true", func(t *testing.T) {
sourceCheckErr = nil
sourceCheckResult = true
logger := proxyprotocol.FallbackLogger{Logger: listener.Logger}
headerParser := NewMockHeaderParser(mockCtrl)
builder.EXPECT().Build(logger).Return(headerParser)
conn, err := listener.Accept()
if err != nil {
t.Errorf("Unexpected error %s", err)
}
trustedAddr := true
expectedConn := proxyprotocol.NewConn(rawConn, logger, headerParser, trustedAddr)
if !reflect.DeepEqual(expectedConn, conn) {
t.Errorf("Unexpected connection %s", conn)
}
})
})
})
}