]> Cypherpunks.ru repositories - goircd.git/blob - client_test.go
Cleaner nickname and channelname validation, not correlated together
[goircd.git] / client_test.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 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 package main
19
20 import (
21         "net"
22         "testing"
23         "time"
24 )
25
26 // Testing network connection that satisfies net.Conn interface
27 // Can send predefined messages and store all written ones
28 type TestingConn struct {
29         msgs     []string
30         msg_ptr  int
31         incoming []string
32         closed   bool
33 }
34
35 func NewTestingConn(msgs ...string) *TestingConn {
36         msgs_crlf := []string{}
37         for _, msg := range msgs {
38                 msgs_crlf = append(msgs_crlf, msg+"\r\n")
39         }
40         return &TestingConn{msgs: msgs_crlf, msg_ptr: -1}
41 }
42
43 func (conn TestingConn) Error() string {
44         return "i am out"
45 }
46
47 func (conn *TestingConn) Read(b []byte) (n int, err error) {
48         conn.msg_ptr++
49         if len(conn.msgs) == conn.msg_ptr {
50                 return 0, TestingConn{}
51         }
52         for n, bt := range []byte(conn.msgs[conn.msg_ptr]) {
53                 b[n] = bt
54         }
55         return len(conn.msgs[conn.msg_ptr]), nil
56 }
57
58 type MyAddr struct{}
59
60 func (a MyAddr) String() string {
61         return "someclient"
62 }
63 func (a MyAddr) Network() string {
64         return "somenet"
65 }
66
67 func (conn *TestingConn) Write(b []byte) (n int, err error) {
68         conn.incoming = append(conn.incoming, string(b))
69         return 0, nil
70 }
71
72 func (conn *TestingConn) Close() error {
73         conn.closed = true
74         return nil
75 }
76
77 func (conn TestingConn) LocalAddr() net.Addr {
78         return nil
79 }
80
81 func (conn TestingConn) RemoteAddr() net.Addr {
82         return MyAddr{}
83 }
84
85 func (conn TestingConn) SetDeadline(t time.Time) error {
86         return nil
87 }
88
89 func (conn TestingConn) SetReadDeadline(t time.Time) error {
90         return nil
91 }
92
93 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
94         return nil
95 }
96
97 // New client creation test. It must send an event about new client,
98 // two predefined messages from it and deletion one
99 func TestNewClient(t *testing.T) {
100         conn := NewTestingConn("foo", "bar")
101         sink := make(chan ClientEvent)
102         client := NewClient("foohost", conn)
103         go client.Processor(sink)
104
105         event := <-sink
106         if event.event_type != EVENT_NEW {
107                 t.Fail()
108         }
109         event = <-sink
110         if (event.event_type != EVENT_MSG) || (event.text != "foo") {
111                 t.Fail()
112         }
113         event = <-sink
114         if (event.event_type != EVENT_MSG) || (event.text != "bar") {
115                 t.Fail()
116         }
117         event = <-sink
118         if event.event_type != EVENT_DEL {
119                 t.Fail()
120         }
121 }
122
123 // Test replies formatting
124 func TestClientReplies(t *testing.T) {
125         conn := NewTestingConn("foo", "bar")
126         client := NewClient("foohost", conn)
127         client.nickname = "мойник"
128
129         client.Reply("hello")
130         if (len(conn.incoming) != 1) || (conn.incoming[0] != ":foohost hello\r\n") {
131                 t.Fatal("did not recieve hello message")
132         }
133
134         client.ReplyParts("200", "foo", "bar")
135         if (len(conn.incoming) != 2) || (conn.incoming[1] != ":foohost 200 foo :bar\r\n") {
136                 t.Fatal("did not recieve 200 message")
137         }
138
139         client.ReplyNicknamed("200", "foo", "bar")
140         if (len(conn.incoming) != 3) || (conn.incoming[2] != ":foohost 200 мойник foo :bar\r\n") {
141                 t.Fatal("did not recieve nicknamed message")
142         }
143
144         client.ReplyNotEnoughParameters("CMD")
145         if (len(conn.incoming) != 4) || (conn.incoming[3] != ":foohost 461 мойник CMD :Not enough parameters\r\n") {
146                 t.Fatal("did not recieve 461 message")
147         }
148 }