blob: d9aa6c7038979694f7e182d2f1c217c86163b298 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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 Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "ccnx-common.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080022#include "ccnx-wrapper.hpp"
23#include "content-server.hpp"
24#include "fetch-manager.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080025#include "object-db.hpp"
26#include "object-manager.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include "scheduler.hpp"
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080028#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080029#include <boost/make_shared.hpp>
30#include <boost/test/unit_test.hpp>
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080031#include <boost/thread/condition_variable.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080032#include <boost/thread/locks.hpp>
33#include <boost/thread/mutex.hpp>
34#include <boost/thread/thread_time.hpp>
Zhenkai Zhu13d224d2013-01-23 19:40:25 -080035#include <ctime>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080036#include <stdio.h>
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080037
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080038#include "logging.hpp"
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080039
40INIT_LOGGER("Test.ServerAndFetch");
41
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070042using namespace Ndnx;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080043using namespace std;
44using namespace boost;
45using namespace boost::filesystem;
46
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080047BOOST_AUTO_TEST_SUITE(TestServeAndFetch)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080048
49path root("test-server-and-fetch");
50path filePath = root / "random-file";
51unsigned char magic = 'm';
52int repeat = 1024 * 400;
53mutex mut;
54condition_variable cond;
55bool finished;
56int ack;
57
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058void
59setup()
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080060{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 if (exists(root)) {
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080062 remove_all(root);
63 }
64
65 create_directory(root);
66
67 // create file
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 FILE* fp = fopen(filePath.string().c_str(), "w");
69 for (int i = 0; i < repeat; i++) {
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080070 fwrite(&magic, 1, sizeof(magic), fp);
71 }
72 fclose(fp);
73
74 ack = 0;
75 finished = false;
76}
77
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078void
79teardown()
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080080{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080081 if (exists(root)) {
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080082 remove_all(root);
83 }
84
85 ack = 0;
86 finished = false;
87}
88
89Name
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090simpleMap(const Name& deviceName)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080091{
92 return Name("/local");
93}
94
95void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080096segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq, PcoPtr pco)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080097{
98 ack++;
99 Bytes co = pco->content();
100 int size = co.size();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 for (int i = 0; i < size; i++) {
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800102 BOOST_CHECK_EQUAL(co[i], magic);
103 }
104}
105
106void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107finishCallback(Name& deviceName, Name& baseName)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800108{
109 BOOST_CHECK_EQUAL(ack, repeat / 1024);
110 unique_lock<mutex> lock(mut);
111 finished = true;
112 cond.notify_one();
113}
114
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115BOOST_AUTO_TEST_CASE(TestServeAndFetch)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800116{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800117 INIT_LOGGERS();
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800118
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800119 _LOG_DEBUG("Setting up test environment ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800120 setup();
121
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700122 NdnxWrapperPtr ndnx_serve = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800123 usleep(1000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700124 NdnxWrapperPtr ndnx_fetch = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800125
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800126 Name deviceName("/test/device");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800127 Name localPrefix("/local");
128 Name broadcastPrefix("/broadcast");
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800129
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800130 const string APPNAME = "test-chronoshare";
131
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800132 time_t start = time(NULL);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800133 _LOG_DEBUG("At time " << start << ", publish local file to database, this is extremely slow ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800134 // publish file to db
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700135 ObjectManager om(ndnx_serve, root, APPNAME);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800136 tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800137 time_t end = time(NULL);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800138 _LOG_DEBUG("At time " << end << ", publish finally finished, used " << end - start
139 << " seconds ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800140
141 ActionLogPtr dummyLog;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700142 ContentServer server(ndnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800143 server.registerPrefix(localPrefix);
144 server.registerPrefix(broadcastPrefix);
145
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800146 FetchManager fm(ccnx_fetch, bind(simpleMap, _1), Name("/local/broadcast"));
147 HashPtr hash = pub.get<0>();
148 Name baseName = Name("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800149
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800150 fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4),
151 bind(finishCallback, _1, _2), 0, pub.get<1>() - 1);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800152
153 unique_lock<mutex> lock(mut);
154 system_time timeout = get_system_time() + posix_time::milliseconds(5000);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800155 while (!finished) {
156 if (!cond.timed_wait(lock, timeout)) {
157 BOOST_FAIL("Fetching has not finished after 5 seconds");
158 break;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800159 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800160 }
161 ccnx_fetch->shutdown();
162 ccnx_serve->shutdown();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800163
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800164 _LOG_DEBUG("Finish");
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800165 usleep(100000);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800166
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800167 teardown();
168}
169
170BOOST_AUTO_TEST_SUITE_END()