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

lightningnetwork / lnd / 14881417238

07 May 2025 10:44AM UTC coverage: 58.589% (-10.4%) from 68.992%
14881417238

Pull #9793

github

web-flow
Merge d971492fd into 67a40c90a
Pull Request #9793: tlv: catch unhandled type in SizeBigSize

97405 of 166252 relevant lines covered (58.59%)

1.82 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
// RandChannelUpdate generates a random ChannelUpdate message using rapid's
18
// generators.
19
func RandPartialSig(t *rapid.T) *PartialSig {
×
20
        // Generate random private key bytes
×
21
        sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "privKeyBytes")
×
22

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

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

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

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

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

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

×
54
        return pub
×
55
}
×
56

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

×
63
        return c
×
64
}
×
65

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

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

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

86
        return featureVec
×
87
}
88

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

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

×
100
        testSig := ecdsa.NewSignature(testRScalar, testSScalar)
×
101

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

107
        return sig
×
108
}
109

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

×
116
        return hash
×
117
}
×
118

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

×
125
        return preimage
×
126
}
×
127

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

×
134
        return hash
×
135
}
×
136

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

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

×
146
        copy(alias[:], aliasBytes)
×
147

×
148
        return alias
×
149
}
×
150

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

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

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

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

193
        return addresses
×
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]) {
×
200

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

×
204
        if numRecords == 0 {
×
205
                return nil, nil
×
206
        }
×
207

208
        rangeStart := 0
×
209
        rangeStop := int(CustomTypeStart)
×
210
        if custom {
×
211
                rangeStart = 70_000
×
212
                rangeStop = 100_000
×
213
        }
×
214

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

×
235
                customRecords[recordType] = record
×
236

×
237
                ignoreSet.Add(recordType)
×
238
        }
239

240
        return customRecords, ignoreSet
×
241
}
242

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

×
249
        return nonce
×
250
}
×
251

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

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

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

×
266
        return ExtraOpaqueData(recordBytes)
×
267
}
268

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

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

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

×
306
        return validCodes[idx]
×
307
}
×
308

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

×
317
        return hash
×
318
}
×
319

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

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

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

×
334
        return &chanType
×
335
}
×
336

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

×
343
        return &exp
×
344
}
×
345

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

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

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