2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 // Testing network connection that satisfies net.Conn interface
27 // Can send predefined messages and store all written ones
28 type TestingConn struct {
35 func NewTestingConn(msgs ...string) *TestingConn {
36 msgs_crlf := []string{}
37 for _, msg := range msgs {
38 msgs_crlf = append(msgs_crlf, msg+"\r\n")
40 return &TestingConn{msgs: msgs_crlf, msg_ptr: -1}
43 func (conn TestingConn) Error() string {
47 func (conn *TestingConn) Read(b []byte) (n int, err error) {
49 if len(conn.msgs) == conn.msg_ptr {
50 return 0, TestingConn{}
52 for n, bt := range []byte(conn.msgs[conn.msg_ptr]) {
55 return len(conn.msgs[conn.msg_ptr]), nil
60 func (a MyAddr) String() string {
63 func (a MyAddr) Network() string {
67 func (conn *TestingConn) Write(b []byte) (n int, err error) {
68 conn.incoming = append(conn.incoming, string(b))
72 func (conn *TestingConn) Close() error {
77 func (conn TestingConn) LocalAddr() net.Addr {
81 func (conn TestingConn) RemoteAddr() net.Addr {
85 func (conn TestingConn) SetDeadline(t time.Time) error {
89 func (conn TestingConn) SetReadDeadline(t time.Time) error {
93 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
97 // New client creation test. It must send an event about new client,
98 // two predefined messages from it and deletion one
99 func TestNewClient(t *testing.T) {
100 conn := NewTestingConn("foo", "bar")
101 sink := make(chan ClientEvent)
102 client := NewClient("foohost", conn)
103 go client.Processor(sink)
106 if event.event_type != EVENT_NEW {
110 if (event.event_type != EVENT_MSG) || (event.text != "foo") {
114 if (event.event_type != EVENT_MSG) || (event.text != "bar") {
118 if event.event_type != EVENT_DEL {
123 // Test replies formatting
124 func TestClientReplies(t *testing.T) {
125 conn := NewTestingConn("foo", "bar")
126 client := NewClient("foohost", conn)
127 client.nickname = "мойник"
129 client.Reply("hello")
130 if (len(conn.incoming) != 1) || (conn.incoming[0] != ":foohost hello\r\n") {
131 t.Fatal("did not recieve hello message")
134 client.ReplyParts("200", "foo", "bar")
135 if (len(conn.incoming) != 2) || (conn.incoming[1] != ":foohost 200 foo :bar\r\n") {
136 t.Fatal("did not recieve 200 message")
139 client.ReplyNicknamed("200", "foo", "bar")
140 if (len(conn.incoming) != 3) || (conn.incoming[2] != ":foohost 200 мойник foo :bar\r\n") {
141 t.Fatal("did not recieve nicknamed message")
144 client.ReplyNotEnoughParameters("CMD")
145 if (len(conn.incoming) != 4) || (conn.incoming[3] != ":foohost 461 мойник CMD :Not enough parameters\r\n") {
146 t.Fatal("did not recieve 461 message")