blob: da6aa1ae043a36fe4c2afc097cd8999f7316729b [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 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
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080040_LOG_INIT(Test.ServerAndFetch);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080041
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 _LOG_DEBUG("Setting up test environment ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800118 setup();
119
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700120 NdnxWrapperPtr ndnx_serve = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800121 usleep(1000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700122 NdnxWrapperPtr ndnx_fetch = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800123
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800124 Name deviceName("/test/device");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800125 Name localPrefix("/local");
126 Name broadcastPrefix("/broadcast");
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800127
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800128 const string APPNAME = "test-chronoshare";
129
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800130 time_t start = time(NULL);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800131 _LOG_DEBUG("At time " << start << ", publish local file to database, this is extremely slow ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800132 // publish file to db
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700133 ObjectManager om(ndnx_serve, root, APPNAME);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800134 tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800135 time_t end = time(NULL);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800136 _LOG_DEBUG("At time " << end << ", publish finally finished, used " << end - start
137 << " seconds ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800138
139 ActionLogPtr dummyLog;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700140 ContentServer server(ndnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800141 server.registerPrefix(localPrefix);
142 server.registerPrefix(broadcastPrefix);
143
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800144 FetchManager fm(ccnx_fetch, bind(simpleMap, _1), Name("/local/broadcast"));
145 HashPtr hash = pub.get<0>();
146 Name baseName = Name("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800147
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800148 fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4),
149 bind(finishCallback, _1, _2), 0, pub.get<1>() - 1);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800150
151 unique_lock<mutex> lock(mut);
152 system_time timeout = get_system_time() + posix_time::milliseconds(5000);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800153 while (!finished) {
154 if (!cond.timed_wait(lock, timeout)) {
155 BOOST_FAIL("Fetching has not finished after 5 seconds");
156 break;
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800157 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800158 }
159 ccnx_fetch->shutdown();
160 ccnx_serve->shutdown();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800161
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800162 _LOG_DEBUG("Finish");
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800163 usleep(100000);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800164
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800165 teardown();
166}
167
168BOOST_AUTO_TEST_SUITE_END()