blob: 84c0cf9d1face912bb83ae9abd9b7f4b3ee3414c [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 Afanasyevf9978f82013-01-23 16:30:31 -080050 ActionLogPtr actionLog = make_shared<ActionLog> (ccnx, tmpdir, syncLog, "top-secret",
51 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
80 BOOST_CHECK_EQUAL (pco->name (), "/alex/action/top-secret/%00%01");
81
82 ActionItemPtr action = actionLog->LookupAction (Name ("/alex/action/top-secret")(0));
83 BOOST_CHECK_EQUAL ((bool)action, false);
84
85 action = actionLog->LookupAction (Name ("/alex/action/top-secret")(1));
86 BOOST_CHECK_EQUAL ((bool)action, true);
87
88 BOOST_CHECK_EQUAL (action->version (), 0);
89 BOOST_CHECK_EQUAL (action->action (), 0);
90
91 BOOST_CHECK_EQUAL (action->filename (), "file.txt");
92 BOOST_CHECK_EQUAL (action->seg_num (), 10);
93 BOOST_CHECK_EQUAL (action->file_hash ().size (), 32);
94 BOOST_CHECK_EQUAL (action->mode (), 0755);
95
96 BOOST_CHECK_EQUAL (action->has_parent_device_name (), false);
97 BOOST_CHECK_EQUAL (action->has_parent_seq_no (), false);
98
99 actionLog->AddLocalActionUpdate ("file.txt", *Hash::FromString ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
100 time (NULL), 0755, 10);
101 BOOST_CHECK_EQUAL (syncLog->SeqNo (localName), 2);
102 BOOST_CHECK_EQUAL (syncLog->LogSize (), 1);
103 BOOST_CHECK_EQUAL (actionLog->LogSize (), 2);
104
105 action = actionLog->LookupAction (Name ("/alex"), 2);
106 BOOST_CHECK_EQUAL ((bool)action, true);
107
108 BOOST_CHECK_EQUAL (action->has_parent_device_name (), true);
109 BOOST_CHECK_EQUAL (action->has_parent_seq_no (), true);
110
111 BOOST_CHECK_EQUAL (action->parent_seq_no (), 1);
112 BOOST_CHECK_EQUAL (action->version (), 1);
113
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800114 BOOST_CHECK_NO_THROW (actionLog->AddRemoteAction (pco));
115 BOOST_CHECK_EQUAL (actionLog->LogSize (), 2);
116
117 // create a real remote action
118 ActionItem item;
119 item.set_action (ActionItem::UPDATE);
120 item.set_filename ("file.txt");
121 item.set_version (2);
122 item.set_timestamp (time (NULL));
123
124 BytesPtr item_msg = serializeMsg (item);
125 Name actionName = Name ("/zhenkai")("action")("top-secret")(1);
126 Bytes actionData = ccnx->createContentObject (actionName, head (*item_msg), item_msg->size ());
127
128 pco = make_shared<ParsedContentObject> (actionData);
129 BOOST_CHECK_NO_THROW (actionLog->AddRemoteAction (pco));
130 BOOST_CHECK_EQUAL (actionLog->LogSize (), 3);
131
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800132 remove_all (tmpdir);
133}
134
135BOOST_AUTO_TEST_SUITE_END()
136
137 // catch (boost::exception &err)
138 // {
139 // cout << *boost::get_error_info<errmsg_info_str> (err) << endl;
140 // }