]> Cypherpunks.ru repositories - goircd.git/blob - client_test.go
Increase maximum nickname length for convenience
[goircd.git] / client_test.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package main
20
21 import (
22         "net"
23         "testing"
24         "time"
25 )
26
27 // Testing network connection that satisfies net.Conn interface
28 // Can send predefined messages and store all written ones
29 type TestingConn struct {
30         inbound  chan string
31         outbound chan string
32         closed   bool
33 }
34
35 func NewTestingConn() *TestingConn {
36         inbound := make(chan string, 8)
37         outbound := make(chan string, 8)
38         return &TestingConn{inbound: inbound, outbound: outbound}
39 }
40
41 func (conn TestingConn) Error() string {
42         return "i am finished"
43 }
44
45 func (conn *TestingConn) Read(b []byte) (n int, err error) {
46         msg := <-conn.inbound
47         if msg == "" {
48                 return 0, conn
49         }
50         for n, bt := range append([]byte(msg), CRLF...) {
51                 b[n] = bt
52         }
53         return len(msg)+2, nil
54 }
55
56 type MyAddr struct{}
57
58 func (a MyAddr) String() string {
59         return "someclient"
60 }
61 func (a MyAddr) Network() string {
62         return "somenet"
63 }
64
65 func (conn *TestingConn) Write(b []byte) (n int, err error) {
66         conn.outbound <- string(b)
67         return len(b), nil
68 }
69
70 func (conn *TestingConn) Close() error {
71         conn.closed = true
72         return nil
73 }
74
75 func (conn TestingConn) LocalAddr() net.Addr {
76         return nil
77 }
78
79 func (conn TestingConn) RemoteAddr() net.Addr {
80         return MyAddr{}
81 }
82
83 func (conn TestingConn) SetDeadline(t time.Time) error {
84         return nil
85 }
86
87 func (conn TestingConn) SetReadDeadline(t time.Time) error {
88         return nil
89 }
90
91 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
92         return nil
93 }
94
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         host := "foohost"
101         client := NewClient(&host, conn)
102         go client.Processor(sink)
103
104         event := <-sink
105         if event.eventType != EventNew {
106                 t.Fatal("no NEW event", event)
107         }
108         conn.inbound <- "foo"
109         event = <-sink
110         if (event.eventType != EventMsg) || (event.text != "foo") {
111                 t.Fatal("no first MSG", event)
112         }
113         conn.inbound <- "bar"
114         event = <-sink
115         if (event.eventType != EventMsg) || (event.text != "bar") {
116                 t.Fatal("no second MSG", event)
117         }
118         conn.inbound <- ""
119         event = <-sink
120         if event.eventType != EventDel {
121                 t.Fatal("no client termination", event)
122         }
123 }
124
125 // Test replies formatting
126 func TestClientReplies(t *testing.T) {
127         conn := NewTestingConn()
128         host := "foohost"
129         client := NewClient(&host, conn)
130         client.nickname = "мойник"
131
132         client.Reply("hello")
133         if r := <-conn.outbound; r != ":foohost hello\r\n" {
134                 t.Fatal("did not recieve hello message", r)
135         }
136
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)
140         }
141
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)
145         }
146
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)
150         }
151 }