llblowfish_tut.cpp

Go to the documentation of this file.
00001 
00037 #include "linden_common.h"
00038 #include "lltut.h"
00039 
00040 #include "llblowfishcipher.h"
00041 
00042 #include "lluuid.h"
00043 
00044 namespace tut
00045 {
00046         class LLData
00047         {
00048         public:
00049                 unsigned char* mInput;
00050                 int mInputSize;
00051 
00052                 LLData()
00053                 {
00054                         // \n to make it easier to create text files
00055                         // for testing with command line openssl
00056                         mInput = (unsigned char*)"01234567890123456789012345678901234\n";
00057                         mInputSize = 36;
00058                 }
00059 
00060                 bool matchFile(const char* filename,
00061                                 const std::string& data)
00062                 {
00063                         FILE* fp = fopen(filename, "rb");
00064                         if (!fp) 
00065                         {
00066                                 // sometimes test is run inside the indra directory
00067                                 std::string path = "test/";
00068                                 path += filename;
00069                                 fp = fopen(path.c_str(), "rb");
00070                         }
00071                         if (!fp)
00072                         {
00073                                 llwarns << "unabled to open " << filename << llendl;
00074                                 return false;
00075                         }
00076 
00077                         std::string good;
00078                         good.resize(256);
00079                         size_t got = fread(&good[0], 1, 256, fp);
00080                         lldebugs << "matchFile read " << got << llendl;
00081                         fclose(fp);
00082                         good.resize(got);
00083                 
00084                         return (good == data);
00085                 }
00086         };
00087         typedef test_group<LLData> blowfish_test;
00088         typedef blowfish_test::object blowfish_object;
00089         // Create test with name that can be selected on
00090         // command line of test app.
00091         tut::blowfish_test blowfish("blowfish");
00092 
00093         template<> template<>
00094         void blowfish_object::test<1>()
00095         {
00096                 LLUUID blank;
00097                 LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES);
00098 
00099                 U32 dst_len = cipher.requiredEncryptionSpace(36);
00100                 ensure("encryption space 36",
00101                                 (dst_len == 40) );
00102 
00103                 // Blowfish adds an additional 8-byte block if your
00104                 // input is an exact multiple of 8
00105                 dst_len = cipher.requiredEncryptionSpace(8);
00106                 ensure("encryption space 8",
00107                                 (dst_len == 16)  );
00108         }
00109 
00110         template<> template<>
00111         void blowfish_object::test<2>()
00112         {
00113                 LLUUID blank;
00114                 LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES);
00115 
00116                 std::string result;
00117                 result.resize(256);
00118                 U32 count = cipher.encrypt(mInput, mInputSize,
00119                                 (U8*) &result[0], 256);
00120 
00121                 ensure("encrypt output count",
00122                                 (count == 40) );
00123                 result.resize(count);
00124 
00125                 ensure("encrypt null key", matchFile("blowfish.1.bin", result));
00126         }
00127 
00128         template<> template<>
00129         void blowfish_object::test<3>()
00130         {
00131                 // same as base64 test id
00132                 LLUUID id("526a1e07-a19d-baed-84c4-ff08a488d15e");
00133                 LLBlowfishCipher cipher(&id.mData[0], UUID_BYTES);
00134 
00135                 std::string result;
00136                 result.resize(256);
00137                 U32 count = cipher.encrypt(mInput, mInputSize,
00138                                 (U8*) &result[0], 256);
00139 
00140                 ensure("encrypt output count",
00141                                 (count == 40) );
00142                 result.resize(count);
00143 
00144                 ensure("encrypt real key", matchFile("blowfish.2.bin", result));
00145         }
00146 }

Generated on Thu Jul 1 06:08:19 2010 for Second Life Viewer by  doxygen 1.4.7