2014-05-11 16:18:55 +00:00
|
|
|
/*
|
|
|
|
goircd -- minimalistic simple Internet Relay Chat (IRC) server
|
2018-03-25 14:57:05 +00:00
|
|
|
Copyright (C) 2014-2018 | wn Sergey Matveev <stargrave@stargrave.org>
|
2014-05-11 16:18:55 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2019-09-27 08:32:53 +00:00
|
|
|
the Free Software Foundation, version 3 of the License.
|
2014-05-11 16:18:55 +00:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-05-09 15:27:06 +00:00
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegistrationWorkflow(t *testing.T) {
|
2014-08-19 14:11:56 +00:00
|
|
|
host := "foohost"
|
2015-10-11 08:12:55 +00:00
|
|
|
hostname = &host
|
2014-05-11 16:18:55 +00:00
|
|
|
events := make(chan ClientEvent)
|
2015-10-11 08:12:55 +00:00
|
|
|
defer func() {
|
|
|
|
events <- ClientEvent{eventType: EventTerm}
|
|
|
|
}()
|
|
|
|
go Processor(events, make(chan struct{}))
|
2014-05-11 16:18:55 +00:00
|
|
|
conn := NewTestingConn()
|
2015-10-11 08:12:55 +00:00
|
|
|
client := NewClient(conn)
|
2014-05-14 12:29:54 +00:00
|
|
|
go client.Processor(events)
|
2014-05-11 16:18:55 +00:00
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
conn.inbound <- "UNEXISTENT CMD" // should receive nothing on this
|
2014-05-14 12:29:54 +00:00
|
|
|
conn.inbound <- "NICK"
|
2014-05-11 16:18:55 +00:00
|
|
|
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; r != ":foohost 431 :No nickname given\r\n" {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("431 for NICK", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
for _, n := range []string{"привет", " foo", "#foo", "mein nick", "foo_bar"} {
|
2014-05-18 11:27:17 +00:00
|
|
|
conn.inbound <- "NICK " + n
|
|
|
|
if r := <-conn.outbound; r != ":foohost 432 * "+n+" :Erroneous nickname\r\n" {
|
|
|
|
t.Fatal("nickname validation", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.inbound <- "NICK meinick\r\nUSER"
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
|
|
|
|
t.Fatal("461 for USER", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
if (*client.nickname != "meinick") || client.registered {
|
2014-05-14 12:29:54 +00:00
|
|
|
t.Fatal("NICK saved")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 11:27:17 +00:00
|
|
|
conn.inbound <- "USER 1 2 3"
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("461 again for USER", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
SendLusers(client)
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("LUSERS", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 11:27:17 +00:00
|
|
|
conn.inbound <- "USER 1 2 3 :4 5"
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("001 after registration", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("002 after registration", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("003 after registration", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("004 after registration", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("251 after registration", r)
|
2014-05-14 12:29:54 +00:00
|
|
|
}
|
|
|
|
if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("422 after registration", r)
|
2014-05-14 12:29:54 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
if (*client.username != "1") || (*client.realname != "4 5") || !client.registered {
|
2014-05-14 12:29:54 +00:00
|
|
|
t.Fatal("client register")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 11:27:17 +00:00
|
|
|
conn.inbound <- "AWAY"
|
|
|
|
conn.inbound <- "UNEXISTENT CMD"
|
2015-10-06 08:27:11 +00:00
|
|
|
<-conn.outbound
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("reply for unexistent command", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
SendLusers(client)
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("1 users logged in", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.inbound <- "PING thishost"
|
|
|
|
if r := <-conn.outbound; r != ":foohost PONG foohost :thishost\r\n" {
|
|
|
|
t.Fatal("PONG", r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 11:27:17 +00:00
|
|
|
conn.inbound <- "QUIT\r\nUNEXISTENT CMD"
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMotd(t *testing.T) {
|
|
|
|
fd, err := ioutil.TempFile("", "motd")
|
|
|
|
if err != nil {
|
2014-06-08 01:16:33 +00:00
|
|
|
t.Fatalf("can not create temporary file: %v", err)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
defer os.Remove(fd.Name())
|
2014-06-08 01:16:33 +00:00
|
|
|
fd.WriteString("catched\n")
|
2014-05-14 12:29:54 +00:00
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
conn := NewTestingConn()
|
2014-08-19 14:11:56 +00:00
|
|
|
host := "foohost"
|
2015-10-11 08:12:55 +00:00
|
|
|
hostname = &host
|
|
|
|
client := NewClient(conn)
|
2014-08-19 14:11:56 +00:00
|
|
|
motdName := fd.Name()
|
2015-10-11 08:12:55 +00:00
|
|
|
motd = &motdName
|
2014-05-11 16:18:55 +00:00
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
SendMotd(client)
|
2014-05-14 12:29:54 +00:00
|
|
|
if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("MOTD start", r)
|
2014-05-14 12:29:54 +00:00
|
|
|
}
|
|
|
|
if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
|
2014-05-18 11:27:17 +00:00
|
|
|
t.Fatal("MOTD contents", r)
|
2014-05-14 12:29:54 +00:00
|
|
|
}
|
2014-06-08 01:16:33 +00:00
|
|
|
if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) {
|
|
|
|
t.Fatalf("MOTD end: got %q, want prefix %q", got, want)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|