]> Cypherpunks.ru repositories - goircd.git/blob - daemon_test.go
Many fixes and additions
[goircd.git] / daemon_test.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
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         hostname = &host
30         events := make(chan ClientEvent)
31         finished := make(chan struct{})
32         defer func() {
33                 events <- ClientEvent{eventType: EventTerm}
34                 <-finished
35         }()
36         go Processor(events, finished)
37         conn := NewTestingConn()
38         client := NewClient(conn, events)
39
40         conn.inbound <- "UNEXISTENT CMD" // should receive nothing on this
41         conn.inbound <- "NICK"
42
43         if r := <-conn.outbound; r != ":foohost 431 :No nickname given\r\n" {
44                 t.Fatal("431 for NICK", r)
45         }
46
47         for _, n := range []string{"привет", " foo", "#foo", "mein nick", "foo_bar"} {
48                 conn.inbound <- "NICK " + n
49                 if r := <-conn.outbound; r != ":foohost 432 * "+n+" :Erroneous nickname\r\n" {
50                         t.Fatal("nickname validation", r)
51                 }
52         }
53
54         conn.inbound <- "NICK meinick\r\nUSER"
55         if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
56                 t.Fatal("461 for USER", r)
57         }
58         if (client.nickname != "meinick") || client.registered {
59                 t.Fatal("NICK saved")
60         }
61
62         conn.inbound <- "USER 1 2 3"
63         if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
64                 t.Fatal("461 again for USER", r)
65         }
66
67         client.SendLusers()
68         if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
69                 t.Fatal("LUSERS", r)
70         }
71
72         conn.inbound <- "USER 1 2 3 :4 5"
73         if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") {
74                 t.Fatal("001 after registration", r)
75         }
76         if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") {
77                 t.Fatal("002 after registration", r)
78         }
79         if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") {
80                 t.Fatal("003 after registration", r)
81         }
82         if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") {
83                 t.Fatal("004 after registration", r)
84         }
85         if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") {
86                 t.Fatal("251 after registration", r)
87         }
88         if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") {
89                 t.Fatal("422 after registration", r)
90         }
91         if (client.username != "1") || (client.realname != "4 5") || !client.registered {
92                 t.Fatal("client register")
93         }
94
95         conn.inbound <- "AWAY"
96         conn.inbound <- "UNEXISTENT CMD"
97         <-conn.outbound
98         if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
99                 t.Fatal("reply for unexistent command", r)
100         }
101
102         client.SendLusers()
103         if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
104                 t.Fatal("1 users logged in", r)
105         }
106
107         conn.inbound <- "PING thishost"
108         if r := <-conn.outbound; r != ":foohost PONG foohost :thishost\r\n" {
109                 t.Fatal("PONG", r)
110         }
111
112         conn.inbound <- "QUIT\r\nUNEXISTENT CMD"
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         hostname = &host
126         client := NewClient(conn, make(chan ClientEvent, 2))
127         defer func() {
128                 client.Close()
129         }()
130         motdName := fd.Name()
131         motd = &motdName
132
133         client.SendMotd()
134         if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
135                 t.Fatal("MOTD start", r)
136         }
137         if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
138                 t.Fatal("MOTD contents", r)
139         }
140         if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) {
141                 t.Fatalf("MOTD end: got %q, want prefix %q", got, want)
142         }
143 }