security: make PrivateKey::getKeySize() work in all cases

Change-Id: I9c010d57220a27c5f2a248aca53e56ae6fe5a539
diff --git a/tests/unit/security/transform/private-key.t.cpp b/tests/unit/security/transform/private-key.t.cpp
index 673f551..79b3599 100644
--- a/tests/unit/security/transform/private-key.t.cpp
+++ b/tests/unit/security/transform/private-key.t.cpp
@@ -47,6 +47,21 @@
 BOOST_AUTO_TEST_SUITE(Transform)
 BOOST_AUTO_TEST_SUITE(TestPrivateKey)
 
+BOOST_AUTO_TEST_CASE(Empty)
+{
+  // test invoking member functions on an empty (default-constructed) PrivateKey
+  PrivateKey sKey;
+  BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::NONE);
+  BOOST_CHECK_EQUAL(sKey.getKeySize(), 0);
+  BOOST_CHECK_THROW(sKey.derivePublicKey(), PrivateKey::Error);
+  const uint8_t theAnswer = 42;
+  BOOST_CHECK_THROW(sKey.decrypt(&theAnswer, sizeof(theAnswer)), PrivateKey::Error);
+  std::ostringstream os;
+  BOOST_CHECK_THROW(sKey.savePkcs1(os), PrivateKey::Error);
+  std::string passwd("password");
+  BOOST_CHECK_THROW(sKey.savePkcs8(os, passwd.data(), passwd.size()), PrivateKey::Error);
+}
+
 BOOST_AUTO_TEST_CASE(LoadRaw)
 {
   const Buffer buf(32);
@@ -64,6 +79,7 @@
 
 struct RsaKeyTestData
 {
+  const size_t keySize = 2048;
   const std::string privateKeyPkcs1 =
       "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n"
       "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n"
@@ -130,6 +146,7 @@
 
 struct EcKeyTestData
 {
+  const size_t keySize = 256;
   const std::string privateKeyPkcs1 =
       "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n"
       "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
@@ -196,6 +213,7 @@
   // load key in base64-encoded pkcs1 format
   PrivateKey sKey;
   BOOST_CHECK_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
+  BOOST_CHECK_EQUAL(sKey.getKeySize(), dataSet.keySize);
 
   std::stringstream ss2(dataSet.privateKeyPkcs1);
   PrivateKey sKey2;
@@ -204,6 +222,7 @@
   // load key in pkcs1 format
   PrivateKey sKey3;
   BOOST_CHECK_NO_THROW(sKey3.loadPkcs1(sKeyPkcs1, sKeyPkcs1Len));
+  BOOST_CHECK_EQUAL(sKey3.getKeySize(), dataSet.keySize);
 
   std::stringstream ss4;
   ss4.write(reinterpret_cast<const char*>(sKeyPkcs1), sKeyPkcs1Len);
@@ -241,6 +260,7 @@
   PrivateKey sKey5;
   BOOST_CHECK_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len,
                                              password.data(), password.size()));
+  BOOST_CHECK_EQUAL(sKey5.getKeySize(), dataSet.keySize);
 
   PrivateKey sKey6;
   BOOST_CHECK_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, pwCallback));
@@ -256,6 +276,7 @@
   // load key in pkcs8 format
   PrivateKey sKey9;
   BOOST_CHECK_NO_THROW(sKey9.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, password.data(), password.size()));
+  BOOST_CHECK_EQUAL(sKey9.getKeySize(), dataSet.keySize);
 
   PrivateKey sKey10;
   BOOST_CHECK_NO_THROW(sKey10.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, pwCallback));
@@ -274,6 +295,8 @@
   PrivateKey sKey13;
   BOOST_CHECK_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, wrongpw.data(), wrongpw.size()),
                     PrivateKey::Error);
+  BOOST_CHECK_EQUAL(sKey13.getKeyType(), KeyType::NONE);
+  BOOST_CHECK_EQUAL(sKey13.getKeySize(), 0);
 
   // save key in base64-encoded pkcs8 format
   OBufferStream os14;
@@ -398,9 +421,10 @@
 
 BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, KeyGenParams)
 {
-  unique_ptr<PrivateKey> sKey = generatePrivateKey(typename T::Params());
-  BOOST_CHECK_NE(sKey->getKeyType(), KeyType::NONE);
-  BOOST_CHECK_GT(sKey->getKeySize(), 0);
+  typename T::Params params;
+  auto sKey = generatePrivateKey(params);
+  BOOST_CHECK_EQUAL(sKey->getKeyType(), params.getKeyType());
+  BOOST_CHECK_EQUAL(sKey->getKeySize(), params.getKeySize());
 
   const uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
   OBufferStream os;
@@ -429,7 +453,7 @@
   BOOST_CHECK(result);
 
   if (typename T::canSavePkcs1()) {
-    unique_ptr<PrivateKey> sKey2 = generatePrivateKey(typename T::Params());
+    auto sKey2 = generatePrivateKey(params);
 
     OBufferStream os1;
     sKey->savePkcs1(os1);