blob: 79c57f4cabe37f6215e2c835857f48ad025aa8ee [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080019 */
20
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080021#include "content-server.hpp"
22#include "fetch-manager.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080023#include "object-db.hpp"
24#include "object-manager.hpp"
Yukai Tu931546f2016-10-24 13:48:01 -070025#include "dummy-forwarder.hpp"
26
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080027#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028#include <boost/make_shared.hpp>
29#include <boost/test/unit_test.hpp>
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080030#include <boost/thread/condition_variable.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include <boost/thread/locks.hpp>
32#include <boost/thread/mutex.hpp>
33#include <boost/thread/thread_time.hpp>
Zhenkai Zhu13d224d2013-01-23 19:40:25 -080034#include <ctime>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035#include <stdio.h>
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080036
Yukai Tu931546f2016-10-24 13:48:01 -070037#include "test-common.hpp"
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080038
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080039
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080040using namespace std;
41using namespace boost;
42using namespace boost::filesystem;
43
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080044
Yukai Tu931546f2016-10-24 13:48:01 -070045namespace ndn {
46namespace chronoshare {
47namespace tests {
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080048
Yukai Tu931546f2016-10-24 13:48:01 -070049_LOG_INIT(Test.ServerAndFetch);
50
51class TestServerAndFetchFixture : public IdentityManagementTimeFixture
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080052{
Yukai Tu931546f2016-10-24 13:48:01 -070053public:
54 TestServerAndFetchFixture()
55 : forwarder(m_io, m_keyChain)
56 , root("test-server-and-fetch")
57 , filePath(root / "random-file")
58 , character('m')
59 , repeat(1024 * 40)
60 , finished(false)
61 , ack(0)
62 {
63 if (exists(root)) {
64 remove_all(root);
65 }
66
67 create_directory(root);
68
69 // create file
70 FILE* fp = fopen(filePath.string().c_str(), "w");
71 for (int i = 0; i < repeat; i++) {
72 fwrite(&character, 1, sizeof(character), fp);
73 }
74 fclose(fp);
75
76 ack = 0;
77 finished = false;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080078 }
79
Yukai Tu931546f2016-10-24 13:48:01 -070080 ~TestServerAndFetchFixture()
81 {
82 if (exists(root)) {
83 remove_all(root);
84 }
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080085 }
86
Yukai Tu931546f2016-10-24 13:48:01 -070087 Name
88 simpleMap(const Name& deviceName)
89 {
90 return Name("/local");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080091 }
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080092
Yukai Tu931546f2016-10-24 13:48:01 -070093 void
94 segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq,
95 ndn::shared_ptr<ndn::Data> data)
96 {
97 ack++;
98 ndn::Block block = Data(data->getContent().blockFromValue()).getContent();
99 int size = block.value_size();
100 const uint8_t* co = block.value();
101 for (int i = 0; i < size; i++) {
102 BOOST_CHECK_EQUAL(co[i], character);
103 }
104
105 _LOG_DEBUG("I'm called!!! segmentCallback!! size " << size << " ack times:" << ack);
106 }
107
108 void
109 finishCallback(Name& deviceName, Name& baseName)
110 {
111 BOOST_CHECK_EQUAL(ack, repeat / 1024);
112 finished = true;
113 cond.notify_one();
114 }
115
116public:
117 DummyForwarder forwarder;
118 path root;
119 path filePath;
120
121 unsigned char character;
122 int repeat;
123
124 boost::condition_variable cond;
125 bool finished;
126 int ack;
127};
128
129BOOST_FIXTURE_TEST_SUITE(TestServeAndFetch, TestServerAndFetchFixture)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800130
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800131BOOST_AUTO_TEST_CASE(TestServeAndFetch)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800132{
Yukai Tu931546f2016-10-24 13:48:01 -0700133 Face& face_serve = forwarder.addFace();
134 Face& face_fetch = forwarder.addFace();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800135
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800136 Name deviceName("/test/device");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800137 Name localPrefix("/local");
Yukai Tu931546f2016-10-24 13:48:01 -0700138 Name broadcastPrefix("/multicast");
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800139
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800140 const string APPNAME = "test-chronoshare";
141
Yukai Tu931546f2016-10-24 13:48:01 -0700142 time_t start = std::time(NULL);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800143 _LOG_DEBUG("At time " << start << ", publish local file to database, this is extremely slow ...");
Yukai Tu931546f2016-10-24 13:48:01 -0700144
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800145 // publish file to db
Yukai Tu931546f2016-10-24 13:48:01 -0700146 ObjectManager om(face_serve, m_keyChain, root, APPNAME);
147 auto pub = om.localFileToObjects(filePath, deviceName);
148
149 time_t end = std::time(NULL);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800150 _LOG_DEBUG("At time " << end << ", publish finally finished, used " << end - start
151 << " seconds ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800152
153 ActionLogPtr dummyLog;
Lijing Wang8e56d082016-12-25 14:45:23 -0800154 ContentServer server(face_serve, dummyLog, root, deviceName, "pentagon's secrets",
155 name::Component(APPNAME), m_keyChain, time::seconds(5));
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800156 server.registerPrefix(localPrefix);
157 server.registerPrefix(broadcastPrefix);
158
Yukai Tu931546f2016-10-24 13:48:01 -0700159 FetchManager fm(face_fetch, bind(&TestServerAndFetchFixture::simpleMap, this, _1), Name("/"));
160 ConstBufferPtr hash = std::get<0>(pub);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800161
Yukai Tu931546f2016-10-24 13:48:01 -0700162 Name baseName = Name(deviceName);
163 baseName.append(APPNAME).append("file").appendImplicitSha256Digest(hash);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800164
Yukai Tu931546f2016-10-24 13:48:01 -0700165 fm.Enqueue(deviceName, baseName, bind(&TestServerAndFetchFixture::segmentCallback, this, _1, _2, _3, _4),
166 bind(&TestServerAndFetchFixture::finishCallback, this, _1, _2), 0, std::get<1>(pub) - 1);
167
168 advanceClocks(time::milliseconds(10), 1000);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800169
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800170 _LOG_DEBUG("Finish");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800171}
172
173BOOST_AUTO_TEST_SUITE_END()
Yukai Tu931546f2016-10-24 13:48:01 -0700174
175} // namespace tests
176} // namespace chronoshare
177} // namespace ndn