blob: 79796c71212363999a45a5748d3bd0b14ba9083f [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
21#include <boost/test/unit_test.hpp>
22#include <boost/lexical_cast.hpp>
23
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "logging.hpp"
25#include "action-log.hpp"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080026
27#include <unistd.h>
28#include <iostream>
29#include <boost/filesystem.hpp>
30#include <boost/make_shared.hpp>
31
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
39BOOST_AUTO_TEST_CASE (ActionLogTest)
40{
41 INIT_LOGGERS ();
42
43 Name localName ("/alex");
44
45 fs::path tmpdir = fs::unique_path (fs::temp_directory_path () / "%%%%-%%%%-%%%%-%%%%");
46 SyncLogPtr syncLog = make_shared<SyncLog> (tmpdir, localName);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070047 NdnxWrapperPtr ndnx = make_shared<NdnxWrapper> ();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080048
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070049 ActionLogPtr actionLog = make_shared<ActionLog> (ndnx, tmpdir, syncLog, "top-secret", "test-chronoshare",
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080050 ActionLog::OnFileAddedOrChangedCallback(), ActionLog::OnFileRemovedCallback ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080051
52// 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);
58
59 BOOST_CHECK_EQUAL (syncLog->LogSize (), 0);
60 BOOST_CHECK_EQUAL (actionLog->LogSize (), 0);
61
62 actionLog->AddLocalActionUpdate ("file.txt", *Hash::FromString ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
63 time (NULL), 0755, 10);
64
65 BOOST_CHECK_EQUAL (syncLog->SeqNo (localName), 1);
66 BOOST_CHECK_EQUAL (syncLog->LogSize (), 0);
67 BOOST_CHECK_EQUAL (actionLog->LogSize (), 1);
68
69 HashPtr hash = syncLog->RememberStateInStateLog ();
70 BOOST_CHECK_EQUAL (syncLog->LogSize (), 1);
71 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
72
73 PcoPtr pco = actionLog->LookupActionPco (localName, 0);
74 BOOST_CHECK_EQUAL ((bool)pco, false);
75
76 pco = actionLog->LookupActionPco (localName, 1);
77 BOOST_CHECK_EQUAL ((bool)pco, true);
78
Alexander Afanasyev4d086752013-02-07 13:06:04 -080079 BOOST_CHECK_EQUAL (pco->name (), "/alex/test-chronoshare/action/top-secret/%00%01");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080080
Alexander Afanasyev4d086752013-02-07 13:06:04 -080081 ActionItemPtr action = actionLog->LookupAction (Name ("/alex/test-chronoshare/action/top-secret")(0));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080082 BOOST_CHECK_EQUAL ((bool)action, false);
83
Alexander Afanasyev4d086752013-02-07 13:06:04 -080084 action = actionLog->LookupAction (Name ("/alex/test-chronoshare/action/top-secret")(1));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080085 BOOST_CHECK_EQUAL ((bool)action, true);
86
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080087 if (action)
88 {
89 BOOST_CHECK_EQUAL (action->version (), 0);
90 BOOST_CHECK_EQUAL (action->action (), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080091
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080092 BOOST_CHECK_EQUAL (action->filename (), "file.txt");
93 BOOST_CHECK_EQUAL (action->seg_num (), 10);
94 BOOST_CHECK_EQUAL (action->file_hash ().size (), 32);
95 BOOST_CHECK_EQUAL (action->mode (), 0755);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080096
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080097 BOOST_CHECK_EQUAL (action->has_parent_device_name (), false);
98 BOOST_CHECK_EQUAL (action->has_parent_seq_no (), false);
99 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800100
101 actionLog->AddLocalActionUpdate ("file.txt", *Hash::FromString ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
102 time (NULL), 0755, 10);
103 BOOST_CHECK_EQUAL (syncLog->SeqNo (localName), 2);
104 BOOST_CHECK_EQUAL (syncLog->LogSize (), 1);
105 BOOST_CHECK_EQUAL (actionLog->LogSize (), 2);
106
107 action = actionLog->LookupAction (Name ("/alex"), 2);
108 BOOST_CHECK_EQUAL ((bool)action, true);
109
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800110 if (action)
111 {
112 BOOST_CHECK_EQUAL (action->has_parent_device_name (), true);
113 BOOST_CHECK_EQUAL (action->has_parent_seq_no (), true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800114
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800115 BOOST_CHECK_EQUAL (action->parent_seq_no (), 1);
116 BOOST_CHECK_EQUAL (action->version (), 1);
117 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800118
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800119 BOOST_CHECK_EQUAL ((bool)actionLog->AddRemoteAction (pco), true);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800120 BOOST_CHECK_EQUAL (actionLog->LogSize (), 2);
121
122 // create a real remote action
123 ActionItem item;
124 item.set_action (ActionItem::UPDATE);
125 item.set_filename ("file.txt");
126 item.set_version (2);
127 item.set_timestamp (time (NULL));
128
129 BytesPtr item_msg = serializeMsg (item);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800130 Name actionName = Name ("/")(Name("/zhenkai/test"))("test-chronoshare")("action")("top-secret")(1);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700131 Bytes actionData = ndnx->createContentObject (actionName, head (*item_msg), item_msg->size ());
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800132
133 pco = make_shared<ParsedContentObject> (actionData);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800134 BOOST_CHECK_EQUAL ((bool)actionLog->AddRemoteAction (pco), true);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800135 BOOST_CHECK_EQUAL (actionLog->LogSize (), 3);
136
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800137 remove_all (tmpdir);
138}
139
140BOOST_AUTO_TEST_SUITE_END()
141
142 // catch (boost::exception &err)
143 // {
144 // cout << *boost::get_error_info<errmsg_info_str> (err) << endl;
145 // }