security: simplify/cleanup PublicKey implementation

Change-Id: I9ead8916d0c6d9264e087f594b8d3aaf07366c29
diff --git a/tests/unit-tests/security/transform/public-key.t.cpp b/tests/unit-tests/security/transform/public-key.t.cpp
index 26903e7..41b0582 100644
--- a/tests/unit-tests/security/transform/public-key.t.cpp
+++ b/tests/unit-tests/security/transform/public-key.t.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -20,13 +20,14 @@
  */
 
 #include "security/transform/public-key.hpp"
-#include "security/transform/buffer-source.hpp"
-#include "security/transform/base64-decode.hpp"
-#include "security/transform/stream-sink.hpp"
-#include "encoding/buffer-stream.hpp"
 
-#include <boost/mpl/list.hpp>
+#include "encoding/buffer-stream.hpp"
+#include "security/transform.hpp"
+
 #include "boost-test.hpp"
+#include <boost/mpl/vector.hpp>
+
+#include <sstream>
 
 namespace ndn {
 namespace security {
@@ -37,12 +38,9 @@
 BOOST_AUTO_TEST_SUITE(Transform)
 BOOST_AUTO_TEST_SUITE(TestPublicKey)
 
-class RsaPublicKeyTestData
+struct RsaKeyTestData
 {
-public:
-  RsaPublicKeyTestData()
-  {
-    publicKeyPkcs8 =
+  const std::string publicKeyPkcs8 =
       "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
       "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
       "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
@@ -50,18 +48,11 @@
       "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
       "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
       "ywIDAQAB\n";
-  }
-
-public:
-  std::string publicKeyPkcs8;
 };
 
-class EcPublicKeyTestData
+struct EcKeyTestData
 {
-public:
-  EcPublicKeyTestData()
-  {
-    publicKeyPkcs8 =
+  const std::string publicKeyPkcs8 =
       "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
       "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
       "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
@@ -69,22 +60,16 @@
       "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
       "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
       "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
-  }
-
-public:
-  std::string publicKeyPkcs8;
 };
 
-typedef boost::mpl::list<RsaPublicKeyTestData,
-                         EcPublicKeyTestData> PublicKeyTestDataSets;
+using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>;
 
-BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, PublicKeyTestDataSets)
+BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
 {
   T dataSet;
 
   const uint8_t* pKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str());
   size_t pKeyPkcs8Base64Len = dataSet.publicKeyPkcs8.size();
-
   OBufferStream os;
   bufferSource(pKeyPkcs8Base64, pKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os);
   ConstBufferPtr pKeyPkcs8Buf = os.buf();
@@ -92,19 +77,19 @@
   size_t pKeyPkcs8Len = pKeyPkcs8Buf->size();
 
   PublicKey pKey1;
-  BOOST_REQUIRE_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64, pKeyPkcs8Base64Len));
+  BOOST_CHECK_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64, pKeyPkcs8Base64Len));
 
   std::stringstream ss2(dataSet.publicKeyPkcs8);
   PublicKey pKey2;
-  BOOST_REQUIRE_NO_THROW(pKey2.loadPkcs8Base64(ss2));
+  BOOST_CHECK_NO_THROW(pKey2.loadPkcs8Base64(ss2));
 
   PublicKey pKey3;
-  BOOST_REQUIRE_NO_THROW(pKey3.loadPkcs8(pKeyPkcs8, pKeyPkcs8Len));
+  BOOST_CHECK_NO_THROW(pKey3.loadPkcs8(pKeyPkcs8, pKeyPkcs8Len));
 
   std::stringstream ss4;
   ss4.write(reinterpret_cast<const char*>(pKeyPkcs8), pKeyPkcs8Len);
   PublicKey pKey4;
-  BOOST_REQUIRE_NO_THROW(pKey4.loadPkcs8(ss4));
+  BOOST_CHECK_NO_THROW(pKey4.loadPkcs8(ss4));
 
   OBufferStream os5;
   BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8Base64(os5));
@@ -117,6 +102,25 @@
                                 os6.buf()->begin(), os6.buf()->end());
 }
 
+// NOTE: We cannot test RSA encryption by comparing the computed ciphertext to
+//       a known-good one, because OAEP padding is randomized and would produce
+//       different results every time. An encrypt/decrypt round-trip test is
+//       performed in private-key.t.cpp
+
+BOOST_AUTO_TEST_CASE(UnsupportedEcEncryption)
+{
+  EcKeyTestData dataSet;
+
+  PublicKey pKey;
+  pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()),
+                       dataSet.publicKeyPkcs8.size());
+
+  OBufferStream os;
+  bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os);
+
+  BOOST_CHECK_THROW(pKey.encrypt(os.buf()->buf(), os.buf()->size()), PublicKey::Error);
+}
+
 BOOST_AUTO_TEST_SUITE_END() // TestPublicKey
 BOOST_AUTO_TEST_SUITE_END() // Transform
 BOOST_AUTO_TEST_SUITE_END() // Security