Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2017, 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}) |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 37 | , consumerFace(io, {true, true}) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 38 | , validator(NdnsValidatorBuilder::create(producerFace)) |
| 39 | , top(m_test.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 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 70 | unique_ptr<security::v2::Validator> validator; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 71 | ndns::NameServer top; |
| 72 | ndns::NameServer net; |
| 73 | ndns::NameServer ndnsim; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | BOOST_AUTO_TEST_SUITE(IterativeQueryController) |
| 78 | |
| 79 | BOOST_FIXTURE_TEST_CASE(Basic, QueryControllerFixture) |
| 80 | { |
| 81 | using std::string; |
| 82 | using ndns::NameServer; |
| 83 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 84 | Name name = m_ndnsim.getName(); |
| 85 | Name dstLabel(name.append("www")); |
| 86 | name::Component rrType("TXT"); |
| 87 | time::milliseconds lifetime(4000); |
| 88 | |
| 89 | bool hasDataBack = false; |
| 90 | auto ctr = make_shared<ndns::IterativeQueryController>( |
| 91 | dstLabel, rrType, lifetime, |
| 92 | [&hasDataBack] (const Data&, const Response&) { |
| 93 | hasDataBack = true; |
| 94 | BOOST_CHECK(true); |
| 95 | }, |
| 96 | [&hasDataBack] (uint32_t errCode, const std::string& errMsg) { |
| 97 | BOOST_CHECK(false); |
| 98 | }, |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 99 | consumerFace); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 100 | |
| 101 | // IterativeQueryController is a whole process |
| 102 | // the tester should not send Interest one by one |
| 103 | // instead of starting it and letting it handle Interest/Data automatically |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 104 | ctr->setStartComponentIndex(1); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 105 | |
| 106 | ctr->start(); |
| 107 | |
| 108 | run(); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 109 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(hasDataBack, true); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 111 | |
| 112 | const std::vector<Interest>& interestRx = consumerFace.sentInterests; |
| 113 | BOOST_CHECK_EQUAL(interestRx.size(), 4); |
| 114 | |
| 115 | std::vector<std::string> interestNames = |
| 116 | { |
| 117 | "/test19/NDNS/net/NS", |
| 118 | "/test19/net/NDNS/ndnsim/NS", |
| 119 | "/test19/net/ndnsim/NDNS/www/NS", |
| 120 | "/test19/net/ndnsim/NDNS/www/TXT" |
| 121 | }; |
| 122 | for (int i = 0; i < 4; i++) { |
| 123 | // check if NDNS do iterative-query with right names |
| 124 | BOOST_CHECK_EQUAL(interestRx[i].getName(), Name(interestNames[i])); |
| 125 | // except for the first one, interest sent should has a Link object |
| 126 | if (i > 0) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 127 | BOOST_CHECK_EQUAL(!interestRx[i].getForwardingHint().empty(), true); |
| 128 | if (!interestRx[i].getForwardingHint().empty()) { |
| 129 | BOOST_CHECK_EQUAL(interestRx[i].getForwardingHint(), m_links[i - 1].getDelegationList()); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_SUITE_END() |
| 137 | |
| 138 | } // namespace tests |
| 139 | } // namespace ndns |
| 140 | } // namespace ndn |