blob: baa2ecf04afcc3b430863073c485ea9b2bcb4a3f [file] [log] [blame]
Yingdi Yufa4ce792014-02-06 18:09:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
8#if __clang__
9#pragma clang diagnostic push
10#pragma clang diagnostic ignored "-Wtautological-compare"
11#pragma clang diagnostic ignored "-Wunused-function"
12#elif __GNUC__
13#pragma GCC diagnostic ignored "-Wunused-function"
14#endif
15
16#include <boost/test/unit_test.hpp>
Qiuhan Ding52f13832015-03-06 14:05:59 -080017#include <boost/filesystem.hpp>
Yingdi Yufa4ce792014-02-06 18:09:22 -080018
Yingdi Yu0b0a7362014-08-05 16:31:30 -070019#include <ndn-cxx/security/key-chain.hpp>
20#include <ndn-cxx/encoding/buffer-stream.hpp>
21#include <ndn-cxx/util/time.hpp>
22#include <ndn-cxx/util/io.hpp>
23#include "cryptopp.hpp"
24#include "endorse-certificate.hpp"
Yingdi Yufa4ce792014-02-06 18:09:22 -080025
Qiuhan Ding52f13832015-03-06 14:05:59 -080026namespace chronochat {
27namespace tests {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070028
29using std::vector;
30using std::string;
31
32using ndn::KeyChain;
Varun Patil3d850902020-11-23 12:19:14 +053033using ndn::security::Certificate;
Yingdi Yufa4ce792014-02-06 18:09:22 -080034
35BOOST_AUTO_TEST_SUITE(TestEndorseCertificate)
36
Varun Patil3d850902020-11-23 12:19:14 +053037std::string
38getTestFile(std::string path) {
39 std::ifstream t(path);
40 std::stringstream buffer;
41 buffer << t.rdbuf();
42 return buffer.str();
43}
Yingdi Yu348f5ea2014-03-01 14:47:25 -080044
45BOOST_AUTO_TEST_CASE(IdCert)
Yingdi Yufa4ce792014-02-06 18:09:22 -080046{
Varun Patil3d850902020-11-23 12:19:14 +053047 const std::string testIdCert = getTestFile("test/cert/testid.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070048 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
49 testIdCert.size());
Varun Patil3d850902020-11-23 12:19:14 +053050 std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070051
Yingdi Yu348f5ea2014-03-01 14:47:25 -080052 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yufa4ce792014-02-06 18:09:22 -080053
Yingdi Yu0b0a7362014-08-05 16:31:30 -070054 BOOST_CHECK_EQUAL(idCert->getName().toUri(),
Varun Patil3d850902020-11-23 12:19:14 +053055 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D/self/%FD%00%00%01u%D55a%B3");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080056}
57
58BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
59{
Varun Patil3d850902020-11-23 12:19:14 +053060 const std::string testIdCert = getTestFile("test/cert/testid.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070061 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
62 testIdCert.size());
Varun Patil3d850902020-11-23 12:19:14 +053063 std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070064
Yingdi Yu348f5ea2014-03-01 14:47:25 -080065 Profile profile(*idCert);
Yingdi Yufa4ce792014-02-06 18:09:22 -080066 vector<string> endorseList;
67 endorseList.push_back("email");
68 endorseList.push_back("homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080069 EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -080070
Varun Patil3d850902020-11-23 12:19:14 +053071 KeyChain keyChain("pib-memory:", "tpm-memory:");
Qiuhan Ding52f13832015-03-06 14:05:59 -080072
Varun Patil3d850902020-11-23 12:19:14 +053073 auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256);
74 keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo()));
Yingdi Yufa4ce792014-02-06 18:09:22 -080075 const Block& endorseDataBlock = endorseCertificate.wireEncode();
76
77 Data decodedEndorseData;
Yingdi Yufa4ce792014-02-06 18:09:22 -080078 decodedEndorseData.wireDecode(endorseDataBlock);
79 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070080 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
Varun Patil3d850902020-11-23 12:19:14 +053081 "/EndorseCertificateTests");
Yingdi Yufa4ce792014-02-06 18:09:22 -080082 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2);
83 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email");
84 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070085 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
Varun Patil3d850902020-11-23 12:19:14 +053086 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D");
87 BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(),
88 "/EndorseCertificateTests/PROFILE-CERT/KEY/6%C7%E3%06%EC%8CB%3D");
89
90 const std::string testIdKey = getTestFile("test/cert/testid.key");
91 ndn::OBufferStream keyOs;
92 {
93 using namespace CryptoPP;
94 StringSource(testIdKey, true, new Base64Decoder(new FileSink(keyOs)));
95 }
96 BOOST_CHECK(idCert->getPublicKey() == *keyOs.buf());
Yingdi Yufa4ce792014-02-06 18:09:22 -080097}
98
Yingdi Yu348f5ea2014-03-01 14:47:25 -080099BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
100{
Varun Patil3d850902020-11-23 12:19:14 +0530101 const std::string testEndorseCert = getTestFile("test/cert/endorse.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700102 boost::iostreams::stream<boost::iostreams::array_source> is(testEndorseCert.c_str(),
103 testEndorseCert.size());
104 shared_ptr<Data> rawData = ndn::io::load<Data>(is);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800105
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800106 EndorseCertificate rawEndorse(*rawData);
107 vector<string> endorseList;
108 endorseList.push_back("institution");
109 endorseList.push_back("group");
110 endorseList.push_back("advisor");
111 Name signer("/EndorseCertificateTests/Singer/ksk-1234567890");
112 EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList);
113
Varun Patil3d850902020-11-23 12:19:14 +0530114 KeyChain keyChain("pib-memory:", "tpm-memory:");
Qiuhan Ding52f13832015-03-06 14:05:59 -0800115
Varun Patil3d850902020-11-23 12:19:14 +0530116 auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256);
117 keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800118
119 const Block& endorseDataBlock = endorseCertificate.wireEncode();
120
121 Data decodedEndorseData;
122 decodedEndorseData.wireDecode(endorseDataBlock);
123 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700124 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
Varun Patil3d850902020-11-23 12:19:14 +0530125 "/EndorseCertificateTests");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800126 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3);
127 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution");
128 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group");
129 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor");
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700130 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
131 "/EndorseCertificateTests/Singer/ksk-1234567890");
Varun Patil3d850902020-11-23 12:19:14 +0530132 BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(),
133 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800135
136BOOST_AUTO_TEST_SUITE_END()
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700137
Qiuhan Ding52f13832015-03-06 14:05:59 -0800138} // namespace tests
139} // namespace chronochat