forked from btcsuite/btcwire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakemessage_test.go
60 lines (50 loc) · 1.56 KB
/
fakemessage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcwire_test
import (
"io"
"github.com/PointCoin/btcwire"
)
// fakeMessage implements the btcwire.Message interface and is used to force
// encode errors in messages.
type fakeMessage struct {
command string
payload []byte
forceEncodeErr bool
forceLenErr bool
}
// BtcDecode doesn't do anything. It just satisfies the btcwire.Message
// interface.
func (msg *fakeMessage) BtcDecode(r io.Reader, pver uint32) error {
return nil
}
// 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
// btcwire.Message interface.
func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32) error {
if msg.forceEncodeErr {
err := &btcwire.MessageError{
Func: "fakeMessage.BtcEncode",
Description: "intentional error",
}
return err
}
_, err := w.Write(msg.payload)
return err
}
// Command returns the command field of the fake message and satisfies the
// btcwire.Message interface.
func (msg *fakeMessage) Command() string {
return msg.command
}
// 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
// satisfies the btcwire.Message interface.
func (msg *fakeMessage) MaxPayloadLength(pver uint32) uint32 {
lenp := uint32(len(msg.payload))
if msg.forceLenErr {
return lenp - 1
}
return lenp
}