reSIProcate/rutil  9694
testCoders.cxx
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #ifdef WIN32
00003 #include <io.h>
00004 #else
00005 #include <unistd.h>
00006 #endif
00007 #include <cassert>
00008 #include <memory>
00009 
00010 #include "rutil/compat.hxx"
00011 #include "rutil/Random.hxx"
00012 #include "rutil/Coders.hxx"
00013 #include <iostream>
00014 
00015 #ifdef __FreeBSD__
00016 #define NEW_THROWS 1
00017 #else
00018 #define NEW_THROWS 0
00019 #endif
00020 
00021 using namespace std;
00022 using namespace resip;
00023 
00024 
00025 Data* randomData(int size)
00026 {
00027    unsigned char * p = new unsigned char[size];
00028 
00029    for(int i = 0 ; i < size; i++)
00030    {
00031           p[i] = static_cast<unsigned char>(Random::getRandom()&0xff);
00032    }
00033 
00034    return new Data(p,size);
00035 }
00036 
00037 
00038 void showData(const Data& data)
00039 {
00040    cout << "Data (n=" << data.size() << "): ";
00041    for(Data::size_type i = 0 ; i < data.size() ; i++)
00042    {
00043       cout << hex << (unsigned int)(((unsigned char *)(data.data()))[i]) << ' ';
00044    }
00045    cout << dec << endl;
00046 }
00047 
00048 void showCoded(const Data& data)
00049 {
00050    showData(data);
00051 }
00052 
00053 int compareData(const Data &a, const Data& b)
00054 {
00055    return a == b;
00056 }
00057 
00058 int
00059 main()
00060 {
00061    using namespace resip;
00062 
00063    assert(sizeof(size_t) == sizeof(void*));
00064 
00065   Data testData("The quick brown fox jumped over the lazy dog.");
00066 
00067   Data encoded =    
00068      Base64Coder::encode(testData);
00069 
00070   Data decoded = Base64Coder::decode(encoded);
00071 
00072   cout << "encoded: '" << encoded << "'" << endl;
00073   cout << "decoded: '" << decoded << "'" << endl;
00074 
00075 
00076   testData = Data("1");
00077 
00078   encoded =    
00079      Base64Coder::encode(testData);
00080 
00081   decoded = Base64Coder::decode(encoded);
00082 
00083   cout << "encoded: '" << encoded << "'" << endl;
00084   cout << "decoded: '" << decoded << "'" << endl;
00085 
00086 
00087   int rVal = 0; // test return val
00088   for(int i=1;i<320;i++)
00089   {
00090      Data* originalData = randomData(i);
00091 
00092      cout << i << "-------" << endl;
00093 
00094 
00095      // encrypt this data
00096 
00097      Data coded = Base64Coder::encode(*originalData);
00098 
00099      showData(*originalData);
00100 
00101      Data decoded = Base64Coder::decode(coded);
00102 
00103      showData(decoded);
00104 
00105      assert(originalData->size() == decoded.size());
00106 
00107      cout << "encoded: " << coded << endl;
00108      
00109      int b = 0;
00110      if ( *originalData != decoded )
00111      {
00112         cout << i << ": symetry failure (encode/decode) at byte " << -b-1 << endl;
00113         rVal = -1;
00114      }
00115      delete originalData;
00116   }
00117   return rVal;
00118 
00119 }
00120 // Local Variables:
00121 // mode:c++
00122 // c-file-style:"ellemtel"
00123 // c-file-offsets:((case-label . +))
00124 // indent-tabs-mode:nil
00125 // End
00126 /* ====================================================================
00127  * The Vovida Software License, Version 1.0 
00128  * 
00129  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00130  * 
00131  * Redistribution and use in source and binary forms, with or without
00132  * modification, are permitted provided that the following conditions
00133  * are met:
00134  * 
00135  * 1. Redistributions of source code must retain the above copyright
00136  *    notice, this list of conditions and the following disclaimer.
00137  * 
00138  * 2. Redistributions in binary form must reproduce the above copyright
00139  *    notice, this list of conditions and the following disclaimer in
00140  *    the documentation and/or other materials provided with the
00141  *    distribution.
00142  * 
00143  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00144  *    and "Vovida Open Communication Application Library (VOCAL)" must
00145  *    not be used to endorse or promote products derived from this
00146  *    software without prior written permission. For written
00147  *    permission, please contact vocal@vovida.org.
00148  *
00149  * 4. Products derived from this software may not be called "VOCAL", nor
00150  *    may "VOCAL" appear in their name, without prior written
00151  *    permission of Vovida Networks, Inc.
00152  * 
00153  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00154  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00155  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00156  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00157  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00158  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00159  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00160  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00161  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00162  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00163  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00164  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00165  * DAMAGE.
00166  * 
00167  * ====================================================================
00168  * 
00169  * This software consists of voluntary contributions made by Vovida
00170  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00171  * Inc.  For more information on Vovida Networks, Inc., please see
00172  * <http://www.vovida.org/>.
00173  *
00174  */