blob: 416972fd827ed4137b09571e2f1975f57a3711dd [file] [log] [blame]
Yingdi Yufa4ce792014-02-06 18:09:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (C) 2020 Regents of the University of California.
Yingdi Yufa4ce792014-02-06 18:09:22 -08004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Varun Patila24bd3e2020-11-24 10:08:33 +05308#include "endorse-certificate.hpp"
Yingdi Yufa4ce792014-02-06 18:09:22 -08009
10#include <boost/test/unit_test.hpp>
Qiuhan Ding52f13832015-03-06 14:05:59 -080011#include <boost/filesystem.hpp>
Yingdi Yufa4ce792014-02-06 18:09:22 -080012
Yingdi Yu0b0a7362014-08-05 16:31:30 -070013#include <ndn-cxx/encoding/buffer-stream.hpp>
Davide Pesavento7676b562020-12-14 00:41:26 -050014#include <ndn-cxx/security/key-chain.hpp>
15#include <ndn-cxx/security/transform/base64-decode.hpp>
16#include <ndn-cxx/security/transform/buffer-source.hpp>
17#include <ndn-cxx/security/transform/stream-sink.hpp>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070018#include <ndn-cxx/util/time.hpp>
19#include <ndn-cxx/util/io.hpp>
Yingdi Yufa4ce792014-02-06 18:09:22 -080020
Qiuhan Ding52f13832015-03-06 14:05:59 -080021namespace chronochat {
22namespace tests {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070023
24using std::vector;
25using std::string;
26
27using ndn::KeyChain;
Varun Patil3d850902020-11-23 12:19:14 +053028using ndn::security::Certificate;
Yingdi Yufa4ce792014-02-06 18:09:22 -080029
30BOOST_AUTO_TEST_SUITE(TestEndorseCertificate)
31
Davide Pesavento7676b562020-12-14 00:41:26 -050032static std::string
33getTestFile(std::string path)
34{
Varun Patil3d850902020-11-23 12:19:14 +053035 std::ifstream t(path);
36 std::stringstream buffer;
37 buffer << t.rdbuf();
38 return buffer.str();
39}
Yingdi Yu348f5ea2014-03-01 14:47:25 -080040
41BOOST_AUTO_TEST_CASE(IdCert)
Yingdi Yufa4ce792014-02-06 18:09:22 -080042{
Varun Patil3d850902020-11-23 12:19:14 +053043 const std::string testIdCert = getTestFile("test/cert/testid.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070044 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
45 testIdCert.size());
Varun Patil3d850902020-11-23 12:19:14 +053046 std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070047
Yingdi Yu348f5ea2014-03-01 14:47:25 -080048 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yufa4ce792014-02-06 18:09:22 -080049
Yingdi Yu0b0a7362014-08-05 16:31:30 -070050 BOOST_CHECK_EQUAL(idCert->getName().toUri(),
Varun Patil3d850902020-11-23 12:19:14 +053051 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D/self/%FD%00%00%01u%D55a%B3");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080052}
53
54BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
55{
Varun Patil3d850902020-11-23 12:19:14 +053056 const std::string testIdCert = getTestFile("test/cert/testid.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070057 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
58 testIdCert.size());
Varun Patil3d850902020-11-23 12:19:14 +053059 std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070060
Yingdi Yu348f5ea2014-03-01 14:47:25 -080061 Profile profile(*idCert);
Yingdi Yufa4ce792014-02-06 18:09:22 -080062 vector<string> endorseList;
63 endorseList.push_back("email");
64 endorseList.push_back("homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080065 EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -080066
Varun Patil3d850902020-11-23 12:19:14 +053067 KeyChain keyChain("pib-memory:", "tpm-memory:");
Qiuhan Ding52f13832015-03-06 14:05:59 -080068
Varun Patil3d850902020-11-23 12:19:14 +053069 auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256);
70 keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo()));
Yingdi Yufa4ce792014-02-06 18:09:22 -080071 const Block& endorseDataBlock = endorseCertificate.wireEncode();
72
73 Data decodedEndorseData;
Yingdi Yufa4ce792014-02-06 18:09:22 -080074 decodedEndorseData.wireDecode(endorseDataBlock);
75 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070076 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
Varun Patil3d850902020-11-23 12:19:14 +053077 "/EndorseCertificateTests");
Yingdi Yufa4ce792014-02-06 18:09:22 -080078 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2);
79 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email");
80 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070081 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
Varun Patil3d850902020-11-23 12:19:14 +053082 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D");
83 BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(),
84 "/EndorseCertificateTests/PROFILE-CERT/KEY/6%C7%E3%06%EC%8CB%3D");
85
86 const std::string testIdKey = getTestFile("test/cert/testid.key");
87 ndn::OBufferStream keyOs;
88 {
Davide Pesavento7676b562020-12-14 00:41:26 -050089 using namespace ndn::security::transform;
90 bufferSource(testIdKey) >> base64Decode() >> streamSink(keyOs);
Varun Patil3d850902020-11-23 12:19:14 +053091 }
92 BOOST_CHECK(idCert->getPublicKey() == *keyOs.buf());
Yingdi Yufa4ce792014-02-06 18:09:22 -080093}
94
Yingdi Yu348f5ea2014-03-01 14:47:25 -080095BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
96{
Varun Patil3d850902020-11-23 12:19:14 +053097 const std::string testEndorseCert = getTestFile("test/cert/endorse.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070098 boost::iostreams::stream<boost::iostreams::array_source> is(testEndorseCert.c_str(),
99 testEndorseCert.size());
100 shared_ptr<Data> rawData = ndn::io::load<Data>(is);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800101
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800102 EndorseCertificate rawEndorse(*rawData);
103 vector<string> endorseList;
104 endorseList.push_back("institution");
105 endorseList.push_back("group");
106 endorseList.push_back("advisor");
107 Name signer("/EndorseCertificateTests/Singer/ksk-1234567890");
108 EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList);
109
Varun Patil3d850902020-11-23 12:19:14 +0530110 KeyChain keyChain("pib-memory:", "tpm-memory:");
Qiuhan Ding52f13832015-03-06 14:05:59 -0800111
Varun Patil3d850902020-11-23 12:19:14 +0530112 auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256);
113 keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114
115 const Block& endorseDataBlock = endorseCertificate.wireEncode();
116
117 Data decodedEndorseData;
118 decodedEndorseData.wireDecode(endorseDataBlock);
119 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700120 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
Varun Patil3d850902020-11-23 12:19:14 +0530121 "/EndorseCertificateTests");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800122 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3);
123 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution");
124 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group");
125 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor");
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700126 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
127 "/EndorseCertificateTests/Singer/ksk-1234567890");
Varun Patil3d850902020-11-23 12:19:14 +0530128 BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(),
129 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800130}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800131
132BOOST_AUTO_TEST_SUITE_END()
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700133
Qiuhan Ding52f13832015-03-06 14:05:59 -0800134} // namespace tests
135} // namespace chronochat