blob: f598276e497544c2a859e8dd694c7d38ede4adb7 [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.
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 INIT_LOGGERS();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080042
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 Name localName("/alex");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080044
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080045 fs::path tmpdir = fs::unique_path(fs::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%");
46 SyncLogPtr syncLog = make_shared<SyncLog>(tmpdir, localName);
47 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper>();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080048
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080049 ActionLogPtr actionLog =
50 make_shared<ActionLog>(ccnx, tmpdir, syncLog, "top-secret", "test-chronoshare",
51 ActionLog::OnFileAddedOrChangedCallback(),
52 ActionLog::OnFileRemovedCallback());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080053
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 // const std::string &filename,
55 // const Hash &hash,
56 // time_t wtime,
57 // int mode,
58 // int seg_num
59 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080060
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
62 BOOST_CHECK_EQUAL(actionLog->LogSize(), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080063
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 actionLog->AddLocalActionUpdate("file.txt",
65 *Hash::FromString(
66 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
67 time(NULL), 0755, 10);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080068
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080069 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 1);
70 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
71 BOOST_CHECK_EQUAL(actionLog->LogSize(), 1);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080072
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073 HashPtr hash = syncLog->RememberStateInStateLog();
74 BOOST_CHECK_EQUAL(syncLog->LogSize(), 1);
75 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
76 "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080077
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 PcoPtr pco = actionLog->LookupActionPco(localName, 0);
79 BOOST_CHECK_EQUAL((bool)pco, false);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080080
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080081 pco = actionLog->LookupActionPco(localName, 1);
82 BOOST_CHECK_EQUAL((bool)pco, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080083
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084 BOOST_CHECK_EQUAL(pco->name(), "/alex/test-chronoshare/action/top-secret/%00%01");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080085
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 ActionItemPtr action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(0));
87 BOOST_CHECK_EQUAL((bool)action, false);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080088
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(1));
90 BOOST_CHECK_EQUAL((bool)action, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080091
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080092 if (action) {
93 BOOST_CHECK_EQUAL(action->version(), 0);
94 BOOST_CHECK_EQUAL(action->action(), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080095
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080096 BOOST_CHECK_EQUAL(action->filename(), "file.txt");
97 BOOST_CHECK_EQUAL(action->seg_num(), 10);
98 BOOST_CHECK_EQUAL(action->file_hash().size(), 32);
99 BOOST_CHECK_EQUAL(action->mode(), 0755);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800100
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 BOOST_CHECK_EQUAL(action->has_parent_device_name(), false);
102 BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false);
103 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800104
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800105 actionLog->AddLocalActionUpdate("file.txt",
106 *Hash::FromString(
107 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
108 time(NULL), 0755, 10);
109 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 2);
110 BOOST_CHECK_EQUAL(syncLog->LogSize(), 1);
111 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800112
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800113 action = actionLog->LookupAction(Name("/alex"), 2);
114 BOOST_CHECK_EQUAL((bool)action, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800115
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800116 if (action) {
117 BOOST_CHECK_EQUAL(action->has_parent_device_name(), true);
118 BOOST_CHECK_EQUAL(action->has_parent_seq_no(), true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800119
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 BOOST_CHECK_EQUAL(action->parent_seq_no(), 1);
121 BOOST_CHECK_EQUAL(action->version(), 1);
122 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800123
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800124 BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true);
125 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800126
127 // create a real remote action
128 ActionItem item;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800129 item.set_action(ActionItem::UPDATE);
130 item.set_filename("file.txt");
131 item.set_version(2);
132 item.set_timestamp(time(NULL));
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800133
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800134 BytesPtr item_msg = serializeMsg(item);
135 Name actionName = Name("/")(Name("/zhenkai/test"))("test-chronoshare")("action")("top-secret")(1);
136 Bytes actionData = ccnx->createContentObject(actionName, head(*item_msg), item_msg->size());
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800137
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800138 pco = make_shared<ParsedContentObject>(actionData);
139 BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true);
140 BOOST_CHECK_EQUAL(actionLog->LogSize(), 3);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800141
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 remove_all(tmpdir);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800143}
144
145BOOST_AUTO_TEST_SUITE_END()
146
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800147// catch (boost::exception &err)
148// {
149// cout << *boost::get_error_info<errmsg_info_str> (err) << endl;
150// }