X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=daemon_test.go;h=b67fbd4664743662709cd8890b220aa3acbf8072;hp=b5f6580302a956a7583f9288f5b1227bc0045369;hb=HEAD;hpb=b2f90fb412218973608b24a32eebadc60de9878a diff --git a/daemon_test.go b/daemon_test.go index b5f6580..b67fbd4 100644 --- a/daemon_test.go +++ b/daemon_test.go @@ -1,140 +1,140 @@ -/* -goircd -- minimalistic simple Internet Relay Chat (IRC) server -Copyright (C) 2014 Sergey Matveev - -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 -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -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 . -*/ +// goircd -- minimalistic simple Internet Relay Chat (IRC) server +// Copyright (C) 2014-2024 Sergey Matveev +// +// 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 +// the Free Software Foundation, version 3 of the License. +// +// 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 . + package main import ( - "fmt" - "io/ioutil" "os" "strings" "testing" - "time" ) func TestRegistrationWorkflow(t *testing.T) { - daemon := NewDaemon("foohost", "", nil, nil) + host := "foohost" + hostname = &host events := make(chan ClientEvent) - go daemon.Processor(events) + finished := make(chan struct{}) + defer func() { + events <- ClientEvent{eventType: EventTerm} + <-finished + }() + go Processor(events, finished) conn := NewTestingConn() - client := NewClient("foohost", conn) + client := NewClient(conn, events) - events <- ClientEvent{client, EVENT_NEW, ""} - events <- ClientEvent{client, EVENT_MSG, "UNEXISTENT CMD"} - time.Sleep(100) - if len(conn.incoming) > 0 { - t.Fail() - } + conn.inbound <- "UNEXISTENT CMD" // should receive nothing on this + conn.inbound <- "NICK" - events <- ClientEvent{client, EVENT_MSG, "NICK"} - time.Sleep(100) - if (len(conn.incoming) != 1) || (conn.incoming[0] != ":foohost 431 :No nickname given\r\n") { - t.Fail() + if r := <-conn.outbound; r != ":foohost 431 :No nickname given\r\n" { + t.Fatal("431 for NICK", r) } - events <- ClientEvent{client, EVENT_MSG, "NICK meinick"} - time.Sleep(100) - if (len(conn.incoming) != 1) || (client.nickname != "meinick") || client.registered { - t.Fail() + for _, n := range []string{"привет", " foo", "#foo", "mein nick", "foo_bar"} { + conn.inbound <- "NICK " + n + if r := <-conn.outbound; r != ":foohost 432 * "+n+" :Erroneous nickname\r\n" { + t.Fatal("nickname validation", r) + } } - events <- ClientEvent{client, EVENT_MSG, "USER"} - time.Sleep(100) - if (len(conn.incoming) != 2) || (conn.incoming[1] != ":foohost 461 meinick USER :Not enough parameters\r\n") { - t.Fail() + conn.inbound <- "NICK meinick\r\nUSER" + if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" { + t.Fatal("461 for USER", r) } - - events <- ClientEvent{client, EVENT_MSG, "USER 1 2 3"} - time.Sleep(100) - if (len(conn.incoming) != 3) || (conn.incoming[2] != ":foohost 461 meinick USER :Not enough parameters\r\n") { - t.Fail() + if (client.nickname != "meinick") || client.registered { + t.Fatal("NICK saved") } - daemon.SendLusers(client) - if !strings.Contains(conn.incoming[len(conn.incoming)-1], "There are 0 users") { - t.Fail() + conn.inbound <- "USER 1 2 3" + if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" { + t.Fatal("461 again for USER", r) } - events <- ClientEvent{client, EVENT_MSG, "USER 1 2 3 :4 5"} - time.Sleep(100) - if (len(conn.incoming) < 4) || (client.username != "1") || (client.realname != "4 5") { - t.Fail() + client.SendLusers() + if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") { + t.Fatal("LUSERS", r) } - statuses := map[int]bool{1: false, 2: false, 3: false, 4: false, 251: false, 422: false} - for _, msg := range conn.incoming { - for k, _ := range statuses { - if strings.HasPrefix(msg, fmt.Sprintf(":foohost %03d", k)) { - statuses[k] = true - } - } + conn.inbound <- "USER 1 2 3 :4 5" + if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") { + t.Fatal("001 after registration", r) } - for _, v := range statuses { - if !v { - t.Fail() - } + if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") { + t.Fatal("002 after registration", r) } - if !client.registered { - t.Fail() + if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") { + t.Fatal("003 after registration", r) } - - events <- ClientEvent{client, EVENT_MSG, "UNEXISTENT CMD"} - time.Sleep(100) - if conn.incoming[len(conn.incoming)-1] != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" { - t.Fail() + if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") { + t.Fatal("004 after registration", r) + } + if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") { + t.Fatal("251 after registration", r) + } + if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") { + t.Fatal("422 after registration", r) + } + if (client.username != "1") || (client.realname != "4 5") || !client.registered { + t.Fatal("client register") } - events <- ClientEvent{client, EVENT_MSG, "AWAY"} - time.Sleep(100) - if conn.incoming[len(conn.incoming)-1] == ":foohost 421 meinick AWAY :Unknown command\r\n" { - t.Fail() + conn.inbound <- "AWAY" + conn.inbound <- "UNEXISTENT CMD" + <-conn.outbound + if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" { + t.Fatal("reply for unexistent command", r) } - daemon.SendLusers(client) - if !strings.Contains(conn.incoming[len(conn.incoming)-1], "There are 1 users") { - t.Fail() + client.SendLusers() + if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") { + t.Fatal("1 users logged in", r) } - events <- ClientEvent{client, EVENT_MSG, "QUIT"} - time.Sleep(100) - if !conn.closed { - t.Fail() + conn.inbound <- "PING thishost" + if r := <-conn.outbound; r != ":foohost PONG foohost :thishost\r\n" { + t.Fatal("PONG", r) } + + conn.inbound <- "QUIT\r\nUNEXISTENT CMD" } func TestMotd(t *testing.T) { - fd, err := ioutil.TempFile("", "motd") + fd, err := os.CreateTemp("", "motd") if err != nil { - t.Fatal("can not create temporary file") + t.Fatalf("can not create temporary file: %v", err) } defer os.Remove(fd.Name()) - fd.Write([]byte("catched\n")) - daemon := NewDaemon("foohost", fd.Name(), nil, nil) - conn := NewTestingConn() - client := NewClient("foohost", conn) + fd.WriteString("catched\n") - daemon.SendMotd(client) - catched := false - for _, msg := range conn.incoming { - if strings.Contains(msg, "372 * :- catched") { - catched = true - } - } - if !catched { - t.Fail() + conn := NewTestingConn() + host := "foohost" + hostname = &host + client := NewClient(conn, make(chan ClientEvent, 2)) + defer func() { + client.Close() + }() + motdName := fd.Name() + motd = &motdName + + client.SendMotd() + if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") { + t.Fatal("MOTD start", r) + } + if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") { + t.Fatal("MOTD contents", r) + } + if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) { + t.Fatalf("MOTD end: got %q, want prefix %q", got, want) } }