dispatch/irc/conn_test.go

237 lines
4.8 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) {
2016-01-13 19:05:14 +00:00
c := testClient()
2015-06-11 00:04:51 +00:00
c.Connect("127.0.0.1:45678")
assert.Equal(t, c.Host, "127.0.0.1")
assert.Equal(t, c.Server, "127.0.0.1:45678")
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) {
2016-01-13 19:05:14 +00:00
c := testClient()
2015-06-11 02:57:52 +00:00
c.TLS = true
c.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
2015-06-11 02:57:52 +00:00
c.Connect("127.0.0.1:45679")
assert.Equal(t, c.Host, "127.0.0.1")
assert.Equal(t, c.Server, "127.0.0.1:45679")
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) {
2016-01-13 19:05:14 +00:00
c := testClient()
2015-06-11 00:04:51 +00:00
c.Connect("127.0.0.1")
assert.Equal(t, "127.0.0.1:6667", c.Server)
2016-01-13 19:05:14 +00:00
c = testClient()
2015-06-11 00:04:51 +00:00
c.TLS = true
c.Connect("127.0.0.1")
assert.Equal(t, "127.0.0.1:6697", c.Server)
}
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) {
2016-01-13 19:05:14 +00:00
c := testClient()
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")
buf.WriteString("001\r\n")
c.reader = bufio.NewReader(buf)
c.ready.Add(1)
2016-01-15 07:26:06 +00:00
c.sendRecv.Add(2)
2016-01-13 19:05:14 +00:00
go c.send()
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)
}
2016-01-13 18:17:13 +00:00
func TestRecvTriggersReconnect(t *testing.T) {
2016-01-13 19:05:14 +00:00
c := testClient()
c.conn = &mockConn{}
2015-06-11 02:57:52 +00:00
c.ready.Add(1)
2016-01-13 19:05:14 +00:00
c.reader = bufio.NewReader(&bytes.Buffer{})
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) {
2016-01-13 19:05:14 +00:00
c := testClient()
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-----
2017-02-17 01:09:50 +00:00
MIIB0zCCAX2gAwIBAgIJAI/M7BYjwB+uMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTIwOTEyMjE1MjAyWhcNMTUwOTEyMjE1MjAyWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANLJ
hPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wok/4xIA+ui35/MmNa
rtNuC+BdZ1tMuVCPFZcCAwEAAaNQME4wHQYDVR0OBBYEFJvKs8RfJaXTH08W+SGv
zQyKn0H8MB8GA1UdIwQYMBaAFJvKs8RfJaXTH08W+SGvzQyKn0H8MAwGA1UdEwQF
MAMBAf8wDQYJKoZIhvcNAQEFBQADQQBJlffJHybjDGxRMqaRmDhX0+6v02TUKZsW
r5QuVbpQhH6u+0UgcW0jp9QwpxoPTLTWGXEWBBBurxFwiCBhkQ+V
2015-06-11 02:57:52 +00:00
-----END CERTIFICATE-----`)
var testKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
2017-02-17 01:09:50 +00:00
MIIBOwIBAAJBANLJhPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wo
k/4xIA+ui35/MmNartNuC+BdZ1tMuVCPFZcCAwEAAQJAEJ2N+zsR0Xn8/Q6twa4G
6OB1M1WO+k+ztnX/1SvNeWu8D6GImtupLTYgjZcHufykj09jiHmjHx8u8ZZB/o1N
MQIhAPW+eyZo7ay3lMz1V01WVjNKK9QSn1MJlb06h/LuYv9FAiEA25WPedKgVyCW
SmUwbPw8fnTcpqDWE3yTO3vKcebqMSsCIBF3UmVue8YU3jybC3NxuXq3wNm34R8T
xVLHwDXh/6NJAiEAl2oHGGLz64BuAfjKrqwz7qMYr9HCLIe/YsoWq/olzScCIQDi
D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g==
2015-06-11 02:57:52 +00:00
-----END RSA PRIVATE KEY-----`)