blob: 22810269f5229f13a329a2d80781ff07de220af8 [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 Afanasyeva35756b2013-01-22 16:59:11 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyeva35756b2013-01-22 16:59:11 -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 Afanasyeva35756b2013-01-22 16:59:11 -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 Afanasyeva35756b2013-01-22 16:59:11 -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 Afanasyeva35756b2013-01-22 16:59:11 -080019 */
20
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080021#include <boost/lexical_cast.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080022#include <boost/test/unit_test.hpp>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080023
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "action-log.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include "logging.hpp"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080026
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080027#include <boost/filesystem.hpp>
28#include <boost/make_shared.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080029#include <iostream>
30#include <unistd.h>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080031
32using namespace std;
33using namespace boost;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070034using namespace Ndnx;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080035namespace fs = boost::filesystem;
36
37BOOST_AUTO_TEST_SUITE(TestActionLog)
38
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039BOOST_AUTO_TEST_CASE(ActionLogTest)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080040{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041 Name localName("/alex");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080042
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 fs::path tmpdir = fs::unique_path(fs::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%");
44 SyncLogPtr syncLog = make_shared<SyncLog>(tmpdir, localName);
45 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper>();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080046
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 ActionLogPtr actionLog =
48 make_shared<ActionLog>(ccnx, tmpdir, syncLog, "top-secret", "test-chronoshare",
49 ActionLog::OnFileAddedOrChangedCallback(),
50 ActionLog::OnFileRemovedCallback());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080051
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 // const std::string &filename,
53 // const Hash &hash,
54 // time_t wtime,
55 // int mode,
56 // int seg_num
57 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080058
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
60 BOOST_CHECK_EQUAL(actionLog->LogSize(), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080061
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 actionLog->AddLocalActionUpdate("file.txt",
63 *Hash::FromString(
64 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
65 time(NULL), 0755, 10);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080066
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 1);
68 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
69 BOOST_CHECK_EQUAL(actionLog->LogSize(), 1);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080070
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 HashPtr hash = syncLog->RememberStateInStateLog();
72 BOOST_CHECK_EQUAL(syncLog->LogSize(), 1);
73 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
74 "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080075
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 PcoPtr pco = actionLog->LookupActionPco(localName, 0);
77 BOOST_CHECK_EQUAL((bool)pco, false);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080078
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 pco = actionLog->LookupActionPco(localName, 1);
80 BOOST_CHECK_EQUAL((bool)pco, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080081
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082 BOOST_CHECK_EQUAL(pco->name(), "/alex/test-chronoshare/action/top-secret/%00%01");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080083
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084 ActionItemPtr action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(0));
85 BOOST_CHECK_EQUAL((bool)action, false);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080086
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080087 action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(1));
88 BOOST_CHECK_EQUAL((bool)action, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080089
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090 if (action) {
91 BOOST_CHECK_EQUAL(action->version(), 0);
92 BOOST_CHECK_EQUAL(action->action(), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080093
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080094 BOOST_CHECK_EQUAL(action->filename(), "file.txt");
95 BOOST_CHECK_EQUAL(action->seg_num(), 10);
96 BOOST_CHECK_EQUAL(action->file_hash().size(), 32);
97 BOOST_CHECK_EQUAL(action->mode(), 0755);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080098
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080099 BOOST_CHECK_EQUAL(action->has_parent_device_name(), false);
100 BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false);
101 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800102
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 actionLog->AddLocalActionUpdate("file.txt",
104 *Hash::FromString(
105 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
106 time(NULL), 0755, 10);
107 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 2);
108 BOOST_CHECK_EQUAL(syncLog->LogSize(), 1);
109 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800110
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800111 action = actionLog->LookupAction(Name("/alex"), 2);
112 BOOST_CHECK_EQUAL((bool)action, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800113
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114 if (action) {
115 BOOST_CHECK_EQUAL(action->has_parent_device_name(), true);
116 BOOST_CHECK_EQUAL(action->has_parent_seq_no(), true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800117
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118 BOOST_CHECK_EQUAL(action->parent_seq_no(), 1);
119 BOOST_CHECK_EQUAL(action->version(), 1);
120 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800121
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true);
123 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800124
125 // create a real remote action
126 ActionItem item;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 item.set_action(ActionItem::UPDATE);
128 item.set_filename("file.txt");
129 item.set_version(2);
130 item.set_timestamp(time(NULL));
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800131
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800132 BytesPtr item_msg = serializeMsg(item);
133 Name actionName = Name("/")(Name("/zhenkai/test"))("test-chronoshare")("action")("top-secret")(1);
134 Bytes actionData = ccnx->createContentObject(actionName, head(*item_msg), item_msg->size());
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800135
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800136 pco = make_shared<ParsedContentObject>(actionData);
137 BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true);
138 BOOST_CHECK_EQUAL(actionLog->LogSize(), 3);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800139
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800140 remove_all(tmpdir);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800141}
142
143BOOST_AUTO_TEST_SUITE_END()
144
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800145// catch (boost::exception &err)
146// {
147// cout << *boost::get_error_info<errmsg_info_str> (err) << endl;
148// }