blob: 0ad2f3c511e7632fa26e43f953a058a1f476a405 [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento49f3a5f2017-09-23 01:36:33 -04002/*
weijia yuan82cf9142018-10-21 12:25:02 -07003 * Copyright (c) 2014-2018, Regents of the University of California.
Shuo Chenca329182014-03-19 18:05:18 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng 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 * repo-ng 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 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "handles/read-handle.hpp"
Weiqi Shif0330d52014-07-09 10:54:27 -070021#include "storage/sqlite-storage.hpp"
22#include "storage/repo-storage.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070023
Weiqi Shif0330d52014-07-09 10:54:27 -070024#include "../repo-storage-fixture.hpp"
Shuo Chenca329182014-03-19 18:05:18 -070025#include "../dataset-fixtures.hpp"
26
Eric Newberryb16edda2017-12-23 17:54:47 -070027#include <boost/asio/io_service.hpp>
Shuo Chenca329182014-03-19 18:05:18 -070028#include <boost/test/unit_test.hpp>
29
weijia yuan82cf9142018-10-21 12:25:02 -070030#include <ndn-cxx/util/time.hpp>
31
Shuo Chenca329182014-03-19 18:05:18 -070032namespace repo {
33namespace tests {
34
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040035// All the test cases in this test suite should be run at once.
Shuo Chenca329182014-03-19 18:05:18 -070036BOOST_AUTO_TEST_SUITE(TestBasicInerestRead)
37
38const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
39
40template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070041class BasicInterestReadFixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070042{
43public:
44 BasicInterestReadFixture()
45 : scheduler(repoFace.getIoService())
weijia yuan82cf9142018-10-21 12:25:02 -070046 , readHandle(repoFace, *handle, 0)
Shuo Chenca329182014-03-19 18:05:18 -070047 , readFace(repoFace.getIoService())
48 {
49 }
50
51 ~BasicInterestReadFixture()
52 {
53 repoFace.getIoService().stop();
54 }
55
56 void
57 startListen()
58 {
59 readHandle.listen("/");
60 }
61
62 void
63 scheduleReadEvent()
64 {
65 int timeCount = 1;
66 for (typename Dataset::DataContainer::iterator i = this->data.begin();
67 i != this->data.end(); ++i) {
68 //First insert a data into database;
69 (*i)->setContent(content, sizeof(content));
weijia yuan82cf9142018-10-21 12:25:02 -070070 (*i)->setFreshnessPeriod(36000_ms);
Shuo Chenca329182014-03-19 18:05:18 -070071 keyChain.sign(**i);
72 bool rc = handle->insertData(**i);
73
74 BOOST_CHECK_EQUAL(rc, true);
Shuo Chenca329182014-03-19 18:05:18 -070075 Interest readInterest((*i)->getName());
76 readInterest.setMustBeFresh(true);
77 scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50),
Alexander Afanasyev42290b22017-03-09 12:58:29 -080078 bind(&BasicInterestReadFixture<Dataset>::sendInterest, this,
Shuo Chenca329182014-03-19 18:05:18 -070079 readInterest));
80 timeCount++;
81 }
82 }
83
84 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080085 onReadData(const ndn::Interest& interest, const ndn::Data& data)
Shuo Chenca329182014-03-19 18:05:18 -070086 {
87 int rc = memcmp(data.getContent().value(), content, sizeof(content));
88 BOOST_CHECK_EQUAL(rc, 0);
Shuo Chenca329182014-03-19 18:05:18 -070089 }
90
91 void
92 onReadTimeout(const ndn::Interest& interest)
93 {
94 BOOST_ERROR("Insert not successfull or Read data does not successfull");
95 }
96
97 void
weijia yuan82cf9142018-10-21 12:25:02 -070098 onReadNack(const ndn::Interest& interest, const ndn::lp::Nack& nack)
99 {
100 BOOST_ERROR("Read nacked");
101 }
102
103 void
Shuo Chenca329182014-03-19 18:05:18 -0700104 sendInterest(const ndn::Interest& interest)
105 {
106 readFace.expressInterest(interest,
107 bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
weijia yuan82cf9142018-10-21 12:25:02 -0700108 bind(&BasicInterestReadFixture::onReadNack, this, _1, _2),
Shuo Chenca329182014-03-19 18:05:18 -0700109 bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
110 }
111
112public:
113 ndn::Face repoFace;
114 ndn::KeyChain keyChain;
115 ndn::Scheduler scheduler;
116 ReadHandle readHandle;
117 ndn::Face readFace;
118};
119
weijia yuan82cf9142018-10-21 12:25:02 -0700120typedef boost::mpl::vector<BasicDataset,
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700121 FetchByPrefixDataset,
122 BasicChildSelectorDataset,
123 ExtendedChildSelectorDataset,
weijia yuan82cf9142018-10-21 12:25:02 -0700124 SamePrefixDataset<10>> Datasets;
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700125
126BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700127{
Shuo Chenca329182014-03-19 18:05:18 -0700128 this->startListen();
weijia yuan82cf9142018-10-21 12:25:02 -0700129 this->scheduler.scheduleEvent(1_s,
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800130 bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
Shuo Chenca329182014-03-19 18:05:18 -0700131
weijia yuan82cf9142018-10-21 12:25:02 -0700132 this->repoFace.processEvents(20_s);
Shuo Chenca329182014-03-19 18:05:18 -0700133
134}
135
136BOOST_AUTO_TEST_SUITE_END()
137
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800138} // namespace tests
139} // namespace repo