Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [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" |
| 21 | #include "storage/sqlite-storage.hpp" |
| 22 | #include "storage/repo-storage.hpp" |
| 23 | |
| 24 | #include "../repo-storage-fixture.hpp" |
| 25 | |
| 26 | #include <boost/test/unit_test.hpp> |
| 27 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 28 | |
| 29 | #define CHECK_INTERESTS(NAME,COMPONENT,BOOL) \ |
| 30 | do { \ |
| 31 | didMatch = false; \ |
| 32 | for (const auto interest : face.sentInterests) { \ |
| 33 | bool isPresent = false; \ |
| 34 | for (const auto section : NAME) { \ |
| 35 | isPresent = isPresent || section == COMPONENT; \ |
| 36 | } \ |
| 37 | didMatch = didMatch || isPresent; \ |
| 38 | } \ |
| 39 | BOOST_CHECK_EQUAL(didMatch,BOOL); \ |
| 40 | } while (0) |
| 41 | |
| 42 | namespace repo { |
| 43 | namespace tests { |
| 44 | |
| 45 | BOOST_AUTO_TEST_SUITE(TestReadHandle) |
| 46 | |
| 47 | class Fixture : public RepoStorageFixture |
| 48 | { |
| 49 | public: |
| 50 | Fixture() |
| 51 | : face(ndn::util::DummyClientFace::Options{true, true}) |
| 52 | , scheduler(face.getIoService()) |
| 53 | , subsetLength(1) |
| 54 | , dataPrefix("/ndn/test/prefix") |
| 55 | , identity("/ndn/test/identity") |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 56 | , readHandle(face, *handle, subsetLength) |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 57 | , numPrefixRegistrations(0) |
| 58 | , numPrefixUnregistrations(0) |
| 59 | { |
| 60 | readHandle.connectAutoListen(); |
| 61 | } |
| 62 | |
| 63 | public: |
| 64 | bool |
| 65 | containsNameComponent(const Name& name, const ndn::name::Component& component) |
| 66 | { |
| 67 | bool isPresent = false; |
| 68 | for (const auto section : name) { |
| 69 | isPresent = isPresent || section == component; |
| 70 | } |
| 71 | return isPresent; |
| 72 | } |
| 73 | |
| 74 | public: |
| 75 | ndn::util::DummyClientFace face; |
| 76 | ndn::KeyChain keyChain; |
| 77 | ndn::Scheduler scheduler; |
| 78 | |
| 79 | size_t subsetLength; |
| 80 | ndn::Name dataPrefix; |
| 81 | ndn::Name identity; |
| 82 | ReadHandle readHandle; |
| 83 | |
| 84 | size_t numPrefixRegistrations; |
| 85 | size_t numPrefixUnregistrations; |
| 86 | }; |
| 87 | |
| 88 | BOOST_FIXTURE_TEST_CASE(DataPrefixes, Fixture) |
| 89 | { |
| 90 | const std::vector<uint8_t> content(100, 'x'); |
| 91 | std::shared_ptr<Data> data1 = std::make_shared<Data>(Name{dataPrefix}.appendNumber(1)); |
| 92 | std::shared_ptr<Data> data2 = std::make_shared<Data>(Name{dataPrefix}.appendNumber(2)); |
| 93 | |
| 94 | data1->setContent(&content[0], content.size()); |
| 95 | data2->setContent(&content[0], content.size()); |
| 96 | |
| 97 | keyChain.createIdentity(identity); |
| 98 | keyChain.sign(*data1, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID, |
| 99 | identity)); |
| 100 | keyChain.sign(*data2, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID, |
| 101 | identity)); |
| 102 | |
| 103 | bool didMatch = false; |
| 104 | face.sentInterests.clear(); |
| 105 | handle->insertData(*data1); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 106 | face.processEvents(-1_ms); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 107 | CHECK_INTERESTS(interest.getName(), name::Component{"register"}, true); |
| 108 | |
| 109 | face.sentInterests.clear(); |
| 110 | handle->deleteData(data1->getFullName()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 111 | face.processEvents(-1_ms); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 112 | CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, true); |
| 113 | |
| 114 | face.sentInterests.clear(); |
| 115 | handle->insertData(*data1); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 116 | face.processEvents(-1_ms); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 117 | CHECK_INTERESTS(interest.getName(), name::Component{"register"}, true); |
| 118 | |
| 119 | face.sentInterests.clear(); |
| 120 | handle->insertData(*data2); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 121 | face.processEvents(-1_ms); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 122 | CHECK_INTERESTS(interest.getName(), name::Component{"register"}, false); |
| 123 | |
| 124 | face.sentInterests.clear(); |
| 125 | handle->deleteData(data1->getFullName()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 126 | face.processEvents(-1_ms); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 127 | CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, false); |
| 128 | |
| 129 | face.sentInterests.clear(); |
| 130 | handle->deleteData(data2->getFullName()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 131 | face.processEvents(-1_ms); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 132 | CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, true); |
| 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_SUITE_END() // TestReadHandle |
| 136 | |
| 137 | } // namespace tests |
| 138 | } // namespace repo |