X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=daemon_test.go;h=b237851353886a849676c288a16054dae43e29a6;hb=964a1bf9dbe211e21cc093001caa8962d513d513;hp=a0c21a0333336d46c6d35a18dbb1a7ab9f8e353d;hpb=49c1eed2e72c62dd7f4a44e3f2ca0647422bda04;p=goircd.git diff --git a/daemon_test.go b/daemon_test.go index a0c21a0..b237851 100644 --- a/daemon_test.go +++ b/daemon_test.go @@ -1,11 +1,10 @@ /* goircd -- minimalistic simple Internet Relay Chat (IRC) server -Copyright (C) 2014 Sergey Matveev +Copyright (C) 2014-2018 | wn 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. +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 @@ -15,6 +14,7 @@ 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 ( @@ -25,99 +25,115 @@ import ( ) func TestRegistrationWorkflow(t *testing.T) { - daemon := NewDaemon("foohost", "", nil, nil) + host := "foohost" + hostname = &host events := make(chan ClientEvent) - go daemon.Processor(events) + defer func() { + events <- ClientEvent{eventType: EventTerm} + }() + go Processor(events, make(chan struct{})) conn := NewTestingConn() - client := NewClient("foohost", conn) + client := NewClient(conn) go client.Processor(events) - conn.inbound <- "UNEXISTENT CMD" // should recieve nothing on this + conn.inbound <- "UNEXISTENT CMD" // should receive nothing on this conn.inbound <- "NICK" if r := <-conn.outbound; r != ":foohost 431 :No nickname given\r\n" { - t.Fatal("431 for NICK") + t.Fatal("431 for NICK", r) + } + + 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) + } } - conn.inbound <- "NICK meinick\r\nUSER\r\n" + 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) } - if (client.nickname != "meinick") || client.registered { + if (*client.nickname != "meinick") || client.registered { t.Fatal("NICK saved") } - conn.inbound <- "USER 1 2 3\r\n" + 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") + t.Fatal("461 again for USER", r) } - daemon.SendLusers(client) + SendLusers(client) if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") { - t.Fatal("LUSERS") + t.Fatal("LUSERS", r) } - conn.inbound <- "USER 1 2 3 :4 5\r\n" + conn.inbound <- "USER 1 2 3 :4 5" if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") { - t.Fatal("001 after registration") + t.Fatal("001 after registration", r) } if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") { - t.Fatal("002 after registration") + t.Fatal("002 after registration", r) } if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") { - t.Fatal("003 after registration") + t.Fatal("003 after registration", r) } if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") { - t.Fatal("004 after registration") + t.Fatal("004 after registration", r) } if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") { - t.Fatal("251 after registration") + t.Fatal("251 after registration", r) } if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") { - t.Fatal("422 after registration") + t.Fatal("422 after registration", r) } - if (client.username != "1") || (client.realname != "4 5") || !client.registered { + if (*client.username != "1") || (*client.realname != "4 5") || !client.registered { t.Fatal("client register") } - conn.inbound <- "AWAY\r\n" - conn.inbound <- "UNEXISTENT CMD\r\n" + 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") + t.Fatal("reply for unexistent command", r) } - daemon.SendLusers(client) + SendLusers(client) if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") { - t.Fatal("1 users logged in") + t.Fatal("1 users logged in", r) } - conn.inbound <- "QUIT\r\nUNEXISTENT CMD\r\n" - <-conn.outbound - if !conn.closed { - t.Fatal("closed connection on QUIT") + 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") 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")) + fd.WriteString("catched\n") conn := NewTestingConn() - client := NewClient("foohost", conn) - daemon := NewDaemon("foohost", fd.Name(), nil, nil) + host := "foohost" + hostname = &host + client := NewClient(conn) + motdName := fd.Name() + motd = &motdName - daemon.SendMotd(client) + SendMotd(client) if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") { - t.Fatal("MOTD start") + t.Fatal("MOTD start", r) } if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") { - t.Fatal("MOTD contents") + t.Fatal("MOTD contents", r) } - if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") { - t.Fatal("MOTD end", r) + if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) { + t.Fatalf("MOTD end: got %q, want prefix %q", got, want) } }