goircd/client_test.go

152 lines
3.6 KiB
Go
Raw Normal View History

2014-05-11 16:18:55 +00:00
/*
goircd -- minimalistic simple Internet Relay Chat (IRC) server
2015-05-09 15:27:06 +00:00
Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
2014-05-11 16:18:55 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2015-05-09 15:27:06 +00:00
2014-05-11 16:18:55 +00:00
package main
import (
"net"
"testing"
"time"
)
// Testing network connection that satisfies net.Conn interface
// Can send predefined messages and store all written ones
type TestingConn struct {
2014-05-14 12:29:54 +00:00
inbound chan string
outbound chan string
2014-05-11 16:18:55 +00:00
closed bool
}
2014-05-14 12:29:54 +00:00
func NewTestingConn() *TestingConn {
inbound := make(chan string, 8)
outbound := make(chan string, 8)
return &TestingConn{inbound: inbound, outbound: outbound}
2014-05-11 16:18:55 +00:00
}
func (conn TestingConn) Error() string {
2014-05-14 12:29:54 +00:00
return "i am finished"
2014-05-11 16:18:55 +00:00
}
func (conn *TestingConn) Read(b []byte) (n int, err error) {
2014-05-14 12:29:54 +00:00
msg := <-conn.inbound
if msg == "" {
return 0, conn
2014-05-11 16:18:55 +00:00
}
2014-05-14 12:29:54 +00:00
for n, bt := range []byte(msg + CRLF) {
2014-05-11 16:18:55 +00:00
b[n] = bt
}
2014-05-14 12:29:54 +00:00
return len(msg), nil
2014-05-11 16:18:55 +00:00
}
type MyAddr struct{}
func (a MyAddr) String() string {
return "someclient"
}
func (a MyAddr) Network() string {
return "somenet"
}
func (conn *TestingConn) Write(b []byte) (n int, err error) {
2014-05-14 12:29:54 +00:00
conn.outbound <- string(b)
return len(b), nil
2014-05-11 16:18:55 +00:00
}
func (conn *TestingConn) Close() error {
conn.closed = true
return nil
}
func (conn TestingConn) LocalAddr() net.Addr {
return nil
}
func (conn TestingConn) RemoteAddr() net.Addr {
return MyAddr{}
}
func (conn TestingConn) SetDeadline(t time.Time) error {
return nil
}
func (conn TestingConn) SetReadDeadline(t time.Time) error {
return nil
}
func (conn TestingConn) SetWriteDeadline(t time.Time) error {
return nil
}
// New client creation test. It must send an event about new client,
// two predefined messages from it and deletion one
func TestNewClient(t *testing.T) {
2014-05-14 12:29:54 +00:00
conn := NewTestingConn()
2014-05-11 16:18:55 +00:00
sink := make(chan ClientEvent)
host := "foohost"
client := NewClient(&host, conn)
2014-05-11 16:18:55 +00:00
go client.Processor(sink)
event := <-sink
2014-08-09 12:42:19 +00:00
if event.eventType != EventNew {
2014-05-18 11:27:17 +00:00
t.Fatal("no NEW event", event)
2014-05-11 16:18:55 +00:00
}
2014-05-14 12:29:54 +00:00
conn.inbound <- "foo"
2014-05-11 16:18:55 +00:00
event = <-sink
2014-08-09 12:42:19 +00:00
if (event.eventType != EventMsg) || (event.text != "foo") {
2014-05-18 11:27:17 +00:00
t.Fatal("no first MSG", event)
2014-05-11 16:18:55 +00:00
}
2014-05-14 12:29:54 +00:00
conn.inbound <- "bar"
2014-05-11 16:18:55 +00:00
event = <-sink
2014-08-09 12:42:19 +00:00
if (event.eventType != EventMsg) || (event.text != "bar") {
2014-05-18 11:27:17 +00:00
t.Fatal("no second MSG", event)
2014-05-11 16:18:55 +00:00
}
2014-05-14 12:29:54 +00:00
conn.inbound <- ""
2014-05-11 16:18:55 +00:00
event = <-sink
2014-08-09 12:42:19 +00:00
if event.eventType != EventDel {
2014-05-18 11:27:17 +00:00
t.Fatal("no client termination", event)
2014-05-11 16:18:55 +00:00
}
}
// Test replies formatting
func TestClientReplies(t *testing.T) {
2014-05-14 12:29:54 +00:00
conn := NewTestingConn()
host := "foohost"
client := NewClient(&host, conn)
2014-05-11 16:18:55 +00:00
client.nickname = "мойник"
client.Reply("hello")
2014-05-14 12:29:54 +00:00
if r := <-conn.outbound; r != ":foohost hello\r\n" {
2014-05-18 11:27:17 +00:00
t.Fatal("did not recieve hello message", r)
2014-05-11 16:18:55 +00:00
}
client.ReplyParts("200", "foo", "bar")
2014-05-14 12:29:54 +00:00
if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
2014-05-18 11:27:17 +00:00
t.Fatal("did not recieve 200 message", r)
2014-05-11 16:18:55 +00:00
}
client.ReplyNicknamed("200", "foo", "bar")
2014-05-14 12:29:54 +00:00
if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
2014-05-18 11:27:17 +00:00
t.Fatal("did not recieve nicknamed message", r)
2014-05-11 16:18:55 +00:00
}
client.ReplyNotEnoughParameters("CMD")
2014-05-14 12:29:54 +00:00
if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
2014-05-18 11:27:17 +00:00
t.Fatal("did not recieve 461 message", r)
2014-05-11 16:18:55 +00:00
}
}