blob: 8e8a3c6aea0277873cb3e2247a667fdabce20cb0 [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 "fetch-manager.hpp"
22#include "ccnx-wrapper.hpp"
23#include "ccnx-common.hpp"
24#include "scheduler.hpp"
25#include "object-db.hpp"
26#include "object-manager.hpp"
27#include "content-server.hpp"
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -080028#include <boost/test/unit_test.hpp>
29#include <boost/make_shared.hpp>
30#include <boost/filesystem.hpp>
31#include <boost/thread/mutex.hpp>
32#include <boost/thread/locks.hpp>
33#include <boost/thread/thread_time.hpp>
34#include <boost/thread/condition_variable.hpp>
35#include <stdio.h>
Zhenkai Zhu13d224d2013-01-23 19:40:25 -080036#include <ctime>
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
58void setup()
59{
60 if (exists(root))
61 {
62 remove_all(root);
63 }
64
65 create_directory(root);
66
67 // create file
68 FILE *fp = fopen(filePath.string().c_str(), "w");
69 for (int i = 0; i < repeat; i++)
70 {
71 fwrite(&magic, 1, sizeof(magic), fp);
72 }
73 fclose(fp);
74
75 ack = 0;
76 finished = false;
77}
78
79void teardown()
80{
81 if (exists(root))
82 {
83 remove_all(root);
84 }
85
86 ack = 0;
87 finished = false;
88}
89
90Name
91simpleMap(const Name &deviceName)
92{
93 return Name("/local");
94}
95
96void
97segmentCallback(const Name &deviceName, const Name &baseName, uint64_t seq, PcoPtr pco)
98{
99 ack++;
100 Bytes co = pco->content();
101 int size = co.size();
102 for (int i = 0; i < size; i++)
103 {
104 BOOST_CHECK_EQUAL(co[i], magic);
105 }
106}
107
108void
109finishCallback(Name &deviceName, Name &baseName)
110{
111 BOOST_CHECK_EQUAL(ack, repeat / 1024);
112 unique_lock<mutex> lock(mut);
113 finished = true;
114 cond.notify_one();
115}
116
117BOOST_AUTO_TEST_CASE (TestServeAndFetch)
118{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800119 INIT_LOGGERS ();
120
121 _LOG_DEBUG ("Setting up test environment ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800122 setup();
123
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700124 NdnxWrapperPtr ndnx_serve = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800125 usleep(1000);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700126 NdnxWrapperPtr ndnx_fetch = make_shared<NdnxWrapper>();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800127
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800128 Name deviceName("/test/device");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800129 Name localPrefix("/local");
130 Name broadcastPrefix("/broadcast");
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800131
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800132 const string APPNAME = "test-chronoshare";
133
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800134 time_t start = time(NULL);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800135 _LOG_DEBUG ("At time " << start << ", publish local file to database, this is extremely slow ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800136 // publish file to db
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700137 ObjectManager om(ndnx_serve, root, APPNAME);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800138 tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800139 time_t end = time(NULL);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800140 _LOG_DEBUG ("At time " << end <<", publish finally finished, used " << end - start << " seconds ...");
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800141
142 ActionLogPtr dummyLog;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700143 ContentServer server(ndnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5);
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800144 server.registerPrefix(localPrefix);
145 server.registerPrefix(broadcastPrefix);
146
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700147 FetchManager fm(ndnx_fetch, bind(simpleMap, _1), Name("/local/broadcast"));
Alexander Afanasyev678f3502013-01-23 17:09:37 -0800148 HashPtr hash = pub.get<0> ();
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800149 Name baseName = Name ("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800150
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800151 fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4), 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);
155 while (!finished)
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800156 {
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800157 if (!cond.timed_wait(lock, timeout))
158 {
159 BOOST_FAIL ("Fetching has not finished after 5 seconds");
160 break;
161 }
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800162 }
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700163 ndnx_fetch->shutdown ();
164 ndnx_serve->shutdown ();
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800165
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800166 _LOG_DEBUG ("Finish");
167 usleep(100000);
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800168
Zhenkai Zhu5f5dd9a2013-01-23 16:39:41 -0800169 teardown();
170}
171
172BOOST_AUTO_TEST_SUITE_END()