Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 1 | /* -*- 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 Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 17 | #include <boost/filesystem.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 18 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 19 | #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 Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 25 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 26 | namespace chronochat { |
| 27 | namespace tests { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 28 | |
| 29 | using std::vector; |
| 30 | using std::string; |
| 31 | |
| 32 | using ndn::KeyChain; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 33 | using ndn::security::Certificate; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(TestEndorseCertificate) |
| 36 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 37 | std::string |
| 38 | getTestFile(std::string path) { |
| 39 | std::ifstream t(path); |
| 40 | std::stringstream buffer; |
| 41 | buffer << t.rdbuf(); |
| 42 | return buffer.str(); |
| 43 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 44 | |
| 45 | BOOST_AUTO_TEST_CASE(IdCert) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 46 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 47 | const std::string testIdCert = getTestFile("test/cert/testid.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 48 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 49 | testIdCert.size()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 50 | std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 51 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 52 | BOOST_CHECK(static_cast<bool>(idCert)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 53 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(idCert->getName().toUri(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 55 | "/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] | 56 | } |
| 57 | |
| 58 | BOOST_AUTO_TEST_CASE(ConstructFromIdCert) |
| 59 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 60 | const std::string testIdCert = getTestFile("test/cert/testid.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 61 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 62 | testIdCert.size()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 63 | std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 64 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 65 | Profile profile(*idCert); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 66 | vector<string> endorseList; |
| 67 | endorseList.push_back("email"); |
| 68 | endorseList.push_back("homepage"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 69 | EndorseCertificate endorseCertificate(*idCert, profile, endorseList); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 70 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 71 | KeyChain keyChain("pib-memory:", "tpm-memory:"); |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 72 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 73 | auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256); |
| 74 | keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo())); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 75 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 76 | |
| 77 | Data decodedEndorseData; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 78 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 79 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 81 | "/EndorseCertificateTests"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 82 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 86 | "/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 Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 99 | BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert) |
| 100 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 101 | const std::string testEndorseCert = getTestFile("test/cert/endorse.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 102 | 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 Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 105 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 106 | 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 Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 114 | KeyChain keyChain("pib-memory:", "tpm-memory:"); |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 115 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 116 | auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256); |
| 117 | keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 118 | |
| 119 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 120 | |
| 121 | Data decodedEndorseData; |
| 122 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 123 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 125 | "/EndorseCertificateTests"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 126 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
| 131 | "/EndorseCertificateTests/Singer/ksk-1234567890"); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 132 | BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(), |
| 133 | "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 134 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 135 | |
| 136 | BOOST_AUTO_TEST_SUITE_END() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 137 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 138 | } // namespace tests |
| 139 | } // namespace chronochat |