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