]> Cypherpunks.ru repositories - goircd.git/blob - common_test.go
Forbid any later GNU GPL versions autousage
[goircd.git] / common_test.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2018 | wn 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         "net"
22         "time"
23 )
24
25 // Testing network connection that satisfies net.Conn interface
26 // Can send predefined messages and store all written ones
27 type TestingConn struct {
28         inbound  chan string
29         outbound chan string
30         closed   bool
31 }
32
33 func NewTestingConn() *TestingConn {
34         inbound := make(chan string, 8)
35         outbound := make(chan string, 8)
36         return &TestingConn{inbound: inbound, outbound: outbound}
37 }
38
39 func (conn TestingConn) Error() string {
40         return "i am finished"
41 }
42
43 func (conn *TestingConn) Read(b []byte) (n int, err error) {
44         msg := <-conn.inbound
45         if msg == "" {
46                 return 0, conn
47         }
48         for n, bt := range append([]byte(msg), CRLF...) {
49                 b[n] = bt
50         }
51         return len(msg) + 2, nil
52 }
53
54 type MyAddr struct{}
55
56 func (a MyAddr) String() string {
57         return "someclient"
58 }
59 func (a MyAddr) Network() string {
60         return "somenet"
61 }
62
63 func (conn *TestingConn) Write(b []byte) (n int, err error) {
64         conn.outbound <- string(b)
65         return len(b), nil
66 }
67
68 func (conn *TestingConn) Close() error {
69         conn.closed = true
70         close(conn.outbound)
71         return nil
72 }
73
74 func (conn TestingConn) LocalAddr() net.Addr {
75         return nil
76 }
77
78 func (conn TestingConn) RemoteAddr() net.Addr {
79         return MyAddr{}
80 }
81
82 func (conn TestingConn) SetDeadline(t time.Time) error {
83         return nil
84 }
85
86 func (conn TestingConn) SetReadDeadline(t time.Time) error {
87         return nil
88 }
89
90 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
91         return nil
92 }