blob: ca71e4c3924ffd3b98d0079b5299699516952626 [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"
Yingdi Yu0a312e52015-07-22 13:14:53 -070023#include "../identity-management-time-fixture.hpp"
Yingdi Yu77627ab2015-07-21 16:13:49 -070024#include <ndn-cxx/util/dummy-client-face.hpp>
25
26#include <boost/filesystem.hpp>
27
28#include "tests/test-common.hpp"
29
30namespace ndn {
31namespace pib {
32namespace tests {
33
Yingdi Yu0a312e52015-07-22 13:14:53 -070034class CertPublisherFixture : public ndn::tests::IdentityManagementTimeFixture
Yingdi Yu77627ab2015-07-21 16:13:49 -070035{
36public:
37 CertPublisherFixture()
38 : tmpPath(boost::filesystem::path(TMP_TESTS_PATH) / "DbTest")
39 , db(tmpPath.c_str())
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000040 , face(io, m_keyChain, {true, true})
Yingdi Yu77627ab2015-07-21 16:13:49 -070041 {
42 }
43
44 ~CertPublisherFixture()
45 {
46 boost::filesystem::remove_all(tmpPath);
47 }
48
49 boost::asio::io_service io;
50 boost::filesystem::path tmpPath;
51 PibDb db;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000052 util::DummyClientFace face;
Yingdi Yu77627ab2015-07-21 16:13:49 -070053};
54
Yingdi Yu0a312e52015-07-22 13:14:53 -070055BOOST_FIXTURE_TEST_SUITE(PibCertPublisher, 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
128BOOST_AUTO_TEST_SUITE_END()
129
130} // namespace tests
131} // namespace pib
132} // namespace ndn