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", r)
42 for _, n := range []string{"привет", " foo", "longlonglong", "#foo", "mein nick", "foo_bar"} {
43 conn.inbound <- "NICK " + n
44 if r := <-conn.outbound; r != ":foohost 432 * "+n+" :Erroneous nickname\r\n" {
45 t.Fatal("nickname validation", r)
49 conn.inbound <- "NICK meinick\r\nUSER"
50 if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
51 t.Fatal("461 for USER", r)
53 if (client.nickname != "meinick") || client.registered {
57 conn.inbound <- "USER 1 2 3"
58 if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
59 t.Fatal("461 again for USER", r)
62 daemon.SendLusers(client)
63 if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
67 conn.inbound <- "USER 1 2 3 :4 5"
68 if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") {
69 t.Fatal("001 after registration", r)
71 if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") {
72 t.Fatal("002 after registration", r)
74 if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") {
75 t.Fatal("003 after registration", r)
77 if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") {
78 t.Fatal("004 after registration", r)
80 if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") {
81 t.Fatal("251 after registration", r)
83 if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") {
84 t.Fatal("422 after registration", r)
86 if (client.username != "1") || (client.realname != "4 5") || !client.registered {
87 t.Fatal("client register")
90 conn.inbound <- "AWAY"
91 conn.inbound <- "UNEXISTENT CMD"
92 if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
93 t.Fatal("reply for unexistent command", r)
96 daemon.SendLusers(client)
97 if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
98 t.Fatal("1 users logged in", r)
101 conn.inbound <- "PING thishost"
102 if r := <-conn.outbound; r != ":foohost PONG foohost :thishost\r\n" {
106 conn.inbound <- "QUIT\r\nUNEXISTENT CMD"
109 t.Fatal("closed connection on QUIT")
113 func TestMotd(t *testing.T) {
114 fd, err := ioutil.TempFile("", "motd")
116 t.Fatalf("can not create temporary file: %v", err)
118 defer os.Remove(fd.Name())
119 fd.WriteString("catched\n")
121 conn := NewTestingConn()
122 client := NewClient("foohost", conn)
123 daemon := NewDaemon("foohost", fd.Name(), nil, nil)
125 daemon.SendMotd(client)
126 if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
127 t.Fatal("MOTD start", r)
129 if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
130 t.Fatal("MOTD contents", r)
132 if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) {
133 t.Fatalf("MOTD end: got %q, want prefix %q", got, want)