testBase64.cpp

Go to the documentation of this file.
00001 /*
00002  * testBase64.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 base 64 encoding/decoding.
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 
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 static void
00114 doDwordTest
00115 (
00116 IN dword_t dw
00117 )
00118 {
00119         int bytes = sizeof(dword_t);
00120         std::string enc = crypto::encodeBase64((byte_t *) &dw, bytes, false);
00121 
00122         DPRINTF("dword 0x%08x --> '%s'", dw, enc.c_str());
00123 }
00124 
00125 
00126 
00127 ////////////////////////////////////////////////////////////////////////////////
00128 //
00129 //      entry point
00130 //
00131 ////////////////////////////////////////////////////////////////////////////////
00132 
00133 int
00134 main
00135 (
00136 IN int argc,
00137 IN const char * argv[]
00138 )
00139 {
00140         int retval = 0;
00141         try {
00142                 for (const test_t * p = s_tests; p->input; ++p) {
00143                         doTest(p->input, p->expected);
00144                 }
00145 
00146                 // attempt to encode some integers
00147                 doDwordTest(0x00000000);
00148                 doDwordTest(0x0000000F);
00149                 doDwordTest(0x000000FF);
00150                 doDwordTest(0x00000FFF);
00151                 doDwordTest(0x0000FFFF);
00152                 doDwordTest(0x000FFFFF);
00153                 doDwordTest(0x00FFFFFF);
00154                 doDwordTest(0x0FFFFFFF);
00155                 doDwordTest(0xFFFFFFFF);
00156                 doDwordTest(0xFFFFFFFE);
00157                 doDwordTest(0xFFFFFFFA);
00158                 doDwordTest(0xFFFFFFF0);
00159                 doDwordTest(0x70000000);
00160                 doDwordTest(0x07000000);
00161                 doDwordTest(0x00000001);
00162         } catch (std::exception& e) {
00163                 DPRINTF("Exception: %s", e.what());
00164                 retval = 1;
00165         } catch (...) {
00166                 DPRINTF("Unknown exception!");
00167                 retval = 2;
00168         }
00169         perf::dumpTimingSummary(std::cerr);
00170 
00171         return retval;
00172 }
00173