mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-08 14:31:53 +02:00
watchtower/wtclient: upgrade pkg to use require
Upgrade all the tests in the wtclient package to make use of the `require` package.
This commit is contained in:
@ -2,9 +2,6 @@ package wtclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
@ -12,7 +9,6 @@ import (
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/input"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
@ -54,14 +50,6 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func makeAddrSlice(size int) []byte {
|
||||
addr := make([]byte, size)
|
||||
if _, err := io.ReadFull(rand.Reader, addr); err != nil {
|
||||
panic("cannot make addr")
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
type backupTaskTest struct {
|
||||
name string
|
||||
chanID lnwire.ChannelID
|
||||
@ -502,35 +490,12 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
|
||||
// Assert that all parameters set during initialization are properly
|
||||
// populated.
|
||||
if task.id.ChanID != test.chanID {
|
||||
t.Fatalf("channel id mismatch, want: %s, got: %s",
|
||||
test.chanID, task.id.ChanID)
|
||||
}
|
||||
|
||||
if task.id.CommitHeight != test.breachInfo.RevokedStateNum {
|
||||
t.Fatalf("commit height mismatch, want: %d, got: %d",
|
||||
test.breachInfo.RevokedStateNum, task.id.CommitHeight)
|
||||
}
|
||||
|
||||
if task.totalAmt != test.expTotalAmt {
|
||||
t.Fatalf("total amount mismatch, want: %d, got: %v",
|
||||
test.expTotalAmt, task.totalAmt)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(task.breachInfo, test.breachInfo) {
|
||||
t.Fatalf("breach info mismatch, want: %v, got: %v",
|
||||
test.breachInfo, task.breachInfo)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(task.toLocalInput, test.expToLocalInput) {
|
||||
t.Fatalf("to-local input mismatch, want: %v, got: %v",
|
||||
test.expToLocalInput, task.toLocalInput)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(task.toRemoteInput, test.expToRemoteInput) {
|
||||
t.Fatalf("to-local input mismatch, want: %v, got: %v",
|
||||
test.expToRemoteInput, task.toRemoteInput)
|
||||
}
|
||||
require.Equal(t, test.chanID, task.id.ChanID)
|
||||
require.Equal(t, test.breachInfo.RevokedStateNum, task.id.CommitHeight)
|
||||
require.Equal(t, test.expTotalAmt, task.totalAmt)
|
||||
require.Equal(t, test.breachInfo, task.breachInfo)
|
||||
require.Equal(t, test.expToLocalInput, task.toLocalInput)
|
||||
require.Equal(t, test.expToRemoteInput, task.toRemoteInput)
|
||||
|
||||
// Reconstruct the expected input.Inputs that will be returned by the
|
||||
// task's inputs() method.
|
||||
@ -545,34 +510,24 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
// Assert that the inputs method returns the correct slice of
|
||||
// input.Inputs.
|
||||
inputs := task.inputs()
|
||||
if !reflect.DeepEqual(expInputs, inputs) {
|
||||
t.Fatalf("inputs mismatch, want: %v, got: %v",
|
||||
expInputs, inputs)
|
||||
}
|
||||
require.Equal(t, expInputs, inputs)
|
||||
|
||||
// Now, bind the session to the task. If successful, this locks in the
|
||||
// session's negotiated parameters and allows the backup task to derive
|
||||
// the final free variables in the justice transaction.
|
||||
err := task.bindSession(test.session)
|
||||
if err != test.bindErr {
|
||||
t.Fatalf("expected: %v when binding session, got: %v",
|
||||
test.bindErr, err)
|
||||
}
|
||||
require.ErrorIs(t, err, test.bindErr)
|
||||
|
||||
// Exit early if the bind was supposed to fail. But first, we check that
|
||||
// all fields set during a bind are still unset. This ensure that a
|
||||
// failed bind doesn't have side-effects if the task is retried with a
|
||||
// different session.
|
||||
if test.bindErr != nil {
|
||||
if task.blobType != 0 {
|
||||
t.Fatalf("blob type should not be set on failed bind, "+
|
||||
"found: %s", task.blobType)
|
||||
}
|
||||
require.Zerof(t, task.blobType, "blob type should not be set "+
|
||||
"on failed bind, found: %s", task.blobType)
|
||||
|
||||
if task.outputs != nil {
|
||||
t.Fatalf("justice outputs should not be set on failed bind, "+
|
||||
"found: %v", task.outputs)
|
||||
}
|
||||
require.Nilf(t, task.outputs, "justice outputs should not be "+
|
||||
" set on failed bind, found: %v", task.outputs)
|
||||
|
||||
return
|
||||
}
|
||||
@ -580,10 +535,7 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
// Otherwise, the binding succeeded. Assert that all values set during
|
||||
// the bind are properly populated.
|
||||
policy := test.session.Policy
|
||||
if task.blobType != policy.BlobType {
|
||||
t.Fatalf("blob type mismatch, want: %s, got %s",
|
||||
policy.BlobType, task.blobType)
|
||||
}
|
||||
require.Equal(t, policy.BlobType, task.blobType)
|
||||
|
||||
// Compute the expected outputs on the justice transaction.
|
||||
var expOutputs = []*wire.TxOut{
|
||||
@ -603,10 +555,7 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
}
|
||||
|
||||
// Assert that the computed outputs match our expected outputs.
|
||||
if !reflect.DeepEqual(expOutputs, task.outputs) {
|
||||
t.Fatalf("justice txn output mismatch, want: %v,\ngot: %v",
|
||||
spew.Sdump(expOutputs), spew.Sdump(task.outputs))
|
||||
}
|
||||
require.Equal(t, expOutputs, task.outputs)
|
||||
|
||||
// Now, we'll construct, sign, and encrypt the blob containing the parts
|
||||
// needed to reconstruct the justice transaction.
|
||||
@ -616,10 +565,7 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
// Verify that the breach hint matches the breach txid's prefix.
|
||||
breachTxID := test.breachInfo.BreachTxHash
|
||||
expHint := blob.NewBreachHintFromHash(&breachTxID)
|
||||
if hint != expHint {
|
||||
t.Fatalf("breach hint mismatch, want: %x, got: %v",
|
||||
expHint, hint)
|
||||
}
|
||||
require.Equal(t, expHint, hint)
|
||||
|
||||
// Decrypt the return blob to obtain the JusticeKit containing its
|
||||
// contents.
|
||||
@ -634,14 +580,8 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
|
||||
// Assert that the blob contained the serialized revocation and to-local
|
||||
// pubkeys.
|
||||
if !bytes.Equal(jKit.RevocationPubKey[:], expRevPK) {
|
||||
t.Fatalf("revocation pk mismatch, want: %x, got: %x",
|
||||
expRevPK, jKit.RevocationPubKey[:])
|
||||
}
|
||||
if !bytes.Equal(jKit.LocalDelayPubKey[:], expToLocalPK) {
|
||||
t.Fatalf("revocation pk mismatch, want: %x, got: %x",
|
||||
expToLocalPK, jKit.LocalDelayPubKey[:])
|
||||
}
|
||||
require.Equal(t, expRevPK, jKit.RevocationPubKey[:])
|
||||
require.Equal(t, expToLocalPK, jKit.LocalDelayPubKey[:])
|
||||
|
||||
// Determine if the breach transaction has a to-remote output and/or
|
||||
// to-local output to spend from. Note the seemingly-reversed
|
||||
@ -650,32 +590,19 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
hasToLocal := test.breachInfo.RemoteOutputSignDesc != nil
|
||||
|
||||
// If the to-remote output is present, assert that the to-remote public
|
||||
// key was included in the blob.
|
||||
if hasToRemote &&
|
||||
!bytes.Equal(jKit.CommitToRemotePubKey[:], expToRemotePK) {
|
||||
t.Fatalf("mismatch to-remote pubkey, want: %x, got: %x",
|
||||
expToRemotePK, jKit.CommitToRemotePubKey)
|
||||
}
|
||||
|
||||
// Otherwise if the to-local output is not present, assert that a blank
|
||||
// public key was inserted.
|
||||
if !hasToRemote &&
|
||||
!bytes.Equal(jKit.CommitToRemotePubKey[:], zeroPK[:]) {
|
||||
t.Fatalf("mismatch to-remote pubkey, want: %x, got: %x",
|
||||
zeroPK, jKit.CommitToRemotePubKey)
|
||||
// key was included in the blob. Otherwise assert that a blank public
|
||||
// key was inserted.
|
||||
if hasToRemote {
|
||||
require.Equal(t, expToRemotePK, jKit.CommitToRemotePubKey[:])
|
||||
} else {
|
||||
require.Equal(t, zeroPK[:], jKit.CommitToRemotePubKey[:])
|
||||
}
|
||||
|
||||
// Assert that the CSV is encoded in the blob.
|
||||
if jKit.CSVDelay != test.breachInfo.RemoteDelay {
|
||||
t.Fatalf("mismatch remote delay, want: %d, got: %v",
|
||||
test.breachInfo.RemoteDelay, jKit.CSVDelay)
|
||||
}
|
||||
require.Equal(t, test.breachInfo.RemoteDelay, jKit.CSVDelay)
|
||||
|
||||
// Assert that the sweep pkscript is included.
|
||||
if !bytes.Equal(jKit.SweepAddress, test.expSweepScript) {
|
||||
t.Fatalf("sweep pkscript mismatch, want: %x, got: %x",
|
||||
test.expSweepScript, jKit.SweepAddress)
|
||||
}
|
||||
require.Equal(t, test.expSweepScript, jKit.SweepAddress)
|
||||
|
||||
// Finally, verify that the signatures are encoded in the justice kit.
|
||||
// We don't validate the actual signatures produced here, since at the
|
||||
@ -684,18 +611,20 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
|
||||
// TODO(conner): include signature validation checks
|
||||
|
||||
emptyToLocalSig := bytes.Equal(jKit.CommitToLocalSig[:], zeroSig[:])
|
||||
switch {
|
||||
case hasToLocal && emptyToLocalSig:
|
||||
t.Fatalf("to-local signature should not be empty")
|
||||
case !hasToLocal && !emptyToLocalSig:
|
||||
t.Fatalf("to-local signature should be empty")
|
||||
if hasToLocal {
|
||||
require.False(t, emptyToLocalSig, "to-local signature should "+
|
||||
"not be empty")
|
||||
} else {
|
||||
require.True(t, emptyToLocalSig, "to-local signature should "+
|
||||
"be empty")
|
||||
}
|
||||
|
||||
emptyToRemoteSig := bytes.Equal(jKit.CommitToRemoteSig[:], zeroSig[:])
|
||||
switch {
|
||||
case hasToRemote && emptyToRemoteSig:
|
||||
t.Fatalf("to-remote signature should not be empty")
|
||||
case !hasToRemote && !emptyToRemoteSig:
|
||||
t.Fatalf("to-remote signature should be empty")
|
||||
if hasToRemote {
|
||||
require.False(t, emptyToRemoteSig, "to-remote signature "+
|
||||
"should not be empty")
|
||||
} else {
|
||||
require.True(t, emptyToRemoteSig, "to-remote signature "+
|
||||
"should be empty")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user