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

lightningnetwork / lnd / 17917482292

22 Sep 2025 01:50PM UTC coverage: 56.562% (-10.1%) from 66.668%
17917482292

Pull #10182

github

web-flow
Merge 9efe3bd8c into 055fb436e
Pull Request #10182: Aux feature bits

32 of 68 new or added lines in 5 files covered. (47.06%)

29734 existing lines in 467 files now uncovered.

98449 of 174056 relevant lines covered (56.56%)

1.18 hits per line

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

0.0
/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
// charset contains valid UTF-8 characters that can be used to generate random
18
// strings for testing purposes.
19
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
20

21
// RandPartialSig generates a random ParialSig message using rapid's
22
// generators.
UNCOV
23
func RandPartialSig(t *rapid.T) *PartialSig {
×
UNCOV
24
        // Generate random private key bytes
×
UNCOV
25
        sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "privKeyBytes")
×
UNCOV
26

×
UNCOV
27
        var s btcec.ModNScalar
×
UNCOV
28
        s.SetByteSlice(sigBytes)
×
UNCOV
29

×
UNCOV
30
        return &PartialSig{
×
UNCOV
31
                Sig: s,
×
UNCOV
32
        }
×
UNCOV
33
}
×
34

35
// RandPartialSigWithNonce generates a random PartialSigWithNonce using rapid
36
// generators.
UNCOV
37
func RandPartialSigWithNonce(t *rapid.T) *PartialSigWithNonce {
×
UNCOV
38
        sigLen := rapid.IntRange(1, 65).Draw(t, "partialSigLen")
×
UNCOV
39
        sigBytes := rapid.SliceOfN(
×
UNCOV
40
                rapid.Byte(), sigLen, sigLen,
×
UNCOV
41
        ).Draw(t, "partialSig")
×
UNCOV
42

×
UNCOV
43
        sigScalar := new(btcec.ModNScalar)
×
UNCOV
44
        sigScalar.SetByteSlice(sigBytes)
×
UNCOV
45

×
UNCOV
46
        return NewPartialSigWithNonce(
×
UNCOV
47
                RandMusig2Nonce(t), *sigScalar,
×
UNCOV
48
        )
×
UNCOV
49
}
×
50

51
// RandPubKey generates a random public key using rapid's generators.
UNCOV
52
func RandPubKey(t *rapid.T) *btcec.PublicKey {
×
UNCOV
53
        privKeyBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(
×
UNCOV
54
                t, "privKeyBytes",
×
UNCOV
55
        )
×
UNCOV
56
        _, pub := btcec.PrivKeyFromBytes(privKeyBytes)
×
UNCOV
57

×
UNCOV
58
        return pub
×
UNCOV
59
}
×
60

61
// RandChannelID generates a random channel ID.
UNCOV
62
func RandChannelID(t *rapid.T) ChannelID {
×
UNCOV
63
        var c ChannelID
×
UNCOV
64
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "channelID")
×
UNCOV
65
        copy(c[:], bytes)
×
UNCOV
66

×
UNCOV
67
        return c
×
UNCOV
68
}
×
69

70
// RandShortChannelID generates a random short channel ID.
UNCOV
71
func RandShortChannelID(t *rapid.T) ShortChannelID {
×
UNCOV
72
        return NewShortChanIDFromInt(
×
UNCOV
73
                uint64(rapid.IntRange(1, 100000).Draw(t, "shortChanID")),
×
UNCOV
74
        )
×
UNCOV
75
}
×
76

77
// RandFeatureVector generates a random feature vector.
UNCOV
78
func RandFeatureVector(t *rapid.T) *RawFeatureVector {
×
UNCOV
79
        featureVec := NewRawFeatureVector()
×
UNCOV
80

×
UNCOV
81
        // Add a random number of random feature bits
×
UNCOV
82
        numFeatures := rapid.IntRange(0, 20).Draw(t, "numFeatures")
×
UNCOV
83
        for i := 0; i < numFeatures; i++ {
×
UNCOV
84
                bit := FeatureBit(rapid.IntRange(0, 100).Draw(
×
UNCOV
85
                        t, fmt.Sprintf("featureBit-%d", i)),
×
UNCOV
86
                )
×
UNCOV
87
                featureVec.Set(bit)
×
UNCOV
88
        }
×
89

UNCOV
90
        return featureVec
×
91
}
92

93
// RandSignature generates a signature for testing.
UNCOV
94
func RandSignature(t *rapid.T) Sig {
×
UNCOV
95
        testRScalar := new(btcec.ModNScalar)
×
UNCOV
96
        testSScalar := new(btcec.ModNScalar)
×
UNCOV
97

×
UNCOV
98
        // Generate random bytes for R and S
×
UNCOV
99
        rBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "rBytes")
