blob: bad1971f702071a476500c07ff88c6e2f9f0a962 [file] [log] [blame]
Alexander Afanasyev150f14d2012-03-05 21:24:49 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyev150f14d2012-03-05 21:24:49 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyev150f14d2012-03-05 21:24:49 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080018 */
19
20#include <boost/test/unit_test.hpp>
Alexander Afanasyev8722d872014-07-02 13:00:29 -070021#include <boost/test/output_test_stream.hpp>
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080022using boost::test_tools::output_test_stream;
23
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080024#include <boost/make_shared.hpp>
25
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070026#include "sync-full-leaf.h"
27#include "sync-diff-leaf.h"
28#include "sync-std-name-info.h"
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080029
30using namespace Sync;
31using namespace std;
32using namespace boost;
33
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080034BOOST_AUTO_TEST_SUITE(LeafTests)
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080035
36BOOST_AUTO_TEST_CASE (LeafBase)
37{
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080038 NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
39 BOOST_CHECK (name != 0);
40
41 // find the same name
42 BOOST_CHECK (name.get () == StdNameInfo::FindOrCreate ("/test/name").get ());
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080043 BOOST_CHECK_EQUAL (name.use_count (), 1);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080044
45 BOOST_CHECK_NO_THROW (DiffLeaf x (name, SeqNo (12)));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080046 BOOST_CHECK_EQUAL (name.use_count (), 1);
Alexander Afanasyev8722d872014-07-02 13:00:29 -070047
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080048 BOOST_CHECK_NO_THROW (DiffLeaf x (name));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080049 BOOST_CHECK_EQUAL (name.use_count (), 1);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080050
51 DiffLeaf updateLeaf (name, SeqNo (12));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080052 BOOST_CHECK_EQUAL (name.use_count (), 2);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080053
54 DiffLeaf removeLeaf (name);
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080055 BOOST_CHECK_EQUAL (name.use_count (), 3);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080056
57 BOOST_CHECK_EQUAL (updateLeaf.getOperation (), UPDATE);
58 BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSession (), 0);
59 BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSeq (), 12);
Alexander Afanasyev8722d872014-07-02 13:00:29 -070060
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080061 BOOST_CHECK_EQUAL (removeLeaf.getOperation (), REMOVE);
62 BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSession (), 0);
63 BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSeq (), 0);
Alexander Afanasyev8722d872014-07-02 13:00:29 -070064
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080065 BOOST_REQUIRE_NO_THROW (FullLeaf x (name, SeqNo (12)));
66 FullLeaf fullLeaf (name, SeqNo (12));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080067 BOOST_CHECK_EQUAL (name.use_count (), 4);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080068}
69
70BOOST_AUTO_TEST_CASE (LeafDigest)
71{
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080072 BOOST_CHECK_EQUAL (StdNameInfo::FindOrCreate ("/test/name").use_count (), 1);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080073 NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
74 FullLeaf fullLeaf (name, SeqNo (12));
75
76 // fullLeafDigest = hash ( hash(name), hash (session, seqNo) )
Alexander Afanasyev8722d872014-07-02 13:00:29 -070077
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080078 // Digest manualDigest;
79
80 // Digest manualNameDigest;
81 // manualNameDigest << "/test/name";
82 // manualNameDigest.finalize ();
83
84 // Digest manualSeqNoDigest;
Alexander Afanasyeva5625322012-03-06 00:03:41 -080085 // manualSeqNoDigest << 0 << 13;
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080086 // manualSeqNoDigest.finalize ();
87
88 // manualDigest << manualNameDigest << manualSeqNoDigest;
89 // manualDigest.finalize ();
90
Alexander Afanasyeva5625322012-03-06 00:03:41 -080091 // cout << manualDigest << "\n\n";
92
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080093 output_test_stream output;
94 output << fullLeaf.getDigest ();
Chaoyi Bianf007f922012-04-09 20:05:37 -070095 // BOOST_CHECK (output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", true)); // for sha1
96 BOOST_CHECK (output.is_equal ("526d63e6e1f05f97502fd500a1729c4907f3841483ae4561b7e6307c40188f35", true)); // for sha256
Alexander Afanasyeva5625322012-03-06 00:03:41 -080097
98 fullLeaf.setSeq (SeqNo (13));
99 output << fullLeaf.getDigest ();
100 BOOST_CHECK (!output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", false));
Chaoyi Bianf007f922012-04-09 20:05:37 -0700101 // BOOST_CHECK (output.is_equal ("585a8687ab41d5c29f86e5906c8f188ddca816b3", true)); // for sha1
102 BOOST_CHECK (output.is_equal ("39fefe65b3e1021776c07d3a9a3023c6c7cdf12724ee7f3a98b813b22f46d5ec", true)); // for sha256
Alexander Afanasyev150f14d2012-03-05 21:24:49 -0800103}
104
105BOOST_AUTO_TEST_SUITE_END()