fakemessage_test.go raw

   1  package wire
   2  
   3  import "io"
   4  
   5  // fakeMessage implements the Message interface and is used to force encode errors in messages.
   6  type fakeMessage struct {
   7  	command        string
   8  	payload        []byte
   9  	forceEncodeErr bool
  10  	forceLenErr    bool
  11  }
  12  
  13  // BtcDecode doesn't do anything.  It just satisfies the wire.Message interface.
  14  func (msg *fakeMessage) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) (e error) {
  15  	return nil
  16  }
  17  
  18  // BtcEncode writes the payload field of the fake message or forces an error if the forceEncodeErr flag of the fake
  19  // message is set. It also satisfies the wire.Message interface.
  20  func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) (e error) {
  21  	if msg.forceEncodeErr {
  22  		e := &MessageError{
  23  			Func:        "fakeMessage.BtcEncode",
  24  			Description: "intentional error",
  25  		}
  26  		return e
  27  	}
  28  	_, e = w.Write(msg.payload)
  29  	return e
  30  }
  31  
  32  // Command returns the command field of the fake message and satisfies the Message interface.
  33  func (msg *fakeMessage) Command() string {
  34  	return msg.command
  35  }
  36  
  37  // MaxPayloadLength returns the length of the payload field of fake message or a smaller value if the forceLenErr flag
  38  // of the fake message is set. It satisfies the Message interface.
  39  func (msg *fakeMessage) MaxPayloadLength(pver uint32) uint32 {
  40  	lenp := uint32(len(msg.payload))
  41  	if msg.forceLenErr {
  42  		return lenp - 1
  43  	}
  44  	return lenp
  45  }
  46