]> Cypherpunks.ru repositories - goircd.git/blob - daemon_test.go
Unify copyright comment format
[goircd.git] / daemon_test.go
1 // goircd -- minimalistic simple Internet Relay Chat (IRC) server
2 // Copyright (C) 2014-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "os"
20         "strings"
21         "testing"
22 )
23
24 func TestRegistrationWorkflow(t *testing.T) {
25         host := "foohost"
26         hostname = &host
27         events := make(chan ClientEvent)
28         finished := make(chan struct{})
29         defer func() {
30                 events <- ClientEvent{eventType: EventTerm}
31                 <-finished
32         }()
33         go Processor(events, finished)
34         conn := NewTestingConn()
35         client := NewClient(conn, events)
36
37         conn.inbound <- "UNEXISTENT CMD" // should receive 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", "#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         client.SendLusers()
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         <-conn.outbound
95         if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
96                 t.Fatal("reply for unexistent command", r)
97         }
98
99         client.SendLusers()
100         if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
101                 t.Fatal("1 users logged in", r)
102         }
103
104         conn.inbound <- "PING thishost"
105         if r := <-conn.outbound; r != ":foohost PONG foohost :thishost\r\n" {
106                 t.Fatal("PONG", r)
107         }
108
109         conn.inbound <- "QUIT\r\nUNEXISTENT CMD"
110 }
111
112 func TestMotd(t *testing.T) {
113         fd, err := os.CreateTemp("", "motd")
114         if err != nil {
115                 t.Fatalf("can not create temporary file: %v", err)
116         }
117         defer os.Remove(fd.Name())
118         fd.WriteString("catched\n")
119
120         conn := NewTestingConn()
121         host := "foohost"
122         hostname = &host
123         client := NewClient(conn, make(chan ClientEvent, 2))
124         defer func() {
125                 client.Close()
126         }()
127         motdName := fd.Name()
128         motd = &motdName
129
130         client.SendMotd()
131         if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
132                 t.Fatal("MOTD start", r)
133         }
134         if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
135                 t.Fatal("MOTD contents", r)
136         }
137         if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) {
138                 t.Fatalf("MOTD end: got %q, want prefix %q", got, want)
139         }
140 }