basesixfourtest.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2004-2007 University of Southern California
00006 // Rob Peters <rjpeters at usc dot edu>
00007 //
00008 // created: Wed Oct 13 13:51:48 2004
00009 // commit: $Id: basesixfourtest.cc 10065 2007-04-12 05:54:56Z rjpeters $
00010 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/pkgs/whitebox/basesixfourtest.cc $
00011 //
00012 // --------------------------------------------------------------------
00013 //
00014 // This file is part of GroovX.
00015 //   [http://ilab.usc.edu/rjpeters/groovx/]
00016 //
00017 // GroovX is free software; you can redistribute it and/or modify it
00018 // under the terms of the GNU General Public License as published by
00019 // the Free Software Foundation; either version 2 of the License, or
00020 // (at your option) any later version.
00021 //
00022 // GroovX is distributed in the hope that it will be useful, but
00023 // WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025 // General Public License for more details.
00026 //
00027 // You should have received a copy of the GNU General Public License
00028 // along with GroovX; if not, write to the Free Software Foundation,
00029 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00030 //
00032 
00033 #ifndef GROOVX_PKGS_WHITEBOX_BASESIXFOURTEST_CC_UTC20050626084022_DEFINED
00034 #define GROOVX_PKGS_WHITEBOX_BASESIXFOURTEST_CC_UTC20050626084022_DEFINED
00035 
00036 #include "pkgs/whitebox/basesixfourtest.h"
00037 
00038 #include "rutz/base64.h"
00039 #include "rutz/bytearray.h"
00040 #include "rutz/fstring.h"
00041 #include "rutz/rand.h"
00042 #include "rutz/unittest.h"
00043 
00044 #include "tcl/pkg.h"
00045 
00046 #include <cstring> // for memcmp()
00047 
00048 #include "rutz/trace.h"
00049 
00050 namespace
00051 {
00052   rutz::fstring makeString(const rutz::byte_array& src)
00053   {
00054     return rutz::fstring
00055       ((rutz::char_range(reinterpret_cast<const char*>(&src.vec[0]),
00056                          src.vec.size())));
00057   }
00058 
00059   void testBase64EncodeDecode1()
00060   {
00061     // Test encoding/decoding of a string with no extra chars (so the
00062     // base64 encoding has no trailing "=")
00063 
00064     const char* decoded1 =
00065       "The Quick Brown Fox Jumped Over The Lazy Dog?";
00066 
00067     const char* encoded1 =
00068       "VGhlIFF1aWNrIEJyb3duIEZveCBKdW1wZWQgT3ZlciBUaGUgTGF6eSBEb2c/";
00069 
00070     rutz::byte_array buf;
00071     rutz::byte_array buf2;
00072 
00073     rutz::base64_encode_string(decoded1, buf, 0);
00074     TEST_REQUIRE_EQ(makeString(buf), encoded1);
00075     rutz::base64_decode(buf, buf2);
00076     TEST_REQUIRE_EQ(makeString(buf2), decoded1);
00077   }
00078 
00079   void testBase64EncodeDecode2()
00080   {
00081     // Test encoding/decoding of a string with one extra char (so the
00082     // base64 encoding has a trailing "==")
00083 
00084     const char* decoded2 =
00085       "The Quick Brown Fox Jumped Over The Lazy Dog?!";
00086 
00087     const char* encoded2 =
00088       "VGhlIFF1aWNrIEJyb3duIEZveCBKdW1wZWQgT3ZlciBUaGUgTGF6eSBEb2c/IQ==";
00089 
00090     rutz::byte_array buf;
00091     rutz::byte_array buf2;
00092 
00093     rutz::base64_encode_string(decoded2, buf, 0);
00094     TEST_REQUIRE_EQ(makeString(buf), encoded2);
00095     rutz::base64_decode(buf, buf2);
00096     TEST_REQUIRE_EQ(makeString(buf2), decoded2);
00097   }
00098 
00099   void testBase64EncodeDecode3()
00100   {
00101     // Test encoding/decoding of a string with two extra chars (so the
00102     // base64 encoding has a trailing "=")
00103 
00104     const char* decoded3 =
00105       "The Quick Brown Fox Jumped Over The Lazy Dog?!?";
00106 
00107     const char* encoded3 =
00108       "VGhlIFF1aWNrIEJyb3duIEZveCBKdW1wZWQgT3ZlciBUaGUgTGF6eSBEb2c/IT8=";
00109 
00110     rutz::byte_array buf;
00111     rutz::byte_array buf2;
00112 
00113     rutz::base64_encode_string(decoded3, buf, 0);
00114     TEST_REQUIRE_EQ(makeString(buf), encoded3);
00115     rutz::base64_decode(buf, buf2);
00116     TEST_REQUIRE_EQ(makeString(buf2), decoded3);
00117   }
00118 
00119   void testBase64EncodeDecode4()
00120   {
00121     // Test encoding/decoding of a set of random binary data
00122 
00123     const unsigned char decoded4[512] =
00124       {
00125         0x50, 0xaa, 0x00, 0xf7, 0x9e, 0x9e, 0xf1, 0x3c, 0xdf, 0xfa, 0x03, 0xe4, 0x58, 0x61, 0x86, 0x37,
00126         0x4f, 0x00, 0x5b, 0x3d, 0x19, 0x88, 0x66, 0x01, 0x24, 0xf2, 0x2e, 0xba, 0x3d, 0x17, 0x4b, 0x5e,
00127         0xbb, 0xaa, 0x99, 0xd4, 0x49, 0xee, 0xca, 0x8b, 0x9b, 0x5c, 0x8c, 0x5f, 0x48, 0x40, 0xe2, 0xc7,
00128         0x14, 0x83, 0x1e, 0xe5, 0xdd, 0xa8, 0xdd, 0x67, 0x4e, 0x3d, 0xf6, 0xc8, 0xba, 0xec, 0xe3, 0x08,
00129         0xbe, 0x4a, 0x27, 0x8a, 0xcd, 0xf6, 0x27, 0xf3, 0xf7, 0x96, 0xb1, 0x0b, 0xd0, 0xb9, 0xe7, 0x5d,
00130         0x6b, 0x2d, 0x35, 0xad, 0xc4, 0x29, 0xfe, 0x30, 0x95, 0x98, 0x26, 0xbc, 0x5f, 0x2c, 0x4d, 0x36,
00131         0x24, 0x62, 0xae, 0x5f, 0x18, 0x14, 0x88, 0xb1, 0xf4, 0x96, 0x25, 0x74, 0xad, 0x94, 0x2d, 0xb8,
00132         0xaa, 0x56, 0x8e, 0x7c, 0x19, 0x15, 0x25, 0xa2, 0x1c, 0x8b, 0x8b, 0x29, 0xab, 0x45, 0x0e, 0x20,
00133         0xab, 0x1c, 0xdc, 0xa3, 0xba, 0xb7, 0x62, 0x27, 0x7b, 0x8d, 0x5f, 0xb4, 0x83, 0xeb, 0x51, 0x72,
00134         0xaf, 0xf3, 0x4c, 0xb6, 0x82, 0x55, 0x85, 0xed, 0xc2, 0xbf, 0x3d, 0x70, 0x66, 0xa2, 0x09, 0xe1,
00135         0xdb, 0x1c, 0xed, 0xe8, 0x16, 0xf5, 0x08, 0x1c, 0x96, 0xc8, 0xbc, 0x29, 0x8d, 0xfe, 0x4f, 0x5d,
00136         0xd0, 0xaf, 0x54, 0xc7, 0xe9, 0x4f, 0x88, 0x35, 0x30, 0x1c, 0x68, 0xdb, 0xa0, 0x4a, 0x74, 0xfd,
00137         0x08, 0xcd, 0x20, 0xae, 0x6d, 0x7f, 0x71, 0x8c, 0x21, 0x00, 0x0d, 0x66, 0xd9, 0x7e, 0x67, 0xe4,
00138         0x5e, 0xf9, 0x81, 0x01, 0xdb, 0xce, 0x7f, 0xbe, 0x85, 0xbb, 0x09, 0xc7, 0x93, 0x67, 0x36, 0xbf,
00139         0x95, 0x75, 0xa7, 0x65, 0xc2, 0x88, 0x28, 0xae, 0x81, 0xb5, 0xb6, 0x63, 0x82, 0x5c, 0x7d, 0x4d,
00140         0x52, 0x45, 0x25, 0x17, 0xb7, 0x36, 0xcd, 0x08, 0xf6, 0x06, 0x52, 0x12, 0x94, 0x87, 0x2e, 0x37,
00141         0xd4, 0xc9, 0x95, 0x2c, 0x45, 0x81, 0x24, 0x6e, 0xe1, 0xaa, 0x6b, 0x9b, 0xf6, 0xfb, 0xb1, 0x4e,
00142         0xcc, 0xc8, 0x31, 0x8b, 0xdd, 0x4d, 0x69, 0xc5, 0xdb, 0x62, 0xa4, 0xd0, 0x58, 0x72, 0xe8, 0xe2,
00143         0x6b, 0x28, 0x3c, 0x50, 0xe8, 0xa9, 0xf8, 0xcf, 0x3d, 0x9e, 0xeb, 0x1f, 0xb7, 0xd9, 0xba, 0x8f,
00144         0x9d, 0x1c, 0xbf, 0x9d, 0x11, 0x40, 0x3c, 0x49, 0xac, 0x4c, 0x24, 0x95, 0x6a, 0xad, 0xfc, 0x41,
00145         0xec, 0x3b, 0x33, 0x77, 0x94, 0xea, 0xe8, 0x65, 0x60, 0x27, 0xa7, 0x43, 0x71, 0x57, 0xcc, 0xeb,
00146         0x4f, 0x0b, 0xe6, 0x78, 0x0a, 0x61, 0x01, 0xa7, 0x2f, 0x96, 0x09, 0xed, 0xd9, 0x24, 0x23, 0xe6,
00147         0x52, 0xd2, 0xe5, 0xfb, 0x8b, 0x59, 0x1e, 0xd7, 0x28, 0x0b, 0x4d, 0xe0, 0x31, 0x32, 0xe7, 0x68,
00148         0x11, 0x61, 0x38, 0xdb, 0xc0, 0x19, 0x2b, 0x8d, 0x40, 0x88, 0x6c, 0x88, 0x3c, 0x8c, 0x9f, 0x8e,
00149         0xac, 0x93, 0xd9, 0xd4, 0x9f, 0x10, 0x85, 0x53, 0xea, 0x2d, 0xc2, 0x23, 0xce, 0xb9, 0x58, 0xbe,
00150         0x7c, 0x37, 0xe1, 0x64, 0xc7, 0x6e, 0x23, 0x37, 0xe8, 0xe1, 0x06, 0x7e, 0x82, 0x81, 0xcc, 0xbc,
00151         0x99, 0x5d, 0x75, 0xf7, 0xd5, 0xf5, 0xfa, 0xa5, 0x5c, 0x5b, 0xd1, 0x23, 0x53, 0x11, 0x99, 0xa2,
00152         0xe5, 0x67, 0x29, 0xde, 0x05, 0x80, 0xe3, 0x36, 0xbb, 0x99, 0xf9, 0xf2, 0xbc, 0x53, 0x10, 0x81,
00153         0x32, 0x2f, 0x69, 0x34, 0x66, 0xc2, 0xc9, 0x63, 0x74, 0x0e, 0x7c, 0x0e, 0x4a, 0xdd, 0x6e, 0xc1,
00154         0xb7, 0x09, 0x78, 0x7a, 0x4d, 0xe1, 0x00, 0xb8, 0x71, 0x8c, 0x42, 0x41, 0x67, 0x6f, 0xd1, 0x1f,
00155         0x6a, 0x13, 0xe9, 0xe5, 0x78, 0x8b, 0x63, 0x89, 0x8c, 0x34, 0x43, 0xbc, 0xf2, 0xa6, 0x01, 0x92,
00156         0xa3, 0xad, 0xbf, 0x5b, 0x99, 0x5f, 0x83, 0x5a, 0x8e, 0xcc, 0xe6, 0xb7, 0xd1, 0x38, 0xff, 0x81,
00157       };
00158 
00159     const char* encoded4 =
00160       "UKoA956e8Tzf+gPkWGGGN08AWz0ZiGYBJPIuuj0XS167qpnUSe7Ki5tcjF9IQOLHFIMe5d2o3WdO"
00161       "PfbIuuzjCL5KJ4rN9ifz95axC9C5511rLTWtxCn+MJWYJrxfLE02JGKuXxgUiLH0liV0rZQtuKpW"
00162       "jnwZFSWiHIuLKatFDiCrHNyjurdiJ3uNX7SD61Fyr/NMtoJVhe3Cvz1wZqIJ4dsc7egW9Qgclsi8"
00163       "KY3+T13Qr1TH6U+INTAcaNugSnT9CM0grm1/cYwhAA1m2X5n5F75gQHbzn++hbsJx5NnNr+Vdadl"
00164       "wogoroG1tmOCXH1NUkUlF7c2zQj2BlISlIcuN9TJlSxFgSRu4aprm/b7sU7MyDGL3U1pxdtipNBY"
00165       "cujiayg8UOip+M89nusft9m6j50cv50RQDxJrEwklWqt/EHsOzN3lOroZWAnp0NxV8zrTwvmeAph"
00166       "Aacvlgnt2SQj5lLS5fuLWR7XKAtN4DEy52gRYTjbwBkrjUCIbIg8jJ+OrJPZ1J8QhVPqLcIjzrlY"
00167       "vnw34WTHbiM36OEGfoKBzLyZXXX31fX6pVxb0SNTEZmi5Wcp3gWA4za7mfnyvFMQgTIvaTRmwslj"
00168       "dA58DkrdbsG3CXh6TeEAuHGMQkFnb9EfahPp5XiLY4mMNEO88qYBkqOtv1uZX4Najszmt9E4/4E=";
00169 
00170     rutz::byte_array buf;
00171     rutz::byte_array buf2;
00172 
00173     rutz::base64_encode(&decoded4[0], 512, buf, 0);
00174     TEST_REQUIRE_EQ(makeString(buf), encoded4);
00175     rutz::base64_decode(buf, buf2);
00176     TEST_REQUIRE_EQ(memcmp(&buf2.vec[0], &decoded4[0], 512), 0);
00177   }
00178 
00179   void testBase64EncodeDecode5()
00180   {
00181     // In this test we just generate some random data, and make sure
00182     // that we get the same thing back after an encoding-decoding
00183     // roundtrip.
00184 
00185     rutz::urand generator(rutz::default_rand_seed);
00186 
00187     const unsigned int SZ = 65530 + generator.idraw(5);
00188 
00189     rutz::byte_array decoded5;
00190     decoded5.vec.resize(SZ);
00191 
00192     for (unsigned int i = 0; i < SZ; ++i)
00193       {
00194         decoded5.vec[i] = generator.idraw(256);
00195       }
00196 
00197     rutz::byte_array buf;
00198     rutz::byte_array buf2;
00199 
00200     rutz::base64_encode(&decoded5.vec[0], SZ, buf, 0);
00201     TEST_REQUIRE_EQ(buf.vec.size(), 4*((SZ+2)/3));
00202     rutz::base64_decode(buf, buf2);
00203     TEST_REQUIRE_EQ(buf2.vec.size(), SZ);
00204     TEST_REQUIRE_EQ(memcmp(&buf2.vec[0], &decoded5.vec[0], SZ), 0);
00205   }
00206 }
00207 
00208 extern "C"
00209 int Basesixfourtest_Init(Tcl_Interp* interp)
00210 {
00211 GVX_TRACE("Basesixfourtest_Init");
00212 
00213   GVX_PKG_CREATE(pkg, interp, "Basesixfourtest", "4.$Revision: 10065 $");
00214 
00215   DEF_TEST(pkg, testBase64EncodeDecode1);
00216   DEF_TEST(pkg, testBase64EncodeDecode2);
00217   DEF_TEST(pkg, testBase64EncodeDecode3);
00218   DEF_TEST(pkg, testBase64EncodeDecode4);
00219   DEF_TEST(pkg, testBase64EncodeDecode5);
00220 
00221   GVX_PKG_RETURN(pkg);
00222 }
00223 
00224 // Need these to avoid dyld errors on Mac OS X
00225 extern "C" int Basesixfourtest_SafeInit(Tcl_Interp*)
00226 { return 1; }
00227 
00228 extern "C" int Basesixfourtest_Unload(Tcl_Interp* interp, int /*flags*/)
00229 {
00230 GVX_TRACE("Basesixfourtest_Unload");
00231   return tcl::pkg::destroy_on_unload(interp, "Basesixfourtest");
00232 }
00233 
00234 extern "C" int Basesixfourtest_SafeUnload(Tcl_Interp*, int /*flags*/)
00235 { return 1; }
00236 
00237 static const char __attribute__((used)) vcid_groovx_pkgs_whitebox_basesixfourtest_cc_utc20050626084022[] = "$Id: basesixfourtest.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00238 #endif // !GROOVX_PKGS_WHITEBOX_BASESIXFOURTEST_CC_UTC20050626084022_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.