Skip to content

Commit

Permalink
[FAB-7462] bccsp/sw enable parallel unit tests
Browse files Browse the repository at this point in the history
Change-Id: I22b49de6fdb1eeff2969db344b0aad729d199e6b
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed Dec 14, 2017
1 parent 2b428bf commit 2bc628b
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 143 deletions.
35 changes: 28 additions & 7 deletions bccsp/sw/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

// TestCBCPKCS7EncryptCBCPKCS7Decrypt encrypts using CBCPKCS7Encrypt and decrypts using CBCPKCS7Decrypt.
func TestCBCPKCS7EncryptCBCPKCS7Decrypt(t *testing.T) {
t.Parallel()

// Note: The purpose of this test is not to test AES-256 in CBC mode's strength
// ... but rather to verify the code wrapping/unwrapping the cipher.
Expand All @@ -55,11 +56,11 @@ func TestCBCPKCS7EncryptCBCPKCS7Decrypt(t *testing.T) {
if string(ptext[:]) != string(decrypted[:]) {
t.Fatal("Decrypt( Encrypt( ptext ) ) != ptext: Ciphertext decryption with the same key must result in the original plaintext!")
}

}

// TestPKCS7Padding verifies the PKCS#7 padding, using a human readable plaintext.
func TestPKCS7Padding(t *testing.T) {
t.Parallel()

// 0 byte/length ptext
ptext := []byte("")
Expand Down Expand Up @@ -109,7 +110,6 @@ func TestPKCS7Padding(t *testing.T) {
if !bytes.Equal(result, expected) {
t.Fatal("Padding error! Expected: '", expected, "', received: '", result, "'")
}

}

// aes.BlockSize length ptext
Expand All @@ -126,11 +126,11 @@ func TestPKCS7Padding(t *testing.T) {
if !bytes.Equal(expected, result) {
t.Fatal("Padding error! Expected: '", expected, "', received: '", result, "'")
}

}

// TestPKCS7UnPadding verifies the PKCS#7 unpadding, using a human readable plaintext.
func TestPKCS7UnPadding(t *testing.T) {
t.Parallel()

// 0 byte/length ptext
expected := []byte("")
Expand Down Expand Up @@ -184,7 +184,6 @@ func TestPKCS7UnPadding(t *testing.T) {
if !bytes.Equal(result, expected) {
t.Fatal("UnPadding error! Expected: '", expected, "', received: '", result, "'")
}

}

// aes.BlockSize length ptext
Expand All @@ -202,6 +201,7 @@ func TestPKCS7UnPadding(t *testing.T) {
// TestCBCEncryptCBCPKCS7Decrypt_BlockSizeLengthPlaintext verifies that CBCPKCS7Decrypt returns an error
// when attempting to decrypt ciphertext of an irreproducible length.
func TestCBCEncryptCBCPKCS7Decrypt_BlockSizeLengthPlaintext(t *testing.T) {
t.Parallel()

// One of the purposes of this test is to also document and clarify the expected behavior, i.e., that an extra
// block is appended to the message at the padding stage, as per the spec of PKCS#7 v1.5 [see RFC-2315 p.21]
Expand All @@ -225,6 +225,7 @@ func TestCBCEncryptCBCPKCS7Decrypt_BlockSizeLengthPlaintext(t *testing.T) {
// TestCBCPKCS7EncryptCBCDecrypt_ExpectingCorruptMessage verifies that CBCDecrypt can decrypt the unpadded
// version of the ciphertext, of a message of BlockSize length.
func TestCBCPKCS7EncryptCBCDecrypt_ExpectingCorruptMessage(t *testing.T) {
t.Parallel()

// One of the purposes of this test is to also document and clarify the expected behavior, i.e., that an extra
// block is appended to the message at the padding stage, as per the spec of PKCS#7 v1.5 [see RFC-2315 p.21]
Expand Down Expand Up @@ -253,11 +254,11 @@ func TestCBCPKCS7EncryptCBCDecrypt_ExpectingCorruptMessage(t *testing.T) {
if !bytes.Equal(decrypted[aes.BlockSize:], bytes.Repeat([]byte{byte(aes.BlockSize)}, aes.BlockSize)) {
t.Fatal("Expected extra block with padding in encrypted ptext", decrypted)
}

}

// TestCBCPKCS7Encrypt_EmptyPlaintext encrypts and pad an empty ptext. Verifying as well that the ciphertext length is as expected.
func TestCBCPKCS7Encrypt_EmptyPlaintext(t *testing.T) {
t.Parallel()

key := make([]byte, 32)
rand.Reader.Read(key)
Expand Down Expand Up @@ -285,6 +286,7 @@ func TestCBCPKCS7Encrypt_EmptyPlaintext(t *testing.T) {

// TestCBCEncrypt_EmptyPlaintext encrypts an empty message. Verifying as well that the ciphertext length is as expected.
func TestCBCEncrypt_EmptyPlaintext(t *testing.T) {
t.Parallel()

key := make([]byte, 32)
rand.Reader.Read(key)
Expand All @@ -309,6 +311,7 @@ func TestCBCEncrypt_EmptyPlaintext(t *testing.T) {

// TestCBCPKCS7Encrypt_VerifyRandomIVs encrypts twice with same key. The first 16 bytes should be different if IV is generated randomly.
func TestCBCPKCS7Encrypt_VerifyRandomIVs(t *testing.T) {
t.Parallel()

key := make([]byte, aes.BlockSize)
rand.Reader.Read(key)
Expand Down Expand Up @@ -341,6 +344,7 @@ func TestCBCPKCS7Encrypt_VerifyRandomIVs(t *testing.T) {

// TestCBCPKCS7Encrypt_CorrectCiphertextLengthCheck verifies that the returned ciphertext lengths are as expected.
func TestCBCPKCS7Encrypt_CorrectCiphertextLengthCheck(t *testing.T) {
t.Parallel()

key := make([]byte, aes.BlockSize)
rand.Reader.Read(key)
Expand All @@ -365,6 +369,7 @@ func TestCBCPKCS7Encrypt_CorrectCiphertextLengthCheck(t *testing.T) {

// TestCBCEncryptCBCDecrypt_KeyMismatch attempts to decrypt with a different key than the one used for encryption.
func TestCBCEncryptCBCDecrypt_KeyMismatch(t *testing.T) {
t.Parallel()

// Generate a random key
key := make([]byte, aes.BlockSize)
Expand Down Expand Up @@ -393,6 +398,7 @@ func TestCBCEncryptCBCDecrypt_KeyMismatch(t *testing.T) {

// TestCBCEncryptCBCDecrypt encrypts with CBCEncrypt and decrypt with CBCDecrypt.
func TestCBCEncryptCBCDecrypt(t *testing.T) {
t.Parallel()

key := make([]byte, 32)
rand.Reader.Read(key)
Expand All @@ -417,6 +423,7 @@ func TestCBCEncryptCBCDecrypt(t *testing.T) {

// TestCBCEncryptWithRandCBCDecrypt encrypts with CBCEncrypt using the passed prng and decrypt with CBCDecrypt.
func TestCBCEncryptWithRandCBCDecrypt(t *testing.T) {
t.Parallel()

key := make([]byte, 32)
rand.Reader.Read(key)
Expand All @@ -441,6 +448,7 @@ func TestCBCEncryptWithRandCBCDecrypt(t *testing.T) {

// TestCBCEncryptWithIVCBCDecrypt encrypts with CBCEncrypt using the passed IV and decrypt with CBCDecrypt.
func TestCBCEncryptWithIVCBCDecrypt(t *testing.T) {
t.Parallel()

key := make([]byte, 32)
rand.Reader.Read(key)
Expand Down Expand Up @@ -469,6 +477,7 @@ func TestCBCEncryptWithIVCBCDecrypt(t *testing.T) {

// TestAESRelatedUtilFunctions tests various functions commonly used in fabric wrt AES
func TestAESRelatedUtilFunctions(t *testing.T) {
t.Parallel()

key, err := GetRandomBytes(32)
if err != nil {
Expand Down Expand Up @@ -498,13 +507,13 @@ func TestAESRelatedUtilFunctions(t *testing.T) {
if 0 != bytes.Compare(msg, msg2) {
t.Fatalf("Wrong decryption output [%x][%x]", msg, msg2)
}

}

}

// TestVariousAESKeyEncoding tests some AES <-> PEM conversions
func TestVariousAESKeyEncoding(t *testing.T) {
t.Parallel()

key, err := GetRandomBytes(32)
if err != nil {
t.Fatalf("Failed generating AES key [%s]", err)
Expand Down Expand Up @@ -535,12 +544,16 @@ func TestVariousAESKeyEncoding(t *testing.T) {
}

func TestPkcs7UnPaddingInvalidInputs(t *testing.T) {
t.Parallel()

_, err := pkcs7UnPadding([]byte{1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
assert.Error(t, err)
assert.Equal(t, "Invalid pkcs7 padding (pad[i] != unpadding)", err.Error())
}

func TestAESCBCEncryptInvalidInputs(t *testing.T) {
t.Parallel()

_, err := aesCBCEncrypt(nil, []byte{0, 1, 2, 3})
assert.Error(t, err)
assert.Equal(t, "Invalid plaintext. It must be a multiple of the block size", err.Error())
Expand All @@ -550,6 +563,8 @@ func TestAESCBCEncryptInvalidInputs(t *testing.T) {
}

func TestAESCBCDecryptInvalidInputs(t *testing.T) {
t.Parallel()

_, err := aesCBCDecrypt([]byte{0}, []byte{1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
assert.Error(t, err)

Expand All @@ -564,6 +579,8 @@ func TestAESCBCDecryptInvalidInputs(t *testing.T) {
// TestAESCBCPKCS7EncryptorDecrypt tests the integration of
// aescbcpkcs7Encryptor and aescbcpkcs7Decryptor
func TestAESCBCPKCS7EncryptorDecrypt(t *testing.T) {
t.Parallel()

raw, err := GetRandomBytes(32)
assert.NoError(t, err)

Expand Down Expand Up @@ -606,6 +623,8 @@ func TestAESCBCPKCS7EncryptorDecrypt(t *testing.T) {
}

func TestAESCBCPKCS7EncryptorWithIVSameCiphertext(t *testing.T) {
t.Parallel()

raw, err := GetRandomBytes(32)
assert.NoError(t, err)

Expand All @@ -630,6 +649,8 @@ func TestAESCBCPKCS7EncryptorWithIVSameCiphertext(t *testing.T) {
}

func TestAESCBCPKCS7EncryptorWithRandSameCiphertext(t *testing.T) {
t.Parallel()

raw, err := GetRandomBytes(32)
assert.NoError(t, err)

Expand Down
8 changes: 8 additions & 0 deletions bccsp/sw/dummyks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,30 @@ import (
)

func TestNewDummyKeyStore(t *testing.T) {
t.Parallel()

ks := NewDummyKeyStore()
assert.NotNil(t, ks)
}

func TestDummyKeyStore_GetKey(t *testing.T) {
t.Parallel()

ks := NewDummyKeyStore()
_, err := ks.GetKey([]byte{0, 1, 2, 3, 4})
assert.Error(t, err)
}

func TestDummyKeyStore_ReadOnly(t *testing.T) {
t.Parallel()

ks := NewDummyKeyStore()
assert.True(t, ks.ReadOnly())
}

func TestDummyKeyStore_StoreKey(t *testing.T) {
t.Parallel()

ks := NewDummyKeyStore()
err := ks.StoreKey(&mocks.MockKey{})
assert.Error(t, err)
Expand Down
10 changes: 9 additions & 1 deletion bccsp/sw/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSignECDSA(t *testing.T) {
func TestSignECDSABadParameter(t *testing.T) {
// Generate a key
lowLevelKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.NoError(t, err)
Expand All @@ -46,6 +46,8 @@ func TestSignECDSA(t *testing.T) {
}

func TestVerifyECDSA(t *testing.T) {
t.Parallel()

// Generate a key
lowLevelKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.NoError(t, err)
Expand Down Expand Up @@ -77,6 +79,8 @@ func TestVerifyECDSA(t *testing.T) {
}

func TestEcdsaSignerSign(t *testing.T) {
t.Parallel()

signer := &ecdsaSigner{}
verifierPrivateKey := &ecdsaPrivateKeyVerifier{}
verifierPublicKey := &ecdsaPublicKeyKeyVerifier{}
Expand Down Expand Up @@ -109,6 +113,8 @@ func TestEcdsaSignerSign(t *testing.T) {
}

func TestEcdsaPrivateKey(t *testing.T) {
t.Parallel()

lowLevelKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.NoError(t, err)
k := &ecdsaPrivateKey{lowLevelKey}
Expand Down Expand Up @@ -141,6 +147,8 @@ func TestEcdsaPrivateKey(t *testing.T) {
}

func TestEcdsaPublicKey(t *testing.T) {
t.Parallel()

lowLevelKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.NoError(t, err)
k := &ecdsaPublicKey{&lowLevelKey.PublicKey}
Expand Down
2 changes: 2 additions & 0 deletions bccsp/sw/enc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
)

func TestEncrypt(t *testing.T) {
t.Parallel()

expectedKey := &mocks2.MockKey{}
expectedPlaintext := []byte{1, 2, 3, 4}
expectedOpts := &mocks2.EncrypterOpts{}
Expand Down
11 changes: 10 additions & 1 deletion bccsp/sw/fileks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ package sw

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestInvalidStoreKey(t *testing.T) {
ks, err := NewFileBasedKeyStore(nil, filepath.Join(os.TempDir(), "bccspks"), false)
t.Parallel()

tempDir, err := ioutil.TempDir("", "bccspks")
assert.NoError(t, err)
defer os.RemoveAll(tempDir)

ks, err := NewFileBasedKeyStore(nil, filepath.Join(tempDir, "bccspks"), false)
if err != nil {
fmt.Printf("Failed initiliazing KeyStore [%s]", err)
os.Exit(-1)
Expand Down
6 changes: 6 additions & 0 deletions bccsp/sw/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
)

func TestHash(t *testing.T) {
t.Parallel()

expectetMsg := []byte{1, 2, 3, 4}
expectedOpts := &mocks2.HashOpts{}
expectetValue := []byte{1, 2, 3, 4, 5}
Expand Down Expand Up @@ -59,6 +61,8 @@ func TestHash(t *testing.T) {
}

func TestGetHash(t *testing.T) {
t.Parallel()

expectedOpts := &mocks2.HashOpts{}
expectetValue := sha256.New()
expectedErr := errors.New("Expected Error")
Expand Down Expand Up @@ -87,6 +91,8 @@ func TestGetHash(t *testing.T) {
}

func TestHasher(t *testing.T) {
t.Parallel()

hasher := &hasher{hash: sha256.New}

msg := []byte("Hello World")
Expand Down
Loading

0 comments on commit 2bc628b

Please sign in to comment.