watchtower/blob/justice_kit_test: use test.Run for sub tests

This commit is contained in:
Conner Fromknecht 2018-10-26 18:01:07 -07:00
parent 832bd8101c
commit ae6f06155a
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -27,7 +27,7 @@ func makeSig(i int) lnwire.Sig {
return sig return sig
} }
var descriptorTests = []struct { type descriptorTest struct {
name string name string
encVersion uint16 encVersion uint16
decVersion uint16 decVersion uint16
@ -40,7 +40,9 @@ var descriptorTests = []struct {
commitToRemoteSig lnwire.Sig commitToRemoteSig lnwire.Sig
encErr error encErr error
decErr error decErr error
}{ }
var descriptorTests = []descriptorTest{
{ {
name: "to-local only", name: "to-local only",
encVersion: 0, encVersion: 0,
@ -89,82 +91,80 @@ var descriptorTests = []struct {
// when passed invalid combinations, and that all successfully encrypted blobs // when passed invalid combinations, and that all successfully encrypted blobs
// are of constant size. // are of constant size.
func TestBlobJusticeKitEncryptDecrypt(t *testing.T) { func TestBlobJusticeKitEncryptDecrypt(t *testing.T) {
for i, test := range descriptorTests { for _, test := range descriptorTests {
boj := &blob.JusticeKit{ t.Run(test.name, func(t *testing.T) {
RevocationPubKey: test.revPubKey, testBlobJusticeKitEncryptDecrypt(t, test)
LocalDelayPubKey: test.delayPubKey, })
CSVDelay: test.csvDelay, }
CommitToLocalSig: test.commitToLocalSig, }
CommitToRemotePubKey: test.commitToRemotePubKey,
CommitToRemoteSig: test.commitToRemoteSig, func testBlobJusticeKitEncryptDecrypt(t *testing.T, test descriptorTest) {
} boj := &blob.JusticeKit{
RevocationPubKey: test.revPubKey,
// Generate a random encryption key for the blob. The key is LocalDelayPubKey: test.delayPubKey,
// sized at 32 byte, as in practice we will be using the remote CSVDelay: test.csvDelay,
// party's commitment txid as the key. CommitToLocalSig: test.commitToLocalSig,
key := make([]byte, blob.KeySize) CommitToRemotePubKey: test.commitToRemotePubKey,
_, err := io.ReadFull(rand.Reader, key) CommitToRemoteSig: test.commitToRemoteSig,
if err != nil { }
t.Fatalf("test #%d %s -- unable to generate blob "+
"encryption key: %v", i, test.name, err) // Generate a random encryption key for the blob. The key is
} // sized at 32 byte, as in practice we will be using the remote
// party's commitment txid as the key.
nonce := make([]byte, blob.NonceSize) key := make([]byte, blob.KeySize)
_, err = io.ReadFull(rand.Reader, nonce) _, err := io.ReadFull(rand.Reader, key)
if err != nil { if err != nil {
t.Fatalf("test #%d %s -- unable to generate nonce "+ t.Fatalf("unable to generate blob encryption key: %v", err)
"nonce: %v", i, test.name, err) }
}
nonce := make([]byte, blob.NonceSize)
// Encrypt the blob plaintext using the generated key and _, err = io.ReadFull(rand.Reader, nonce)
// target version for this test. if err != nil {
ctxt, err := boj.Encrypt(nonce, key, test.encVersion) t.Fatalf("unable to generate nonce nonce: %v", err)
if err != test.encErr { }
t.Fatalf("test #%d %s -- unable to encrypt blob: %v",
i, test.name, err) // Encrypt the blob plaintext using the generated key and
} else if test.encErr != nil { // target version for this test.
// If the test expected an encryption failure, we can ctxt, err := boj.Encrypt(nonce, key, test.encVersion)
// continue to the next test. if err != test.encErr {
continue t.Fatalf("unable to encrypt blob: %v", err)
} } else if test.encErr != nil {
// If the test expected an encryption failure, we can
// Ensure that all encrypted blobs are padded out to the same // continue to the next test.
// size: 282 bytes for version 0. return
if len(ctxt) != blob.Size(test.encVersion) { }
t.Fatalf("test #%d %s -- expected blob to have "+
"size %d, got %d instead", i, test.name, // Ensure that all encrypted blobs are padded out to the same
blob.Size(test.encVersion), len(ctxt)) // size: 282 bytes for version 0.
if len(ctxt) != blob.Size(test.encVersion) {
} t.Fatalf("expected blob to have size %d, got %d instead",
blob.Size(test.encVersion), len(ctxt))
// Decrypt the encrypted blob, reconstructing the original
// blob plaintext from the decrypted contents. We use the target }
// decryption version specified by this test case.
boj2, err := blob.Decrypt(nonce, key, ctxt, test.decVersion) // Decrypt the encrypted blob, reconstructing the original
if err != test.decErr { // blob plaintext from the decrypted contents. We use the target
t.Fatalf("test #%d %s -- unable to decrypt blob: %v", // decryption version specified by this test case.
i, test.name, err) boj2, err := blob.Decrypt(nonce, key, ctxt, test.decVersion)
} else if test.decErr != nil { if err != test.decErr {
// If the test expected an decryption failure, we can t.Fatalf("unable to decrypt blob: %v", err)
// continue to the next test. } else if test.decErr != nil {
continue // If the test expected an decryption failure, we can
} // continue to the next test.
return
// Check that the decrypted blob properly reports whether it has }
// a to-remote output or not.
if boj2.HasCommitToRemoteOutput() != test.hasCommitToRemote { // Check that the decrypted blob properly reports whether it has
t.Fatalf("test #%d %s -- expected blob has_to_remote "+ // a to-remote output or not.
"to be %v, got %v", i, test.name, if boj2.HasCommitToRemoteOutput() != test.hasCommitToRemote {
test.hasCommitToRemote, t.Fatalf("expected blob has_to_remote to be %v, got %v",
boj2.HasCommitToRemoteOutput()) test.hasCommitToRemote, boj2.HasCommitToRemoteOutput())
} }
// Check that the original blob plaintext matches the // Check that the original blob plaintext matches the
// one reconstructed from the encrypted blob. // one reconstructed from the encrypted blob.
if !reflect.DeepEqual(boj, boj2) { if !reflect.DeepEqual(boj, boj2) {
t.Fatalf("test #%d %s -- decrypted plaintext does not "+ t.Fatalf("decrypted plaintext does not match original, "+
"match original, want: %v, got %v", "want: %v, got %v", boj, boj2)
i, test.name, boj, boj2)
}
} }
} }