Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "wave-crypto/wave-crypto.h"
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
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
00060 { NULL, NULL }
00061 };
00062
00063
00064
00065
00066
00067
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;
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
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
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