blob: 80288947a34d2d52266aec1ed6f093a48b8c62d6 [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
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
30namespace repo {
31namespace tests {
32
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040033// All the test cases in this test suite should be run at once.
Shuo Chenca329182014-03-19 18:05:18 -070034BOOST_AUTO_TEST_SUITE(TestBasicInerestRead)
35
36const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6};
37
38template<class Dataset>
Weiqi Shif0330d52014-07-09 10:54:27 -070039class BasicInterestReadFixture : public RepoStorageFixture, public Dataset
Shuo Chenca329182014-03-19 18:05:18 -070040{
41public:
42 BasicInterestReadFixture()
43 : scheduler(repoFace.getIoService())
Nick Gordon190e4dc2017-10-04 16:54:10 -050044 , readHandle(repoFace, *handle, keyChain, scheduler, 0)
Shuo Chenca329182014-03-19 18:05:18 -070045 , readFace(repoFace.getIoService())
46 {
47 }
48
49 ~BasicInterestReadFixture()
50 {
51 repoFace.getIoService().stop();
52 }
53
54 void
55 startListen()
56 {
57 readHandle.listen("/");
58 }
59
60 void
61 scheduleReadEvent()
62 {
63 int timeCount = 1;
64 for (typename Dataset::DataContainer::iterator i = this->data.begin();
65 i != this->data.end(); ++i) {
66 //First insert a data into database;
67 (*i)->setContent(content, sizeof(content));
68 (*i)->setFreshnessPeriod(ndn::time::milliseconds(36000));
69 keyChain.sign(**i);
70 bool rc = handle->insertData(**i);
71
72 BOOST_CHECK_EQUAL(rc, true);
Shuo Chenca329182014-03-19 18:05:18 -070073 Interest readInterest((*i)->getName());
74 readInterest.setMustBeFresh(true);
75 scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50),
Alexander Afanasyev42290b22017-03-09 12:58:29 -080076 bind(&BasicInterestReadFixture<Dataset>::sendInterest, this,
Shuo Chenca329182014-03-19 18:05:18 -070077 readInterest));
78 timeCount++;
79 }
80 }
81
82 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080083 onReadData(const ndn::Interest& interest, const ndn::Data& data)
Shuo Chenca329182014-03-19 18:05:18 -070084 {
85 int rc = memcmp(data.getContent().value(), content, sizeof(content));
86 BOOST_CHECK_EQUAL(rc, 0);
Shuo Chenca329182014-03-19 18:05:18 -070087 }
88
89 void
90 onReadTimeout(const ndn::Interest& interest)
91 {
92 BOOST_ERROR("Insert not successfull or Read data does not successfull");
93 }
94
95 void
96 sendInterest(const ndn::Interest& interest)
97 {
98 readFace.expressInterest(interest,
99 bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800100 bind(&BasicInterestReadFixture::onReadTimeout, this, _1), // Nack
Shuo Chenca329182014-03-19 18:05:18 -0700101 bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
102 }
103
104public:
105 ndn::Face repoFace;
106 ndn::KeyChain keyChain;
107 ndn::Scheduler scheduler;
108 ReadHandle readHandle;
109 ndn::Face readFace;
110};
111
112
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700113typedef boost::mpl::vector< BasicDataset,
114 FetchByPrefixDataset,
115 BasicChildSelectorDataset,
116 ExtendedChildSelectorDataset,
117 SamePrefixDataset<10> > Datasets;
118
119BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>)
Shuo Chenca329182014-03-19 18:05:18 -0700120{
121 // Insert dataset
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700122 // for (typename T::DataContainer::iterator i = this->data.begin();
123 // i != this->data.end(); ++i) {
124 // BOOST_CHECK_EQUAL(this->handle.insertData(**i), true);
125 // }
Shuo Chenca329182014-03-19 18:05:18 -0700126
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -0700127 // BOOST_CHECK_EQUAL(this->handle.size(), this->data.size());
Shuo Chenca329182014-03-19 18:05:18 -0700128
129 this->startListen();
130 this->scheduler.scheduleEvent(ndn::time::seconds(0),
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800131 bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
Shuo Chenca329182014-03-19 18:05:18 -0700132
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000133 this->repoFace.processEvents(ndn::time::seconds(20));
Shuo Chenca329182014-03-19 18:05:18 -0700134
135}
136
137BOOST_AUTO_TEST_SUITE_END()
138
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800139} // namespace tests
140} // namespace repo