github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/wire/fakemessage_test.go (about)

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