×
UNCOV
100
        sBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "sBytes")
×
UNCOV
101
        _ = testRScalar.SetByteSlice(rBytes)
×
UNCOV
102
        _ = testSScalar.SetByteSlice(sBytes)
×
UNCOV
103

×
UNCOV
104
        testSig := ecdsa.NewSignature(testRScalar, testSScalar)
×
UNCOV
105

×
UNCOV
106
        sig, err := NewSigFromSignature(testSig)
×
UNCOV
107
        if err != nil {
×
108
                panic(fmt.Sprintf("unable to create signature: %v", err))
×
109
        }
110

UNCOV
111
        return sig
×
112
}
113

114
// RandPaymentHash generates a random payment hash.
115
func RandPaymentHash(t *rapid.T) [32]byte {
×
116
        var hash [32]byte
×
117
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "paymentHash")
×
118
        copy(hash[:], bytes)
×
119

×
120
        return hash
×
121
}
×
122

123
// RandPaymentPreimage generates a random payment preimage.
UNCOV
124
func RandPaymentPreimage(t *rapid.T) [32]byte {
×
UNCOV
125
        var preimage [32]byte
×
UNCOV
126
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "preimage")
×
UNCOV
127
        copy(preimage[:], bytes)
×
UNCOV
128

×
UNCOV
129
        return preimage
×
UNCOV
130
}
×
131

132
// RandChainHash generates a random chain hash.
UNCOV
133
func RandChainHash(t *rapid.T) chainhash.Hash {
×
UNCOV
134
        var hash [32]byte
×
UNCOV
135
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "chainHash")
×
UNCOV
136
        copy(hash[:], bytes)
×
UNCOV
137

×
UNCOV
138
        return hash
×
UNCOV
139
}
×
140

141
// RandNodeAlias generates a random node alias.
UNCOV
142
func RandNodeAlias(t *rapid.T) NodeAlias {
×
UNCOV
143
        var alias NodeAlias
×
UNCOV
144
        aliasLength := rapid.IntRange(0, 32).Draw(t, "aliasLength")
×
UNCOV
145

×
UNCOV
146
        aliasBytes := rapid.SliceOfN(
×
UNCOV
147
                rapid.SampledFrom([]rune(charset)), aliasLength, aliasLength,
×
UNCOV
148
        ).Draw(t, "alias")
×
UNCOV
149

×
UNCOV
150
        copy(alias[:], string(aliasBytes))
×
UNCOV
151

×
UNCOV
152
        return alias
×
UNCOV
153
}
×
154

