blob: 65ab32be7a155d3d2736eb8ead5fc2a57db7f5d5 [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/*
Alexander Afanasyev08d18742018-03-15 16:31:28 -04003 * Copyright (c) 2014-2018, 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
Shock Jiang698e6ed2014-11-09 11:22:24 -080030class QueryControllerFixture : public DbTestData
31{
32public:
33 QueryControllerFixture()
Junxiao Shibbf7ac82016-07-14 14:45:15 +000034 : producerFace(io, {false, true})
Yumin Xia4e561892016-10-21 10:48:01 -070035 , consumerFace(io, {true, true})
Yumin Xia2c509c22017-02-09 14:37:36 -080036 , 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 Jiang698e6ed2014-11-09 11:22:24 -080040 {
41 run();
Junxiao Shibbf7ac82016-07-14 14:45:15 +000042 producerFace.onSendInterest.connect([this] (const Interest& interest) {
43 io.post([=] { consumerFace.receive(interest); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070044 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +000045 consumerFace.onSendInterest.connect([this] (const Interest& interest) {
46 io.post([=] { producerFace.receive(interest); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070047 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +000048 producerFace.onSendData.connect([this] (const Data& data) {
49 io.post([=] { consumerFace.receive(data); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070050 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +000051 consumerFace.onSendData.connect([this] (const Data& data) {
52 io.post([=] { producerFace.receive(data); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070053 });
Shock Jiang698e6ed2014-11-09 11:22:24 -080054 }
55
56 void
57 run()
58 {
59 io.poll();
60 io.reset();
61 }
62
63public:
64 boost::asio::io_service io;
Junxiao Shibbf7ac82016-07-14 14:45:15 +000065 ndn::util::DummyClientFace producerFace;
66 ndn::util::DummyClientFace consumerFace;
Shock Jiang698e6ed2014-11-09 11:22:24 -080067
Yumin Xia2c509c22017-02-09 14:37:36 -080068 unique_ptr<security::v2::Validator> validator;
Shock Jiang698e6ed2014-11-09 11:22:24 -080069 ndns::NameServer top;
70 ndns::NameServer net;
71 ndns::NameServer ndnsim;
72};
73
74
75BOOST_AUTO_TEST_SUITE(IterativeQueryController)
76
77BOOST_FIXTURE_TEST_CASE(Basic, QueryControllerFixture)
78{
79 using std::string;
80 using ndns::NameServer;
81
Shock Jiang698e6ed2014-11-09 11:22:24 -080082 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 Afanasyev08d18742018-03-15 16:31:28 -040088 auto ctr = std::make_shared<ndns::IterativeQueryController>(
Shock Jiang698e6ed2014-11-09 11:22:24 -080089 dstLabel, rrType, lifetime,
90 [&hasDataBack] (const Data&, const Response&) {
91 hasDataBack = true;
92 BOOST_CHECK(true);
93 },
94 [&hasDataBack] (uint32_t errCode, const std::string& errMsg) {
95 BOOST_CHECK(false);
96 },
Junxiao Shibbf7ac82016-07-14 14:45:15 +000097 consumerFace);
Shock Jiang698e6ed2014-11-09 11:22:24 -080098
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 Jiang5d5928c2014-12-03 13:41:22 -0800102 ctr->setStartComponentIndex(1);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800103
104 ctr->start();
105
106 run();
Yumin Xia4e561892016-10-21 10:48:01 -0700107
Shock Jiang698e6ed2014-11-09 11:22:24 -0800108 BOOST_CHECK_EQUAL(hasDataBack, true);
Yumin Xia4e561892016-10-21 10:48:01 -0700109
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 Xia2c509c22017-02-09 14:37:36 -0800125 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 Xia4e561892016-10-21 10:48:01 -0700128 }
129 }
130 }
131
Shock Jiang698e6ed2014-11-09 11:22:24 -0800132}
133
134BOOST_AUTO_TEST_SUITE_END()
135
136} // namespace tests
137} // namespace ndns
138} // namespace ndn