blob: 3663a412affe80da5f0b367da25c369c643ff899 [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
25#include <unistd.h>
26#include "action-log.h"
27#include <iostream>
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080028#include <ccnx-name.h>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029#include <boost/filesystem.hpp>
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080030
31using namespace std;
32using namespace boost;
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080033using namespace Ccnx;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080034namespace fs = boost::filesystem;
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080035
36BOOST_AUTO_TEST_SUITE(DatabaseTest)
37
38
39BOOST_AUTO_TEST_CASE (BasicDatabaseTest)
40{
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080041 fs::path tmpdir = fs::unique_path (fs::temp_directory_path () / "%%%%-%%%%-%%%%-%%%%");
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080042 SyncLog db (tmpdir, "/alex");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080043
44 HashPtr hash = db.RememberStateInStateLog ();
45 // should be empty
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080046 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080047
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080048 db.UpdateDeviceSeqNo (Name ("/alex"), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080049 hash = db.RememberStateInStateLog ();
50
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080051 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080052
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080053 db.UpdateDeviceSeqNo (Name ("/alex"), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080054 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080055 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080056
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080057 db.UpdateDeviceSeqNo (Name ("/alex"), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080058 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080059 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080060
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080061 db.UpdateDeviceSeqNo (Name ("/alex"), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080062 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080063 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080064
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080065 db.UpdateLocator(Name ("/alex"), Name("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080066
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080067 BOOST_CHECK_EQUAL(db.LookupLocator(Name ("/alex")), Name ("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080068
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080069 SyncStateMsgPtr msg = db.FindStateDifferences ("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
70 BOOST_CHECK_EQUAL (msg->state_size(), 0);
71
72 msg = db.FindStateDifferences ("00", "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
73 BOOST_CHECK_EQUAL (msg->state_size(), 1);
74 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
75 BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080076
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080077 msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", "00");
78 BOOST_CHECK_EQUAL (msg->state_size(), 1);
79 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::DELETE);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080080
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080081 msg = db.FindStateDifferences ("7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405",
82 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
83 BOOST_CHECK_EQUAL (msg->state_size(), 1);
84 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
85 BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080086
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080087 msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c",
88 "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
89 BOOST_CHECK_EQUAL (msg->state_size(), 1);
90 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
91 BOOST_CHECK_EQUAL (msg->state (0).seq (), 0);
92
93 db.UpdateDeviceSeqNo (Name ("/bob"), 1);
94 hash = db.RememberStateInStateLog ();
95 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
96
97 msg = db.FindStateDifferences ("00", "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
98 BOOST_CHECK_EQUAL (msg->state_size(), 2);
99 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE);
100 BOOST_CHECK_EQUAL (msg->state (0).seq (), 2);
101
102 BOOST_CHECK_EQUAL (msg->state (1).type (), SyncState::UPDATE);
103 BOOST_CHECK_EQUAL (msg->state (1).seq (), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800104
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800105 remove_all (tmpdir);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800106}
107
108BOOST_AUTO_TEST_SUITE_END()