blob: 307ce8badceedf204060b8dbbbc7d0fde9a3eb1b [file] [log] [blame]
Shock Jiang698e6ed2014-11-09 11:22:24 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
3 * Copyright (c) 2014-2017, Regents of the University of California.
Shock Jiang698e6ed2014-11-09 11:22:24 -08004 *
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 Jiang698e6ed2014-11-09 11:22:24 -080022
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080023#include "test-common.hpp"
24#include "unit/database-test-data.hpp"
Shock Jiang698e6ed2014-11-09 11:22:24 -080025
26namespace ndn {
27namespace ndns {
28namespace tests {
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070029
30NDNS_LOG_INIT("IterativeQueryControllerTest")
Shock Jiang698e6ed2014-11-09 11:22:24 -080031
32class QueryControllerFixture : public DbTestData
33{
34public:
35 QueryControllerFixture()
Junxiao Shibbf7ac82016-07-14 14:45:15 +000036 : producerFace(io, {false, true})
Yumin Xia4e561892016-10-21 10:48:01 -070037 , consumerFace(io, {true, true})
Yumin Xia2c509c22017-02-09 14:37:36 -080038 , 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 Jiang698e6ed2014-11-09 11:22:24 -080042 {
43 run();
Junxiao Shibbf7ac82016-07-14 14:45:15 +000044 producerFace.onSendInterest.connect([this] (const Interest& interest) {
45 io.post([=] { consumerFace.receive(interest); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070046 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +000047 consumerFace.onSendInterest.connect([this] (const Interest& interest) {
48 io.post([=] { producerFace.receive(interest); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070049 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +000050 producerFace.onSendData.connect([this] (const Data& data) {
51 io.post([=] { consumerFace.receive(data); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070052 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +000053 consumerFace.onSendData.connect([this] (const Data& data) {
54 io.post([=] { producerFace.receive(data); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070055 });
Shock Jiang698e6ed2014-11-09 11:22:24 -080056 }
57
58 void
59 run()
60 {
61 io.poll();
62 io.reset();
63 }
64
65public:
66 boost::asio::io_service io;
Junxiao Shibbf7ac82016-07-14 14:45:15 +000067 ndn::util::DummyClientFace producerFace;
68 ndn::util::DummyClientFace consumerFace;
Shock Jiang698e6ed2014-11-09 11:22:24 -080069
Yumin Xia2c509c22017-02-09 14:37:36 -080070 unique_ptr<security::v2::Validator> validator;
Shock Jiang698e6ed2014-11-09 11:22:24 -080071 ndns::NameServer top;
72 ndns::NameServer net;
73 ndns::NameServer ndnsim;
74};
75
76
77BOOST_AUTO_TEST_SUITE(IterativeQueryController)
78
79BOOST_FIXTURE_TEST_CASE(Basic, QueryControllerFixture)
80{
81 using std::string;
82 using ndns::NameServer;
83
Shock Jiang698e6ed2014-11-09 11:22:24 -080084 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 Shibbf7ac82016-07-14 14:45:15 +000099 consumerFace);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800100
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 Jiang5d5928c2014-12-03 13:41:22 -0800104 ctr->setStartComponentIndex(1);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800105
106 ctr->start();
107
108 run();
Yumin Xia4e561892016-10-21 10:48:01 -0700109
Shock Jiang698e6ed2014-11-09 11:22:24 -0800110 BOOST_CHECK_EQUAL(hasDataBack, true);
Yumin Xia4e561892016-10-21 10:48:01 -0700111
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 Xia2c509c22017-02-09 14:37:36 -0800127 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 Xia4e561892016-10-21 10:48:01 -0700130 }
131 }
132 }
133
Shock Jiang698e6ed2014-11-09 11:22:24 -0800134}
135
136BOOST_AUTO_TEST_SUITE_END()
137
138} // namespace tests
139} // namespace ndns
140} // namespace ndn