2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 // Testing network connection that satisfies net.Conn interface
27 // Can send predefined messages and store all written ones
28 type TestingConn struct {
34 func NewTestingConn() *TestingConn {
35 inbound := make(chan string, 8)
36 outbound := make(chan string, 8)
37 return &TestingConn{inbound: inbound, outbound: outbound}
40 func (conn TestingConn) Error() string {
41 return "i am finished"
44 func (conn *TestingConn) Read(b []byte) (n int, err error) {
49 for n, bt := range []byte(msg + CRLF) {
57 func (a MyAddr) String() string {
60 func (a MyAddr) Network() string {
64 func (conn *TestingConn) Write(b []byte) (n int, err error) {
65 conn.outbound <- string(b)
69 func (conn *TestingConn) Close() error {
75 func (conn TestingConn) LocalAddr() net.Addr {
79 func (conn TestingConn) RemoteAddr() net.Addr {
83 func (conn TestingConn) SetDeadline(t time.Time) error {
87 func (conn TestingConn) SetReadDeadline(t time.Time) error {
91 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
95 // New client creation test. It must send an event about new client,
96 // two predefined messages from it and deletion one
97 func TestNewClient(t *testing.T) {
98 conn := NewTestingConn()
99 sink := make(chan ClientEvent)
100 client := NewClient("foohost", conn)
101 go client.Processor(sink)
104 if event.event_type != EVENT_NEW {
105 t.Fatal("no NEW event")
107 conn.inbound <- "foo"
109 if (event.event_type != EVENT_MSG) || (event.text != "foo") {
110 t.Fatal("no first MSG")
112 conn.inbound <- "bar"
114 if (event.event_type != EVENT_MSG) || (event.text != "bar") {
115 t.Fatal("no second MSG")
119 if event.event_type != EVENT_DEL {
120 t.Fatal("no client termination")
124 // Test replies formatting
125 func TestClientReplies(t *testing.T) {
126 conn := NewTestingConn()
127 client := NewClient("foohost", conn)
128 client.nickname = "мойник"
130 client.Reply("hello")
131 if r := <-conn.outbound; r != ":foohost hello\r\n" {
132 t.Fatal("did not recieve hello message")
135 client.ReplyParts("200", "foo", "bar")
136 if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
137 t.Fatal("did not recieve 200 message")
140 client.ReplyNicknamed("200", "foo", "bar")
141 if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
142 t.Fatal("did not recieve nicknamed message")
145 client.ReplyNotEnoughParameters("CMD")
146 if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
147 t.Fatal("did not recieve 461 message")