2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
27 func TestRegistrationWorkflow(t *testing.T) {
28 daemon := NewDaemon("foohost", "", nil, nil)
29 events := make(chan ClientEvent)
30 go daemon.Processor(events)
31 conn := NewTestingConn()
32 client := NewClient("foohost", conn)
33 go client.Processor(events)
35 conn.inbound <- "UNEXISTENT CMD" // should recieve nothing on this
36 conn.inbound <- "NICK"
38 if r := <-conn.outbound; r != ":foohost 431 :No nickname given\r\n" {
39 t.Fatal("431 for NICK")
42 conn.inbound <- "NICK meinick\r\nUSER\r\n"
43 if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
44 t.Fatal("461 for USER", r)
46 if (client.nickname != "meinick") || client.registered {
50 conn.inbound <- "USER 1 2 3\r\n"
51 if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
52 t.Fatal("461 again for USER")
55 daemon.SendLusers(client)
56 if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
60 conn.inbound <- "USER 1 2 3 :4 5\r\n"
61 if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") {
62 t.Fatal("001 after registration")
64 if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") {
65 t.Fatal("002 after registration")
67 if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") {
68 t.Fatal("003 after registration")
70 if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") {
71 t.Fatal("004 after registration")
73 if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") {
74 t.Fatal("251 after registration")
76 if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") {
77 t.Fatal("422 after registration")
79 if (client.username != "1") || (client.realname != "4 5") || !client.registered {
80 t.Fatal("client register")
83 conn.inbound <- "AWAY\r\n"
84 conn.inbound <- "UNEXISTENT CMD\r\n"
85 if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
86 t.Fatal("reply for unexistent command")
89 daemon.SendLusers(client)
90 if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
91 t.Fatal("1 users logged in")
94 conn.inbound <- "QUIT\r\nUNEXISTENT CMD\r\n"
97 t.Fatal("closed connection on QUIT")
101 func TestMotd(t *testing.T) {
102 fd, err := ioutil.TempFile("", "motd")
104 t.Fatal("can not create temporary file")
106 defer os.Remove(fd.Name())
107 fd.Write([]byte("catched\n"))
109 conn := NewTestingConn()
110 client := NewClient("foohost", conn)
111 daemon := NewDaemon("foohost", fd.Name(), nil, nil)
113 daemon.SendMotd(client)
114 if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
115 t.Fatal("MOTD start")
117 if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
118 t.Fatal("MOTD contents")
120 if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") {
121 t.Fatal("MOTD end", r)