fakemessage_test.go raw

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