Add more IRC client tests

This commit is contained in:
Ken-Håvard Lieng 2015-06-11 04:57:52 +02:00
parent 4f0ad5e6d9
commit 6a5680e299
4 changed files with 148 additions and 26 deletions

View File

@ -59,10 +59,6 @@ func (c *Client) Connected() bool {
return c.connected
}
func (c *Client) Pass(password string) {
c.write("PASS " + password)
}
func (c *Client) Nick(nick string) {
c.Write("NICK " + nick)
@ -71,10 +67,6 @@ func (c *Client) Nick(nick string) {
c.lock.Unlock()
}
func (c *Client) User(username, realname string) {
c.writef("USER %s 0 * :%s", username, realname)
}
func (c *Client) Oper(name, password string) {
c.Write("OPER " + name + " " + password)
}
@ -89,7 +81,9 @@ func (c *Client) Quit() {
c.write("QUIT")
}
close(c.quit)
c.lock.Lock()
c.conn.Close()
c.lock.Unlock()
}()
}
@ -128,3 +122,23 @@ func (c *Client) Whois(nick string) {
func (c *Client) Away(message string) {
c.Write("AWAY :" + message)
}
func (c *Client) writePass(password string) {
c.write("PASS " + password)
}
func (c *Client) writeNick(nick string) {
c.write("NICK " + nick)
}
func (c *Client) writeUser(username, realname string) {
c.writef("USER %s 0 * :%s", username, realname)
}
func (c *Client) register() {
if c.Password != "" {
c.writePass(c.Password)
}
c.writeNick(c.nick)
c.writeUser(c.Username, c.Realname)
}

View File

@ -16,7 +16,7 @@ func init() {
func initTestClient() {
c = NewClient("test", "testing")
conn = &mockConn{hook: make(chan string, 1)}
conn = &mockConn{hook: make(chan string, 3)}
c.conn = conn
go c.send()
}
@ -36,7 +36,7 @@ func (c *mockConn) Close() error {
}
func TestPass(t *testing.T) {
c.Pass("pass")
c.writePass("pass")
assert.Equal(t, "PASS pass\r\n", <-conn.hook)
}
@ -44,10 +44,13 @@ func TestNick(t *testing.T) {
c.Nick("test2")
assert.Equal(t, "test2", c.GetNick())
assert.Equal(t, "NICK test2\r\n", <-conn.hook)
c.writeNick("nick")
assert.Equal(t, "NICK nick\r\n", <-conn.hook)
}
func TestUser(t *testing.T) {
c.User("user", "rn")
c.writeUser("user", "rn")
assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
}
@ -121,3 +124,18 @@ func TestAway(t *testing.T) {
c.Away("not here")
assert.Equal(t, "AWAY :not here\r\n", <-conn.hook)
}
func TestRegister(t *testing.T) {
c.nick = "nick"
c.Username = "user"
c.Realname = "rn"
c.register()
assert.Equal(t, "NICK nick\r\n", <-conn.hook)
assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
c.Password = "pass"
c.register()
assert.Equal(t, "PASS pass\r\n", <-conn.hook)
assert.Equal(t, "NICK nick\r\n", <-conn.hook)
assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
}

View File

@ -45,6 +45,9 @@ func (c *Client) writef(format string, a ...interface{}) {
}
func (c *Client) connect() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.TLS {
if c.TLSConfig == nil {
c.TLSConfig = &tls.Config{InsecureSkipVerify: true}
@ -63,17 +66,10 @@ func (c *Client) connect() error {
}
}
c.lock.Lock()
c.connected = true
c.lock.Unlock()
c.reader = bufio.NewReader(c.conn)
if c.Password != "" {
c.Pass(c.Password)
}
c.write("NICK " + c.nick)
c.User(c.Username, c.Realname)
c.register()
c.ready.Add(1)
go c.send()

View File

@ -1,6 +1,9 @@
package irc
import (
"bufio"
"bytes"
"crypto/tls"
"log"
"net"
"testing"
@ -29,11 +32,27 @@ type mockIrcd struct {
}
func (i *mockIrcd) start() {
ln, err := net.Listen("tcp", ":45678")
ln, err := net.Listen("tcp", "127.0.0.1:45678")
if err != nil {
log.Fatal(err)
}
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)
}
go i.accept(ln)
go i.accept(lnTLS)
}
func (i *mockIrcd) accept(ln net.Listener) {
@ -66,6 +85,15 @@ func TestConnect(t *testing.T) {
initTestClient()
}
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()
}
func TestConnectDefaultPorts(t *testing.T) {
c.Connect("127.0.0.1")
assert.Equal(t, "127.0.0.1:6667", c.Server)
@ -88,6 +116,43 @@ func TestWrite(t *testing.T) {
assert.Equal(t, "test 2\r\n", <-conn.hook)
}
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()
}
func TestRecvTriggersReconnect(t *testing.T) {
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")
}
}
func TestClose(t *testing.T) {
defer initTestClient()
c.close()
@ -111,18 +176,47 @@ func TestClose(t *testing.T) {
func waitConnAndClose(t *testing.T) {
done := make(chan struct{})
quit := make(chan struct{})
go func() {
<-ircd.conn
c.Quit()
quit <- struct{}{}
<-ircd.connClosed
close(done)
}()
select {
case <-done:
return
for {
select {
case <-done:
return
case <-time.After(500 * time.Millisecond):
t.Error("Took too long")
case <-quit:
assert.True(t, c.Connected())
c.Quit()
case <-time.After(500 * time.Millisecond):
t.Error("Took too long")
return
}
}
}
var testCert = []byte(`-----BEGIN CERTIFICATE-----
MIIBdzCCASOgAwIBAgIBADALBgkqhkiG9w0BAQUwEjEQMA4GA1UEChMHQWNtZSBD
bzAeFw03MDAxMDEwMDAwMDBaFw00OTEyMzEyMzU5NTlaMBIxEDAOBgNVBAoTB0Fj
b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