github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/libp2p/internal/handshake/mock/stream.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mock 6 7 import ( 8 "bytes" 9 10 "github.com/ethersphere/bee/v2/pkg/p2p" 11 ) 12 13 type Stream struct { 14 readBuffer *bytes.Buffer 15 writeBuffer *bytes.Buffer 16 writeCounter int 17 readCounter int 18 readError error 19 writeError error 20 readErrCheckmark int 21 writeErrCheckmark int 22 } 23 24 func NewStream(readBuffer, writeBuffer *bytes.Buffer) *Stream { 25 return &Stream{readBuffer: readBuffer, writeBuffer: writeBuffer} 26 } 27 func (s *Stream) SetReadErr(err error, checkmark int) { 28 s.readError = err 29 s.readErrCheckmark = checkmark 30 } 31 32 func (s *Stream) SetWriteErr(err error, checkmark int) { 33 s.writeError = err 34 s.writeErrCheckmark = checkmark 35 } 36 37 func (s *Stream) Read(p []byte) (n int, err error) { 38 if s.readError != nil && s.readErrCheckmark <= s.readCounter { 39 return 0, s.readError 40 } 41 42 s.readCounter++ 43 return s.readBuffer.Read(p) 44 } 45 46 func (s *Stream) Write(p []byte) (n int, err error) { 47 if s.writeError != nil && s.writeErrCheckmark <= s.writeCounter { 48 return 0, s.writeError 49 } 50 51 s.writeCounter++ 52 return s.writeBuffer.Write(p) 53 } 54 55 func (s *Stream) Headers() p2p.Headers { 56 return nil 57 } 58 59 func (s *Stream) ResponseHeaders() p2p.Headers { 60 return nil 61 } 62 63 func (s *Stream) Close() error { 64 return nil 65 } 66 67 func (s *Stream) FullClose() error { 68 return nil 69 } 70 71 func (s *Stream) Reset() error { 72 return nil 73 }