blob: 1e22b4ebbe3880728ca6eeb139c81ad85ff71ce7 [file] [log] [blame]
Nick Gordon190e4dc2017-10-04 16:54:10 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento4a160042020-04-13 16:50:02 -04002/*
3 * Copyright (c) 2014-2020, Regents of the University of California.
Nick Gordon190e4dc2017-10-04 16:54:10 -05004 *
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
Davide Pesavento9bf1a1d2020-04-14 16:50:03 -040029#define CHECK_INTERESTS(NAME, COMPONENT, EXPECTED) \
30 do { \
31 bool didMatch = false; \
32 for (const auto& interest : face.sentInterests) { \
33 didMatch = didMatch || containsNameComponent(NAME, COMPONENT); \
34 } \
35 BOOST_CHECK_EQUAL(didMatch, EXPECTED); \
36 } while (false)
Nick Gordon190e4dc2017-10-04 16:54:10 -050037
38namespace repo {
39namespace tests {
40
41BOOST_AUTO_TEST_SUITE(TestReadHandle)
42
43class Fixture : public RepoStorageFixture
44{
45public:
46 Fixture()
Davide Pesavento9bf1a1d2020-04-14 16:50:03 -040047 : face({true, true})
Nick Gordon190e4dc2017-10-04 16:54:10 -050048 , scheduler(face.getIoService())
49 , subsetLength(1)
50 , dataPrefix("/ndn/test/prefix")
51 , identity("/ndn/test/identity")
weijia yuan82cf9142018-10-21 12:25:02 -070052 , readHandle(face, *handle, subsetLength)
Nick Gordon190e4dc2017-10-04 16:54:10 -050053 , numPrefixRegistrations(0)
54 , numPrefixUnregistrations(0)
55 {
56 readHandle.connectAutoListen();
57 }
58
Davide Pesavento9bf1a1d2020-04-14 16:50:03 -040059 static bool
Nick Gordon190e4dc2017-10-04 16:54:10 -050060 containsNameComponent(const Name& name, const ndn::name::Component& component)
61 {
Davide Pesavento9bf1a1d2020-04-14 16:50:03 -040062 for (const auto& c : name) {
63 if (c == component)
64 return true;
Nick Gordon190e4dc2017-10-04 16:54:10 -050065 }
Davide Pesavento9bf1a1d2020-04-14 16:50:03 -040066 return false;
Nick Gordon190e4dc2017-10-04 16:54:10 -050067 }
68
69public:
70 ndn::util::DummyClientFace face;
71 ndn::KeyChain keyChain;
72 ndn::Scheduler scheduler;
73
74 size_t subsetLength;
75 ndn::Name dataPrefix;
76 ndn::Name identity;
77 ReadHandle readHandle;
78
79 size_t numPrefixRegistrations;
80 size_t numPrefixUnregistrations;
81};
82
83BOOST_FIXTURE_TEST_CASE(DataPrefixes, Fixture)
84{
85 const std::vector<uint8_t> content(100, 'x');
Davide Pesavento4a160042020-04-13 16:50:02 -040086 auto data1 = std::make_shared<Data>(Name{dataPrefix}.appendNumber(1));
87 auto data2 = std::make_shared<Data>(Name{dataPrefix}.appendNumber(2));
Nick Gordon190e4dc2017-10-04 16:54:10 -050088
89 data1->setContent(&content[0], content.size());
90 data2->setContent(&content[0], content.size());
91
92 keyChain.createIdentity(identity);
93 keyChain.sign(*data1, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
94 identity));
95 keyChain.sign(*data2, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
96 identity));
97
Nick Gordon190e4dc2017-10-04 16:54:10 -050098 face.sentInterests.clear();
99 handle->insertData(*data1);
weijia yuan82cf9142018-10-21 12:25:02 -0700100 face.processEvents(-1_ms);
Nick Gordon190e4dc2017-10-04 16:54:10 -0500101 CHECK_INTERESTS(interest.getName(), name::Component{"register"}, true);
102
103 face.sentInterests.clear();
104 handle->deleteData(data1->getFullName());
weijia yuan82cf9142018-10-21 12:25:02 -0700105 face.processEvents(-1_ms);
Nick Gordon190e4dc2017-10-04 16:54:10 -0500106 CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, true);
107
108 face.sentInterests.clear();
109 handle->insertData(*data1);
weijia yuan82cf9142018-10-21 12:25:02 -0700110 face.processEvents(-1_ms);
Nick Gordon190e4dc2017-10-04 16:54:10 -0500111 CHECK_INTERESTS(interest.getName(), name::Component{"register"}, true);
112
113 face.sentInterests.clear();
114 handle->insertData(*data2);
weijia yuan82cf9142018-10-21 12:25:02 -0700115 face.processEvents(-1_ms);
Nick Gordon190e4dc2017-10-04 16:54:10 -0500116 CHECK_INTERESTS(interest.getName(), name::Component{"register"}, false);
117
118 face.sentInterests.clear();
119 handle->deleteData(data1->getFullName());
weijia yuan82cf9142018-10-21 12:25:02 -0700120 face.processEvents(-1_ms);
Nick Gordon190e4dc2017-10-04 16:54:10 -0500121 CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, false);
122
123 face.sentInterests.clear();
124 handle->deleteData(data2->getFullName());
weijia yuan82cf9142018-10-21 12:25:02 -0700125 face.processEvents(-1_ms);
Nick Gordon190e4dc2017-10-04 16:54:10 -0500126 CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, true);
127}
128
129BOOST_AUTO_TEST_SUITE_END() // TestReadHandle
130
131} // namespace tests
132} // namespace repo