blob: 3663a412affe80da5f0b367da25c369c643ff899 [file] [log] [blame]
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012 University of California, Los Angeles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
* Zhenkai Zhu <zhenkai@cs.ucla.edu>
*/
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include <unistd.h>
#include "action-log.h"
#include <iostream>
#include <ccnx-name.h>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost;
using namespace Ccnx;
namespace fs = boost::filesystem;
BOOST_AUTO_TEST_SUITE(DatabaseTest)
BOOST_AUTO_TEST_CASE (BasicDatabaseTest)
{
fs::path tmpdir = fs::unique_path (fs::temp_directory_path () / "%%%%-%%%%-%%%%-%%%%");
SyncLog db (tmpdir, "/alex");
HashPtr hash = db.RememberStateInStateLog ();
// should be empty
BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
db.UpdateDeviceSeqNo (Name ("/alex"), 1);
hash = db.RememberStateInStateLog ();
BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
db.UpdateDeviceSeqNo (Name ("/alex"), 2);
hash = db.RememberStateInStateLog ();
BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
db.UpdateDeviceSeqNo (Name ("/alex"), 2);
hash = db.RememberStateInStateLog ();
BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
db.UpdateDeviceSeqNo (Name ("/alex"), 1);
hash = db.RememberStateInStateLog ();
BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
db.UpdateLocator(Name ("/alex"), Name("/hawaii"));
BOOST_CHECK_EQUAL(db.LookupLocator(Name ("/alex")), Name ("/hawaii"));
SyncStateMsgPtr msg = db.FindStateDifferences ("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
BOOST_CHECK_EQUAL (msg->state_size(), 0);
msg = db.FindStateDifferences ("00", "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
BOOST_CHECK_EQUAL (msg->state_size(), 1);
BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", "00");
BOOST_CHECK_EQUAL (msg->state_size(), 1);
BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::DELETE);
msg = db.FindStateDifferences ("7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405",
"2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
BOOST_CHECK_EQUAL (msg->state_size(), 1);
BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c",
"7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
BOOST_CHECK_EQUAL (msg->state_size(), 1);
BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
BOOST_CHECK_EQUAL (msg->state (0).seq (), 0);
db.UpdateDeviceSeqNo (Name ("/bob"), 1);
hash = db.RememberStateInStateLog ();
BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
msg = db.FindStateDifferences ("00", "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
BOOST_CHECK_EQUAL (msg->state_size(), 2);
BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
BOOST_CHECK_EQUAL (msg->state (1).type (), SyncState::UPDATE);
BOOST_CHECK_EQUAL (msg->state (1).seq (), 1);
remove_all (tmpdir);
}
BOOST_AUTO_TEST_SUITE_END()