-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
conn_test.go
192 lines (150 loc) · 4.06 KB
/
conn_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
package irc_test
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/irc.v4"
)
var errorWriterErr = errors.New("errorWriter: error")
type errorWriter struct{}
func (ew *errorWriter) Write([]byte) (int, error) {
return 0, errorWriterErr
}
type nopCloser struct {
io.Reader
io.Writer
}
func newNopCloser(inner io.ReadWriter) *nopCloser {
return &nopCloser{
Reader: inner,
Writer: inner,
}
}
func (nc *nopCloser) Close() error {
return nil
}
var _ io.ReadWriteCloser = (*nopCloser)(nil)
type readWriteCloser struct {
io.Reader
io.Writer
io.Closer
}
type testReadWriteCloser struct {
client *bytes.Buffer
server *bytes.Buffer
}
func newTestReadWriteCloser() *testReadWriteCloser {
return &testReadWriteCloser{
client: &bytes.Buffer{},
server: &bytes.Buffer{},
}
}
func (t *testReadWriteCloser) Read(p []byte) (int, error) {
return t.server.Read(p)
}
func (t *testReadWriteCloser) Write(p []byte) (int, error) {
return t.client.Write(p)
}
func testReadMessage(t *testing.T, c *irc.Conn) *irc.Message {
t.Helper()
m, err := c.ReadMessage()
assert.NoError(t, err)
return m
}
func testLines(t *testing.T, rwc *testReadWriteCloser, expected []string) {
t.Helper()
lines := strings.Split(rwc.client.String(), "\r\n")
var line, clientLine string
for len(expected) > 0 {
line, expected = expected[0], expected[1:]
clientLine, lines = lines[0], lines[1:]
assert.Equal(t, line, clientLine)
}
for _, line := range lines {
assert.Equal(t, "", strings.TrimSpace(line), "Extra non-empty lines")
}
// Reset the contents
rwc.client.Reset()
rwc.server.Reset()
}
func TestWriteMessageError(t *testing.T) {
t.Parallel()
rw := &readWriteCloser{
&bytes.Buffer{},
&errorWriter{},
nil,
}
c := irc.NewConn(rw)
err := c.WriteMessage(irc.MustParseMessage("PING :hello world"))
assert.Error(t, err)
err = c.Writef("PING :hello world")
assert.Error(t, err)
err = c.Write("PING :hello world")
assert.Error(t, err)
}
func TestConn(t *testing.T) {
t.Parallel()
rwc := newTestReadWriteCloser()
c := irc.NewConn(rwc)
// Test writing a message
m := &irc.Message{Prefix: &irc.Prefix{}, Command: "PING", Params: []string{"Hello World"}}
err := c.WriteMessage(m)
assert.NoError(t, err)
testLines(t, rwc, []string{
"PING :Hello World",
})
// Test with Writef
err = c.Writef("PING :%s", "Hello World")
assert.NoError(t, err)
testLines(t, rwc, []string{
"PING :Hello World",
})
m = irc.MustParseMessage("PONG :Hello World")
rwc.server.WriteString(m.String() + "\r\n")
m2 := testReadMessage(t, c)
assert.EqualValues(t, m, m2, "Message returned by client did not match input")
// Test welcome message
m = irc.MustParseMessage("001 test_nick")
rwc.server.WriteString(m.String() + "\r\n")
m2 = testReadMessage(t, c)
assert.EqualValues(t, m, m2, "Message returned by client did not match input")
rwc.server.WriteString(":invalid_message\r\n")
_, err = c.ReadMessage()
assert.Equal(t, irc.ErrMissingDataAfterPrefix, err)
// Ensure empty messages are ignored
m = irc.MustParseMessage("001 test_nick")
rwc.server.WriteString("\r\n" + m.String() + "\r\n")
m2 = testReadMessage(t, c)
assert.EqualValues(t, m, m2, "Message returned by client did not match input")
// This is an odd one... if there wasn't any output, it'll hit
// EOF, so we expect an error here so we can test an error
// condition.
_, err = c.ReadMessage()
assert.Equal(t, io.EOF, err, "Didn't get expected EOF")
}
func TestDebugCallback(t *testing.T) {
t.Parallel()
var readerHit, writerHit bool
rwc := newTestReadWriteCloser()
c := irc.NewConn(rwc)
c.Writer.DebugCallback = func(string) {
writerHit = true
}
c.Reader.DebugCallback = func(string) {
readerHit = true
}
m := &irc.Message{Prefix: &irc.Prefix{}, Command: "PING", Params: []string{"Hello World"}}
err := c.WriteMessage(m)
assert.NoError(t, err)
testLines(t, rwc, []string{
"PING :Hello World",
})
m = irc.MustParseMessage("PONG :Hello World")
rwc.server.WriteString(m.String() + "\r\n")
testReadMessage(t, c)
assert.True(t, readerHit)
assert.True(t, writerHit)
}