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