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