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

lightningnetwork / lnd / 13999301927

21 Mar 2025 07:18PM UTC coverage: 68.989% (+9.9%) from 59.126%
13999301927

push

github

web-flow
Merge pull request #9623 from Roasbeef/size-msg-test-msg

Size msg test msg

1461 of 1572 new or added lines in 43 files covered. (92.94%)

28 existing lines in 6 files now uncovered.

132898 of 192637 relevant lines covered (68.99%)

22200.59 hits per line

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

97.05
/lnwire/test_utils.go
1
package lnwire
2

3
import (
4
        "crypto/sha256"
5
        "fmt"
6
        "net"
7

8
        "github.com/btcsuite/btcd/btcec/v2"
9
        "github.com/btcsuite/btcd/btcec/v2/ecdsa"
10
        "github.com/btcsuite/btcd/chaincfg/chainhash"
11
        "github.com/btcsuite/btcd/wire"
12
        "github.com/lightningnetwork/lnd/fn/v2"
13
        "github.com/stretchr/testify/require"
14
        "pgregory.net/rapid"
15
)
16

17
// RandChannelUpdate generates a random ChannelUpdate message using rapid's
18
// generators.
19
func RandPartialSig(t *rapid.T) *PartialSig {
100✔
20
        // Generate random private key bytes
100✔
21
        sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "privKeyBytes")
100✔
22

100✔
23
        var s btcec.ModNScalar
100✔
24
        s.SetByteSlice(sigBytes)
100✔
25

100✔
26
        return &PartialSig{
100✔
27
                Sig: s,
100✔
28
        }
100✔
29
}
100✔
30

31
// RandPartialSigWithNonce generates a random PartialSigWithNonce using rapid
32
// generators.
33
func RandPartialSigWithNonce(t *rapid.T) *PartialSigWithNonce {
151✔
34
        sigLen := rapid.IntRange(1, 65).Draw(t, "partialSigLen")
151✔
35
        sigBytes := rapid.SliceOfN(
151✔
36
                rapid.Byte(), sigLen, sigLen,
151✔
37
        ).Draw(t, "partialSig")
151✔
38

151✔
39
        sigScalar := new(btcec.ModNScalar)
151✔
40
        sigScalar.SetByteSlice(sigBytes)
151✔
41

151✔
42
        return NewPartialSigWithNonce(
151✔
43
                RandMusig2Nonce(t), *sigScalar,
151✔
44
        )
151✔
45
}
151✔
46

47
// RandPubKey generates a random public key using rapid's generators.
48
func RandPubKey(t *rapid.T) *btcec.PublicKey {
2,394✔
49
        privKeyBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(
2,394✔
50
                t, "privKeyBytes",
2,394✔
51
        )
2,394✔
52
        _, pub := btcec.PrivKeyFromBytes(privKeyBytes)
2,394✔
53

2,394✔
54
        return pub
2,394✔
55
}
2,394✔
56

57
// RandChannelID generates a random channel ID.
58
func RandChannelID(t *rapid.T) ChannelID {
2,213✔
59
        var c ChannelID
2,213✔
60
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "channelID")
2,213✔
61
        copy(c[:], bytes)
2,213✔
62

2,213✔
63
        return c
2,213✔
64
}
2,213✔
65

66
// RandShortChannelID generates a random short channel ID.
67
func RandShortChannelID(t *rapid.T) ShortChannelID {
2,262✔
68
        return NewShortChanIDFromInt(
2,262✔
69
                uint64(rapid.IntRange(1, 100000).Draw(t, "shortChanID")),
2,262✔
70
        )
2,262✔
71
}
2,262✔
72

73
// RandFeatureVector generates a random feature vector.
74
func RandFeatureVector(t *rapid.T) *RawFeatureVector {
512✔
75
        featureVec := NewRawFeatureVector()
512✔
76

512✔
77
        // Add a random number of random feature bits
512✔
78
        numFeatures := rapid.IntRange(0, 20).Draw(t, "numFeatures")
512✔
79
        for i := 0; i < numFeatures; i++ {
4,309✔
80
                bit := FeatureBit(rapid.IntRange(0, 100).Draw(
3,797✔
81
                        t, fmt.Sprintf("featureBit-%d", i)),
3,797✔
82
                )
3,797✔
83
                featureVec.Set(bit)
3,797✔
84
        }
3,797✔
85

86
        return featureVec
512✔
87
}
88

