blob: 3cb124ec4f1b6b436f5af19ac07d5bbe555c99ea [file] [log] [blame]
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -08001/* -*- 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080023#define BOOST_TEST_MAIN 1
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080024#include <boost/test/unit_test.hpp>
25#include <boost/test/output_test_stream.hpp>
26using boost::test_tools::output_test_stream;
27
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070028#include "sync-digest.h"
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080029#include <iostream>
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080030#include <sstream>
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080031
32using namespace Sync;
Alexander Afanasyevc1030192012-03-08 22:21:28 -080033using namespace Sync::Error;
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080034using namespace std;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080035using namespace boost;
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080036
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080037BOOST_AUTO_TEST_SUITE(DigestTests)
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080038
39BOOST_AUTO_TEST_CASE (BasicTest)
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080040{
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080041 Digest d0;
42 BOOST_REQUIRE (d0.empty ());
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080043}
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080044
45BOOST_AUTO_TEST_CASE (DigestGenerationTest)
46{
47 Digest d1;
48 BOOST_CHECK_NO_THROW (d1 << "1\n");
49
50 // without explicit finalizing, Digest will not be complete and printing out will cause assert
51 BOOST_CHECK (d1.empty ());
52
53 // fix hash
Alexander Afanasyeva5858032012-03-09 15:55:10 -080054 d1.finalize ();
55
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080056 BOOST_CHECK_NO_THROW (d1.getHash ());
57 BOOST_CHECK (!d1.empty ());
58 BOOST_CHECK (d1 == d1);
59
60 BOOST_CHECK_THROW (d1 << "2", DigestCalculationError);
61
62 output_test_stream output;
63 BOOST_CHECK_NO_THROW (output << d1);
Chaoyi Bianf007f922012-04-09 20:05:37 -070064 // BOOST_CHECK (output.check_length (40, false) );
65 // BOOST_CHECK (output.is_equal ("e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e", true)); // for sha1
66 BOOST_CHECK (output.check_length (64, false) );
67 BOOST_CHECK (output.is_equal ("4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865", true)); // for sha256
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080068}
69
70BOOST_AUTO_TEST_CASE (DigestComparison)
71{
72 Digest d1;
73 BOOST_CHECK_NO_THROW (d1 << "1\n");
74 BOOST_CHECK_THROW (d1 == d1, DigestCalculationError);
Alexander Afanasyeva5858032012-03-09 15:55:10 -080075 BOOST_CHECK_NO_THROW (d1.finalize ());
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080076 BOOST_CHECK (d1 == d1);
77
78 Digest d2;
79 BOOST_CHECK_NO_THROW (d2 << "2\n");
Alexander Afanasyeva5858032012-03-09 15:55:10 -080080 BOOST_CHECK_NO_THROW (d2.finalize ());
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080081 BOOST_CHECK (d1 != d2);
82
83 Digest d3;
Chaoyi Bianf007f922012-04-09 20:05:37 -070084 // istringstream is (string ("e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // real sha-1 for "1\n"
85 istringstream is (string ("4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865")); // real sha256 for "1\n"
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080086 BOOST_CHECK_NO_THROW (is >> d3);
87 BOOST_CHECK (!d3.empty ());
88 BOOST_CHECK (d3 == d1);
89 BOOST_CHECK (d3 != d2);
90
91 istringstream is2 (string ("25fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // some fake hash
92 BOOST_CHECK_THROW (is2 >> d3, DigestCalculationError); // >> can be used only once
93
94 Digest d4;
95 BOOST_CHECK_THROW (is2 >> d4, DigestCalculationError); // is2 is now empty. empty >> is not allowed
96
97 istringstream is3 (string ("25fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); // some fake hash
98 BOOST_CHECK_NO_THROW (is3 >> d4);
99
100 BOOST_CHECK (d4 != d1);
101 BOOST_CHECK (d4 != d2);
102 BOOST_CHECK (d4 != d3);
103}
104
105BOOST_AUTO_TEST_SUITE_END()