2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire_test
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2014-07-03 02:43:33 +02:00
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
// fakeMessage implements the wire.Message interface and is used to force
|
2013-05-12 06:32:20 +02:00
|
|
|
// encode errors in messages.
|
2013-05-08 21:31:00 +02:00
|
|
|
type fakeMessage struct {
|
2013-05-12 06:32:20 +02:00
|
|
|
command string
|
|
|
|
payload []byte
|
|
|
|
forceEncodeErr bool
|
2013-05-16 16:07:04 +02:00
|
|
|
forceLenErr bool
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
// BtcDecode doesn't do anything. It just satisfies the wire.Message
|
2013-05-08 21:31:00 +02:00
|
|
|
// interface.
|
|
|
|
func (msg *fakeMessage) BtcDecode(r io.Reader, pver uint32) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-12 06:32:20 +02:00
|
|
|
// BtcEncode writes the payload field of the fake message or forces an error
|
|
|
|
// if the forceEncodeErr flag of the fake message is set. It also satisfies the
|
2015-01-31 19:32:55 +01:00
|
|
|
// wire.Message interface.
|
2013-05-08 21:31:00 +02:00
|
|
|
func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32) error {
|
2013-05-12 06:32:20 +02:00
|
|
|
if msg.forceEncodeErr {
|
2015-01-31 19:32:55 +01:00
|
|
|
err := &wire.MessageError{
|
2013-05-12 06:32:20 +02:00
|
|
|
Func: "fakeMessage.BtcEncode",
|
|
|
|
Description: "intentional error",
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write(msg.payload)
|
|
|
|
return err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the command field of the fake message and satisfies the
|
2015-01-31 19:32:55 +01:00
|
|
|
// wire.Message interface.
|
2013-05-08 21:31:00 +02:00
|
|
|
func (msg *fakeMessage) Command() string {
|
|
|
|
return msg.command
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:07:04 +02:00
|
|
|
// MaxPayloadLength returns the length of the payload field of fake message
|
|
|
|
// or a smaller value if the forceLenErr flag of the fake message is set. It
|
2015-01-31 19:32:55 +01:00
|
|
|
// satisfies the wire.Message interface.
|
2013-05-08 21:31:00 +02:00
|
|
|
func (msg *fakeMessage) MaxPayloadLength(pver uint32) uint32 {
|
2013-05-16 16:07:04 +02:00
|
|
|
lenp := uint32(len(msg.payload))
|
|
|
|
if msg.forceLenErr {
|
|
|
|
return lenp - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return lenp
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|