89
// RandSignature generates a signature for testing.
90
func RandSignature(t *rapid.T) Sig {
2,465✔
91
        testRScalar := new(btcec.ModNScalar)
2,465✔
92
        testSScalar := new(btcec.ModNScalar)
2,465✔
93

2,465✔
94
        // Generate random bytes for R and S
2,465✔
95
        rBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "rBytes")
2,465✔
96
        sBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "sBytes")
2,465✔
97
        _ = testRScalar.SetByteSlice(rBytes)
2,465✔
98
        _ = testSScalar.SetByteSlice(sBytes)
2,465✔
99

2,465✔
100
        testSig := ecdsa.NewSignature(testRScalar, testSScalar)
2,465✔
101

2,465✔
102
        sig, err := NewSigFromSignature(testSig)
2,465✔
103
        if err != nil {
2,465✔
NEW
104
                panic(fmt.Sprintf("unable to create signature: %v", err))
×
105
        }
106

107
        return sig
2,465✔
108
}
109

110
// RandPaymentHash generates a random payment hash.
NEW
111
func RandPaymentHash(t *rapid.T) [32]byte {
×
NEW
112
        var hash [32]byte
×
NEW
113
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "paymentHash")
×
NEW
114
        copy(hash[:], bytes)
×
NEW
115

×
NEW
116
        return hash
×
NEW
117
}
×
118

119
// RandPaymentPreimage generates a random payment preimage.
120
func RandPaymentPreimage(t *rapid.T) [32]byte {
200✔
121
        var preimage [32]byte
200✔
122
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "preimage")
200✔
123
        copy(preimage[:], bytes)
200✔
124

200✔
125
        return preimage
200✔
126
}
200✔
127

128
// RandChainHash generates a random chain hash.
129
func RandChainHash(t *rapid.T) chainhash.Hash {
700✔
130
        var hash [32]byte
700✔
131
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "chainHash")
700✔
132
        copy(hash[:], bytes)
700✔
133

700✔
134
        return hash
700✔
135
}
700✔
136

137
// RandNodeAlias generates a random node alias.
138
func RandNodeAlias(t *rapid.T) NodeAlias {
100✔
139
        var alias NodeAlias
100✔
140
        aliasLength := rapid.IntRange(0, 32).Draw(t, "aliasLength")
100✔
141

100✔
142
        aliasBytes := rapid.StringN(
100✔
143
                0, aliasLength, aliasLength,
100✔
144
        ).Draw(t, "alias")
100✔
145

100✔
146
        copy(alias[:], aliasBytes)
100✔
147

100✔
148
        return alias
100✔
149
}
100✔
150

151
// RandNetAddrs generates random network addresses.
152
func RandNetAddrs(t *rapid.T) []net.Addr {
100✔
153
        numAddresses := rapid.IntRange(0, 5).Draw(t, "numAddresses")
100✔
154
        if numAddresses == 0 {
129✔
155
                return nil
29✔
156
        }
29✔
157

158
        addresses := make([]net.Addr, numAddresses)
71✔
159
        for i := 0; i < numAddresses; i++ {
269✔
160
                addressType := rapid.IntRange(0, 1).Draw(
198✔
161
                        t, fmt.Sprintf("addressType-%d", i),
198✔
162
                )
198✔
163

198✔
164
                switch addressType {
198✔
165
                // IPv4.
166
                case 0:
101✔
167
                        ipBytes := rapid.SliceOfN(rapid.Byte(), 4, 4).Draw(
101✔
168
                                t, fmt.Sprintf("ipv4-%d", i),
101✔
169
                        )
101✔
170
                        port := rapid.IntRange(1, 65535).Draw(
101✔
171
                                t, fmt.Sprintf("port-%d", i),
101✔
172
                        )
101✔
173
                        addresses[i] = &net.TCPAddr{
101✔
174
                                IP:   ipBytes,
101✔
175
                                Port: port,
101✔
176
                        }
101✔
177

178
                // IPv6.
179
                case 1:
97✔
180
                        ipBytes := rapid.SliceOfN(rapid.Byte(), 16, 16).Draw(
97✔
181
                                t, fmt.Sprintf("ipv6-%d", i),
97✔
182
                        )
97✔
183
                        port := rapid.IntRange(1, 65535).Draw(
97✔
184
                                t, fmt.Sprintf("port-%d", i),
97✔
185
                        )
97✔
186
                        addresses[i] = &net.TCPAddr{
97✔
187
                                IP:   ipBytes,
97✔
188
                                Port: port,
97✔
189
                        }
97✔
190
                }
191
        }
192

193
        return addresses
71✔
194
}
195

