blob: 308683ea558945445f4e5aad5a3b4052b1ecb02b [file] [log] [blame]
Shock Jiang698e6ed2014-11-09 11:22:24 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shibbf7ac82016-07-14 14:45:15 +00003 * Copyright (c) 2014-2016, 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})
Junxiao Shibbf7ac82016-07-14 14:45:15 +000038 , 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 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
Shock Jiang698e6ed2014-11-09 11:22:24 -080070 Validator validator;
71 ndns::NameServer top;
72 ndns::NameServer net;
73 ndns::NameServer ndnsim;
74};
75
76
77BOOST_AUTO_TEST_SUITE(IterativeQueryController)
78
Yumin Xia4e561892016-10-21 10:48:01 -070079BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(Basic, 3)
80
Shock Jiang698e6ed2014-11-09 11:22:24 -080081BOOST_FIXTURE_TEST_CASE(Basic, QueryControllerFixture)
82{
83 using std::string;
84 using ndns::NameServer;
85
Shock Jiang698e6ed2014-11-09 11:22:24 -080086 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 Shibbf7ac82016-07-14 14:45:15 +0000101 consumerFace);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800102
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 Jiang5d5928c2014-12-03 13:41:22 -0800106 ctr->setStartComponentIndex(1);
Shock Jiang698e6ed2014-11-09 11:22:24 -0800107
108 ctr->start();
109
110 run();
Yumin Xia4e561892016-10-21 10:48:01 -0700111
Shock Jiang698e6ed2014-11-09 11:22:24 -0800112 BOOST_CHECK_EQUAL(hasDataBack, true);
Yumin Xia4e561892016-10-21 10:48:01 -0700113
114 const std::vector<Interest>& interestRx = consumerFace.sentInterests;
115 BOOST_CHECK_EQUAL(interestRx.size(), 4);
116
117 std::vector<std::string> interestNames =
118 {
119 "/test19/NDNS/net/NS",
120 "/test19/net/NDNS/ndnsim/NS",
121 "/test19/net/ndnsim/NDNS/www/NS",
122 "/test19/net/ndnsim/NDNS/www/TXT"
123 };
124 for (int i = 0; i < 4; i++) {
125 // check if NDNS do iterative-query with right names
126 BOOST_CHECK_EQUAL(interestRx[i].getName(), Name(interestNames[i]));
127 // except for the first one, interest sent should has a Link object
128 if (i > 0) {
129 BOOST_CHECK_EQUAL(interestRx[i].hasLink(), true);
130 if (interestRx[i].hasLink()) {
131 BOOST_CHECK_EQUAL(interestRx[i].getLink(), m_links[i - 1]);
132 }
133 }
134 }
135
Shock Jiang698e6ed2014-11-09 11:22:24 -0800136}
137
138BOOST_AUTO_TEST_SUITE_END()
139
140} // namespace tests
141} // namespace ndns
142} // namespace ndn