155
// RandNetAddrs generates random network addresses.
UNCOV
156
func RandNetAddrs(t *rapid.T) []net.Addr {
×
UNCOV
157
        numAddresses := rapid.IntRange(0, 5).Draw(t, "numAddresses")
×
UNCOV
158
        if numAddresses == 0 {
×
UNCOV
159
                return nil
×
UNCOV
160
        }
×
161

UNCOV
162
        addresses := make([]net.Addr, numAddresses)
×
UNCOV
163
        for i := 0; i < numAddresses; i++ {
×
UNCOV
164
                addressType := rapid.IntRange(0, 1).Draw(
×
UNCOV
165
                        t, fmt.Sprintf("addressType-%d", i),
×
UNCOV
166
                )
×
UNCOV
167

×
UNCOV
168
                switch addressType {
×
169
                // IPv4.
UNCOV
170
                case 0:
×
UNCOV
171
                        ipBytes := rapid.SliceOfN(rapid.Byte(), 4, 4).Draw(
×
UNCOV
172
                                t, fmt.Sprintf("ipv4-%d", i),
×
UNCOV
173
                        )
×
UNCOV
174
                        port := rapid.IntRange(1, 65535).Draw(
×
UNCOV
175
                                t, fmt.Sprintf("port-%d", i),
×
UNCOV
176
                        )
×
UNCOV
177
                        addresses[i] = &net.TCPAddr{
×
UNCOV
178
                                IP:   ipBytes,
×
UNCOV
179
                                Port: port,
×
UNCOV
180
                        }
×
181

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

UNCOV
197
        return addresses
×
198
}
199

200
// RandCustomRecords generates random custom TLV records.
201
func RandCustomRecords(t *rapid.T, ignoreRecords fn.Set[uint64]) (CustomRecords,
UNCOV
202
        fn.Set[uint64]) {
×
UNCOV
203

×
UNCOV
204
        customRecords, set := RandTLVRecords(
×
UNCOV
205
                t, ignoreRecords, MinCustomRecordsTlvType,
×
UNCOV
206
        )
×
UNCOV
207

×
UNCOV
208
        // Validate the custom records as a sanity check.
×
UNCOV
209
        require.NoError(t, customRecords.Validate())
×
UNCOV
210

×
UNCOV
211
        return customRecords, set
×
UNCOV
212
}
×
213

214
// RandSignedRangeRecords generates a random set of signed records in the
215
// second "signed" tlv range for pure TLV messages.
UNCOV
216
func RandSignedRangeRecords(t *rapid.T) (CustomRecords, fn.Set[uint64]) {
×
UNCOV
217
        return RandTLVRecords(t, nil, pureTLVSignedSecondRangeStart)
×
UNCOV
218
}
×
219

220
// RandTLVRecords generates custom TLV records.
221
func RandTLVRecords(t *rapid.T, ignoreRecords fn.Set[uint64],
UNCOV
222
        rangeStart int) (CustomRecords, fn.Set[uint64]) {
×
UNCOV
223

×
UNCOV
224
        numRecords := rapid.IntRange(0, 5).Draw(t, "numRecords")
×
UNCOV
225
        customRecords := make(CustomRecords)
×
UNCOV
226

×
UNCOV
227
        if numRecords == 0 {
×
UNCOV
228
                return nil, nil
×
UNCOV
229
        }
×
230

UNCOV
231
        rangeStop := rangeStart + 30_000
×
UNCOV
232

×
UNCOV
233
        ignoreSet := fn.NewSet[uint64]()
×
UNCOV
234
        for i := 0; i < numRecords; i++ {
×
UNCOV
235
                recordType := uint64(
×
UNCOV
236
                        rapid.IntRange(rangeStart, rangeStop).
×
UNCOV
237
                                Filter(func(i int) bool {
×
UNCOV
238
                                        return !ignoreRecords.Contains(
×
UNCOV
239
                                                uint64(i),
×
UNCOV
240
                                        )
×
UNCOV
241
                                }).
×
242
                                Draw(
243
                                        t, fmt.Sprintf("recordType-%d", i),
244
                                ),
245
                )
UNCOV
246
                recordLen := rapid.IntRange(4, 64).Draw(
×
UNCOV
247
                        t, fmt.Sprintf("recordLen-%d", i),
×
UNCOV
248
                )
×
UNCOV
249
                record := rapid.SliceOfN(
×
UNCOV
250
                        rapid.Byte(), recordLen, recordLen,
×
UNCOV
251
                ).Draw(t, fmt.Sprintf("record-%d", i))
×
UNCOV
252

×
UNCOV
253
                customRecords[recordType] = record
×
UNCOV
254

×
UNCOV
255
                ignoreSet.Add(recordType)
×
256
        }
257

UNCOV
258
        return customRecords, ignoreSet
×
259
}
260

261
// RandMusig2Nonce generates a random musig2 nonce.
UNCOV
262
func RandMusig2Nonce(t *rapid.T) Musig2Nonce {
×
UNCOV
263
        var nonce Musig2Nonce
×
UNCOV
264
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "nonce")
×
UNCOV
265
        copy(nonce[:], bytes)
×
UNCOV
266

×
UNCOV
267
        return nonce
×
UNCOV
268
}
×
269

270
// RandExtraOpaqueData generates random extra opaque data.
271
func RandExtraOpaqueData(t *rapid.T,
UNCOV
272
        ignoreRecords fn.Set[uint64]) ExtraOpaqueData {
×
UNCOV
273

×
UNCOV
274
        // Make some random records.
×
UNCOV
275
        cRecords, _ := RandTLVRecords(t, ignoreRecords, 0)
×
UNCOV
276
        if cRecords == nil {
×
UNCOV
277
                return ExtraOpaqueData{}
×
UNCOV
278
        }
×
279

280
        // Encode those records as opaque data.
UNCOV
281
        recordBytes, err := cRecords.Serialize()
×
UNCOV
282
        require.NoError(t, err)
×
UNCOV
283

×
UNCOV
284
        return ExtraOpaqueData(recordBytes)
×
285
}
286

287
// RandOpaqueReason generates a random opaque reason for HTLC failures.
UNCOV
288
func RandOpaqueReason(t *rapid.T) OpaqueReason {
×
UNCOV
289
        reasonLen := rapid.IntRange(32, 300).Draw(t, "reasonLen")
×
UNCOV
290
        return rapid.SliceOfN(rapid.Byte(), reasonLen, reasonLen).Draw(
×
UNCOV
291
                t, "opaqueReason",
×
UNCOV
292
        )
×
UNCOV
293
}
×
294

295
// RandFailCode generates a random HTLC failure code.
UNCOV
296
func RandFailCode(t *rapid.T) FailCode {
×
UNCOV
297
        // List of known failure codes to choose from Using only the documented
×
UNCOV
298
        // codes.
×
UNCOV
299
        validCodes := []FailCode{
×
UNCOV
300
                CodeInvalidRealm,
×
UNCOV
301
                CodeTemporaryNodeFailure,
×
UNCOV
302
                CodePermanentNodeFailure,
×
UNCOV
303
                CodeRequiredNodeFeatureMissing,
×
UNCOV
304
                CodePermanentChannelFailure,
×
UNCOV
305
                CodeRequiredChannelFeatureMissing,
×
UNCOV
306
                CodeUnknownNextPeer,
×
UNCOV
307
                CodeIncorrectOrUnknownPaymentDetails,
×
UNCOV
308
                CodeIncorrectPaymentAmount,
×
UNCOV
309
                CodeFinalExpiryTooSoon,
×
UNCOV
310
                CodeInvalidOnionVersion,
×
UNCOV
311
                CodeInvalidOnionHmac,
×
UNCOV
312
                CodeInvalidOnionKey,
×
UNCOV
313
                CodeTemporaryChannelFailure,
×
UNCOV
314
                CodeChannelDisabled,
×
UNCOV
315
                CodeExpiryTooSoon,
×
UNCOV
316
                CodeMPPTimeout,
×
UNCOV
317
                CodeInvalidOnionPayload,
×
UNCOV
318
                CodeFeeInsufficient,
×
UNCOV
319
        }
×
UNCOV
320

×
UNCOV
321
        // Choose a random code from the list.
×
UNCOV
322
        idx := rapid.IntRange(0, len(validCodes)-1).Draw(t, "failCodeIndex")
×
UNCOV
323

×
UNCOV
324
        return validCodes[idx]
×
UNCOV
325
}
×
326

327
// RandSHA256Hash generates a random SHA256 hash.
UNCOV
328
func RandSHA256Hash(t *rapid.T) [sha256.Size]byte {
×
UNCOV
329
        var hash [sha256.Size]byte
×
UNCOV
330
        bytes := rapid.SliceOfN(rapid.Byte(), sha256.Size, sha256.Size).Draw(
×
UNCOV
331
                t, "sha256Hash",
×
UNCOV
332
        )
×
UNCOV
333
        copy(hash[:], bytes)
×
UNCOV
334

×
UNCOV
335
        return hash
×
UNCOV
336
}
×
337

338
// RandDeliveryAddress generates a random delivery address (script).
UNCOV
339
func RandDeliveryAddress(t *rapid.T) DeliveryAddress {
×
UNCOV
340
        addrLen := rapid.IntRange(1, 34).Draw(t, "addrLen")
×
UNCOV
341

×
UNCOV
342
        return rapid.SliceOfN(rapid.Byte(), addrLen, addrLen).Draw(
×
UNCOV
343
                t, "deliveryAddress",
×
UNCOV
344
        )
×
UNCOV
345
}
×
346

347
// RandChannelType generates a random channel type.
UNCOV
348
func RandChannelType(t *rapid.T) *ChannelType {
×
UNCOV
349
        vec := RandFeatureVector(t)
×
UNCOV
350
        chanType := ChannelType(*vec)
×
UNCOV
351

×
UNCOV
352
        return &chanType
×
UNCOV
353
}
×
354

355
// RandLeaseExpiry generates a random lease expiry.
UNCOV
356
func RandLeaseExpiry(t *rapid.T) *LeaseExpiry {
×
UNCOV
357
        exp := LeaseExpiry(
×
UNCOV
358
                uint32(rapid.IntRange(1000, 1000000).Draw(t, "leaseExpiry")),
×
UNCOV
359
        )
×
UNCOV
360

×
UNCOV
361
        return &exp
×
UNCOV
362
}
×
363

364
// RandOutPoint generates a random transaction outpoint.
UNCOV
365
func RandOutPoint(t *rapid.T) wire.OutPoint {
×
UNCOV
366
        // Generate a random transaction ID
×
UNCOV
367
        var txid chainhash.Hash
×
UNCOV
368
        txidBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "txid")
×
UNCOV
369
        copy(txid[:], txidBytes)
×
UNCOV
370

×
UNCOV
371
        // Generate a random output index
×
UNCOV
372
        vout := uint32(rapid.IntRange(0, 10).Draw(t, "vout"))
×
UNCOV
373

×
UNCOV
374
        return wire.OutPoint{
×
UNCOV
375
                Hash:  txid,
×
UNCOV
376
                Index: vout,
×
UNCOV
377
        }
×
UNCOV
378
}
×
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