• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13980275562

20 Mar 2025 10:06PM UTC coverage: 58.6% (-10.2%) from 68.789%
13980275562

Pull #9623

github

web-flow
Merge b9b960345 into 09b674508
Pull Request #9623: Size msg test msg

0 of 1518 new or added lines in 42 files covered. (0.0%)

26603 existing lines in 443 files now uncovered.

96807 of 165200 relevant lines covered (58.6%)

1.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lnwire/ping.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "io"
7

8
        "pgregory.net/rapid"
9
)
10

11
// PingPayload is a set of opaque bytes used to pad out a ping message.
12
type PingPayload []byte
13

14
// Ping defines a message which is sent by peers periodically to determine if
15
// the connection is still valid. Each ping message carries the number of bytes
16
// to pad the pong response with, and also a number of bytes to be ignored at
17
// the end of the ping message (which is padding).
18
type Ping struct {
19
        // NumPongBytes is the number of bytes the pong response to this
20
        // message should carry.
21
        NumPongBytes uint16
22

23
        // PaddingBytes is a set of opaque bytes used to pad out this ping
24
        // message. Using this field in conjunction to the one above, it's
25
        // possible for node to generate fake cover traffic.
26
        PaddingBytes PingPayload
27
}
28

29
// NewPing returns a new Ping message.
UNCOV
30
func NewPing(numBytes uint16) *Ping {
×
UNCOV
31
        return &Ping{
×
UNCOV
32
                NumPongBytes: numBytes,
×
UNCOV
33
        }
×
UNCOV
34
}
×
35

36
// A compile time check to ensure Ping implements the lnwire.Message interface.
37
var _ Message = (*Ping)(nil)
38

39
// A compile time check to ensure Ping implements the lnwire.SizeableMessage interface.
40
var _ SizeableMessage = (*Ping)(nil)
41

42
// Decode deserializes a serialized Ping message stored in the passed io.Reader
43
// observing the specified protocol version.
44
//
45
// This is part of the lnwire.Message interface.
UNCOV
46
func (p *Ping) Decode(r io.Reader, pver uint32) error {
×
UNCOV
47
        err := ReadElements(r, &p.NumPongBytes, &p.PaddingBytes)
×
UNCOV
48
        if err != nil {
×
UNCOV
49
                return err
×
UNCOV
50
        }
×
51

UNCOV
52
        if p.NumPongBytes > MaxPongBytes {
×
UNCOV
53
                return ErrMaxPongBytesExceeded
×
UNCOV
54
        }
×
55

UNCOV
56
        return nil
×
57
}
58

59
// Encode serializes the target Ping into the passed io.Writer observing the
60
// protocol version specified.
61
//
62
// This is part of the lnwire.Message interface.
UNCOV
63
func (p *Ping) Encode(w *bytes.Buffer, pver uint32) error {
×
UNCOV
64
        if err := WriteUint16(w, p.NumPongBytes); err != nil {
×
UNCOV
65
                return err
×
66
        }
×
67

UNCOV
68
        return WritePingPayload(w, p.PaddingBytes)
×
69
}
70

71
// MsgType returns the integer uniquely identifying this message type on the
72
// wire.
73
//
74
// This is part of the lnwire.Message interface.
UNCOV
75
func (p *Ping) MsgType() MessageType {
×
UNCOV
76
        return MsgPing
×
UNCOV
77
}
×
78

79
// SerializedSize returns the serialized size of the message in bytes.
80
//
81
// This is part of the lnwire.SizeableMessage interface.
NEW
82
func (p *Ping) SerializedSize() (uint32, error) {
×
NEW
83
        return MessageSerializedSize(p)
×
NEW
84
}
×
85

86
// RandTestMessage populates the message with random data suitable for testing.
87
// It uses the rapid testing framework to generate random values.
88
//
89
// This is part of the TestMessage interface.
NEW
90
func (p *Ping) RandTestMessage(t *rapid.T) Message {
×
NEW
91
        numPongBytes := uint16(rapid.IntRange(0, int(MaxPongBytes)).Draw(
×
NEW
92
                t, "numPongBytes"),
×
NEW
93
        )
×
NEW
94

×
NEW
95
        // Generate padding bytes (but keeping within allowed message size)
×
NEW
96
        // MaxMsgBody - 2 (for NumPongBytes) - 2 (for padding length)
×
NEW
97
        maxPaddingLen := MaxMsgBody - 4
×
NEW
98
        paddingLen := rapid.IntRange(0, int(maxPaddingLen)).Draw(
×
NEW
99
                t, "paddingLen",
×
NEW
100
        )
×
NEW
101
        padding := make(PingPayload, paddingLen)
×
NEW
102

×
NEW
103
        // Fill padding with random bytes
×
NEW
104
        for i := 0; i < paddingLen; i++ {
×
NEW
105
                padding[i] = byte(rapid.IntRange(0, 255).Draw(
×
NEW
106
                        t, fmt.Sprintf("paddingByte%d", i)),
×
NEW
107
                )
×
NEW
108
        }
×
109

NEW
110
        return &Ping{
×
NEW
111
                NumPongBytes: numPongBytes,
×
NEW
112
                PaddingBytes: padding,
×
NEW
113
        }
×
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc