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