blob: eb412e8ed69bb01d3dbc5b993380e7fef47b715c [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
27#include <ndn-cxx/util/random.hpp>
28
29#include <boost/test/unit_test.hpp>
30
31namespace repo {
32namespace tests {
33
34//All the test cases in this test suite should be run at once.
35BOOST_AUTO_TEST_SUITE(TestBasicInerestRead)
36
37const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
38
39template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070040class BasicInterestReadFixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070041{
42public:
43 BasicInterestReadFixture()
44 : scheduler(repoFace.getIoService())
45 , readHandle(repoFace, *handle, keyChain, scheduler)
46 , readFace(repoFace.getIoService())
47 {
48 }
49
50 ~BasicInterestReadFixture()
51 {
52 repoFace.getIoService().stop();
53 }
54
55 void
56 startListen()
57 {
58 readHandle.listen("/");
59 }
60
61 void
62 scheduleReadEvent()
63 {
64 int timeCount = 1;
65 for (typename Dataset::DataContainer::iterator i = this->data.begin();
66 i != this->data.end(); ++i) {
67 //First insert a data into database;
68 (*i)->setContent(content, sizeof(content));
69 (*i)->setFreshnessPeriod(ndn::time::milliseconds(36000));
70 keyChain.sign(**i);
71 bool rc = handle->insertData(**i);
72
73 BOOST_CHECK_EQUAL(rc, true);
Shuo Chenca329182014-03-19 18:05:18 -070074 Interest readInterest((*i)->getName());
75 readInterest.setMustBeFresh(true);
76 scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50),
Alexander Afanasyev42290b22017-03-09 12:58:29 -080077 bind(&BasicInterestReadFixture<Dataset>::sendInterest, this,
Shuo Chenca329182014-03-19 18:05:18 -070078 readInterest));
79 timeCount++;
80 }
81 }
82
83 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080084 onReadData(const ndn::Interest& interest, const ndn::Data& data)
Shuo Chenca329182014-03-19 18:05:18 -070085 {
86 int rc = memcmp(data.getContent().value(), content, sizeof(content));
87 BOOST_CHECK_EQUAL(rc, 0);
Shuo Chenca329182014-03-19 18:05:18 -070088 }
89
90 void
91 onReadTimeout(const ndn::Interest& interest)
92 {
93 BOOST_ERROR("Insert not successfull or Read data does not successfull");
94 }
95
96 void
97 sendInterest(const ndn::Interest& interest)
98 {
99 readFace.expressInterest(interest,
100 bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800101 bind(&BasicInterestReadFixture::onReadTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700102 bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
103 }
104
105public:
106 ndn::Face repoFace;
107 ndn::KeyChain keyChain;
108 ndn::Scheduler scheduler;
109 ReadHandle readHandle;
110 ndn::Face readFace;
111};
112
113
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700114typedef boost::mpl::vector< BasicDataset,
115 FetchByPrefixDataset,
116 BasicChildSelectorDataset,
117 ExtendedChildSelectorDataset,
118 SamePrefixDataset<10> > Datasets;
119
120BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700121{
122 // Insert dataset
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700123 // for (typename T::DataContainer::iterator i = this->data.begin();
124 // i != this->data.end(); ++i) {
125 // BOOST_CHECK_EQUAL(this->handle.insertData(**i), true);
126 // }
Shuo Chenca329182014-03-19 18:05:18 -0700127
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700128 // BOOST_CHECK_EQUAL(this->handle.size(), this->data.size());
Shuo Chenca329182014-03-19 18:05:18 -0700129
130 this->startListen();
131 this->scheduler.scheduleEvent(ndn::time::seconds(0),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800132 bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
Shuo Chenca329182014-03-19 18:05:18 -0700133
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000134 this->repoFace.processEvents(ndn::time::seconds(20));
Shuo Chenca329182014-03-19 18:05:18 -0700135
136}
137
138BOOST_AUTO_TEST_SUITE_END()
139
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800140} // namespace tests
141} // namespace repo