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