testBase64.cpp

Go to the documentation of this file.
00001 /*
00002  * testBase64.cpp
00003  *
00004  * Copyright (C) 2007  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 base 64 encoding/decoding.
00031  */
00032 
00033 // includes --------------------------------------------------------------------
00034 #include "crypto/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 
00043 struct test_t {
00044         const char *    input;
00045         const char *    expected;
00046 };
00047 
00048 
00049 static const test_t s_tests[] = {
00050         // input                        // expected
00051         { "test",                       "AAAFc2V0AAB0" },
00052         { "y",                          "AAACAAB5" },
00053         { "",                           "AAABAAAA" },
00054         { "This is a much longer string!  Yes, verily.",
00055            "AAAsaWhUaSBzYSBzdW0gIGhjbm9scmVndHMgbmlyICFnZVkgICxzcmV2eWxpAAAu" },
00056         { "00000000123456789abcdefghijklmnopqrstruvwxyz",
00057            "AAAtMDAwMDAwMTAwNDMyNzY1YTk4ZGNiZ2ZlamlobWxrcG9uc3JxdXJ0eHd2AHp5"},
00058 
00059         // must be last!
00060         { NULL, NULL }
00061 };
00062 
00063 
00064 
00065 ////////////////////////////////////////////////////////////////////////////////
00066 //
00067 //      static helper methods
00068 //
00069 ////////////////////////////////////////////////////////////////////////////////
00070 
00071 static void
00072 doTest
00073 (
00074 IN const char * data,
00075 IN const char * expected
00076 )
00077 {
00078         ASSERT(data, "null");
00079         ASSERT(expected, "null");
00080 
00081         long bytes = strlen(data) + 1;          // include null terminator
00082         DPRINTF("'%s': %ld bytes", data, bytes);
00083         std::string enc = crypto::encodeBase64((byte_t *) data, bytes, true);
00084 
00085         DPRINTF("  base64: %s", enc.c_str());
00086 
00087         crypto::vec_byte_t out;
00088         crypto::decodeBase64(enc.c_str(), out);
00089 
00090         DPRINTF("  Decoding found %d bytes", (int) out.size());
00091 
00092         const char * back = (const char *)&out[0];
00093         DPRINTF("  Decoded: '%s'", back);
00094 
00095         if (bytes != (long) out.size()) {
00096                 WAVE_EX(wex);
00097                 wex << "Received an improper count on decoding!";
00098         }
00099         if (strcmp(data, back)) {
00100                 WAVE_EX(wex);
00101                 wex << "Decoding didn't return what we encoded!";
00102         }
00103         if (strcmp(enc.c_str(), expected)) {
00104                 WAVE_EX(wex);
00105                 wex << "Encoding didn't produce what we expected!\n";
00106                 wex << "Expected: " << expected << "\n";
00107                 wex << "Got: " << enc;
00108         }
00109 }
00110 
00111 
00112 
00113 ////////////////////////////////////////////////////////////////////////////////
00114 //
00115 //      entry point
00116 //
00117 ////////////////////////////////////////////////////////////////////////////////
00118 
00119 int
00120 main
00121 (
00122 IN int argc,
00123 IN const char * argv[]
00124 )
00125 {
00126         int retval = 0;
00127         try {
00128                 for (const test_t * p = s_tests; p->input; ++p) {
00129                         doTest(p->input, p->expected);
00130                 }
00131 
00132         } catch (std::exception& e) {
00133                 DPRINTF("Exception: %s", e.what());
00134                 retval = 1;
00135         } catch (...) {
00136                 DPRINTF("Unknown exception!");
00137                 retval = 2;
00138         }
00139         perf::dumpTimingSummary(std::cerr);
00140 
00141         return retval;
00142 }
00143