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,7 +91,14 @@ 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 {
t.Run(test.name, func(t *testing.T) {
testBlobJusticeKitEncryptDecrypt(t, test)
})
}
}
func testBlobJusticeKitEncryptDecrypt(t *testing.T, test descriptorTest) {
boj := &blob.JusticeKit{ boj := &blob.JusticeKit{
RevocationPubKey: test.revPubKey, RevocationPubKey: test.revPubKey,
LocalDelayPubKey: test.delayPubKey, LocalDelayPubKey: test.delayPubKey,
@ -105,34 +114,30 @@ func TestBlobJusticeKitEncryptDecrypt(t *testing.T) {
key := make([]byte, blob.KeySize) key := make([]byte, blob.KeySize)
_, err := io.ReadFull(rand.Reader, key) _, err := io.ReadFull(rand.Reader, key)
if err != nil { if err != nil {
t.Fatalf("test #%d %s -- unable to generate blob "+ t.Fatalf("unable to generate blob encryption key: %v", err)
"encryption key: %v", i, test.name, err)
} }
nonce := make([]byte, blob.NonceSize) nonce := make([]byte, blob.NonceSize)
_, err = io.ReadFull(rand.Reader, nonce) _, err = io.ReadFull(rand.Reader, nonce)
if err != nil { if err != nil {
t.Fatalf("test #%d %s -- unable to generate nonce "+ t.Fatalf("unable to generate nonce nonce: %v", err)
"nonce: %v", i, test.name, err)
} }
// Encrypt the blob plaintext using the generated key and // Encrypt the blob plaintext using the generated key and
// target version for this test. // target version for this test.
ctxt, err := boj.Encrypt(nonce, key, test.encVersion) ctxt, err := boj.Encrypt(nonce, key, test.encVersion)
if err != test.encErr { if err != test.encErr {
t.Fatalf("test #%d %s -- unable to encrypt blob: %v", t.Fatalf("unable to encrypt blob: %v", err)
i, test.name, err)
} else if test.encErr != nil { } else if test.encErr != nil {
// If the test expected an encryption failure, we can // If the test expected an encryption failure, we can
// continue to the next test. // continue to the next test.
continue return
} }
// Ensure that all encrypted blobs are padded out to the same // Ensure that all encrypted blobs are padded out to the same
// size: 282 bytes for version 0. // size: 282 bytes for version 0.
if len(ctxt) != blob.Size(test.encVersion) { if len(ctxt) != blob.Size(test.encVersion) {
t.Fatalf("test #%d %s -- expected blob to have "+ t.Fatalf("expected blob to have size %d, got %d instead",
"size %d, got %d instead", i, test.name,
blob.Size(test.encVersion), len(ctxt)) blob.Size(test.encVersion), len(ctxt))
} }
@ -142,29 +147,24 @@ func TestBlobJusticeKitEncryptDecrypt(t *testing.T) {
// decryption version specified by this test case. // decryption version specified by this test case.
boj2, err := blob.Decrypt(nonce, key, ctxt, test.decVersion) boj2, err := blob.Decrypt(nonce, key, ctxt, test.decVersion)
if err != test.decErr { if err != test.decErr {
t.Fatalf("test #%d %s -- unable to decrypt blob: %v", t.Fatalf("unable to decrypt blob: %v", err)
i, test.name, err)
} else if test.decErr != nil { } else if test.decErr != nil {
// If the test expected an decryption failure, we can // If the test expected an decryption failure, we can
// continue to the next test. // continue to the next test.
continue return
} }
// Check that the decrypted blob properly reports whether it has // Check that the decrypted blob properly reports whether it has
// a to-remote output or not. // a to-remote output or not.
if boj2.HasCommitToRemoteOutput() != test.hasCommitToRemote { if boj2.HasCommitToRemoteOutput() != test.hasCommitToRemote {
t.Fatalf("test #%d %s -- expected blob has_to_remote "+ t.Fatalf("expected blob has_to_remote to be %v, got %v",
"to be %v, got %v", i, test.name, test.hasCommitToRemote, 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)
}
} }
} }