blob: 311f9377a3f99d5b34785ba1e8b7fc8ad83ac084 [file] [log] [blame]
Alexander Afanasyevee7e6132013-01-03 20:03:14 -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
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080025#include "logging.h"
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080026#include <unistd.h>
27#include "action-log.h"
28#include <iostream>
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080029#include <ccnx-name.h>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080030#include <boost/filesystem.hpp>
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080031
32using namespace std;
33using namespace boost;
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080034using namespace Ccnx;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080035namespace fs = boost::filesystem;
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080036
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080037BOOST_AUTO_TEST_SUITE(TestSyncLog)
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080038
39
40BOOST_AUTO_TEST_CASE (BasicDatabaseTest)
41{
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080042 INIT_LOGGERS ();
43
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080044 fs::path tmpdir = fs::unique_path (fs::temp_directory_path () / "%%%%-%%%%-%%%%-%%%%");
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080045 SyncLog db (tmpdir, "/alex");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080046
47 HashPtr hash = db.RememberStateInStateLog ();
48 // should be empty
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080049 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080050
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080051 db.UpdateDeviceSeqNo (Name ("/alex"), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080052 hash = db.RememberStateInStateLog ();
53
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080054 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080055
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080056 db.UpdateDeviceSeqNo (Name ("/alex"), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080057 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080058 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080059
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080060 db.UpdateDeviceSeqNo (Name ("/alex"), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080061 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080062 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080063
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080064 db.UpdateDeviceSeqNo (Name ("/alex"), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080065 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080066 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080067
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080068 db.UpdateLocator(Name ("/alex"), Name("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080069
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080070 BOOST_CHECK_EQUAL(db.LookupLocator(Name ("/alex")), Name ("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080071
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080072 SyncStateMsgPtr msg = db.FindStateDifferences ("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
73 BOOST_CHECK_EQUAL (msg->state_size(), 0);
74
75 msg = db.FindStateDifferences ("00", "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
76 BOOST_CHECK_EQUAL (msg->state_size(), 1);
77 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
78 BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080079
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080080 msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", "00");
81 BOOST_CHECK_EQUAL (msg->state_size(), 1);
82 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::DELETE);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080083
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080084 msg = db.FindStateDifferences ("7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405",
85 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
86 BOOST_CHECK_EQUAL (msg->state_size(), 1);
87 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
88 BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080089
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080090 msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c",
91 "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
92 BOOST_CHECK_EQUAL (msg->state_size(), 1);
93 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
94 BOOST_CHECK_EQUAL (msg->state (0).seq (), 0);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080095
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080096 db.UpdateDeviceSeqNo (Name ("/bob"), 1);
97 hash = db.RememberStateInStateLog ();
98 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
99
100 msg = db.FindStateDifferences ("00", "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
101 BOOST_CHECK_EQUAL (msg->state_size(), 2);
102 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
103 BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
104
105 BOOST_CHECK_EQUAL (msg->state (1).type (), SyncState::UPDATE);
106 BOOST_CHECK_EQUAL (msg->state (1).seq (), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800107
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800108 remove_all (tmpdir);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800109}
110
111BOOST_AUTO_TEST_SUITE_END()