Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * 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 Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * 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 Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * 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 Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <boost/test/unit_test.hpp> |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 21 | #include <boost/test/output_test_stream.hpp> |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 22 | using boost::test_tools::output_test_stream; |
| 23 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 24 | #include <boost/make_shared.hpp> |
| 25 | |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 26 | #include "sync-full-leaf.h" |
| 27 | #include "sync-diff-leaf.h" |
| 28 | #include "sync-std-name-info.h" |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace Sync; |
| 31 | using namespace std; |
| 32 | using namespace boost; |
| 33 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(LeafTests) |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_CASE (LeafBase) |
| 37 | { |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 38 | 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 Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL (name.use_count (), 1); |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 44 | |
| 45 | BOOST_CHECK_NO_THROW (DiffLeaf x (name, SeqNo (12))); |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL (name.use_count (), 1); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 48 | BOOST_CHECK_NO_THROW (DiffLeaf x (name)); |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL (name.use_count (), 1); |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 50 | |
| 51 | DiffLeaf updateLeaf (name, SeqNo (12)); |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL (name.use_count (), 2); |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 53 | |
| 54 | DiffLeaf removeLeaf (name); |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL (name.use_count (), 3); |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 56 | |
| 57 | BOOST_CHECK_EQUAL (updateLeaf.getOperation (), UPDATE); |
| 58 | BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSession (), 0); |
| 59 | BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSeq (), 12); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL (removeLeaf.getOperation (), REMOVE); |
| 62 | BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSession (), 0); |
| 63 | BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSeq (), 0); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 64 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 65 | BOOST_REQUIRE_NO_THROW (FullLeaf x (name, SeqNo (12))); |
| 66 | FullLeaf fullLeaf (name, SeqNo (12)); |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL (name.use_count (), 4); |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE (LeafDigest) |
| 71 | { |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL (StdNameInfo::FindOrCreate ("/test/name").use_count (), 1); |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 73 | NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name"); |
| 74 | FullLeaf fullLeaf (name, SeqNo (12)); |
| 75 | |
| 76 | // fullLeafDigest = hash ( hash(name), hash (session, seqNo) ) |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 78 | // Digest manualDigest; |
| 79 | |
| 80 | // Digest manualNameDigest; |
| 81 | // manualNameDigest << "/test/name"; |
| 82 | // manualNameDigest.finalize (); |
| 83 | |
| 84 | // Digest manualSeqNoDigest; |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 85 | // manualSeqNoDigest << 0 << 13; |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 86 | // manualSeqNoDigest.finalize (); |
| 87 | |
| 88 | // manualDigest << manualNameDigest << manualSeqNoDigest; |
| 89 | // manualDigest.finalize (); |
| 90 | |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 91 | // cout << manualDigest << "\n\n"; |
| 92 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 93 | output_test_stream output; |
| 94 | output << fullLeaf.getDigest (); |
Chaoyi Bian | f007f92 | 2012-04-09 20:05:37 -0700 | [diff] [blame] | 95 | // BOOST_CHECK (output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", true)); // for sha1 |
| 96 | BOOST_CHECK (output.is_equal ("526d63e6e1f05f97502fd500a1729c4907f3841483ae4561b7e6307c40188f35", true)); // for sha256 |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 97 | |
| 98 | fullLeaf.setSeq (SeqNo (13)); |
| 99 | output << fullLeaf.getDigest (); |
| 100 | BOOST_CHECK (!output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", false)); |
Chaoyi Bian | f007f92 | 2012-04-09 20:05:37 -0700 | [diff] [blame] | 101 | // BOOST_CHECK (output.is_equal ("585a8687ab41d5c29f86e5906c8f188ddca816b3", true)); // for sha1 |
| 102 | BOOST_CHECK (output.is_equal ("39fefe65b3e1021776c07d3a9a3023c6c7cdf12724ee7f3a98b813b22f46d5ec", true)); // for sha256 |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_SUITE_END() |