]> Cypherpunks.ru repositories - goircd.git/blob - common_test.go
Unify copyright comment format
[goircd.git] / common_test.go
1 // goircd -- minimalistic simple Internet Relay Chat (IRC) server
2 // Copyright (C) 2014-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "io"
20         "net"
21         "time"
22 )
23
24 type TestingConn struct {
25         inbound  chan string
26         outbound chan string
27         closed   chan struct{}
28 }
29
30 func NewTestingConn() *TestingConn {
31         return &TestingConn{
32                 inbound:  make(chan string, 8),
33                 outbound: make(chan string, 8),
34                 closed:   make(chan struct{}),
35         }
36 }
37
38 func (conn TestingConn) Error() string {
39         return "i am finished"
40 }
41
42 func (conn *TestingConn) Read(b []byte) (n int, err error) {
43         select {
44         case 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         case <-conn.closed:
53                 return 0, io.EOF
54         }
55 }
56
57 type MyAddr struct{}
58
59 func (a MyAddr) String() string {
60         return "someclient"
61 }
62 func (a MyAddr) Network() string {
63         return "somenet"
64 }
65
66 func (conn *TestingConn) Write(b []byte) (n int, err error) {
67         conn.outbound <- string(b)
68         return len(b), nil
69 }
70
71 func (conn *TestingConn) Close() error {
72         close(conn.closed)
73         close(conn.outbound)
74         return nil
75 }
76
77 func (conn TestingConn) LocalAddr() net.Addr {
78         return nil
79 }
80
81 func (conn TestingConn) RemoteAddr() net.Addr {
82         return MyAddr{}
83 }
84
85 func (conn TestingConn) SetDeadline(t time.Time) error {
86         return nil
87 }
88
89 func (conn TestingConn) SetReadDeadline(t time.Time) error {
90         return nil
91 }
92
93 func (conn TestingConn) SetWriteDeadline(t time.Time) error {
94         return nil
95 }