Debian Codegraph @

testio/testio.go

raw
1 // Package testio implements various io utility types. Included are
2 // BrokenWriter, which fails after writing a certain number of bytes;
3 // a BufCloser, which wraps a bytes.Buffer in a Close method; a
4 // BrokenReadWriter, which fails after writing a certain number of
5 // bytes and/or reading a certain number of bytes; a LoggingBuffer
6 // that logs all reads and writes; and a BufferConn, that is designed
7 // to simulate net.Conn.
8 package testio
9
10 import (
11 "bytes"
12 "errors"
13 "fmt"
14 "io"
15 "os"
16 )
17
18 // BrokenWriter implements an io.Writer that fails after a certain
19 // number of bytes. This can be used to simulate a network connection
20 // that breaks during write or a file on a filesystem that becomes
21 // full, for example. A BrokenWriter doesn't actually store any data.
22 type BrokenWriter struct {
23 current, limit int
24 }
25
26 // NewBrokenWriter creates a new BrokenWriter that can store only
27 // limit bytes.
28 func NewBrokenWriter(limit int) *BrokenWriter {
29 return &BrokenWriter{limit: limit}
30 }
31
32 // Write will write the byte slice to the BrokenWriter, failing if the
33 // maximum number of bytes has been reached.
34 func (w *BrokenWriter) Write(p []byte) (int, error) {
35 if (len(p) + w.current) <= w.limit {
36 w.current += len(p)
37 } else {
38 spill := (len(p) + w.current) - w.limit
39 w.current = w.limit
40 return len(p) - spill, errors.New("testio: write failed")
41 }
42
43 return len(p), nil
44 }
45
46 // Extend increases the byte limit to allow more data to be written.
47 func (w *BrokenWriter) Extend(n int) {
48 w.limit += n
49 }
50
51 // Reset clears the limit and bytes in the BrokenWriter. Extend needs
52 // to be called to allow data to be written.
53 func (w *BrokenWriter) Reset() {
54 w.limit = 0
55 w.current = 0
56 }
57
58 // Close is provided to satisfy the Closer interface.
59 func (w *BrokenWriter) Close() error {
60 w.Reset()
61 return nil
62 }
63
64 // SilentBrokenWriter implements an io.Writer that fails after a
65 // certain number of bytes. However, this failure is silent: it just
66 // reports fewer bytes written than p. It doesn't actually store any
67 // data, and is used to verify that io.Writer implementations properly
68 // return errors on short writes.
69 type SilentBrokenWriter struct {
70 current, limit int
71 }
72
73 // NewSilentBrokenWriter creates a new SilentBrokenWriter that can store only
74 // limit bytes.
75 func NewSilentBrokenWriter(limit int) *SilentBrokenWriter {
76 return &SilentBrokenWriter{limit: limit}
77 }
78
79 // Write will write the byte slice to the SilentBrokenWriter, failing if the
80 // maximum number of bytes has been reached.
81 func (w *SilentBrokenWriter) Write(p []byte) (int, error) {
82 if (len(p) + w.current) <= w.limit {
83 w.current += len(p)
84 } else {
85 spill := (len(p) + w.current) - w.limit
86 w.current = w.limit
87 return len(p) - spill, nil
88 }
89
90 return len(p), nil
91 }
92
93 // Extend increases the byte limit to allow more data to be written.
94 func (w *SilentBrokenWriter) Extend(n int) {
95 w.limit += n
96 }
97
98 // Reset clears the limit and bytes in the SilentBrokenWriter. Extend needs
99 // to be called to allow data to be written.
100 func (w *SilentBrokenWriter) Reset() {
101 w.limit = 0
102 w.current = 0
103 }
104
105 // Close is provided to satisfy the Closer interface.
106 func (w *SilentBrokenWriter) Close() error {
107 w.Reset()
108 return nil
109 }
110
111 // BrokenReadWriter implements a broken reader and writer, backed by a
112 // bytes.Buffer.
113 type BrokenReadWriter struct {
114 rlimit, wlimit int
115 buf *bytes.Buffer
116 }
117
118 // NewBrokenReadWriter initialises a new BrokerReadWriter with an empty
119 // reader and the specified limits.
120 func NewBrokenReadWriter(wlimit, rlimit int) *BrokenReadWriter {
121 return &BrokenReadWriter{
122 wlimit: wlimit,
123 rlimit: rlimit,
124 buf: &bytes.Buffer{},
125 }
126 }
127
128 // Write satisfies the Writer interface.
129 func (brw *BrokenReadWriter) Write(p []byte) (int, error) {
130 if (len(p) + brw.buf.Len()) > brw.wlimit {
131 remain := brw.wlimit - brw.buf.Len()
132 if remain > 0 {
133 brw.buf.Write(p[:remain])
134 return remain, errors.New("testio: write failed")
135 }
136 return 0, errors.New("testio: write failed")
137 }
138 return brw.buf.Write(p)
139 }
140
141 // Read satisfies the Reader interface.
142 func (brw *BrokenReadWriter) Read(p []byte) (int, error) {
143 remain := brw.rlimit - brw.buf.Len()
144 if len(p) > remain {
145 tmp := make([]byte, len(p)-remain)
146 n, err := brw.buf.Read(tmp)
147 if err == nil {
148 err = io.EOF
149 }
150 copy(p, tmp)
151 return n, err
152 }
153 return brw.buf.Read(p)
154 }
155
156 // Extend increases the BrokenReadWriter limit.
157 func (brw *BrokenReadWriter) Extend(w, r int) {
158 brw.rlimit += r
159 brw.wlimit += w
160 }
161
162 // Reset clears the internal buffer. It retains its original limit.
163 func (brw *BrokenReadWriter) Reset() {
164 brw.buf.Reset()
165 }
166
167 // BufCloser is a buffer wrapped with a Close method.
168 type BufCloser struct {
169 buf *bytes.Buffer
170 }
171
172 // Write writes the data to the BufCloser.
173 func (buf *BufCloser) Write(p []byte) (int, error) {
174 return buf.buf.Write(p)
175 }
176
177 // Read reads data from the BufCloser.
178 func (buf *BufCloser) Read(p []byte) (int, error) {
179 return buf.buf.Read(p)
180 }
181
182 // Close is a stub function to satisfy the io.Closer interface.
183 func (buf *BufCloser) Close() error {
184 return nil
185 }
186
187 // Reset clears the internal buffer.
188 func (buf *BufCloser) Reset() {
189 buf.buf.Reset()
190 }
191
192 // Bytes returns the contents of the buffer as a byte slice.
193 func (buf *BufCloser) Bytes() []byte {
194 return buf.buf.Bytes()
195 }
196
197 // NewBufCloser creates and initializes a new BufCloser using buf as
198 // its initial contents. It is intended to prepare a BufCloser to read
199 // existing data. It can also be used to size the internal buffer for
200 // writing. To do that, buf should have the desired capacity but a
201 // length of zero.
202 func NewBufCloser(buf []byte) *BufCloser {
203 bc := new(BufCloser)
204 bc.buf = bytes.NewBuffer(buf)
205 return bc
206 }
207
208 // NewBufCloserString creates and initializes a new Buffer using
209 // string s as its initial contents. It is intended to prepare a
210 // buffer to read an existing string.
211 func NewBufCloserString(s string) *BufCloser {
212 buf := new(BufCloser)
213 buf.buf = bytes.NewBufferString(s)
214 return buf
215 }
216
217 // A LoggingBuffer is an io.ReadWriter that prints the hex value of
218 // the data for all reads and writes.
219 type LoggingBuffer struct {
220 rw io.ReadWriter
221 w io.Writer
222 name string
223 }
224
225 // NewLoggingBuffer creates a logging buffer from an existing
226 // io.ReadWriter. By default, it will log to standard error.
227 func NewLoggingBuffer(rw io.ReadWriter) *LoggingBuffer {
228 return &LoggingBuffer{
229 rw: rw,
230 w: os.Stderr,
231 }
232 }
233
234 // LogTo sets the io.Writer that the buffer will write logs to.
235 func (lb *LoggingBuffer) LogTo(w io.Writer) {
236 lb.w = w
237 }
238
239 // SetName gives a name to the logging buffer to help distinguish
240 // output from this buffer.
241 func (lb *LoggingBuffer) SetName(name string) {
242 lb.name = name
243 }
244
245 // Write writes the data to the logging buffer and writes the data to
246 // the logging writer.
247 func (lb *LoggingBuffer) Write(p []byte) (int, error) {
248 if lb.name != "" {
249 fmt.Fprintf(lb.w, "[%s] ", lb.name)
250 }
251
252 fmt.Fprintf(lb.w, "[WRITE] %x\n", p)
253 return lb.rw.Write(p)
254 }
255
256 // Read reads the data from the logging buffer and writes the data to
257 // the logging writer.
258 func (lb *LoggingBuffer) Read(p []byte) (int, error) {
259 n, err := lb.rw.Read(p)
260 if err != nil {
261 return n, err
262 }
263 if lb.name != "" {
264 fmt.Fprintf(lb.w, "[%s] ", lb.name)
265 }
266
267 fmt.Fprintf(lb.w, "[READ] %x\n", p)
268 return n, err
269 }
270
271 // BufferConn is a type that can be used to simulate network
272 // connections between a "client" (the code that uses the BufferConn)
273 // and some simulated "peer". Writes go to a "client" buffer, which is
274 // used to record the data sent by the caller, which may be read with
275 // ReadPeer. The peer's responses may be simulated by calling
276 // WritePeer; when the client reads from the BufferConn, they will see
277 // this data.
278 type BufferConn struct {
279 client, peer *bytes.Buffer
280 }
281
282 // NewBufferConn initialises a new simulated network connection.
283 func NewBufferConn() *BufferConn {
284 return &BufferConn{
285 client: &bytes.Buffer{},
286 peer: &bytes.Buffer{},
287 }
288 }
289
290 // Write writes to the client buffer.
291 func (bc *BufferConn) Write(p []byte) (int, error) {
292 return bc.client.Write(p)
293 }
294
295 // Read reads from the peer buffer.
296 func (bc *BufferConn) Read(p []byte) (int, error) {
297 return bc.peer.Read(p)
298 }
299
300 // WritePeer writes data to the peer buffer.
301 func (bc *BufferConn) WritePeer(p []byte) (int, error) {
302 return bc.peer.Write(p)
303 }
304
305 // ReadClient reads data from the client buffer.
306 func (bc *BufferConn) ReadClient(p []byte) (int, error) {
307 return bc.client.Read(p)
308 }
309
310 // Close is a dummy operation that allows the BufferConn to be used as
311 // an io.Closer.
312 func (bc *BufferConn) Close() error {
313 return nil
314 }
315
316 // BrokenCloser is a BufCloser that fails to close.
317 type BrokenCloser struct {
318 buf *bytes.Buffer
319 }
320
321 // Write writes the data to the BrokenCloser.
322 func (buf *BrokenCloser) Write(p []byte) (int, error) {
323 return buf.buf.Write(p)
324 }
325
326 // Read reads data from the BrokenCloser.
327 func (buf *BrokenCloser) Read(p []byte) (int, error) {
328 return buf.buf.Read(p)
329 }
330
331 // Close is a stub function to satisfy the io.Closer interface.
332 func (buf *BrokenCloser) Close() error {
333 return errors.New("testio: broken closer is broken")
334 }
335
336 // Reset clears the internal buffer.
337 func (buf *BrokenCloser) Reset() {
338 buf.buf.Reset()
339 }
340
341 // Bytes returns the contents of the buffer as a byte slice.
342 func (buf *BrokenCloser) Bytes() []byte {
343 return buf.buf.Bytes()
344 }
345
346 // NewBrokenCloser creates and initializes a new BrokenCloser using buf as
347 // its initial contents. It is intended to prepare a BrokenCloser to read
348 // existing data. It can also be used to size the internal buffer for
349 // writing. To do that, buf should have the desired capacity but a
350 // length of zero.
351 func NewBrokenCloser(buf []byte) *BrokenCloser {
352 bc := new(BrokenCloser)
353 bc.buf = bytes.NewBuffer(buf)
354 return bc
355 }
356
357 // NewBrokenCloserString creates and initializes a new Buffer using
358 // string s as its initial contents. It is intended to prepare a
359 // buffer to read an existing string.
360 func NewBrokenCloserString(s string) *BrokenCloser {
361 buf := new(BrokenCloser)
362 buf.buf = bytes.NewBufferString(s)
363 return buf
364 }