blob: c4d6a9bc63770b5608ff5caf8896913b148a4312 [file] [log] [blame]
Alexander Afanasyeva35756b2013-01-22 16:59:11 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
22#include <boost/test/unit_test.hpp>
23#include <boost/lexical_cast.hpp>
24
25#include "logging.h"
26#include "action-log.h"
27
28#include <unistd.h>
29#include <iostream>
30#include <boost/filesystem.hpp>
31#include <boost/make_shared.hpp>
32
33using namespace std;
34using namespace boost;
35using namespace Ccnx;
36namespace fs = boost::filesystem;
37
38BOOST_AUTO_TEST_SUITE(TestActionLog)
39
40BOOST_AUTO_TEST_CASE (ActionLogTest)
41{
42 INIT_LOGGERS ();
43
44 Name localName ("/alex");
45
46 fs::path tmpdir = fs::unique_path (fs::temp_directory_path () / "%%%%-%%%%-%%%%-%%%%");
47 SyncLogPtr syncLog = make_shared<SyncLog> (tmpdir, localName);
48 CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> ();
49
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080050 ActionLogPtr actionLog = make_shared<ActionLog> (ccnx, tmpdir, syncLog, "top-secret", "test-chronoshare",
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080051 ActionLog::OnFileAddedOrChangedCallback(), ActionLog::OnFileRemovedCallback ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080052
53// const std::string &filename,
54// const Hash &hash,
55// time_t wtime,
56// int mode,
57// int seg_num
58 BOOST_CHECK_EQUAL (syncLog->SeqNo (localName), 0);
59
60 BOOST_CHECK_EQUAL (syncLog->LogSize (), 0);
61 BOOST_CHECK_EQUAL (actionLog->LogSize (), 0);
62
63 actionLog->AddLocalActionUpdate ("file.txt", *Hash::FromString ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
64 time (NULL), 0755, 10);
65
66 BOOST_CHECK_EQUAL (syncLog->SeqNo (localName), 1);
67 BOOST_CHECK_EQUAL (syncLog->LogSize (), 0);
68 BOOST_CHECK_EQUAL (actionLog->LogSize (), 1);
69
70 HashPtr hash = syncLog->RememberStateInStateLog ();
71 BOOST_CHECK_EQUAL (syncLog->LogSize (), 1);
72 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
73
74 PcoPtr pco = actionLog->LookupActionPco (localName, 0);
75 BOOST_CHECK_EQUAL ((bool)pco, false);
76
77 pco = actionLog->LookupActionPco (localName, 1);
78 BOOST_CHECK_EQUAL ((bool)pco, true);
79
Alexander Afanasyev4d086752013-02-07 13:06:04 -080080 BOOST_CHECK_EQUAL (pco->name (), "/alex/test-chronoshare/action/top-secret/%00%01");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080081
Alexander Afanasyev4d086752013-02-07 13:06:04 -080082 ActionItemPtr action = actionLog->LookupAction (Name ("/alex/test-chronoshare/action/top-secret")(0));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080083 BOOST_CHECK_EQUAL ((bool)action, false);
84
Alexander Afanasyev4d086752013-02-07 13:06:04 -080085 action = actionLog->LookupAction (Name ("/alex/test-chronoshare/action/top-secret")(1));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080086 BOOST_CHECK_EQUAL ((bool)action, true);
87
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080088 if (action)
89 {
90 BOOST_CHECK_EQUAL (action->version (), 0);
91 BOOST_CHECK_EQUAL (action->action (), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080092
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080093 BOOST_CHECK_EQUAL (action->filename (), "file.txt");
94 BOOST_CHECK_EQUAL (action->seg_num (), 10);
95 BOOST_CHECK_EQUAL (action->file_hash ().size (), 32);
96 BOOST_CHECK_EQUAL (action->mode (), 0755);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080097
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080098 BOOST_CHECK_EQUAL (action->has_parent_device_name (), false);
99 BOOST_CHECK_EQUAL (action->has_parent_seq_no (), false);
100 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800101
102 actionLog->AddLocalActionUpdate ("file.txt", *Hash::FromString ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
103 time (NULL), 0755, 10);
104 BOOST_CHECK_EQUAL (syncLog->SeqNo (localName), 2);
105 BOOST_CHECK_EQUAL (syncLog->LogSize (), 1);
106 BOOST_CHECK_EQUAL (actionLog->LogSize (), 2);
107
108 action = actionLog->LookupAction (Name ("/alex"), 2);
109 BOOST_CHECK_EQUAL ((bool)action, true);
110
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800111 if (action)
112 {
113 BOOST_CHECK_EQUAL (action->has_parent_device_name (), true);
114 BOOST_CHECK_EQUAL (action->has_parent_seq_no (), true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800115
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800116 BOOST_CHECK_EQUAL (action->parent_seq_no (), 1);
117 BOOST_CHECK_EQUAL (action->version (), 1);
118 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800119
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800120 BOOST_CHECK_EQUAL ((bool)actionLog->AddRemoteAction (pco), true);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800121 BOOST_CHECK_EQUAL (actionLog->LogSize (), 2);
122
123 // create a real remote action
124 ActionItem item;
125 item.set_action (ActionItem::UPDATE);
126 item.set_filename ("file.txt");
127 item.set_version (2);
128 item.set_timestamp (time (NULL));
129
130 BytesPtr item_msg = serializeMsg (item);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800131 Name actionName = Name ("/")(Name("/zhenkai/test"))("test-chronoshare")("action")("top-secret")(1);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800132 Bytes actionData = ccnx->createContentObject (actionName, head (*item_msg), item_msg->size ());
133
134 pco = make_shared<ParsedContentObject> (actionData);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800135 BOOST_CHECK_EQUAL ((bool)actionLog->AddRemoteAction (pco), true);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800136 BOOST_CHECK_EQUAL (actionLog->LogSize (), 3);
137
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800138 remove_all (tmpdir);
139}
140
141BOOST_AUTO_TEST_SUITE_END()
142
143 // catch (boost::exception &err)
144 // {
145 // cout << *boost::get_error_info<errmsg_info_str> (err) << endl;
146 // }