]> Cypherpunks.ru repositories - goircd.git/blob - daemon_test.go
a0c21a0333336d46c6d35a18dbb1a7ab9f8e353d
[goircd.git] / daemon_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         "io/ioutil"
22         "os"
23         "strings"
24         "testing"
25 )
26
27 func TestRegistrationWorkflow(t *testing.T) {
28         daemon := NewDaemon("foohost", "", nil, nil)
29         events := make(chan ClientEvent)
30         go daemon.Processor(events)
31         conn := NewTestingConn()
32         client := NewClient("foohost", conn)
33         go client.Processor(events)
34
35         conn.inbound <- "UNEXISTENT CMD" // should recieve nothing on this
36         conn.inbound <- "NICK"
37
38         if r := <-conn.outbound; r != ":foohost 431 :No nickname given\r\n" {
39                 t.Fatal("431 for NICK")
40         }
41
42         conn.inbound <- "NICK meinick\r\nUSER\r\n"
43         if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
44                 t.Fatal("461 for USER", r)
45         }
46         if (client.nickname != "meinick") || client.registered {
47                 t.Fatal("NICK saved")
48         }
49
50         conn.inbound <- "USER 1 2 3\r\n"
51         if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
52                 t.Fatal("461 again for USER")
53         }
54
55         daemon.SendLusers(client)
56         if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
57                 t.Fatal("LUSERS")
58         }
59
60         conn.inbound <- "USER 1 2 3 :4 5\r\n"
61         if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") {
62                 t.Fatal("001 after registration")
63         }
64         if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") {
65                 t.Fatal("002 after registration")
66         }
67         if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") {
68                 t.Fatal("003 after registration")
69         }
70         if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") {
71                 t.Fatal("004 after registration")
72         }
73         if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") {
74                 t.Fatal("251 after registration")
75         }
76         if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") {
77                 t.Fatal("422 after registration")
78         }
79         if (client.username != "1") || (client.realname != "4 5") || !client.registered {
80                 t.Fatal("client register")
81         }
82
83         conn.inbound <- "AWAY\r\n"
84         conn.inbound <- "UNEXISTENT CMD\r\n"
85         if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
86                 t.Fatal("reply for unexistent command")
87         }
88
89         daemon.SendLusers(client)
90         if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
91                 t.Fatal("1 users logged in")
92         }
93
94         conn.inbound <- "QUIT\r\nUNEXISTENT CMD\r\n"
95         <-conn.outbound
96         if !conn.closed {
97                 t.Fatal("closed connection on QUIT")
98         }
99 }
100
101 func TestMotd(t *testing.T) {
102         fd, err := ioutil.TempFile("", "motd")
103         if err != nil {
104                 t.Fatal("can not create temporary file")
105         }
106         defer os.Remove(fd.Name())
107         fd.Write([]byte("catched\n"))
108
109         conn := NewTestingConn()
110         client := NewClient("foohost", conn)
111         daemon := NewDaemon("foohost", fd.Name(), nil, nil)
112
113         daemon.SendMotd(client)
114         if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
115                 t.Fatal("MOTD start")
116         }
117         if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
118                 t.Fatal("MOTD contents")
119         }
120         if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") {
121                 t.Fatal("MOTD end", r)
122         }
123 }