]> Cypherpunks.ru repositories - goircd.git/blob - common_test.go
Many fixes and additions
[goircd.git] / common_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         "io"
22         "net"
23         "time"
24 )
25
26 type TestingConn struct {
27         inbound  chan string
28         outbound chan string
29         closed   chan struct{}
30 }
31
32 func NewTestingConn() *TestingConn {
33         return &TestingConn{
34                 inbound:  make(chan string, 8),
35                 outbound: make(chan string, 8),
36                 closed:   make(chan struct{}),
37         }
38 }
39
40 func (conn TestingConn) Error() string {
41         return "i am finished"
42 }
43
44 func (conn *TestingConn) Read(b []byte) (n int, err error) {
45         select {
46         case msg := <-conn.inbound:
47                 if msg == "" {
48                         return 0, conn
49                 }
50                 for n, bt := range append([]byte(msg), CRLF...) {
51                         b[n] = bt
52                 }
53                 return len(msg) + 2, nil
54         case <-conn.closed:
55                 return 0, io.EOF
56         }
57 }
58
59 type MyAddr struct{}
60
61 func (a MyAddr) String() string {
62         return "someclient"
63 }
64 func (a MyAddr) Network() string {
65         return "somenet"
66 }
67
68 func (conn *TestingConn) Write(b []byte) (n int, err error) {
69         conn.outbound <- string(b)
70         return len(b), nil
71 }
72
73 func (conn *TestingConn) Close() error {
74         close(conn.closed)
75         close(conn.outbound)
76         return nil
77 }
78
79 func (conn TestingConn) LocalAddr() net.Addr {
80         return nil
81 }
82
83 func (conn TestingConn) RemoteAddr() net.Addr {
84         return MyAddr{}
85 }
86
87 func (conn TestingConn) SetDeadline(t time.Time) error {
88         return nil
89 }
90
91 func (conn TestingConn) SetReadDeadline(t time.Time) error {
92         return nil
93 }
94
95 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
96         return nil
97 }