Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 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 { |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 33 | |
| 34 | NDNS_LOG_INIT("IterativeQueryControllerTest") |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 35 | |
| 36 | class QueryControllerFixture : public DbTestData |
| 37 | { |
| 38 | public: |
| 39 | QueryControllerFixture() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 40 | : producerFace(io, {false, true}) |
| 41 | , consumerFace(io, {false, true}) |
| 42 | , validator(producerFace) |
| 43 | , top(m_root.getName(), m_certName, producerFace, m_session, m_keyChain, validator) |
| 44 | , net(m_net.getName(), m_certName, producerFace, m_session, m_keyChain, validator) |
| 45 | , ndnsim(m_ndnsim.getName(), m_certName, producerFace, m_session, m_keyChain, validator) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 46 | { |
| 47 | run(); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 48 | producerFace.onSendInterest.connect([this] (const Interest& interest) { |
| 49 | io.post([=] { consumerFace.receive(interest); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 50 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 51 | consumerFace.onSendInterest.connect([this] (const Interest& interest) { |
| 52 | io.post([=] { producerFace.receive(interest); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 53 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 54 | producerFace.onSendData.connect([this] (const Data& data) { |
| 55 | io.post([=] { consumerFace.receive(data); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 56 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 57 | consumerFace.onSendData.connect([this] (const Data& data) { |
| 58 | io.post([=] { producerFace.receive(data); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 59 | }); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
| 63 | run() |
| 64 | { |
| 65 | io.poll(); |
| 66 | io.reset(); |
| 67 | } |
| 68 | |
| 69 | public: |
| 70 | boost::asio::io_service io; |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 71 | ndn::util::DummyClientFace producerFace; |
| 72 | ndn::util::DummyClientFace consumerFace; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 73 | |
| 74 | Name hint; |
| 75 | Validator validator; |
| 76 | ndns::NameServer top; |
| 77 | ndns::NameServer net; |
| 78 | ndns::NameServer ndnsim; |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | BOOST_AUTO_TEST_SUITE(IterativeQueryController) |
| 83 | |
| 84 | BOOST_FIXTURE_TEST_CASE(Basic, QueryControllerFixture) |
| 85 | { |
| 86 | using std::string; |
| 87 | using ndns::NameServer; |
| 88 | |
| 89 | string hint; |
| 90 | Name name = m_ndnsim.getName(); |
| 91 | Name dstLabel(name.append("www")); |
| 92 | name::Component rrType("TXT"); |
| 93 | time::milliseconds lifetime(4000); |
| 94 | |
| 95 | bool hasDataBack = false; |
| 96 | auto ctr = make_shared<ndns::IterativeQueryController>( |
| 97 | dstLabel, rrType, lifetime, |
| 98 | [&hasDataBack] (const Data&, const Response&) { |
| 99 | hasDataBack = true; |
| 100 | BOOST_CHECK(true); |
| 101 | }, |
| 102 | [&hasDataBack] (uint32_t errCode, const std::string& errMsg) { |
| 103 | BOOST_CHECK(false); |
| 104 | }, |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 105 | consumerFace); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 106 | |
| 107 | // IterativeQueryController is a whole process |
| 108 | // the tester should not send Interest one by one |
| 109 | // instead of starting it and letting it handle Interest/Data automatically |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 110 | ctr->setStartComponentIndex(1); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 111 | |
| 112 | ctr->start(); |
| 113 | |
| 114 | run(); |
| 115 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_SUITE_END() |
| 119 | |
| 120 | } // namespace tests |
| 121 | } // namespace ndns |
| 122 | } // namespace ndn |