blob: d9aa6c7038979694f7e182d2f1c217c86163b298 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2016, Regents of the University of California.
*
* This file is part of ChronoShare, a decentralized file sharing application over NDN.
*
* ChronoShare is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received copies of the GNU General Public License along with
* ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ChronoShare authors and contributors.
*/
#include "ccnx-common.hpp"
#include "ccnx-wrapper.hpp"
#include "content-server.hpp"
#include "fetch-manager.hpp"
#include "object-db.hpp"
#include "object-manager.hpp"
#include "scheduler.hpp"
#include <boost/filesystem.hpp>
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread_time.hpp>
#include <ctime>
#include <stdio.h>
#include "logging.hpp"
INIT_LOGGER("Test.ServerAndFetch");
using namespace Ndnx;
using namespace std;
using namespace boost;
using namespace boost::filesystem;
BOOST_AUTO_TEST_SUITE(TestServeAndFetch)
path root("test-server-and-fetch");
path filePath = root / "random-file";
unsigned char magic = 'm';
int repeat = 1024 * 400;
mutex mut;
condition_variable cond;
bool finished;
int ack;
void
setup()
{
if (exists(root)) {
remove_all(root);
}
create_directory(root);
// create file
FILE* fp = fopen(filePath.string().c_str(), "w");
for (int i = 0; i < repeat; i++) {
fwrite(&magic, 1, sizeof(magic), fp);
}
fclose(fp);
ack = 0;
finished = false;
}
void
teardown()
{
if (exists(root)) {
remove_all(root);
}
ack = 0;
finished = false;
}
Name
simpleMap(const Name& deviceName)
{
return Name("/local");
}
void
segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq, PcoPtr pco)
{
ack++;
Bytes co = pco->content();
int size = co.size();
for (int i = 0; i < size; i++) {
BOOST_CHECK_EQUAL(co[i], magic);
}
}
void
finishCallback(Name& deviceName, Name& baseName)
{
BOOST_CHECK_EQUAL(ack, repeat / 1024);
unique_lock<mutex> lock(mut);
finished = true;
cond.notify_one();
}
BOOST_AUTO_TEST_CASE(TestServeAndFetch)
{
INIT_LOGGERS();
_LOG_DEBUG("Setting up test environment ...");
setup();
NdnxWrapperPtr ndnx_serve = make_shared<NdnxWrapper>();
usleep(1000);
NdnxWrapperPtr ndnx_fetch = make_shared<NdnxWrapper>();
Name deviceName("/test/device");
Name localPrefix("/local");
Name broadcastPrefix("/broadcast");
const string APPNAME = "test-chronoshare";
time_t start = time(NULL);
_LOG_DEBUG("At time " << start << ", publish local file to database, this is extremely slow ...");
// publish file to db
ObjectManager om(ndnx_serve, root, APPNAME);
tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
time_t end = time(NULL);
_LOG_DEBUG("At time " << end << ", publish finally finished, used " << end - start
<< " seconds ...");
ActionLogPtr dummyLog;
ContentServer server(ndnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5);
server.registerPrefix(localPrefix);
server.registerPrefix(broadcastPrefix);
FetchManager fm(ccnx_fetch, bind(simpleMap, _1), Name("/local/broadcast"));
HashPtr hash = pub.get<0>();
Name baseName = Name("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes());
fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4),
bind(finishCallback, _1, _2), 0, pub.get<1>() - 1);
unique_lock<mutex> lock(mut);
system_time timeout = get_system_time() + posix_time::milliseconds(5000);
while (!finished) {
if (!cond.timed_wait(lock, timeout)) {
BOOST_FAIL("Fetching has not finished after 5 seconds");
break;
}
}
ccnx_fetch->shutdown();
ccnx_serve->shutdown();
_LOG_DEBUG("Finish");
usleep(100000);
teardown();
}
BOOST_AUTO_TEST_SUITE_END()