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