blob: 2828b0a440699ee609ef86cb5c1d334aaedc8552 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu28fd32f2014-01-28 19:03:03 -08002/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu28fd32f2014-01-28 19:03:03 -080020 */
21
Yingdi Yuf56c68f2014-04-24 21:50:13 -070022#include "security/sec-public-info-sqlite3.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080023#include "security/key-chain.hpp"
Yingdi Yu40b53092014-06-17 17:10:02 -070024#include "security/cryptopp.hpp"
25#include "encoding/buffer-stream.hpp"
Yingdi Yu9a335352014-01-31 11:57:46 -080026#include "util/time.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080027
Yingdi Yu41546342014-11-30 23:37:53 -080028#include <boost/filesystem.hpp>
29#include <boost/lexical_cast.hpp>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030#include "boost-test.hpp"
31
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080032namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080033namespace tests {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080034
Yingdi Yu41546342014-11-30 23:37:53 -080035class PibTmpPathFixture
36{
37public:
38 PibTmpPathFixture()
39 {
40 boost::system::error_code error;
41 tmpPath = boost::filesystem::temp_directory_path(error);
42 BOOST_REQUIRE(boost::system::errc::success == error.value());
43 tmpPath /= boost::lexical_cast<std::string>(random::generateWord32());
44 }
45
46 ~PibTmpPathFixture()
47 {
48 boost::filesystem::remove_all(tmpPath);
49 }
50
51public:
52 boost::filesystem::path tmpPath;
53};
54
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080055BOOST_AUTO_TEST_SUITE(SecuritySecPublicInfoSqlite3)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080056
Yingdi Yu40b53092014-06-17 17:10:02 -070057const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
58hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
59sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
60zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
61yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
62y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
63DwIDAQAB");
64const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
651Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
66
Yingdi Yu41546342014-11-30 23:37:53 -080067BOOST_FIXTURE_TEST_CASE(Basic, PibTmpPathFixture)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080068{
Yingdi Yu41546342014-11-30 23:37:53 -080069 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080070
Yingdi Yu41546342014-11-30 23:37:53 -080071 BOOST_CHECK(pib.doesTableExist("Identity"));
72 BOOST_CHECK(pib.doesTableExist("Key"));
73 BOOST_CHECK(pib.doesTableExist("Certificate"));
74}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075
Yingdi Yu41546342014-11-30 23:37:53 -080076BOOST_FIXTURE_TEST_CASE(TpmLocatorTest, PibTmpPathFixture)
77{
78 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080079
Yingdi Yu41546342014-11-30 23:37:53 -080080 BOOST_REQUIRE_THROW(pib.getTpmLocator(), SecPublicInfo::Error);
81 pib.addIdentity("/test/id1");
82 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070083
Yingdi Yu41546342014-11-30 23:37:53 -080084 // Pib does not have tpmInfo set yet, setTpmInfo simply set the tpmInfo.
85 std::string tpmLocator("tpm-file:");
86 tpmLocator.append((tmpPath / "tpm").generic_string());
87 pib.setTpmLocator(tpmLocator);
88 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070089
Yingdi Yu41546342014-11-30 23:37:53 -080090 BOOST_REQUIRE_NO_THROW(pib.getTpmLocator());
91 BOOST_CHECK_EQUAL(tpmLocator, pib.getTpmLocator());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070092
Yingdi Yu41546342014-11-30 23:37:53 -080093 // Pib has tpmInfo set, set a different tpmInfo will reset Pib content.
94 std::string tpmLocator3("tpm-osxkeychain:");
95 pib.setTpmLocator(tpmLocator3);
96 BOOST_CHECK(!pib.doesIdentityExist("/test/id1"));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080097}
98
Yingdi Yu40b53092014-06-17 17:10:02 -070099BOOST_AUTO_TEST_CASE(KeyTypeRsa)
100{
101 using namespace CryptoPP;
102
103 OBufferStream os;
104 StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(),
105 true, new Base64Decoder(new FileSink(os)));
106
107 shared_ptr<PublicKey> rsaKey;
108 BOOST_REQUIRE_NO_THROW(rsaKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
109 os.buf()->size())));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700110 Name rsaKeyName("/TestSecPublicInfoSqlite3/KeyType/RSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700111 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800112 pib.addKey(rsaKeyName, *rsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700113
114 BOOST_CHECK_EQUAL(KEY_TYPE_RSA, pib.getPublicKeyType(rsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700115
116 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/RSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700117}
118
119BOOST_AUTO_TEST_CASE(KeyTypeEcdsa)
120{
121 using namespace CryptoPP;
122
123 OBufferStream os;
124 StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(),
125 true, new Base64Decoder(new FileSink(os)));
126
127 shared_ptr<PublicKey> ecdsaKey;
128 BOOST_REQUIRE_NO_THROW(ecdsaKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
129 os.buf()->size())));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700130 Name ecdsaKeyName("/TestSecPublicInfoSqlite3/KeyType/ECDSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700131 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800132 pib.addKey(ecdsaKeyName, *ecdsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700133
134 BOOST_CHECK_EQUAL(KEY_TYPE_ECDSA, pib.getPublicKeyType(ecdsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700135 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/ECDSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700136}
137
138BOOST_AUTO_TEST_CASE(KeyTypeNonExist)
139{
140 Name nullKeyName("/TestSecPublicInfoSqlite3/KeyType/Null");
141 SecPublicInfoSqlite3 pib;
142
143 BOOST_CHECK_EQUAL(KEY_TYPE_NULL, pib.getPublicKeyType(nullKeyName));
144
145}
146
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800147BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800148
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800149} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800150} // namespace ndn