dispatch/pkg/irc/conn_test.go

248 lines
5.3 KiB
Go
Raw Normal View History

2015-06-10 03:48:54 +00:00
package irc
import (
2015-06-11 02:57:52 +00:00
"bufio"
"bytes"
"crypto/tls"
2015-06-11 00:04:51 +00:00
"log"
"net"
2015-06-10 03:48:54 +00:00
"testing"
2015-06-11 00:04:51 +00:00
"time"
2015-06-10 03:48:54 +00:00
2016-03-01 00:51:26 +00:00
"github.com/stretchr/testify/assert"
2015-06-10 03:48:54 +00:00
)
2015-06-11 00:04:51 +00:00
var ircd *mockIrcd
func init() {
initTestServer()
}
func initTestServer() {
ircd = &mockIrcd{
conn: make(chan bool, 1),
connClosed: make(chan bool, 1),
}
ircd.start()
}
type mockIrcd struct {
conn chan bool
connClosed chan bool
}
func (i *mockIrcd) start() {
2015-06-11 02:57:52 +00:00
ln, err := net.Listen("tcp", "127.0.0.1:45678")
2015-06-11 00:04:51 +00:00
if err != nil {
log.Fatal(err)
}
2015-06-11 02:57:52 +00:00
cert, err := tls.X509KeyPair(testCert, testKey)
if err != nil {
log.Fatal(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
}
lnTLS, err := tls.Listen("tcp", "127.0.0.1:45679", tlsConfig)
if err != nil {
log.Fatal(err)
}
2015-06-11 00:04:51 +00:00
go i.accept(ln)
2015-06-11 02:57:52 +00:00
go i.accept(lnTLS)
2015-06-11 00:04:51 +00:00
}
func (i *mockIrcd) accept(ln net.Listener) {
for {
conn, err := ln.Accept()
if err != nil {
return
}
go i.handle(conn)
i.conn <- true
}
}
func (i *mockIrcd) handle(conn net.Conn) {
buf := make([]byte, 1024)
for {
_, err := conn.Read(buf)
if err != nil {
i.connClosed <- true
return
}
}
}
func TestConnect(t *testing.T) {
c := NewClient(&Config{
2020-05-23 07:42:20 +00:00
Host: "127.0.0.1",
Port: "45678",
})
c.Connect()
2016-01-13 19:05:14 +00:00
waitConnAndClose(t, c)
2015-06-11 00:04:51 +00:00
}
2015-06-11 02:57:52 +00:00
func TestConnectTLS(t *testing.T) {
c := NewClient(&Config{
2020-05-23 07:42:20 +00:00
Host: "127.0.0.1",
Port: "45679",
TLS: true,
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
})
c.Connect()
2016-01-13 19:05:14 +00:00
waitConnAndClose(t, c)
2015-06-11 02:57:52 +00:00
}
2015-06-11 00:04:51 +00:00
func TestConnectDefaultPorts(t *testing.T) {
c := NewClient(&Config{
2020-05-23 07:42:20 +00:00
Host: "127.0.0.1",
})
assert.Equal(t, "6667", c.Config.Port)
c = NewClient(&Config{
2020-05-23 07:42:20 +00:00
Host: "127.0.0.1",
TLS: true,
})
assert.Equal(t, "6697", c.Config.Port)
2015-06-11 00:04:51 +00:00
}
2015-06-10 03:48:54 +00:00
func TestWrite(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.write("test")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "test\r\n", <-out)
2015-06-10 03:48:54 +00:00
c.Write("test")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "test\r\n", <-out)
2015-06-10 03:48:54 +00:00
c.writef("test %d", 2)
2016-01-13 19:05:14 +00:00
assert.Equal(t, "test 2\r\n", <-out)
2015-06-10 03:48:54 +00:00
c.Writef("test %d", 2)
2016-01-13 19:05:14 +00:00
assert.Equal(t, "test 2\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
2015-06-11 00:04:51 +00:00
2015-06-11 02:57:52 +00:00
func TestRecv(t *testing.T) {
c := NewClient(&Config{})
2016-01-13 19:05:14 +00:00
conn := &mockConn{hook: make(chan string, 16)}
c.conn = conn
2015-06-11 02:57:52 +00:00
buf := &bytes.Buffer{}
buf.WriteString("CMD\r\n")
buf.WriteString("PING :test\r\n")
2017-04-12 23:59:16 +00:00
buf.WriteString("001 foo\r\n")
c.scan = bufio.NewScanner(buf)
2015-06-11 02:57:52 +00:00
2017-07-03 21:31:14 +00:00
c.sendRecv.Add(1)
2015-06-11 02:57:52 +00:00
go c.recv()
assert.Equal(t, "PONG :test\r\n", <-conn.hook)
assert.Equal(t, &Message{Command: "CMD"}, <-c.Messages)
assert.Equal(t, &Message{Command: PING, Params: []string{"test"}}, <-c.Messages)
assert.Equal(t, &Message{Command: RPL_WELCOME, Params: []string{"foo"}}, <-c.Messages)
2015-06-11 02:57:52 +00:00
}
2016-01-13 18:17:13 +00:00
func TestRecvTriggersReconnect(t *testing.T) {
c := NewClient(&Config{})
2016-01-13 19:05:14 +00:00
c.conn = &mockConn{}
c.scan = bufio.NewScanner(bytes.NewBufferString(""))
2015-06-11 02:57:52 +00:00
done := make(chan struct{})
ok := false
go func() {
2016-01-15 07:26:06 +00:00
c.sendRecv.Add(1)
2015-06-11 02:57:52 +00:00
c.recv()
_, ok = <-c.reconnect
close(done)
}()
select {
case <-done:
assert.False(t, ok)
return
case <-time.After(100 * time.Millisecond):
t.Error("Reconnect not triggered")
}
2016-01-13 18:17:13 +00:00
}
2015-06-11 02:57:52 +00:00
2015-06-11 00:04:51 +00:00
func TestClose(t *testing.T) {
c := NewClient(&Config{})
2016-01-15 07:26:06 +00:00
close(c.quit)
2015-06-11 00:04:51 +00:00
ok := false
done := make(chan struct{})
go func() {
_, ok = <-c.Messages
close(done)
}()
2016-01-15 07:26:06 +00:00
c.run()
2015-06-11 00:04:51 +00:00
select {
case <-done:
assert.False(t, ok)
return
case <-time.After(100 * time.Millisecond):
t.Error("Channels not closed")
}
}
2016-01-13 19:05:14 +00:00
func waitConnAndClose(t *testing.T, c *Client) {
2015-06-11 00:04:51 +00:00
done := make(chan struct{})
2015-06-11 02:57:52 +00:00
quit := make(chan struct{})
2015-06-11 00:04:51 +00:00
go func() {
<-ircd.conn
2015-06-11 02:57:52 +00:00
quit <- struct{}{}
2015-06-11 00:04:51 +00:00
<-ircd.connClosed
close(done)
}()
2015-06-11 02:57:52 +00:00
for {
select {
case <-done:
return
case <-quit:
assert.True(t, c.Connected())
c.Quit()
2015-06-11 00:04:51 +00:00
2015-06-11 02:57:52 +00:00
case <-time.After(500 * time.Millisecond):
t.Error("Took too long")
return
}
2015-06-11 00:04:51 +00:00
}
}
2015-06-11 02:57:52 +00:00
var testCert = []byte(`-----BEGIN CERTIFICATE-----
2018-12-21 00:53:35 +00:00
MIICEzCCAXygAwIBAgIQMIMChMLGrR+QvmQvpwAU6zANBgkqhkiG9w0BAQsFADAS
MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw
MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB
iQKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9SjY1bIw4
iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZBl2+XsDul
rKBxKKtD1rGxlG4LjncdabFn9gvLZad2bSysqz/qTAUStTvqJQIDAQABo2gwZjAO
BgNVHQ8BAf8EBAMCAqQwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUw
AwEB/zAuBgNVHREEJzAlggtleGFtcGxlLmNvbYcEfwAAAYcQAAAAAAAAAAAAAAAA
AAAAATANBgkqhkiG9w0BAQsFAAOBgQCEcetwO59EWk7WiJsG4x8SY+UIAA+flUI9
tyC4lNhbcF2Idq9greZwbYCqTTTr2XiRNSMLCOjKyI7ukPoPjo16ocHj+P3vZGfs
h1fIw3cSS2OolhloGw/XM6RWPWtPAlGykKLciQrBru5NAPvCMsb/I1DAceTiotQM
fblo6RBxUQ==
2015-06-11 02:57:52 +00:00
-----END CERTIFICATE-----`)
var testKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
2018-12-21 00:53:35 +00:00
MIICXgIBAAKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9
SjY1bIw4iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZB
l2+XsDulrKBxKKtD1rGxlG4LjncdabFn9gvLZad2bSysqz/qTAUStTvqJQIDAQAB
AoGAGRzwwir7XvBOAy5tM/uV6e+Zf6anZzus1s1Y1ClbjbE6HXbnWWF/wbZGOpet
3Zm4vD6MXc7jpTLryzTQIvVdfQbRc6+MUVeLKwZatTXtdZrhu+Jk7hx0nTPy8Jcb
uJqFk541aEw+mMogY/xEcfbWd6IOkp+4xqjlFLBEDytgbIECQQDvH/E6nk+hgN4H
qzzVtxxr397vWrjrIgPbJpQvBsafG7b0dA4AFjwVbFLmQcj2PprIMmPcQrooz8vp
jy4SHEg1AkEA/v13/5M47K9vCxmb8QeD/asydfsgS5TeuNi8DoUBEmiSJwma7FXY
fFUtxuvL7XvjwjN5B30pNEbc6Iuyt7y4MQJBAIt21su4b3sjXNueLKH85Q+phy2U
fQtuUE9txblTu14q3N7gHRZB4ZMhFYyDy8CKrN2cPg/Fvyt0Xlp/DoCzjA0CQQDU
y2ptGsuSmgUtWj3NM9xuwYPm+Z/F84K6+ARYiZ6PYj013sovGKUFfYAqVXVlxtIX
qyUBnu3X9ps8ZfjLZO7BAkEAlT4R5Yl6cGhaJQYZHOde3JEMhNRcVFMO8dJDaFeo
f9Oeos0UUothgiDktdQHxdNEwLjQf7lJJBzV+5OtwswCWA==
2015-06-11 02:57:52 +00:00
-----END RSA PRIVATE KEY-----`)