blob: 7b721c17ce8619a667fc2f3f77371fab7969c5e6 [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.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -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.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -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.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -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.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "logging.hpp"
22#include "object-manager.hpp"
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080023
24#include <boost/filesystem.hpp>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080025#include <boost/filesystem/fstream.hpp>
26
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080027#include <boost/make_shared.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028#include <boost/test/unit_test.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029#include <iostream>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080030#include <iterator>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include <unistd.h>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080032
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080033_LOG_INIT(Test.ObjectManager);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080034
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070035using namespace Ndnx;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080036using namespace std;
37using namespace boost;
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080038namespace fs = boost::filesystem;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080039
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080040BOOST_AUTO_TEST_SUITE(TestObjectManager)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080041
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042BOOST_AUTO_TEST_CASE(ObjectManagerTest)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080043{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080044 fs::path tmpdir = fs::unique_path(fs::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%");
45 _LOG_DEBUG("tmpdir: " << tmpdir);
46 Name deviceName("/device");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080047
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper>();
49 ObjectManager manager(ccnx, tmpdir, "test-chronoshare");
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080050
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080051 tuple<HashPtr, int> hash_semgents =
52 manager.localFileToObjects(fs::path("test") / "test-object-manager.cc", deviceName);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080053
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 BOOST_CHECK_EQUAL(hash_semgents.get<1>(), 3);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080055
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 bool ok = manager.objectsToLocalFile(deviceName, *hash_semgents.get<0>(), tmpdir / "test.cc");
57 BOOST_CHECK_EQUAL(ok, true);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080058
59 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080060 fs::ifstream origFile(fs::path("test") / "test-object-manager.cc");
61 fs::ifstream newFile(tmpdir / "test.cc");
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080062
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080063 istream_iterator<char> eof, origFileI(origFile), newFileI(newFile);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080064
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080065 BOOST_CHECK_EQUAL_COLLECTIONS(origFileI, eof, newFileI, eof);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080066 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080067
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 remove_all(tmpdir);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080069}
70
71
72BOOST_AUTO_TEST_SUITE_END()