Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (C) 2020 Regents of the University of California. |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 8 | #include "endorse-certificate.hpp" |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 9 | |
| 10 | #include <boost/test/unit_test.hpp> |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 11 | #include <boost/filesystem.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 12 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 13 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 14 | #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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 18 | #include <ndn-cxx/util/time.hpp> |
| 19 | #include <ndn-cxx/util/io.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 20 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 21 | namespace chronochat { |
| 22 | namespace tests { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 23 | |
| 24 | using std::vector; |
| 25 | using std::string; |
| 26 | |
| 27 | using ndn::KeyChain; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 28 | using ndn::security::Certificate; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestEndorseCertificate) |
| 31 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 32 | static std::string |
| 33 | getTestFile(std::string path) |
| 34 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 35 | std::ifstream t(path); |
| 36 | std::stringstream buffer; |
| 37 | buffer << t.rdbuf(); |
| 38 | return buffer.str(); |
| 39 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE(IdCert) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 42 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 43 | const std::string testIdCert = getTestFile("test/cert/testid.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 44 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 45 | testIdCert.size()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 46 | std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 48 | BOOST_CHECK(static_cast<bool>(idCert)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 49 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(idCert->getName().toUri(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 51 | "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D/self/%FD%00%00%01u%D55a%B3"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(ConstructFromIdCert) |
| 55 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 56 | const std::string testIdCert = getTestFile("test/cert/testid.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 57 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 58 | testIdCert.size()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 59 | std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 61 | Profile profile(*idCert); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 62 | vector<string> endorseList; |
| 63 | endorseList.push_back("email"); |
| 64 | endorseList.push_back("homepage"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 65 | EndorseCertificate endorseCertificate(*idCert, profile, endorseList); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 66 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 67 | KeyChain keyChain("pib-memory:", "tpm-memory:"); |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 68 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 69 | auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256); |
| 70 | keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo())); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 71 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 72 | |
| 73 | Data decodedEndorseData; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 74 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 75 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 77 | "/EndorseCertificateTests"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 78 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 82 | "/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 Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 89 | using namespace ndn::security::transform; |
| 90 | bufferSource(testIdKey) >> base64Decode() >> streamSink(keyOs); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 91 | } |
| 92 | BOOST_CHECK(idCert->getPublicKey() == *keyOs.buf()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert) |
| 96 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 97 | const std::string testEndorseCert = getTestFile("test/cert/endorse.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 98 | 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 Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 101 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 102 | 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 Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 110 | KeyChain keyChain("pib-memory:", "tpm-memory:"); |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 111 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 112 | auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256); |
| 113 | keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 114 | |
| 115 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 116 | |
| 117 | Data decodedEndorseData; |
| 118 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 119 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 121 | "/EndorseCertificateTests"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 122 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
| 127 | "/EndorseCertificateTests/Singer/ksk-1234567890"); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(), |
| 129 | "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 130 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 131 | |
| 132 | BOOST_AUTO_TEST_SUITE_END() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 133 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 134 | } // namespace tests |
| 135 | } // namespace chronochat |