blob: 361793a8b1f188d729924ef2b503c33273e87552 [file] [log] [blame]
Alexander Afanasyev150f14d2012-03-05 21:24:49 -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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25using boost::test_tools::output_test_stream;
26
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080027#include <boost/make_shared.hpp>
28
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070029#include "sync-full-leaf.h"
30#include "sync-diff-leaf.h"
31#include "sync-std-name-info.h"
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080032
33using namespace Sync;
34using namespace std;
35using namespace boost;
36
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080037BOOST_AUTO_TEST_SUITE(LeafTests)
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080038
39BOOST_AUTO_TEST_CASE (LeafBase)
40{
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080041 NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
42 BOOST_CHECK (name != 0);
43
44 // find the same name
45 BOOST_CHECK (name.get () == StdNameInfo::FindOrCreate ("/test/name").get ());
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080046 BOOST_CHECK_EQUAL (name.use_count (), 1);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080047
48 BOOST_CHECK_NO_THROW (DiffLeaf x (name, SeqNo (12)));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080049 BOOST_CHECK_EQUAL (name.use_count (), 1);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080050
51 BOOST_CHECK_NO_THROW (DiffLeaf x (name));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080052 BOOST_CHECK_EQUAL (name.use_count (), 1);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080053
54 DiffLeaf updateLeaf (name, SeqNo (12));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080055 BOOST_CHECK_EQUAL (name.use_count (), 2);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080056
57 DiffLeaf removeLeaf (name);
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080058 BOOST_CHECK_EQUAL (name.use_count (), 3);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080059
60 BOOST_CHECK_EQUAL (updateLeaf.getOperation (), UPDATE);
61 BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSession (), 0);
62 BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSeq (), 12);
63
64 BOOST_CHECK_EQUAL (removeLeaf.getOperation (), REMOVE);
65 BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSession (), 0);
66 BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSeq (), 0);
67
68 BOOST_REQUIRE_NO_THROW (FullLeaf x (name, SeqNo (12)));
69 FullLeaf fullLeaf (name, SeqNo (12));
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080070 BOOST_CHECK_EQUAL (name.use_count (), 4);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080071}
72
73BOOST_AUTO_TEST_CASE (LeafDigest)
74{
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080075 BOOST_CHECK_EQUAL (StdNameInfo::FindOrCreate ("/test/name").use_count (), 1);
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080076 NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
77 FullLeaf fullLeaf (name, SeqNo (12));
78
79 // fullLeafDigest = hash ( hash(name), hash (session, seqNo) )
80
81 // Digest manualDigest;
82
83 // Digest manualNameDigest;
84 // manualNameDigest << "/test/name";
85 // manualNameDigest.finalize ();
86
87 // Digest manualSeqNoDigest;
Alexander Afanasyeva5625322012-03-06 00:03:41 -080088 // manualSeqNoDigest << 0 << 13;
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080089 // manualSeqNoDigest.finalize ();
90
91 // manualDigest << manualNameDigest << manualSeqNoDigest;
92 // manualDigest.finalize ();
93
Alexander Afanasyeva5625322012-03-06 00:03:41 -080094 // cout << manualDigest << "\n\n";
95
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080096 output_test_stream output;
97 output << fullLeaf.getDigest ();
98 BOOST_CHECK (output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", true));
Alexander Afanasyeva5625322012-03-06 00:03:41 -080099
100 fullLeaf.setSeq (SeqNo (13));
101 output << fullLeaf.getDigest ();
102 BOOST_CHECK (!output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", false));
103 BOOST_CHECK (output.is_equal ("585a8687ab41d5c29f86e5906c8f188ddca816b3", true));
Alexander Afanasyev150f14d2012-03-05 21:24:49 -0800104}
105
106BOOST_AUTO_TEST_SUITE_END()