2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2015 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/>.
27 // Testing network connection that satisfies net.Conn interface
28 // Can send predefined messages and store all written ones
29 type TestingConn struct {
35 func NewTestingConn() *TestingConn {
36 inbound := make(chan string, 8)
37 outbound := make(chan string, 8)
38 return &TestingConn{inbound: inbound, outbound: outbound}
41 func (conn TestingConn) Error() string {
42 return "i am finished"
45 func (conn *TestingConn) Read(b []byte) (n int, err error) {
50 for n, bt := range []byte(msg + CRLF) {
58 func (a MyAddr) String() string {
61 func (a MyAddr) Network() string {
65 func (conn *TestingConn) Write(b []byte) (n int, err error) {
66 conn.outbound <- string(b)
70 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)
101 client := NewClient(&host, conn)
102 go client.Processor(sink)
105 if event.eventType != EventNew {
106 t.Fatal("no NEW event", event)
108 conn.inbound <- "foo"
110 if (event.eventType != EventMsg) || (event.text != "foo") {
111 t.Fatal("no first MSG", event)
113 conn.inbound <- "bar"
115 if (event.eventType != EventMsg) || (event.text != "bar") {
116 t.Fatal("no second MSG", event)
120 if event.eventType != EventDel {
121 t.Fatal("no client termination", event)
125 // Test replies formatting
126 func TestClientReplies(t *testing.T) {
127 conn := NewTestingConn()
129 client := NewClient(&host, conn)
130 client.nickname = "мойник"
132 client.Reply("hello")
133 if r := <-conn.outbound; r != ":foohost hello\r\n" {
134 t.Fatal("did not recieve hello message", r)
137 client.ReplyParts("200", "foo", "bar")
138 if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
139 t.Fatal("did not recieve 200 message", r)
142 client.ReplyNicknamed("200", "foo", "bar")
143 if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
144 t.Fatal("did not recieve nicknamed message", r)
147 client.ReplyNotEnoughParameters("CMD")
148 if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
149 t.Fatal("did not recieve 461 message", r)