]> Cypherpunks.ru repositories - goircd.git/blob - client_test.go
22cc401ea920bacc0e36c245145359be47d851d1
[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         inbound  chan string
30         outbound chan string
31         closed   bool
32 }
33
34 func NewTestingConn() *TestingConn {
35         inbound := make(chan string, 8)
36         outbound := make(chan string, 8)
37         return &TestingConn{inbound: inbound, outbound: outbound}
38 }
39
40 func (conn TestingConn) Error() string {
41         return "i am finished"
42 }
43
44 func (conn *TestingConn) Read(b []byte) (n int, err error) {
45         msg := <-conn.inbound
46         if msg == "" {
47                 return 0, conn
48         }
49         for n, bt := range []byte(msg + CRLF) {
50                 b[n] = bt
51         }
52         return len(msg), nil
53 }
54
55 type MyAddr struct{}
56
57 func (a MyAddr) String() string {
58         return "someclient"
59 }
60 func (a MyAddr) Network() string {
61         return "somenet"
62 }
63
64 func (conn *TestingConn) Write(b []byte) (n int, err error) {
65         conn.outbound <- string(b)
66         return len(b), nil
67 }
68
69 func (conn *TestingConn) Close() error {
70         conn.closed = true
71         return nil
72 }
73
74 func (conn TestingConn) LocalAddr() net.Addr {
75         return nil
76 }
77
78 func (conn TestingConn) RemoteAddr() net.Addr {
79         return MyAddr{}
80 }
81
82 func (conn TestingConn) SetDeadline(t time.Time) error {
83         return nil
84 }
85
86 func (conn TestingConn) SetReadDeadline(t time.Time) error {
87         return nil
88 }
89
90 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
91         return nil
92 }
93
94 // New client creation test. It must send an event about new client,
95 // two predefined messages from it and deletion one
96 func TestNewClient(t *testing.T) {
97         conn := NewTestingConn()
98         sink := make(chan ClientEvent)
99         client := NewClient("foohost", conn)
100         go client.Processor(sink)
101
102         event := <-sink
103         if event.event_type != EVENT_NEW {
104                 t.Fatal("no NEW event", event)
105         }
106         conn.inbound <- "foo"
107         event = <-sink
108         ts1 := client.timestamp
109         if (event.event_type != EVENT_MSG) || (event.text != "foo") {
110                 t.Fatal("no first MSG", event)
111         }
112         conn.inbound <- "bar"
113         event = <-sink
114         if (event.event_type != EVENT_MSG) || (event.text != "bar") {
115                 t.Fatal("no second MSG", event)
116         }
117         conn.inbound <- ""
118         if client.timestamp.Before(ts1) || client.timestamp.Equal(ts1) {
119                 t.Fatal("timestamp updating")
120         }
121         event = <-sink
122         if event.event_type != EVENT_DEL {
123                 t.Fatal("no client termination", event)
124         }
125 }
126
127 // Test replies formatting
128 func TestClientReplies(t *testing.T) {
129         conn := NewTestingConn()
130         client := NewClient("foohost", conn)
131         client.nickname = "мойник"
132
133         client.Reply("hello")
134         if r := <-conn.outbound; r != ":foohost hello\r\n" {
135                 t.Fatal("did not recieve hello message", r)
136         }
137
138         client.ReplyParts("200", "foo", "bar")
139         if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
140                 t.Fatal("did not recieve 200 message", r)
141         }
142
143         client.ReplyNicknamed("200", "foo", "bar")
144         if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
145                 t.Fatal("did not recieve nicknamed message", r)
146         }
147
148         client.ReplyNotEnoughParameters("CMD")
149         if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
150                 t.Fatal("did not recieve 461 message", r)
151         }
152 }