]> Cypherpunks.ru repositories - goircd.git/blobdiff - daemon_test.go
Many fixes and additions
[goircd.git] / daemon_test.go
index 1c86f351d819816682b095d063d5f98116471d35..76d620305dcf83725ef3e43f7d68e21028f4b6d3 100644 (file)
@@ -1,11 +1,10 @@
 /*
 goircd -- minimalistic simple Internet Relay Chat (IRC) server
-Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
 
 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 <http://www.gnu.org/licenses/>.
 */
+
 package main
 
 import (
@@ -25,21 +25,26 @@ import (
 )
 
 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)
-       go client.Processor(events)
+       client := NewClient(conn, 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", r)
        }
 
-       for _, n := range []string{"привет", " foo", "longlonglong", "#foo", "mein nick", "foo_bar"} {
+       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)
@@ -59,7 +64,7 @@ func TestRegistrationWorkflow(t *testing.T) {
                t.Fatal("461 again for USER", r)
        }
 
-       daemon.SendLusers(client)
+       client.SendLusers()
        if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
                t.Fatal("LUSERS", r)
        }
@@ -89,11 +94,12 @@ func TestRegistrationWorkflow(t *testing.T) {
 
        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)
+       client.SendLusers()
        if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
                t.Fatal("1 users logged in", r)
        }
@@ -104,32 +110,34 @@ func TestRegistrationWorkflow(t *testing.T) {
        }
 
        conn.inbound <- "QUIT\r\nUNEXISTENT CMD"
-       <-conn.outbound
-       if !conn.closed {
-               t.Fatal("closed connection on QUIT")
-       }
 }
 
 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)
-
-       daemon.SendMotd(client)
+       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 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)
        }
 }