Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 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" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 21 | #include "storage/sqlite-storage.hpp" |
| 22 | #include "storage/repo-storage.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 23 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 24 | #include "../repo-storage-fixture.hpp" |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 25 | #include "../dataset-fixtures.hpp" |
| 26 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 27 | #include <boost/test/unit_test.hpp> |
| 28 | |
| 29 | namespace repo { |
| 30 | namespace tests { |
| 31 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 32 | // All the test cases in this test suite should be run at once. |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestBasicInerestRead) |
| 34 | |
| 35 | const static uint8_t content[8] = {3, 1, 4, 1, 5, 9, 2, 6}; |
| 36 | |
| 37 | template<class Dataset> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 38 | class BasicInterestReadFixture : public RepoStorageFixture, public Dataset |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
| 41 | BasicInterestReadFixture() |
| 42 | : scheduler(repoFace.getIoService()) |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame^] | 43 | , readHandle(repoFace, *handle, keyChain, scheduler, 0) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 44 | , 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 Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 72 | Interest readInterest((*i)->getName()); |
| 73 | readInterest.setMustBeFresh(true); |
| 74 | scheduler.scheduleEvent(ndn::time::milliseconds(timeCount * 50), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 75 | bind(&BasicInterestReadFixture<Dataset>::sendInterest, this, |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 76 | readInterest)); |
| 77 | timeCount++; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 82 | onReadData(const ndn::Interest& interest, const ndn::Data& data) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 83 | { |
| 84 | int rc = memcmp(data.getContent().value(), content, sizeof(content)); |
| 85 | BOOST_CHECK_EQUAL(rc, 0); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 86 | } |
| 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 Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 99 | bind(&BasicInterestReadFixture::onReadTimeout, this, _1), // Nack |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 100 | bind(&BasicInterestReadFixture::onReadTimeout, this, _1)); |
| 101 | } |
| 102 | |
| 103 | public: |
| 104 | ndn::Face repoFace; |
| 105 | ndn::KeyChain keyChain; |
| 106 | ndn::Scheduler scheduler; |
| 107 | ReadHandle readHandle; |
| 108 | ndn::Face readFace; |
| 109 | }; |
| 110 | |
| 111 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 112 | typedef boost::mpl::vector< BasicDataset, |
| 113 | FetchByPrefixDataset, |
| 114 | BasicChildSelectorDataset, |
| 115 | ExtendedChildSelectorDataset, |
| 116 | SamePrefixDataset<10> > Datasets; |
| 117 | |
| 118 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Read, T, Datasets, BasicInterestReadFixture<T>) |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 119 | { |
| 120 | // Insert dataset |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 121 | // 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 Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 126 | // BOOST_CHECK_EQUAL(this->handle.size(), this->data.size()); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 127 | |
| 128 | this->startListen(); |
| 129 | this->scheduler.scheduleEvent(ndn::time::seconds(0), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 130 | bind(&BasicInterestReadFixture<T>::scheduleReadEvent, this)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 131 | |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 132 | this->repoFace.processEvents(ndn::time::seconds(20)); |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 133 | |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_SUITE_END() |
| 137 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 138 | } // namespace tests |
| 139 | } // namespace repo |