Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS 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. |
| 11 | * |
| 12 | * NDNS 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "clients/iterative-query-controller.hpp" |
| 21 | #include "daemon/name-server.hpp" |
| 22 | #include "logger.hpp" |
| 23 | #include "../database-test-data.hpp" |
| 24 | #include "../../boost-test.hpp" |
| 25 | |
| 26 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 27 | |
| 28 | #include <boost/asio.hpp> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | namespace tests { |
| 33 | NDNS_LOG_INIT("IterativeQueryControllerTest"); |
| 34 | |
| 35 | class QueryControllerFixture : public DbTestData |
| 36 | { |
| 37 | public: |
| 38 | QueryControllerFixture() |
| 39 | : producerFace(ndn::util::makeDummyClientFace(io, { false, true })) |
| 40 | , consumerFace(ndn::util::makeDummyClientFace(io, { false, true })) |
| 41 | , validator(*producerFace) |
| 42 | , top(m_root.getName(), m_certName, *producerFace, m_session, m_keyChain, validator) |
| 43 | , net(m_net.getName(), m_certName, *producerFace, m_session, m_keyChain, validator) |
| 44 | , ndnsim(m_ndnsim.getName(), m_certName, *producerFace, m_session, m_keyChain, validator) |
| 45 | { |
| 46 | run(); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame^] | 47 | producerFace->onSendInterest.connect([this] (const Interest& interest) { |
| 48 | io.post([=] { consumerFace->receive(interest); }); |
| 49 | }); |
| 50 | consumerFace->onSendInterest.connect([this] (const Interest& interest) { |
| 51 | io.post([=] { producerFace->receive(interest); }); |
| 52 | }); |
| 53 | producerFace->onSendData.connect([this] (const Data& data) { |
| 54 | io.post([=] { consumerFace->receive(data); }); |
| 55 | }); |
| 56 | consumerFace->onSendData.connect([this] (const Data& data) { |
| 57 | io.post([=] { producerFace->receive(data); }); |
| 58 | }); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
| 62 | run() |
| 63 | { |
| 64 | io.poll(); |
| 65 | io.reset(); |
| 66 | } |
| 67 | |
| 68 | public: |
| 69 | boost::asio::io_service io; |
| 70 | shared_ptr<ndn::util::DummyClientFace> producerFace; |
| 71 | shared_ptr<ndn::util::DummyClientFace> consumerFace; |
| 72 | |
| 73 | Name hint; |
| 74 | Validator validator; |
| 75 | ndns::NameServer top; |
| 76 | ndns::NameServer net; |
| 77 | ndns::NameServer ndnsim; |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | BOOST_AUTO_TEST_SUITE(IterativeQueryController) |
| 82 | |
| 83 | BOOST_FIXTURE_TEST_CASE(Basic, QueryControllerFixture) |
| 84 | { |
| 85 | using std::string; |
| 86 | using ndns::NameServer; |
| 87 | |
| 88 | string hint; |
| 89 | Name name = m_ndnsim.getName(); |
| 90 | Name dstLabel(name.append("www")); |
| 91 | name::Component rrType("TXT"); |
| 92 | time::milliseconds lifetime(4000); |
| 93 | |
| 94 | bool hasDataBack = false; |
| 95 | auto ctr = make_shared<ndns::IterativeQueryController>( |
| 96 | dstLabel, rrType, lifetime, |
| 97 | [&hasDataBack] (const Data&, const Response&) { |
| 98 | hasDataBack = true; |
| 99 | BOOST_CHECK(true); |
| 100 | }, |
| 101 | [&hasDataBack] (uint32_t errCode, const std::string& errMsg) { |
| 102 | BOOST_CHECK(false); |
| 103 | }, |
| 104 | *consumerFace); |
| 105 | |
| 106 | // IterativeQueryController is a whole process |
| 107 | // the tester should not send Interest one by one |
| 108 | // instead of starting it and letting it handle Interest/Data automatically |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 109 | ctr->setStartComponentIndex(1); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 110 | |
| 111 | ctr->start(); |
| 112 | |
| 113 | run(); |
| 114 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_SUITE_END() |
| 118 | |
| 119 | } // namespace tests |
| 120 | } // namespace ndns |
| 121 | } // namespace ndn |