]> Cypherpunks.ru repositories - goircd.git/blob - client_test.go
4af1dd34f521348769c3cc9976dd62a87ed52f1d
[goircd.git] / client_test.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "testing"
22 )
23
24 // New client creation test. It must send an event about new client,
25 // two predefined messages from it and deletion one
26 func TestNewClient(t *testing.T) {
27         conn := NewTestingConn()
28         sink := make(chan ClientEvent)
29         host := "foohost"
30         hostname = &host
31         client := NewClient(conn)
32         go client.Processor(sink)
33
34         event := <-sink
35         if event.eventType != EventNew {
36                 t.Fatal("no NEW event", event)
37         }
38         conn.inbound <- "foo"
39         event = <-sink
40         if (event.eventType != EventMsg) || (event.text != "foo") {
41                 t.Fatal("no first MSG", event)
42         }
43         conn.inbound <- "bar"
44         event = <-sink
45         if (event.eventType != EventMsg) || (event.text != "bar") {
46                 t.Fatal("no second MSG", event)
47         }
48         conn.inbound <- ""
49         event = <-sink
50         if event.eventType != EventDel {
51                 t.Fatal("no client termination", event)
52         }
53 }
54
55 // Test replies formatting
56 func TestClientReplies(t *testing.T) {
57         conn := NewTestingConn()
58         host := "foohost"
59         hostname = &host
60         client := NewClient(conn)
61         nickname := "мойник"
62         client.nickname = &nickname
63
64         client.Reply("hello")
65         if r := <-conn.outbound; r != ":foohost hello\r\n" {
66                 t.Fatal("did not recieve hello message", r)
67         }
68
69         client.ReplyParts("200", "foo", "bar")
70         if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
71                 t.Fatal("did not recieve 200 message", r)
72         }
73
74         client.ReplyNicknamed("200", "foo", "bar")
75         if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
76                 t.Fatal("did not recieve nicknamed message", r)
77         }
78
79         client.ReplyNotEnoughParameters("CMD")
80         if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
81                 t.Fatal("did not recieve 461 message", r)
82         }
83 }