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 | /* |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 30 | class QueryControllerFixture : public DbTestData |
| 31 | { |
| 32 | public: |
| 33 | QueryControllerFixture() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 34 | : producerFace(io, {false, true}) |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 35 | , consumerFace(io, {true, true}) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 36 | , validator(NdnsValidatorBuilder::create(producerFace)) |
| 37 | , top(m_test.getName(), m_certName, producerFace, m_session, m_keyChain, *validator) |
| 38 | , net(m_net.getName(), m_certName, producerFace, m_session, m_keyChain, *validator) |
| 39 | , ndnsim(m_ndnsim.getName(), m_certName, producerFace, m_session, m_keyChain, *validator) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 40 | { |
| 41 | run(); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 42 | producerFace.onSendInterest.connect([this] (const Interest& interest) { |
| 43 | io.post([=] { consumerFace.receive(interest); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 44 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 45 | consumerFace.onSendInterest.connect([this] (const Interest& interest) { |
| 46 | io.post([=] { producerFace.receive(interest); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 47 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 48 | producerFace.onSendData.connect([this] (const Data& data) { |
| 49 | io.post([=] { consumerFace.receive(data); }); |
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.onSendData.connect([this] (const Data& data) { |
| 52 | io.post([=] { producerFace.receive(data); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 53 | }); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
| 57 | run() |
| 58 | { |
| 59 | io.poll(); |
| 60 | io.reset(); |
| 61 | } |
| 62 | |
| 63 | public: |
| 64 | boost::asio::io_service io; |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 65 | ndn::util::DummyClientFace producerFace; |
| 66 | ndn::util::DummyClientFace consumerFace; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 67 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 68 | unique_ptr<security::v2::Validator> validator; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 69 | ndns::NameServer top; |
| 70 | ndns::NameServer net; |
| 71 | ndns::NameServer ndnsim; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | BOOST_AUTO_TEST_SUITE(IterativeQueryController) |
| 76 | |
| 77 | BOOST_FIXTURE_TEST_CASE(Basic, QueryControllerFixture) |
| 78 | { |
| 79 | using std::string; |
| 80 | using ndns::NameServer; |
| 81 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 82 | Name name = m_ndnsim.getName(); |
| 83 | Name dstLabel(name.append("www")); |
| 84 | name::Component rrType("TXT"); |
| 85 | time::milliseconds lifetime(4000); |
| 86 | |
| 87 | bool hasDataBack = false; |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 88 | auto ctr = std::make_shared<ndns::IterativeQueryController>( |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 89 | dstLabel, rrType, lifetime, |
| 90 | [&hasDataBack] (const Data&, const Response&) { |
| 91 | hasDataBack = true; |
| 92 | BOOST_CHECK(true); |
| 93 | }, |
Eric Newberry | 9edaf26 | 2018-06-07 23:44:57 -0700 | [diff] [blame^] | 94 | [] (uint32_t errCode, const std::string& errMsg) { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 95 | BOOST_CHECK(false); |
| 96 | }, |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 97 | consumerFace); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 98 | |
| 99 | // IterativeQueryController is a whole process |
| 100 | // the tester should not send Interest one by one |
| 101 | // instead of starting it and letting it handle Interest/Data automatically |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 102 | ctr->setStartComponentIndex(1); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 103 | |
| 104 | ctr->start(); |
| 105 | |
| 106 | run(); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 107 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(hasDataBack, true); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 109 | |
| 110 | const std::vector<Interest>& interestRx = consumerFace.sentInterests; |
| 111 | BOOST_CHECK_EQUAL(interestRx.size(), 4); |
| 112 | |
| 113 | std::vector<std::string> interestNames = |
| 114 | { |
| 115 | "/test19/NDNS/net/NS", |
| 116 | "/test19/net/NDNS/ndnsim/NS", |
| 117 | "/test19/net/ndnsim/NDNS/www/NS", |
| 118 | "/test19/net/ndnsim/NDNS/www/TXT" |
| 119 | }; |
| 120 | for (int i = 0; i < 4; i++) { |
| 121 | // check if NDNS do iterative-query with right names |
| 122 | BOOST_CHECK_EQUAL(interestRx[i].getName(), Name(interestNames[i])); |
| 123 | // except for the first one, interest sent should has a Link object |
| 124 | if (i > 0) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(!interestRx[i].getForwardingHint().empty(), true); |
| 126 | if (!interestRx[i].getForwardingHint().empty()) { |
| 127 | BOOST_CHECK_EQUAL(interestRx[i].getForwardingHint(), m_links[i - 1].getDelegationList()); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_SUITE_END() |
| 135 | |
| 136 | } // namespace tests |
| 137 | } // namespace ndns |
| 138 | } // namespace ndn |