Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -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 | ec1d4a7 | 2012-03-05 11:06:54 -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 | ec1d4a7 | 2012-03-05 11:06:54 -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 | ec1d4a7 | 2012-03-05 11:06:54 -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 | ec1d4a7 | 2012-03-05 11:06:54 -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 | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 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 | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 22 | using boost::test_tools::output_test_stream; |
| 23 | |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 24 | #include "sync-digest.h" |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 25 | #include <iostream> |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 26 | #include <sstream> |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace Sync; |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 29 | using namespace Sync::Error; |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 30 | using namespace std; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 31 | using namespace boost; |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(DigestTests) |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_CASE (BasicTest) |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 36 | { |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 37 | Digest d0; |
| 38 | BOOST_REQUIRE (d0.empty ()); |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 39 | } |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE (DigestGenerationTest) |
| 42 | { |
| 43 | Digest d1; |
| 44 | BOOST_CHECK_NO_THROW (d1 << "1\n"); |
| 45 | |
| 46 | // without explicit finalizing, Digest will not be complete and printing out will cause assert |
| 47 | BOOST_CHECK (d1.empty ()); |
| 48 | |
| 49 | // fix hash |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 50 | d1.finalize (); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 52 | BOOST_CHECK_NO_THROW (d1.getHash ()); |
| 53 | BOOST_CHECK (!d1.empty ()); |
| 54 | BOOST_CHECK (d1 == d1); |
| 55 | |
| 56 | BOOST_CHECK_THROW (d1 << "2", DigestCalculationError); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 58 | output_test_stream output; |
| 59 | BOOST_CHECK_NO_THROW (output << d1); |
Chaoyi Bian | f007f92 | 2012-04-09 20:05:37 -0700 | [diff] [blame] | 60 | // BOOST_CHECK (output.check_length (40, false) ); |
| 61 | // BOOST_CHECK (output.is_equal ("e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e", true)); // for sha1 |
| 62 | BOOST_CHECK (output.check_length (64, false) ); |
| 63 | BOOST_CHECK (output.is_equal ("4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865", true)); // for sha256 |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE (DigestComparison) |
| 67 | { |
| 68 | Digest d1; |
| 69 | BOOST_CHECK_NO_THROW (d1 << "1\n"); |
Alexander Afanasyev | f46eac5 | 2013-07-26 11:27:39 -0700 | [diff] [blame] | 70 | // BOOST_CHECK_THROW (d1 == d1, DigestCalculationError); |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 71 | BOOST_CHECK_NO_THROW (d1.finalize ()); |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 72 | BOOST_CHECK (d1 == d1); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 74 | Digest d2; |
| 75 | BOOST_CHECK_NO_THROW (d2 << "2\n"); |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 76 | BOOST_CHECK_NO_THROW (d2.finalize ()); |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 77 | BOOST_CHECK (d1 != d2); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 79 | Digest d3; |
Chaoyi Bian | f007f92 | 2012-04-09 20:05:37 -0700 | [diff] [blame] | 80 | // istringstream is (string ("e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // real sha-1 for "1\n" |
| 81 | istringstream is (string ("4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865")); // real sha256 for "1\n" |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 82 | BOOST_CHECK_NO_THROW (is >> d3); |
| 83 | BOOST_CHECK (!d3.empty ()); |
| 84 | BOOST_CHECK (d3 == d1); |
| 85 | BOOST_CHECK (d3 != d2); |
| 86 | |
| 87 | istringstream is2 (string ("25fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // some fake hash |
| 88 | BOOST_CHECK_THROW (is2 >> d3, DigestCalculationError); // >> can be used only once |
| 89 | |
| 90 | Digest d4; |
| 91 | BOOST_CHECK_THROW (is2 >> d4, DigestCalculationError); // is2 is now empty. empty >> is not allowed |
| 92 | |
| 93 | istringstream is3 (string ("25fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // some fake hash |
| 94 | BOOST_CHECK_NO_THROW (is3 >> d4); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 96 | BOOST_CHECK (d4 != d1); |
| 97 | BOOST_CHECK (d4 != d2); |
| 98 | BOOST_CHECK (d4 != d3); |
| 99 | } |
| 100 | |
| 101 | BOOST_AUTO_TEST_SUITE_END() |