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

lightningnetwork / lnd / 17011395530

16 Aug 2025 06:08PM UTC coverage: 57.298% (-9.5%) from 66.765%
17011395530

Pull #10167

github

web-flow
Merge 3c250722d into fb1adfc21
Pull Request #10167: multi: bump Go to 1.24.6

99112 of 172975 relevant lines covered (57.3%)

1.78 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.
23
func RandPartialSig(t *rapid.T) *PartialSig {
×
24
        // Generate random private key bytes
×
25
        sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "privKeyBytes")
×
26

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

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

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

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

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

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

×
58
        return pub
×
59
}
×
60

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

×
67
        return c
×
68
}
×
69

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

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

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

90
        return featureVec
×
91
}
92

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

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

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

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

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.
124
func RandPaymentPreimage(t *rapid.T) [32]byte {
×
125
        var preimage [32]byte
×
126
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "preimage")
×
127
        copy(preimage[:], bytes)
×
128

×
129
        return preimage
×
130
}
×
131

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

×
138
        return hash
×
139
}
×
140

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

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

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

×
152
        return alias
×
153
}
×
154

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

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

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

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

197
        return addresses
×
198
}
199

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

×
205
        numRecords := rapid.IntRange(0, 5).Draw(t, "numCustomRecords")
×
206
        customRecords := make(CustomRecords)
×
207

×
208
        if numRecords == 0 {
×
209
                return nil, nil
×
210
        }
×
211

212
        rangeStart := 0
×
213
        rangeStop := int(CustomTypeStart)
×
214
        if custom {
×
215
                rangeStart = 70_000
×
216
                rangeStop = 100_000
×
217
        }
×
218

219
        ignoreSet := fn.NewSet[uint64]()
×
220
        for i := 0; i < numRecords; i++ {
×
221
                recordType := uint64(
×
222
                        rapid.IntRange(rangeStart, rangeStop).
×
223
                                Filter(func(i int) bool {
×
224
                                        return !ignoreRecords.Contains(
×
225
                                                uint64(i),
×
226
                                        )
×
227
                                }).
×
228
                                Draw(
229
                                        t, fmt.Sprintf("recordType-%d", i),
230
                                ),
231
                )
232
                recordLen := rapid.IntRange(4, 64).Draw(
×
233
                        t, fmt.Sprintf("recordLen-%d", i),
×
234
                )
×
235
                record := rapid.SliceOfN(
×
236
                        rapid.Byte(), recordLen, recordLen,
×
237
                ).Draw(t, fmt.Sprintf("record-%d", i))
×
238

×
239
                customRecords[recordType] = record
×
240

×
241
                ignoreSet.Add(recordType)
×
242
        }
243

244
        return customRecords, ignoreSet
×
245
}
246

247
// RandMusig2Nonce generates a random musig2 nonce.
248
func RandMusig2Nonce(t *rapid.T) Musig2Nonce {
×
249
        var nonce Musig2Nonce
×
250
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "nonce")
×
251
        copy(nonce[:], bytes)
×
252

×
253
        return nonce
×
254
}
×
255

256
// RandExtraOpaqueData generates random extra opaque data.
257
func RandExtraOpaqueData(t *rapid.T,
258
        ignoreRecords fn.Set[uint64]) ExtraOpaqueData {
×
259

×
260
        // Make some random records.
×
261
        cRecords, _ := RandCustomRecords(t, ignoreRecords, false)
×
262
        if cRecords == nil {
×
263
                return ExtraOpaqueData{}
×
264
        }
×
265

266
        // Encode those records as opaque data.
267
        recordBytes, err := cRecords.Serialize()
×
268
        require.NoError(t, err)
×
269

×
270
        return ExtraOpaqueData(recordBytes)
×
271
}
272

273
// RandOpaqueReason generates a random opaque reason for HTLC failures.
274
func RandOpaqueReason(t *rapid.T) OpaqueReason {
×
275
        reasonLen := rapid.IntRange(32, 300).Draw(t, "reasonLen")
×
276
        return rapid.SliceOfN(rapid.Byte(), reasonLen, reasonLen).Draw(
×
277
                t, "opaqueReason",
×
278
        )
×
279
}
×
280

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

×
307
        // Choose a random code from the list.
×
308
        idx := rapid.IntRange(0, len(validCodes)-1).Draw(t, "failCodeIndex")
×
309

×
310
        return validCodes[idx]
×
311
}
×
312

313
// RandSHA256Hash generates a random SHA256 hash.
314
func RandSHA256Hash(t *rapid.T) [sha256.Size]byte {
×
315
        var hash [sha256.Size]byte
×
316
        bytes := rapid.SliceOfN(rapid.Byte(), sha256.Size, sha256.Size).Draw(
×
317
                t, "sha256Hash",
×
318
        )
×
319
        copy(hash[:], bytes)
×
320

×
321
        return hash
×
322
}
×
323

324
// RandDeliveryAddress generates a random delivery address (script).
325
func RandDeliveryAddress(t *rapid.T) DeliveryAddress {
×
326
        addrLen := rapid.IntRange(1, 34).Draw(t, "addrLen")
×
327

×
328
        return rapid.SliceOfN(rapid.Byte(), addrLen, addrLen).Draw(
×
329
                t, "deliveryAddress",
×
330
        )
×
331
}
×
332

333
// RandChannelType generates a random channel type.
334
func RandChannelType(t *rapid.T) *ChannelType {
×
335
        vec := RandFeatureVector(t)
×
336
        chanType := ChannelType(*vec)
×
337

×
338
        return &chanType
×
339
}
×
340

341
// RandLeaseExpiry generates a random lease expiry.
342
func RandLeaseExpiry(t *rapid.T) *LeaseExpiry {
×
343
        exp := LeaseExpiry(
×
344
                uint32(rapid.IntRange(1000, 1000000).Draw(t, "leaseExpiry")),
×
345
        )
×
346

×
347
        return &exp
×
348
}
×
349

350
// RandOutPoint generates a random transaction outpoint.
351
func RandOutPoint(t *rapid.T) wire.OutPoint {
×
352
        // Generate a random transaction ID
×
353
        var txid chainhash.Hash
×
354
        txidBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "txid")
×
355
        copy(txid[:], txidBytes)
×
356

×
357
        // Generate a random output index
×
358
        vout := uint32(rapid.IntRange(0, 10).Draw(t, "vout"))
×
359

×
360
        return wire.OutPoint{
×
361
                Hash:  txid,
×
362
                Index: vout,
×
363
        }
×
364
}
×
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