196
// RandCustomRecords generates random custom TLV records.
197
func RandCustomRecords(t *rapid.T,
198
        ignoreRecords fn.Set[uint64],
199
        custom bool) (CustomRecords, fn.Set[uint64]) {
3,389✔
200

3,389✔
201
        numRecords := rapid.IntRange(0, 5).Draw(t, "numCustomRecords")
3,389✔
202
        customRecords := make(CustomRecords)
3,389✔
203

3,389✔
204
        if numRecords == 0 {
4,100✔
205
                return nil, nil
711✔
206
        }
711✔
207

208
        rangeStart := 0
2,678✔
209
        rangeStop := int(CustomTypeStart)
2,678✔
210
        if custom {
2,968✔
211
                rangeStart = 70_000
290✔
212
                rangeStop = 100_000
290✔
213
        }
290✔
214

215
        ignoreSet := fn.NewSet[uint64]()
2,678✔
216
        for i := 0; i < numRecords; i++ {
10,321✔
217
                recordType := uint64(
7,643✔
218
                        rapid.IntRange(rangeStart, rangeStop).
7,643✔
219
                                Filter(func(i int) bool {
15,286✔
220
                                        return !ignoreRecords.Contains(
7,643✔
221
                                                uint64(i),
7,643✔
222
                                        )
7,643✔
223
                                }).
7,643✔
224
                                Draw(
225
                                        t, fmt.Sprintf("recordType-%d", i),
226
                                ),
227
                )
228
                recordLen := rapid.IntRange(4, 64).Draw(
7,643✔
229
                        t, fmt.Sprintf("recordLen-%d", i),
7,643✔
230
                )
7,643✔
231
                record := rapid.SliceOfN(
7,643✔
232
                        rapid.Byte(), recordLen, recordLen,
7,643✔
233
                ).Draw(t, fmt.Sprintf("record-%d", i))
7,643✔
234

7,643✔
235
                customRecords[recordType] = record
7,643✔
236

7,643✔
237
                ignoreSet.Add(recordType)
7,643✔
238
        }
239

240
        return customRecords, ignoreSet
2,678✔
241
}
242

243
// RandMusig2Nonce generates a random musig2 nonce.
244
func RandMusig2Nonce(t *rapid.T) Musig2Nonce {
551✔
245
        var nonce Musig2Nonce
551✔
246
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "nonce")
551✔
247
        copy(nonce[:], bytes)
551✔
248

551✔
249
        return nonce
551✔
250
}
551✔
251

252
// RandExtraOpaqueData generates random extra opaque data.
253
func RandExtraOpaqueData(t *rapid.T,
254
        ignoreRecords fn.Set[uint64]) ExtraOpaqueData {
3,004✔
255

3,004✔
256
        // Make some random records.
3,004✔
257
        cRecords, _ := RandCustomRecords(t, ignoreRecords, false)
3,004✔
258
        if cRecords == nil {
3,620✔
259
                return ExtraOpaqueData{}
616✔
260
        }
616✔
261

262
        // Encode those records as opaque data.
263
        recordBytes, err := cRecords.Serialize()
2,388✔
264
        require.NoError(t, err)
2,388✔
265

2,388✔
266
        return ExtraOpaqueData(recordBytes)
2,388✔
267
}
268

269
// RandOpaqueReason generates a random opaque reason for HTLC failures.
270
func RandOpaqueReason(t *rapid.T) OpaqueReason {
100✔
271
        reasonLen := rapid.IntRange(32, 300).Draw(t, "reasonLen")
100✔
272
        return rapid.SliceOfN(rapid.Byte(), reasonLen, reasonLen).Draw(
100✔
273
                t, "opaqueReason",
100✔
274
        )
100✔
275
}
100✔
276

