watchtower/wtdb+wtserver: pass properly sized blob in unit tests

This commit is contained in:
Conner Fromknecht 2019-06-13 17:36:09 -07:00
parent 29009fdc34
commit dce24d1d47
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
2 changed files with 88 additions and 43 deletions

@ -1,6 +1,7 @@
package wtdb_test package wtdb_test
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"io/ioutil" "io/ioutil"
"os" "os"
@ -16,6 +17,10 @@ import (
"github.com/lightningnetwork/lnd/watchtower/wtpolicy" "github.com/lightningnetwork/lnd/watchtower/wtpolicy"
) )
var (
testBlob = make([]byte, blob.Size(blob.TypeAltruistCommit))
)
// dbInit is a closure used to initialize a watchtower.DB instance and its // dbInit is a closure used to initialize a watchtower.DB instance and its
// cleanup function. // cleanup function.
type dbInit func(*testing.T) (watchtower.DB, func()) type dbInit func(*testing.T) (watchtower.DB, func())
@ -137,6 +142,9 @@ func testInsertSession(h *towerDBHarness) {
session := &wtdb.SessionInfo{ session := &wtdb.SessionInfo{
ID: id, ID: id,
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 100, MaxUpdates: 100,
}, },
RewardAddress: []byte{0x01, 0x02, 0x03}, RewardAddress: []byte{0x01, 0x02, 0x03},
@ -157,6 +165,7 @@ func testInsertSession(h *towerDBHarness) {
update := &wtdb.SessionStateUpdate{ update := &wtdb.SessionStateUpdate{
ID: id, ID: id,
SeqNum: 1, SeqNum: 1,
EncryptedBlob: testBlob,
} }
h.insertUpdate(update, nil) h.insertUpdate(update, nil)
@ -176,6 +185,9 @@ func testMultipleMatches(h *towerDBHarness) {
session := &wtdb.SessionInfo{ session := &wtdb.SessionInfo{
ID: id, ID: id,
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -186,6 +198,7 @@ func testMultipleMatches(h *towerDBHarness) {
ID: id, ID: id,
SeqNum: 1, SeqNum: 1,
Hint: hint, // Use same hint to cause multiple matches Hint: hint, // Use same hint to cause multiple matches
EncryptedBlob: testBlob,
} }
h.insertUpdate(update, nil) h.insertUpdate(update, nil)
} }
@ -267,6 +280,9 @@ func testDeleteSession(h *towerDBHarness) {
session0 := &wtdb.SessionInfo{ session0 := &wtdb.SessionInfo{
ID: *id0, ID: *id0,
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -285,6 +301,9 @@ func testDeleteSession(h *towerDBHarness) {
session1 := &wtdb.SessionInfo{ session1 := &wtdb.SessionInfo{
ID: *id1, ID: *id1,
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -297,13 +316,13 @@ func testDeleteSession(h *towerDBHarness) {
ID: *id0, ID: *id0,
Hint: hint, Hint: hint,
SeqNum: 1, SeqNum: 1,
EncryptedBlob: []byte{}, EncryptedBlob: testBlob,
} }
update1 := &wtdb.SessionStateUpdate{ update1 := &wtdb.SessionStateUpdate{
ID: *id1, ID: *id1,
Hint: hint, Hint: hint,
SeqNum: 1, SeqNum: 1,
EncryptedBlob: []byte{}, EncryptedBlob: testBlob,
} }
// Insert both updates should succeed. // Insert both updates should succeed.
@ -414,7 +433,7 @@ func runStateUpdateTest(test stateUpdateTest) func(*towerDBHarness) {
var stateUpdateNoSession = stateUpdateTest{ var stateUpdateNoSession = stateUpdateTest{
session: nil, session: nil,
updates: []*wtdb.SessionStateUpdate{ updates: []*wtdb.SessionStateUpdate{
{ID: *id(0), SeqNum: 1, LastApplied: 0}, updateFromInt(id(0), 1, 0),
}, },
updateErrs: []error{ updateErrs: []error{
wtdb.ErrSessionNotFound, wtdb.ErrSessionNotFound,
@ -425,6 +444,9 @@ var stateUpdateExhaustSession = stateUpdateTest{
session: &wtdb.SessionInfo{ session: &wtdb.SessionInfo{
ID: *id(0), ID: *id(0),
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -444,6 +466,9 @@ var stateUpdateSeqNumEqualLastApplied = stateUpdateTest{
session: &wtdb.SessionInfo{ session: &wtdb.SessionInfo{
ID: *id(0), ID: *id(0),
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -463,6 +488,9 @@ var stateUpdateSeqNumLTLastApplied = stateUpdateTest{
session: &wtdb.SessionInfo{ session: &wtdb.SessionInfo{
ID: *id(0), ID: *id(0),
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -481,6 +509,9 @@ var stateUpdateSeqNumZeroInvalid = stateUpdateTest{
session: &wtdb.SessionInfo{ session: &wtdb.SessionInfo{
ID: *id(0), ID: *id(0),
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -497,6 +528,9 @@ var stateUpdateSkipSeqNum = stateUpdateTest{
session: &wtdb.SessionInfo{ session: &wtdb.SessionInfo{
ID: *id(0), ID: *id(0),
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -513,6 +547,9 @@ var stateUpdateRevertSeqNum = stateUpdateTest{
session: &wtdb.SessionInfo{ session: &wtdb.SessionInfo{
ID: *id(0), ID: *id(0),
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -531,6 +568,9 @@ var stateUpdateRevertLastApplied = stateUpdateTest{
session: &wtdb.SessionInfo{ session: &wtdb.SessionInfo{
ID: *id(0), ID: *id(0),
Policy: wtpolicy.Policy{ Policy: wtpolicy.Policy{
TxPolicy: wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit,
},
MaxUpdates: 3, MaxUpdates: 3,
}, },
RewardAddress: []byte{}, RewardAddress: []byte{},
@ -710,12 +750,14 @@ func updateFromInt(id *wtdb.SessionID, i int,
copy(hint[:4], id[:4]) copy(hint[:4], id[:4])
binary.BigEndian.PutUint16(hint[4:6], uint16(i)) binary.BigEndian.PutUint16(hint[4:6], uint16(i))
blobSize := blob.Size(blob.TypeAltruistCommit)
return &wtdb.SessionStateUpdate{ return &wtdb.SessionStateUpdate{
ID: *id, ID: *id,
Hint: hint, Hint: hint,
SeqNum: uint16(i), SeqNum: uint16(i),
LastApplied: lastApplied, LastApplied: lastApplied,
EncryptedBlob: []byte{byte(i)}, EncryptedBlob: bytes.Repeat([]byte{byte(i)}, blobSize),
} }
} }

@ -27,6 +27,8 @@ var (
addrScript, _ = txscript.PayToAddrScript(addr) addrScript, _ = txscript.PayToAddrScript(addr)
testnetChainHash = *chaincfg.TestNet3Params.GenesisHash testnetChainHash = *chaincfg.TestNet3Params.GenesisHash
testBlob = make([]byte, blob.Size(blob.TypeAltruistCommit))
) )
// randPubKey generates a new secp keypair, and returns the public key. // randPubKey generates a new secp keypair, and returns the public key.
@ -302,6 +304,7 @@ func testServerCreateSession(t *testing.T, i int, test createSessionTestCase) {
update := &wtwire.StateUpdate{ update := &wtwire.StateUpdate{
SeqNum: 1, SeqNum: 1,
IsComplete: 1, IsComplete: 1,
EncryptedBlob: testBlob,
} }
sendMsg(t, update, peer, timeoutDuration) sendMsg(t, update, peer, timeoutDuration)
@ -323,8 +326,8 @@ func testServerCreateSession(t *testing.T, i int, test createSessionTestCase) {
// Ensure that the server's reply matches our expected response for a // Ensure that the server's reply matches our expected response for a
// duplicate send. // duplicate send.
if !reflect.DeepEqual(reply, test.expDupReply) { if !reflect.DeepEqual(reply, test.expDupReply) {
t.Fatalf("[test %d] expected reply %v, got %d", t.Fatalf("[test %d] expected reply %v, got %v",
i, test.expReply, reply) i, test.expDupReply, reply)
} }
// Finally, check that the server tore down the connection. // Finally, check that the server tore down the connection.
@ -355,10 +358,10 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 2, LastApplied: 1}, {SeqNum: 2, LastApplied: 1, EncryptedBlob: testBlob},
{SeqNum: 3, LastApplied: 2}, {SeqNum: 3, LastApplied: 2, EncryptedBlob: testBlob},
{SeqNum: 3, LastApplied: 3}, {SeqNum: 3, LastApplied: 3, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{Code: wtwire.CodeOK, LastApplied: 1}, {Code: wtwire.CodeOK, LastApplied: 1},
@ -385,7 +388,7 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 2, LastApplied: 0}, {SeqNum: 2, LastApplied: 0, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{ {
@ -409,9 +412,9 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 2, LastApplied: 0}, {SeqNum: 2, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{Code: wtwire.CodeOK, LastApplied: 1}, {Code: wtwire.CodeOK, LastApplied: 1},
@ -437,10 +440,10 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 2, LastApplied: 1}, {SeqNum: 2, LastApplied: 1, EncryptedBlob: testBlob},
{SeqNum: 3, LastApplied: 2}, {SeqNum: 3, LastApplied: 2, EncryptedBlob: testBlob},
{SeqNum: 4, LastApplied: 1}, {SeqNum: 4, LastApplied: 1, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{Code: wtwire.CodeOK, LastApplied: 1}, {Code: wtwire.CodeOK, LastApplied: 1},
@ -465,11 +468,11 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 2, LastApplied: 1}, {SeqNum: 2, LastApplied: 1, EncryptedBlob: testBlob},
nil, // Wait for read timeout to drop conn, then reconnect. nil, // Wait for read timeout to drop conn, then reconnect.
{SeqNum: 3, LastApplied: 2}, {SeqNum: 3, LastApplied: 2, EncryptedBlob: testBlob},
{SeqNum: 4, LastApplied: 3}, {SeqNum: 4, LastApplied: 3, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{Code: wtwire.CodeOK, LastApplied: 1}, {Code: wtwire.CodeOK, LastApplied: 1},
@ -495,11 +498,11 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 2, LastApplied: 0}, {SeqNum: 2, LastApplied: 0, EncryptedBlob: testBlob},
nil, // Wait for read timeout to drop conn, then reconnect. nil, // Wait for read timeout to drop conn, then reconnect.
{SeqNum: 3, LastApplied: 0}, {SeqNum: 3, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 4, LastApplied: 3}, {SeqNum: 4, LastApplied: 3, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{Code: wtwire.CodeOK, LastApplied: 1}, {Code: wtwire.CodeOK, LastApplied: 1},
@ -525,12 +528,12 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 2, LastApplied: 0}, {SeqNum: 2, LastApplied: 0, EncryptedBlob: testBlob},
nil, // Wait for read timeout to drop conn, then reconnect. nil, // Wait for read timeout to drop conn, then reconnect.
{SeqNum: 2, LastApplied: 0}, {SeqNum: 2, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 3, LastApplied: 0}, {SeqNum: 3, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 4, LastApplied: 3}, {SeqNum: 4, LastApplied: 3, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{Code: wtwire.CodeOK, LastApplied: 1}, {Code: wtwire.CodeOK, LastApplied: 1},
@ -556,10 +559,10 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 1, LastApplied: 0}, {SeqNum: 1, LastApplied: 0, EncryptedBlob: testBlob},
{SeqNum: 2, LastApplied: 1}, {SeqNum: 2, LastApplied: 1, EncryptedBlob: testBlob},
{SeqNum: 3, LastApplied: 2}, {SeqNum: 3, LastApplied: 2, EncryptedBlob: testBlob},
{SeqNum: 4, LastApplied: 3}, {SeqNum: 4, LastApplied: 3, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{Code: wtwire.CodeOK, LastApplied: 1}, {Code: wtwire.CodeOK, LastApplied: 1},
@ -586,7 +589,7 @@ var stateUpdateTests = []stateUpdateTestCase{
SweepFeeRate: 1, SweepFeeRate: 1,
}, },
updates: []*wtwire.StateUpdate{ updates: []*wtwire.StateUpdate{
{SeqNum: 0, LastApplied: 0}, {SeqNum: 0, LastApplied: 0, EncryptedBlob: testBlob},
}, },
replies: []*wtwire.StateUpdateReply{ replies: []*wtwire.StateUpdateReply{
{ {