blob: cbc5cedb39a4c22ef65b0b69beec13d0c23461a2 [file] [log] [blame]
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * 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 Afanasyevec1d4a72012-03-05 11:06:54 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * 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 Afanasyevec1d4a72012-03-05 11:06:54 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * 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 Afanasyevec1d4a72012-03-05 11:06:54 -080018 */
19
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080020#include <boost/test/unit_test.hpp>
Alexander Afanasyev8722d872014-07-02 13:00:29 -070021#include <boost/test/output_test_stream.hpp>
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080022using boost::test_tools::output_test_stream;
23
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070024#include "sync-digest.h"
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080025#include <iostream>
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080026#include <sstream>
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080027
28using namespace Sync;
Alexander Afanasyevc1030192012-03-08 22:21:28 -080029using namespace Sync::Error;
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080030using namespace std;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080031using namespace boost;
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080032
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080033BOOST_AUTO_TEST_SUITE(DigestTests)
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080034
35BOOST_AUTO_TEST_CASE (BasicTest)
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080036{
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080037 Digest d0;
38 BOOST_REQUIRE (d0.empty ());
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080039}
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080040
41BOOST_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 Afanasyeva5858032012-03-09 15:55:10 -080050 d1.finalize ();
Alexander Afanasyev8722d872014-07-02 13:00:29 -070051
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080052 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 Afanasyev8722d872014-07-02 13:00:29 -070057
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080058 output_test_stream output;
59 BOOST_CHECK_NO_THROW (output << d1);
Chaoyi Bianf007f922012-04-09 20:05:37 -070060 // 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 Afanasyevb71beab2012-03-05 21:13:49 -080064}
65
66BOOST_AUTO_TEST_CASE (DigestComparison)
67{
68 Digest d1;
69 BOOST_CHECK_NO_THROW (d1 << "1\n");
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070070 // BOOST_CHECK_THROW (d1 == d1, DigestCalculationError);
Alexander Afanasyeva5858032012-03-09 15:55:10 -080071 BOOST_CHECK_NO_THROW (d1.finalize ());
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080072 BOOST_CHECK (d1 == d1);
Alexander Afanasyev8722d872014-07-02 13:00:29 -070073
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080074 Digest d2;
75 BOOST_CHECK_NO_THROW (d2 << "2\n");
Alexander Afanasyeva5858032012-03-09 15:55:10 -080076 BOOST_CHECK_NO_THROW (d2.finalize ());
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080077 BOOST_CHECK (d1 != d2);
Alexander Afanasyev8722d872014-07-02 13:00:29 -070078
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080079 Digest d3;
Chaoyi Bianf007f922012-04-09 20:05:37 -070080 // istringstream is (string ("e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // real sha-1 for "1\n"
81 istringstream is (string ("4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865")); // real sha256 for "1\n"
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080082 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 Afanasyev8722d872014-07-02 13:00:29 -070095
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080096 BOOST_CHECK (d4 != d1);
97 BOOST_CHECK (d4 != d2);
98 BOOST_CHECK (d4 != d3);
99}
100
101BOOST_AUTO_TEST_SUITE_END()