blob: fb8ee23685a9129b39ffc45a1e492e5053210b4c [file] [log] [blame]
Yingdi Yu8dceb1d2014-02-18 12:45:10 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Yingdi Yu <yingdi0@cs.ucla.edu>
4 * See COPYING for copyright and distribution information.
5 */
6
7#include <boost/test/unit_test.hpp>
8
9#include "security/key-chain.hpp"
10#include "util/time.hpp"
11
12using namespace std;
13
14namespace ndn {
15
16BOOST_AUTO_TEST_SUITE(TestKeyChain)
17
18BOOST_AUTO_TEST_CASE (ExportIdentity)
19{
20 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
21
22 Name identity(string("/TestKeyChain/ExportIdentity/") + boost::lexical_cast<std::string>(time::now()));
23 keyChain.createIdentity(identity);
24
Yingdi Yube4150e2014-02-18 13:02:46 -080025 Block exported = keyChain.exportIdentity(identity, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080026
27 Name keyName = keyChain.getDefaultKeyNameForIdentity(identity);
28 Name certName = keyChain.getDefaultCertificateNameForKey(keyName);
29
30 keyChain.deleteIdentity(identity);
31
32 BOOST_REQUIRE(keyChain.doesIdentityExist(identity) == false);
33 BOOST_REQUIRE(keyChain.doesPublicKeyExist(keyName) == false);
34 BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
35 BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
36 BOOST_REQUIRE(keyChain.doesCertificateExist(certName) == false);
37
Yingdi Yube4150e2014-02-18 13:02:46 -080038 keyChain.importIdentity(exported, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080039
40 BOOST_REQUIRE(keyChain.doesIdentityExist(identity));
41 BOOST_REQUIRE(keyChain.doesPublicKeyExist(keyName));
42 BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE));
43 BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC));
44 BOOST_REQUIRE(keyChain.doesCertificateExist(certName));
45
46 keyChain.deleteIdentity(identity);
47
48 BOOST_REQUIRE(keyChain.doesIdentityExist(identity) == false);
49 BOOST_REQUIRE(keyChain.doesPublicKeyExist(keyName) == false);
50 BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
51 BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
52 BOOST_REQUIRE(keyChain.doesCertificateExist(certName) == false);
53}
54
Yingdi Yuc55680b2014-02-26 12:31:35 -080055BOOST_AUTO_TEST_CASE (PrepareIdentityCertificate)
56{
57 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
58
59 Name identity(string("/TestKeyChain/PrepareIdentityCertificate/") + boost::lexical_cast<std::string>(time::now()));
60 keyChain.createIdentity(identity);
61
62 vector<CertificateSubjectDescription> subjectDescription;
63 Name lowerIdentity = identity;
64 lowerIdentity.append("Lower").append(boost::lexical_cast<std::string>(time::now()));
65 Name lowerKeyName = keyChain.generateRSAKeyPair(lowerIdentity, true);
66 shared_ptr<IdentityCertificate> idCert
67 = keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
68 time::now() / 1000000,
69 time::now() / 1000000 + 630720000,
70 subjectDescription);
71 BOOST_CHECK(static_cast<bool>(idCert));
72 BOOST_CHECK(idCert->getName().getPrefix(5) == Name().append(identity).append("KEY").append("Lower"));
73
74
75 Name anotherIdentity(string("/TestKeyChain/PrepareIdentityCertificate/Another/") + boost::lexical_cast<std::string>(time::now()));
76 Name anotherKeyName = keyChain.generateRSAKeyPair(anotherIdentity, true);
77 shared_ptr<IdentityCertificate> idCert2
78 = keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
79 time::now() / 1000000,
80 time::now() / 1000000 + 630720000,
81 subjectDescription);
82 BOOST_CHECK(static_cast<bool>(idCert2));
83 BOOST_CHECK(idCert2->getName().getPrefix(5) == Name().append(anotherIdentity).append("KEY"));
84
85
86 Name wrongKeyName1;
87 shared_ptr<IdentityCertificate> idCert3
88 = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity,
89 time::now() / 1000000,
90 time::now() / 1000000 + 630720000,
91 subjectDescription);
92 BOOST_CHECK(!static_cast<bool>(idCert3));
93
94
95 Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate");
96 shared_ptr<IdentityCertificate> idCert4
97 = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity,
98 time::now() / 1000000,
99 time::now() / 1000000 + 630720000,
100 subjectDescription);
101 BOOST_CHECK(!static_cast<bool>(idCert4));
102
103
104 Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234");
105 shared_ptr<IdentityCertificate> idCert5
106 = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity,
107 time::now() / 1000000,
108 time::now() / 1000000 + 630720000,
109 subjectDescription);
110 BOOST_CHECK(!static_cast<bool>(idCert5));
111
112 keyChain.deleteIdentity(identity);
113 keyChain.deleteIdentity(lowerIdentity);
114 keyChain.deleteIdentity(anotherIdentity);
115}
116
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800117BOOST_AUTO_TEST_SUITE_END()
118
119} // namespace ndn