blob: 49afcc5f57642825d7136890955ad45cbaf88781 [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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>
19 * Chaoyi Bian <bcy@pku.edu.cn>
20 * 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
27#include <boost/make_shared.hpp>
28
29#include "sync-full-leaf.h"
30#include "sync-diff-leaf.h"
31#include "sync-std-name-info.h"
32
33using namespace Sync;
34using namespace std;
35using namespace boost;
36
37BOOST_AUTO_TEST_SUITE(LeafTests)
38
39BOOST_AUTO_TEST_CASE (LeafBase)
40{
41 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 ());
46 BOOST_CHECK_EQUAL (name.use_count (), 1);
47
48 BOOST_CHECK_NO_THROW (DiffLeaf x (name, SeqNo (12)));
49 BOOST_CHECK_EQUAL (name.use_count (), 1);
50
51 BOOST_CHECK_NO_THROW (DiffLeaf x (name));
52 BOOST_CHECK_EQUAL (name.use_count (), 1);
53
54 DiffLeaf updateLeaf (name, SeqNo (12));
55 BOOST_CHECK_EQUAL (name.use_count (), 2);
56
57 DiffLeaf removeLeaf (name);
58 BOOST_CHECK_EQUAL (name.use_count (), 3);
59
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));
70 BOOST_CHECK_EQUAL (name.use_count (), 4);
71}
72
73BOOST_AUTO_TEST_CASE (LeafDigest)
74{
75 BOOST_CHECK_EQUAL (StdNameInfo::FindOrCreate ("/test/name").use_count (), 1);
76 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;
88 // manualSeqNoDigest << 0 << 13;
89 // manualSeqNoDigest.finalize ();
90
91 // manualDigest << manualNameDigest << manualSeqNoDigest;
92 // manualDigest.finalize ();
93
94 // cout << manualDigest << "\n\n";
95
96 output_test_stream output;
97 output << fullLeaf.getDigest ();
98 // BOOST_CHECK (output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", true)); // for sha1
99 BOOST_CHECK (output.is_equal ("526d63e6e1f05f97502fd500a1729c4907f3841483ae4561b7e6307c40188f35", true)); // for sha256
100
101 fullLeaf.setSeq (SeqNo (13));
102 output << fullLeaf.getDigest ();
103 BOOST_CHECK (!output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", false));
104 // BOOST_CHECK (output.is_equal ("585a8687ab41d5c29f86e5906c8f188ddca816b3", true)); // for sha1
105 BOOST_CHECK (output.is_equal ("39fefe65b3e1021776c07d3a9a3023c6c7cdf12724ee7f3a98b813b22f46d5ec", true)); // for sha256
106}
107
108BOOST_AUTO_TEST_SUITE_END()