Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [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 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 23 | #define BOOST_TEST_MAIN 1 |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 24 | #include <boost/test/unit_test.hpp> |
| 25 | #include <boost/test/output_test_stream.hpp> |
| 26 | using boost::test_tools::output_test_stream; |
| 27 | |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 28 | #include "../model/sync-digest.h" |
| 29 | #include <iostream> |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 30 | #include <sstream> |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace Sync; |
| 33 | using namespace std; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 34 | using namespace boost; |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(DigestTests) |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 37 | |
| 38 | BOOST_AUTO_TEST_CASE (BasicTest) |
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 | Digest d0; |
| 41 | BOOST_REQUIRE (d0.empty ()); |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 42 | } |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 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 | BOOST_CHECK_NO_THROW (d1.getHash ()); |
| 54 | BOOST_CHECK (!d1.empty ()); |
| 55 | BOOST_CHECK (d1 == d1); |
| 56 | |
| 57 | BOOST_CHECK_THROW (d1 << "2", DigestCalculationError); |
| 58 | |
| 59 | output_test_stream output; |
| 60 | BOOST_CHECK_NO_THROW (output << d1); |
| 61 | BOOST_CHECK (output.check_length (40,false) ); |
| 62 | BOOST_CHECK (output.is_equal ("e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e", true)); |
| 63 | } |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE (DigestComparison) |
| 66 | { |
| 67 | Digest d1; |
| 68 | BOOST_CHECK_NO_THROW (d1 << "1\n"); |
| 69 | BOOST_CHECK_THROW (d1 == d1, DigestCalculationError); |
| 70 | BOOST_CHECK_NO_THROW (d1.getHash ()); |
| 71 | BOOST_CHECK (d1 == d1); |
| 72 | |
| 73 | Digest d2; |
| 74 | BOOST_CHECK_NO_THROW (d2 << "2\n"); |
| 75 | BOOST_CHECK_NO_THROW (d2.getHash ()); |
| 76 | BOOST_CHECK (d1 != d2); |
| 77 | |
| 78 | Digest d3; |
| 79 | istringstream is (string ("e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); //real sha-1 for "1\n" |
| 80 | BOOST_CHECK_NO_THROW (is >> d3); |
| 81 | BOOST_CHECK (!d3.empty ()); |
| 82 | BOOST_CHECK (d3 == d1); |
| 83 | BOOST_CHECK (d3 != d2); |
| 84 | |
| 85 | istringstream is2 (string ("25fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // some fake hash |
| 86 | BOOST_CHECK_THROW (is2 >> d3, DigestCalculationError); // >> can be used only once |
| 87 | |
| 88 | Digest d4; |
| 89 | BOOST_CHECK_THROW (is2 >> d4, DigestCalculationError); // is2 is now empty. empty >> is not allowed |
| 90 | |
| 91 | istringstream is3 (string ("25fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // some fake hash |
| 92 | BOOST_CHECK_NO_THROW (is3 >> d4); |
| 93 | |
| 94 | BOOST_CHECK (d4 != d1); |
| 95 | BOOST_CHECK (d4 != d2); |
| 96 | BOOST_CHECK (d4 != d3); |
| 97 | } |
| 98 | |
| 99 | BOOST_AUTO_TEST_SUITE_END() |