testSHA1.cpp

Go to the documentation of this file.
00001 /*
00002  * testSHA1.cpp
00003  *
00004  * Copyright (C) 2007,2011  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * Quick test for the SHA1 algorithm
00031  */
00032 
00033 // includes --------------------------------------------------------------------
00034 #include "wave-crypto/wave-crypto.h"// always include our own header file first!
00035 
00036 #include <iostream>
00037 
00038 #include "common/wave_ex.h"
00039 #include "perf/perf.h"
00040 
00041 
00042 struct test_t {
00043         const char *    expected;
00044         const char *    input;
00045 };
00046 
00047 static const test_t s_tests[] = {
00048 
00049         // expected result                      input data
00050         { "rES0P2EGN9bImr6Vr77QGQFVrKw2",       "test1" },
00051         { "S58Q11A8ct-wmymd6fjGZpDvHx-X",       "test2" },
00052         { "kGxPdcGOQOonOT6Hn3qb_TalTU1q",       "A is for apple." },
00053         { "Yfd_EVCRCeqNirIqqzoraj_jAwML",       "I like to test things." },
00054         { "zdt0nAGmFR4rngAHQCnMyPOyDQ3r",       "It was the best of times, it was the worst of times." },
00055         { "oznaa17uMg1L779VGGCV2K-QCQkH",       "" },
00056 
00057         // must be last!
00058         { NULL, NULL }
00059 };
00060 
00061 
00062 
00063 ////////////////////////////////////////////////////////////////////////////////
00064 //
00065 //      static helper methods
00066 //
00067 ////////////////////////////////////////////////////////////////////////////////
00068 
00069 static void
00070 doTest
00071 (
00072 IN const char * data,
00073 IN const char * expected
00074 )
00075 {
00076         ASSERT(data, "null");
00077         ASSERT(expected, "null");
00078 
00079         std::string sha1 = crypto::getSHA1(data);
00080 
00081         DPRINTF("%s = SHA1(\"%s\")", sha1.c_str(), data);
00082 
00083         if (expected != sha1) {
00084                 WAVE_EX(wex);
00085                 wex << "Bad sha1 encryption encrypting: '" << data << "'.\n";
00086                 wex << "Expected " << expected << " , got " << sha1;
00087         }
00088 }
00089 
00090 
00091 
00092 ////////////////////////////////////////////////////////////////////////////////
00093 //
00094 //      entry point
00095 //
00096 ////////////////////////////////////////////////////////////////////////////////
00097 
00098 int
00099 main
00100 (
00101 IN int argc,
00102 IN const char * argv[]
00103 )
00104 {
00105         int retval = 0;
00106         try {
00107                 for (const test_t * p = s_tests; p->input; ++p) {
00108                         doTest(p->input, p->expected);
00109                 }
00110 
00111         } catch (std::exception& e) {
00112                 DPRINTF("Exception: %s", e.what());
00113                 retval = 1;
00114         } catch (...) {
00115                 DPRINTF("Unknown exception!");
00116                 retval = 2;
00117         }
00118         perf::dumpTimingSummary(std::cerr);
00119 
00120         return retval;
00121 }
00122