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
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/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.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")
|
|
|
|
waitConnAndClose(t)
|
|
|
|
initTestClient()
|
|
|
|
}
|
|
|
|
|
2015-06-11 02:57:52 +00:00
|
|
|
func TestConnectTLS(t *testing.T) {
|
|
|
|
c.TLS = true
|
|
|
|
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")
|
|
|
|
waitConnAndClose(t)
|
|
|
|
initTestClient()
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:04:51 +00:00
|
|
|
func TestConnectDefaultPorts(t *testing.T) {
|
|
|
|
c.Connect("127.0.0.1")
|
|
|
|
assert.Equal(t, "127.0.0.1:6667", c.Server)
|
|
|
|
initTestClient()
|
|
|
|
|
|
|
|
c.TLS = true
|
|
|
|
c.Connect("127.0.0.1")
|
|
|
|
assert.Equal(t, "127.0.0.1:6697", c.Server)
|
|
|
|
initTestClient()
|
|
|
|
}
|
|
|
|
|
2015-06-10 03:48:54 +00:00
|
|
|
func TestWrite(t *testing.T) {
|
|
|
|
c.write("test")
|
|
|
|
assert.Equal(t, "test\r\n", <-conn.hook)
|
|
|
|
c.Write("test")
|
|
|
|
assert.Equal(t, "test\r\n", <-conn.hook)
|
|
|
|
c.writef("test %d", 2)
|
|
|
|
assert.Equal(t, "test 2\r\n", <-conn.hook)
|
|
|
|
c.Writef("test %d", 2)
|
|
|
|
assert.Equal(t, "test 2\r\n", <-conn.hook)
|
|
|
|
}
|
2015-06-11 00:04:51 +00:00
|
|
|
|
2015-06-11 02:57:52 +00:00
|
|
|
func TestRecv(t *testing.T) {
|
|
|
|
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)
|
|
|
|
close(c.quit)
|
|
|
|
go c.recv()
|
|
|
|
|
|
|
|
assert.Equal(t, "PONG :test\r\n", <-conn.hook)
|
|
|
|
assert.Equal(t, &Message{Command: "CMD"}, <-c.Messages)
|
|
|
|
initTestClient()
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:06:55 +00:00
|
|
|
func TestRecvRecoversPanic(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
assert.Nil(t, recover())
|
|
|
|
}()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte("CMD\r\n"))
|
|
|
|
c.reader = bufio.NewReader(buf)
|
|
|
|
close(c.Messages)
|
|
|
|
c.recv()
|
|
|
|
|
|
|
|
c.Messages = make(chan *Message, 32)
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:17:13 +00:00
|
|
|
func TestRecvTriggersReconnect(t *testing.T) {
|
2015-06-11 02:57:52 +00:00
|
|
|
c.reader = bufio.NewReader(&bytes.Buffer{})
|
|
|
|
c.ready.Add(1)
|
|
|
|
done := make(chan struct{})
|
|
|
|
ok := false
|
|
|
|
go func() {
|
|
|
|
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) {
|
|
|
|
defer initTestClient()
|
|
|
|
c.close()
|
|
|
|
ok := false
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
_, ok = <-c.out
|
|
|
|
_, ok = <-c.Messages
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
assert.False(t, ok)
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Error("Channels not closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitConnAndClose(t *testing.T) {
|
|
|
|
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-----
|
|
|
|
MIIBdzCCASOgAwIBAgIBADALBgkqhkiG9w0BAQUwEjEQMA4GA1UEChMHQWNtZSBD
|
|
|
|
bzAeFw03MDAxMDEwMDAwMDBaFw00OTEyMzEyMzU5NTlaMBIxEDAOBgNVBAoTB0Fj
|
|
|
|
bWUgQ28wWjALBgkqhkiG9w0BAQEDSwAwSAJBAN55NcYKZeInyTuhcCwFMhDHCmwa
|
|
|
|
IUSdtXdcbItRB/yfXGBhiex00IaLXQnSU+QZPRZWYqeTEbFSgihqi1PUDy8CAwEA
|
|
|
|
AaNoMGYwDgYDVR0PAQH/BAQDAgCkMBMGA1UdJQQMMAoGCCsGAQUFBwMBMA8GA1Ud
|
|
|
|
EwEB/wQFMAMBAf8wLgYDVR0RBCcwJYILZXhhbXBsZS5jb22HBH8AAAGHEAAAAAAA
|
|
|
|
AAAAAAAAAAAAAAEwCwYJKoZIhvcNAQEFA0EAAoQn/ytgqpiLcZu9XKbCJsJcvkgk
|
|
|
|
Se6AbGXgSlq+ZCEVo0qIwSgeBqmsJxUu7NCSOwVJLYNEBO2DtIxoYVk+MA==
|
|
|
|
-----END CERTIFICATE-----`)
|
|
|
|
|
|
|
|
var testKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIIBPAIBAAJBAN55NcYKZeInyTuhcCwFMhDHCmwaIUSdtXdcbItRB/yfXGBhiex0
|
|
|
|
0IaLXQnSU+QZPRZWYqeTEbFSgihqi1PUDy8CAwEAAQJBAQdUx66rfh8sYsgfdcvV
|
|
|
|
NoafYpnEcB5s4m/vSVe6SU7dCK6eYec9f9wpT353ljhDUHq3EbmE4foNzJngh35d
|
|
|
|
AekCIQDhRQG5Li0Wj8TM4obOnnXUXf1jRv0UkzE9AHWLG5q3AwIhAPzSjpYUDjVW
|
|
|
|
MCUXgckTpKCuGwbJk7424Nb8bLzf3kllAiA5mUBgjfr/WtFSJdWcPQ4Zt9KTMNKD
|
|
|
|
EUO0ukpTwEIl6wIhAMbGqZK3zAAFdq8DD2jPx+UJXnh0rnOkZBzDtJ6/iN69AiEA
|
|
|
|
1Aq8MJgTaYsDQWyU/hDq5YkDJc9e9DSCvUIzqxQWMQE=
|
|
|
|
-----END RSA PRIVATE KEY-----`)
|