277
// RandFailCode generates a random HTLC failure code.
278
func RandFailCode(t *rapid.T) FailCode {
100✔
279
        // List of known failure codes to choose from Using only the documented
100✔
280
        // codes.
100✔
281
        validCodes := []FailCode{
100✔
282
                CodeInvalidRealm,
100✔
283
                CodeTemporaryNodeFailure,
100✔
284
                CodePermanentNodeFailure,
100✔
285
                CodeRequiredNodeFeatureMissing,
100✔
286
                CodePermanentChannelFailure,
100✔
287
                CodeRequiredChannelFeatureMissing,
100✔
288
                CodeUnknownNextPeer,
100✔
289
                CodeIncorrectOrUnknownPaymentDetails,
100✔
290
                CodeIncorrectPaymentAmount,
100✔
291
                CodeFinalExpiryTooSoon,
100✔
292
                CodeInvalidOnionVersion,
100✔
293
                CodeInvalidOnionHmac,
100✔
294
                CodeInvalidOnionKey,
100✔
295
                CodeTemporaryChannelFailure,
100✔
296
                CodeChannelDisabled,
100✔
297
                CodeExpiryTooSoon,
100✔
298
                CodeMPPTimeout,
100✔
299
                CodeInvalidOnionPayload,
100✔
300
                CodeFeeInsufficient,
100✔
301
        }
100✔
302

100✔
303
        // Choose a random code from the list.
100✔
304
        idx := rapid.IntRange(0, len(validCodes)-1).Draw(t, "failCodeIndex")
100✔
305

100✔
306
        return validCodes[idx]
100✔
307
}
100✔
308

309
// RandSHA256Hash generates a random SHA256 hash.
310
func RandSHA256Hash(t *rapid.T) [sha256.Size]byte {
149✔
311
        var hash [sha256.Size]byte
149✔
312
        bytes := rapid.SliceOfN(rapid.Byte(), sha256.Size, sha256.Size).Draw(
149✔
313
                t, "sha256Hash",
149✔
314
        )
149✔
315
        copy(hash[:], bytes)
149✔
316

149✔
317
        return hash
149✔
318
}
149✔
319

320
// RandDeliveryAddress generates a random delivery address (script).
321
func RandDeliveryAddress(t *rapid.T) DeliveryAddress {
601✔
322
        addrLen := rapid.IntRange(1, 34).Draw(t, "addrLen")
601✔
323

601✔
324
        return rapid.SliceOfN(rapid.Byte(), addrLen, addrLen).Draw(
601✔
325
                t, "deliveryAddress",
601✔
326
        )
601✔
327
}
601✔
328

329
// RandChannelType generates a random channel type.
330
func RandChannelType(t *rapid.T) *ChannelType {
159✔
331
        vec := RandFeatureVector(t)
159✔
332
        chanType := ChannelType(*vec)
159✔
333

159✔
334
        return &chanType
159✔
335
}
159✔
336

337
// RandLeaseExpiry generates a random lease expiry.
338
func RandLeaseExpiry(t *rapid.T) *LeaseExpiry {
103✔
339
        exp := LeaseExpiry(
103✔
340
                uint32(rapid.IntRange(1000, 1000000).Draw(t, "leaseExpiry")),
103✔
341
        )
103✔
342

103✔
343
        return &exp
103✔
344
}
103✔
345

346
// RandOutPoint generates a random transaction outpoint.
347
func RandOutPoint(t *rapid.T) wire.OutPoint {
100✔
348
        // Generate a random transaction ID
100✔
349
        var txid chainhash.Hash
100✔
350
        txidBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "txid")
100✔
351
        copy(txid[:], txidBytes)
100✔
352

100✔
353
        // Generate a random output index
100✔
354
        vout := uint32(rapid.IntRange(0, 10).Draw(t, "vout"))
100✔
355

100✔
356
        return wire.OutPoint{
100✔
357
                Hash:  txid,
100✔
358
                Index: vout,
100✔
359
        }
100✔
360
}
100✔
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