blob: dfcfef6a9586f758c98786665f2c5ad1ab23dcbf [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00003 * Copyright (c) 2014-2016, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
Yingdi Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Yingdi Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Yingdi Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
21
22#include "tools/pib/cert-publisher.hpp"
Davide Pesavento013de9b2016-09-01 12:06:56 +000023
24#include "tests/identity-management-fixture.hpp"
25
Yingdi Yu77627ab2015-07-21 16:13:49 -070026#include <ndn-cxx/util/dummy-client-face.hpp>
Yingdi Yu77627ab2015-07-21 16:13:49 -070027#include <boost/filesystem.hpp>
28
Yingdi Yu77627ab2015-07-21 16:13:49 -070029namespace ndn {
30namespace pib {
31namespace tests {
32
Yingdi Yu0a312e52015-07-22 13:14:53 -070033class CertPublisherFixture : public ndn::tests::IdentityManagementTimeFixture
Yingdi Yu77627ab2015-07-21 16:13:49 -070034{
35public:
36 CertPublisherFixture()
37 : tmpPath(boost::filesystem::path(TMP_TESTS_PATH) / "DbTest")
38 , db(tmpPath.c_str())
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000039 , face(io, m_keyChain, {true, true})
Yingdi Yu77627ab2015-07-21 16:13:49 -070040 {
41 }
42
43 ~CertPublisherFixture()
44 {
45 boost::filesystem::remove_all(tmpPath);
46 }
47
48 boost::asio::io_service io;
49 boost::filesystem::path tmpPath;
50 PibDb db;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000051 util::DummyClientFace face;
Yingdi Yu77627ab2015-07-21 16:13:49 -070052};
53
Davide Pesavento013de9b2016-09-01 12:06:56 +000054BOOST_AUTO_TEST_SUITE(Pib)
55BOOST_FIXTURE_TEST_SUITE(TestCertPublisher, CertPublisherFixture)
Yingdi Yu77627ab2015-07-21 16:13:49 -070056
57BOOST_AUTO_TEST_CASE(Basic)
58{
59 // Initialize id1
60 Name id1("/test/identity");
61 addIdentity(id1);
62 Name certName111 = m_keyChain.getDefaultCertificateNameForIdentity(id1);
63 shared_ptr<IdentityCertificate> cert111 = m_keyChain.getCertificate(certName111);
64 Name keyName11 = cert111->getPublicKeyName();
65
66 advanceClocks(io, time::milliseconds(100));
67 shared_ptr<IdentityCertificate> cert112 = m_keyChain.selfSign(keyName11);
68 Name certName112 = cert112->getName();
69
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000070 CertPublisher certPublisher(face, db);
Yingdi Yu77627ab2015-07-21 16:13:49 -070071
72 // Add a certificate
73 db.addCertificate(*cert111);
74 advanceClocks(io, time::milliseconds(2), 50);
75
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000076 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -070077 auto interest111 = make_shared<Interest>(cert111->getName().getPrefix(-1));
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000078 face.receive(*interest111);
Yingdi Yu77627ab2015-07-21 16:13:49 -070079 advanceClocks(io, time::milliseconds(2), 50);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000080 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
81 BOOST_CHECK(face.sentData[0].wireEncode() == cert111->wireEncode());
82 face.sentData.clear();
Yingdi Yu77627ab2015-07-21 16:13:49 -070083
84 // Add another certificate
85 db.addCertificate(*cert112);
86 advanceClocks(io, time::milliseconds(2), 50);
87
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000088 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -070089 auto interest112 = make_shared<Interest>(cert112->getName().getPrefix(-1));
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000090 face.receive(*interest112);
Yingdi Yu77627ab2015-07-21 16:13:49 -070091 advanceClocks(io, time::milliseconds(2), 50);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000092 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
93 BOOST_CHECK(face.sentData[0].wireEncode() == cert111->wireEncode());
94 face.sentData.clear();
Yingdi Yu77627ab2015-07-21 16:13:49 -070095
96 Exclude exclude;
97 exclude.excludeOne(cert111->getName().get(-1));
98 interest112->setExclude(exclude);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000099 face.receive(*interest112);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700100 advanceClocks(io, time::milliseconds(2), 50);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000101 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
102 BOOST_CHECK(face.sentData[0].wireEncode() == cert112->wireEncode());
103 face.sentData.clear();
Yingdi Yu77627ab2015-07-21 16:13:49 -0700104
105 // delete a certificate
106 db.deleteCertificate(certName112);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000107 face.receive(*interest112);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700108 advanceClocks(io, time::milliseconds(2), 50);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000109 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700110
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000111 face.receive(*interest111);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700112 advanceClocks(io, time::milliseconds(2), 50);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000113 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
114 BOOST_CHECK(face.sentData[0].wireEncode() == cert111->wireEncode());
115 face.sentData.clear();
Yingdi Yu77627ab2015-07-21 16:13:49 -0700116
117 // delete another certificate
118 db.deleteCertificate(certName111);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000119 face.receive(*interest112);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700120 advanceClocks(io, time::milliseconds(2), 50);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000121 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700122
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000123 face.receive(*interest111);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700124 advanceClocks(io, time::milliseconds(2), 50);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000125 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700126}
127
Davide Pesavento013de9b2016-09-01 12:06:56 +0000128BOOST_AUTO_TEST_SUITE_END() // TestPibPublisher
129BOOST_AUTO_TEST_SUITE_END() // Pib
Yingdi Yu77627ab2015-07-21 16:13:49 -0700130
131} // namespace tests
132} // namespace pib
133} // namespace ndn