]> Cypherpunks.ru repositories - goircd.git/blob - daemon_test.go
Cleaner nickname and channelname validation, not correlated together
[goircd.git] / daemon_test.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 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, either version 3 of the License, or
8 (at your option) any later version.
9
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.
14
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/>.
17 */
18 package main
19
20 import (
21         "fmt"
22         "io/ioutil"
23         "os"
24         "strings"
25         "testing"
26         "time"
27 )
28
29 func TestRegistrationWorkflow(t *testing.T) {
30         daemon := NewDaemon("foohost", "", nil, nil)
31         events := make(chan ClientEvent)
32         go daemon.Processor(events)
33         conn := NewTestingConn()
34         client := NewClient("foohost", conn)
35
36         events <- ClientEvent{client, EVENT_NEW, ""}
37         events <- ClientEvent{client, EVENT_MSG, "UNEXISTENT CMD"}
38         time.Sleep(100)
39         if len(conn.incoming) > 0 {
40                 t.Fail()
41         }
42
43         events <- ClientEvent{client, EVENT_MSG, "NICK"}
44         time.Sleep(100)
45         if (len(conn.incoming) != 1) || (conn.incoming[0] != ":foohost 431 :No nickname given\r\n") {
46                 t.Fail()
47         }
48
49         events <- ClientEvent{client, EVENT_MSG, "NICK meinick"}
50         time.Sleep(100)
51         if (len(conn.incoming) != 1) || (client.nickname != "meinick") || client.registered {
52                 t.Fail()
53         }
54
55         events <- ClientEvent{client, EVENT_MSG, "USER"}
56         time.Sleep(100)
57         if (len(conn.incoming) != 2) || (conn.incoming[1] != ":foohost 461 meinick USER :Not enough parameters\r\n") {
58                 t.Fail()
59         }
60
61         events <- ClientEvent{client, EVENT_MSG, "USER 1 2 3"}
62         time.Sleep(100)
63         if (len(conn.incoming) != 3) || (conn.incoming[2] != ":foohost 461 meinick USER :Not enough parameters\r\n") {
64                 t.Fail()
65         }
66
67         daemon.SendLusers(client)
68         if !strings.Contains(conn.incoming[len(conn.incoming)-1], "There are 0 users") {
69                 t.Fail()
70         }
71
72         events <- ClientEvent{client, EVENT_MSG, "USER 1 2 3 :4 5"}
73         time.Sleep(100)
74         if (len(conn.incoming) < 4) || (client.username != "1") || (client.realname != "4 5") {
75                 t.Fail()
76         }
77
78         statuses := map[int]bool{1: false, 2: false, 3: false, 4: false, 251: false, 422: false}
79         for _, msg := range conn.incoming {
80                 for k, _ := range statuses {
81                         if strings.HasPrefix(msg, fmt.Sprintf(":foohost %03d", k)) {
82                                 statuses[k] = true
83                         }
84                 }
85         }
86         for _, v := range statuses {
87                 if !v {
88                         t.Fail()
89                 }
90         }
91         if !client.registered {
92                 t.Fail()
93         }
94
95         events <- ClientEvent{client, EVENT_MSG, "UNEXISTENT CMD"}
96         time.Sleep(100)
97         if conn.incoming[len(conn.incoming)-1] != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
98                 t.Fail()
99         }
100
101         events <- ClientEvent{client, EVENT_MSG, "AWAY"}
102         time.Sleep(100)
103         if conn.incoming[len(conn.incoming)-1] == ":foohost 421 meinick AWAY :Unknown command\r\n" {
104                 t.Fail()
105         }
106
107         daemon.SendLusers(client)
108         if !strings.Contains(conn.incoming[len(conn.incoming)-1], "There are 1 users") {
109                 t.Fail()
110         }
111
112         events <- ClientEvent{client, EVENT_MSG, "QUIT"}
113         time.Sleep(100)
114         if !conn.closed {
115                 t.Fail()
116         }
117 }
118
119 func TestMotd(t *testing.T) {
120         fd, err := ioutil.TempFile("", "motd")
121         if err != nil {
122                 t.Fatal("can not create temporary file")
123         }
124         defer os.Remove(fd.Name())
125         fd.Write([]byte("catched\n"))
126         daemon := NewDaemon("foohost", fd.Name(), nil, nil)
127         conn := NewTestingConn()
128         client := NewClient("foohost", conn)
129
130         daemon.SendMotd(client)
131         catched := false
132         for _, msg := range conn.incoming {
133                 if strings.Contains(msg, "372 * :- catched") {
134                         catched = true
135                 }
136         }
137         if !catched {
138                 t.Fail()
139         }
140 }