blob: 0ab2346059f2e6a33b1458bf59f2ee417edbb280 [file] [log] [blame]
Shuo Chenca329182014-03-19 18:05:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
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"
21#include "storage/storage-handle.hpp"
22#include "storage/sqlite-handle.hpp"
23
24#include "../sqlite-fixture.hpp"
25#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>
40class BasicInterestReadFixture : public SqliteFixture, public Dataset
41{
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);
74
75 Interest readInterest((*i)->getName());
76 readInterest.setMustBeFresh(true);
77 scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50),
78 ndn::bind(&BasicInterestReadFixture<Dataset>::sendInterest, this,
79 readInterest));
80 timeCount++;
81 }
82 }
83
84 void
85 stopFaceProcess()
86 {
87 repoFace.getIoService().stop();
88 }
89
90 void
91 onReadData(const ndn::Interest& interest, ndn::Data& data)
92 {
93 int rc = memcmp(data.getContent().value(), content, sizeof(content));
94 BOOST_CHECK_EQUAL(rc, 0);
95 //then delete the data
96 BOOST_CHECK_EQUAL(handle->deleteData(data.getName()), true);
97 }
98
99 void
100 onReadTimeout(const ndn::Interest& interest)
101 {
102 BOOST_ERROR("Insert not successfull or Read data does not successfull");
103 }
104
105 void
106 sendInterest(const ndn::Interest& interest)
107 {
108 readFace.expressInterest(interest,
109 bind(&BasicInterestReadFixture::onReadData, this, _1, _2),
110 bind(&BasicInterestReadFixture::onReadTimeout, this, _1));
111 }
112
113public:
114 ndn::Face repoFace;
115 ndn::KeyChain keyChain;
116 ndn::Scheduler scheduler;
117 ReadHandle readHandle;
118 ndn::Face readFace;
119};
120
121
122BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, DatasetFixtures, BasicInterestReadFixture<T>)
123{
124 // Insert dataset
125 for (typename T::DataContainer::iterator i = this->data.begin();
126 i != this->data.end(); ++i) {
127 BOOST_CHECK_EQUAL(this->handle->insertData(**i), true);
128 }
129
130 BOOST_CHECK_EQUAL(this->handle->size(), this->data.size());
131
132 this->startListen();
133 this->scheduler.scheduleEvent(ndn::time::seconds(0),
134 ndn::bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this));
135
136 // schedule an event to terminate IO
137 this->scheduler.scheduleEvent(ndn::time::seconds(10),
138 ndn::bind(&BasicInterestReadFixture<T>::stopFaceProcess, this));
139 this->repoFace.getIoService().run();
140
141}
142
143BOOST_AUTO_TEST_SUITE_END()
144
145} //namespace tests
146} //namespace repo