blob: a14731c65f35ea48a75df7d5e2c60d1fe9d98905 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0a312e52015-07-22 13:14:53 -07003 * Copyright (c) 2014-2015, 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())
40 , face(util::makeDummyClientFace(io, {true, true}))
41 {
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;
52 shared_ptr<util::DummyClientFace> face;
53};
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
70 CertPublisher certPublisher(*face, db);
71
72 // Add a certificate
73 db.addCertificate(*cert111);
74 advanceClocks(io, time::milliseconds(2), 50);
75
76 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 0);
77 auto interest111 = make_shared<Interest>(cert111->getName().getPrefix(-1));
78 face->receive(*interest111);
79 advanceClocks(io, time::milliseconds(2), 50);
80 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
81 BOOST_CHECK(face->sentDatas[0].wireEncode() == cert111->wireEncode());
82 face->sentDatas.clear();
83
84 // Add another certificate
85 db.addCertificate(*cert112);
86 advanceClocks(io, time::milliseconds(2), 50);
87
88 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 0);
89 auto interest112 = make_shared<Interest>(cert112->getName().getPrefix(-1));
90 face->receive(*interest112);
91 advanceClocks(io, time::milliseconds(2), 50);
92 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
93 BOOST_CHECK(face->sentDatas[0].wireEncode() == cert111->wireEncode());
94 face->sentDatas.clear();
95
96 Exclude exclude;
97 exclude.excludeOne(cert111->getName().get(-1));
98 interest112->setExclude(exclude);
99 face->receive(*interest112);
100 advanceClocks(io, time::milliseconds(2), 50);
101 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
102 BOOST_CHECK(face->sentDatas[0].wireEncode() == cert112->wireEncode());
103 face->sentDatas.clear();
104
105 // delete a certificate
106 db.deleteCertificate(certName112);
107 face->receive(*interest112);
108 advanceClocks(io, time::milliseconds(2), 50);
109 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 0);
110
111 face->receive(*interest111);
112 advanceClocks(io, time::milliseconds(2), 50);
113 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
114 BOOST_CHECK(face->sentDatas[0].wireEncode() == cert111->wireEncode());
115 face->sentDatas.clear();
116
117 // delete another certificate
118 db.deleteCertificate(certName111);
119 face->receive(*interest112);
120 advanceClocks(io, time::milliseconds(2), 50);
121 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 0);
122
123 face->receive(*interest111);
124 advanceClocks(io, time::milliseconds(2), 50);
125 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 0);
126}
127
128BOOST_AUTO_TEST_SUITE_END()
129
130} // namespace tests
131} // namespace pib
132} // namespace ndn