blob: 1009bd94bb2c81bc3de9dd0b8bde59f0654a2de8 [file] [log] [blame]
Yingdi Yu202a2e92015-07-12 16:49:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "security/transform/public-key.hpp"
23#include "security/transform/buffer-source.hpp"
24#include "security/transform/base64-decode.hpp"
25#include "security/transform/stream-sink.hpp"
26#include "encoding/buffer-stream.hpp"
27
28#include <boost/mpl/list.hpp>
29#include "boost-test.hpp"
30
31namespace ndn {
32namespace security {
33namespace transform {
34namespace tests {
35
36BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(Transform)
38BOOST_AUTO_TEST_SUITE(TestPublicKey)
39
40class RsaPublicKeyTestData
41{
42public:
43 RsaPublicKeyTestData()
44 {
45 publicKeyPkcs8 =
46 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
47 "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
48 "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
49 "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
50 "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
51 "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
52 "ywIDAQAB\n";
53 }
54
55public:
56 std::string publicKeyPkcs8;
57};
58
59class EcdsaPublicKeyTestData
60{
61public:
62 EcdsaPublicKeyTestData()
63 {
64 publicKeyPkcs8 =
65 "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
66 "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
67 "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
68 "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n"
69 "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
70 "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
71 "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
72 }
73
74public:
75 std::string publicKeyPkcs8;
76};
77
78typedef boost::mpl::list<RsaPublicKeyTestData,
79 EcdsaPublicKeyTestData> PublicKeyTestDataSets;
80
81BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, PublicKeyTestDataSets)
82{
83 T dataSet;
84
85 const uint8_t* pKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str());
86 size_t pKeyPkcs8Base64Len = dataSet.publicKeyPkcs8.size();
87
88 OBufferStream os;
89 bufferSource(pKeyPkcs8Base64, pKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os);
90 ConstBufferPtr pKeyPkcs8Buf = os.buf();
91 const uint8_t* pKeyPkcs8 = pKeyPkcs8Buf->buf();
92 size_t pKeyPkcs8Len = pKeyPkcs8Buf->size();
93
94 PublicKey pKey1;
95 BOOST_REQUIRE_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64, pKeyPkcs8Base64Len));
96
97 std::stringstream ss2(dataSet.publicKeyPkcs8);
98 PublicKey pKey2;
99 BOOST_REQUIRE_NO_THROW(pKey2.loadPkcs8Base64(ss2));
100
101 PublicKey pKey3;
102 BOOST_REQUIRE_NO_THROW(pKey3.loadPkcs8(pKeyPkcs8, pKeyPkcs8Len));
103
104 std::stringstream ss4;
105 ss4.write(reinterpret_cast<const char*>(pKeyPkcs8), pKeyPkcs8Len);
106 PublicKey pKey4;
107 BOOST_REQUIRE_NO_THROW(pKey4.loadPkcs8(ss4));
108
109 OBufferStream os5;
110 BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8Base64(os5));
111 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8Base64, pKeyPkcs8Base64 + pKeyPkcs8Base64Len,
112 os5.buf()->begin(), os5.buf()->end());
113
114 OBufferStream os6;
115 BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8(os6));
116 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8, pKeyPkcs8 + pKeyPkcs8Len,
117 os6.buf()->begin(), os6.buf()->end());
118}
119
120BOOST_AUTO_TEST_SUITE_END() // TestPublicKey
121BOOST_AUTO_TEST_SUITE_END() // Transform
122BOOST_AUTO_TEST_SUITE_END() // Security
123
124} // namespace tests
125} // namespace transform
126} // namespace security
127} // namespace ndn