]> Cypherpunks.ru repositories - goircd.git/blob - daemon_test.go
Additional unittests
[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", r)
40         }
41
42         for _, n := range []string{"привет", " foo", "longlonglong", "#foo", "mein nick", "foo_bar"} {
43                 conn.inbound <- "NICK " + n
44                 if r := <-conn.outbound; r != ":foohost 432 * "+n+" :Erroneous nickname\r\n" {
45                         t.Fatal("nickname validation", r)
46                 }
47         }
48
49         conn.inbound <- "NICK meinick\r\nUSER"
50         if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
51                 t.Fatal("461 for USER", r)
52         }
53         if (client.nickname != "meinick") || client.registered {
54                 t.Fatal("NICK saved")
55         }
56
57         conn.inbound <- "USER 1 2 3"
58         if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
59                 t.Fatal("461 again for USER", r)
60         }
61
62         daemon.SendLusers(client)
63         if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
64                 t.Fatal("LUSERS", r)
65         }
66
67         conn.inbound <- "USER 1 2 3 :4 5"
68         if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") {
69                 t.Fatal("001 after registration", r)
70         }
71         if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") {
72                 t.Fatal("002 after registration", r)
73         }
74         if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") {
75                 t.Fatal("003 after registration", r)
76         }
77         if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") {
78                 t.Fatal("004 after registration", r)
79         }
80         if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") {
81                 t.Fatal("251 after registration", r)
82         }
83         if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") {
84                 t.Fatal("422 after registration", r)
85         }
86         if (client.username != "1") || (client.realname != "4 5") || !client.registered {
87                 t.Fatal("client register")
88         }
89
90         conn.inbound <- "AWAY"
91         conn.inbound <- "UNEXISTENT CMD"
92         if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
93                 t.Fatal("reply for unexistent command", r)
94         }
95
96         daemon.SendLusers(client)
97         if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
98                 t.Fatal("1 users logged in", r)
99         }
100
101         conn.inbound <- "PING thishost"
102         if r := <-conn.outbound; r != ":foohost PONG foohost :thishost\r\n" {
103                 t.Fatal("PONG", r)
104         }
105
106         conn.inbound <- "QUIT\r\nUNEXISTENT CMD"
107         <-conn.outbound
108         if !conn.closed {
109                 t.Fatal("closed connection on QUIT")
110         }
111 }
112
113 func TestMotd(t *testing.T) {
114         fd, err := ioutil.TempFile("", "motd")
115         if err != nil {
116                 t.Fatal("can not create temporary file")
117         }
118         defer os.Remove(fd.Name())
119         fd.Write([]byte("catched\n"))
120
121         conn := NewTestingConn()
122         client := NewClient("foohost", conn)
123         daemon := NewDaemon("foohost", fd.Name(), nil, nil)
124
125         daemon.SendMotd(client)
126         if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
127                 t.Fatal("MOTD start", r)
128         }
129         if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
130                 t.Fatal("MOTD contents", r)
131         }
132         if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") {
133                 t.Fatal("MOTD end", r)
134         }
135 }