blob: f33f3255ae9a52bca79fc84c4a261ea9a1a57ce7 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "tools/pib/cert-publisher.hpp"
23#include "identity-management-time-fixture.hpp"
24#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
34class CertPublisherFixture : public ndn::security::IdentityManagementTimeFixture
35{
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
55BOOST_FIXTURE_TEST_SUITE(TestCertPublisher, CertPublisherFixture)
56
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