blob: 2c96d1d7477657a846e4c7123f699213b1a95379 [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/*
Alexander Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, 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
Shuo Chenca329182014-03-19 18:05:18 -070027#include <boost/test/unit_test.hpp>
28
29namespace repo {
30namespace tests {
31
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040032// All the test cases in this test suite should be run at once.
Shuo Chenca329182014-03-19 18:05:18 -070033BOOST_AUTO_TEST_SUITE(TestBasicInerestRead)
34
35const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
36
37template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070038class BasicInterestReadFixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070039{
40public:
41 BasicInterestReadFixture()
42 : scheduler(repoFace.getIoService())
Nick Gordon190e4dc2017-10-04 16:54:10 -050043 , readHandle(repoFace, *handle, keyChain, scheduler, 0)
Shuo Chenca329182014-03-19 18:05:18 -070044 , readFace(repoFace.getIoService())
45 {
46 }
47
48 ~BasicInterestReadFixture()
49 {
50 repoFace.getIoService().stop();
51 }
52
53 void
54 startListen()
55 {
56 readHandle.listen("/");
57 }
58
59 void
60 scheduleReadEvent()
61 {
62 int timeCount = 1;
63 for (typename Dataset::DataContainer::iterator i = this->data.begin();
64 i != this->data.end(); ++i) {
65 //First insert a data into database;
66 (*i)->setContent(content, sizeof(content));
67 (*i)->setFreshnessPeriod(ndn::time::milliseconds(36000));
68 keyChain.sign(**i);
69 bool rc = handle->insertData(**i);
70
71 BOOST_CHECK_EQUAL(rc, true);
Shuo Chenca329182014-03-19 18:05:18 -070072 Interest readInterest((*i)->getName());
73 readInterest.setMustBeFresh(true);
74 scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50),
Alexander Afanasyev42290b22017-03-09 12:58:29 -080075 bind(&BasicInterestReadFixture<Dataset>::sendInterest, this,
Shuo Chenca329182014-03-19 18:05:18 -070076 readInterest));
77 timeCount++;
78 }
79 }
80
81 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080082 onReadData(const ndn::Interest& interest, const ndn::Data& data)
Shuo Chenca329182014-03-19 18:05:18 -070083 {
84 int rc = memcmp(data.getContent().value(), content, sizeof(content));
85 BOOST_CHECK_EQUAL(rc, 0);
Shuo Chenca329182014-03-19 18:05:18 -070086 }
87
88 void
89 onReadTimeout(const ndn::Interest& interest)
90 {
91 BOOST_ERROR("Insert not successfull or Read data does not successfull");
92 }
93
94 void
95 sendInterest(const ndn::Interest& interest)
96 {
97 readFace.expressInterest(interest,
98 bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -080099 bind(&BasicInterestReadFixture::onReadTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700100 bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
101 }
102
103public:
104 ndn::Face repoFace;
105 ndn::KeyChain keyChain;
106 ndn::Scheduler scheduler;
107 ReadHandle readHandle;
108 ndn::Face readFace;
109};
110
111
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700112typedef boost::mpl::vector< BasicDataset,
113 FetchByPrefixDataset,
114 BasicChildSelectorDataset,
115 ExtendedChildSelectorDataset,
116 SamePrefixDataset<10> > Datasets;
117
118BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700119{
120 // Insert dataset
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700121 // for (typename T::DataContainer::iterator i = this->data.begin();
122 // i != this->data.end(); ++i) {
123 // BOOST_CHECK_EQUAL(this->handle.insertData(**i), true);
124 // }
Shuo Chenca329182014-03-19 18:05:18 -0700125
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700126 // BOOST_CHECK_EQUAL(this->handle.size(), this->data.size());
Shuo Chenca329182014-03-19 18:05:18 -0700127
128 this->startListen();
129 this->scheduler.scheduleEvent(ndn::time::seconds(0),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800130 bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
Shuo Chenca329182014-03-19 18:05:18 -0700131
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000132 this->repoFace.processEvents(ndn::time::seconds(20));
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