llxorcipher_tut.cpp

Go to the documentation of this file.
00001 
00034 #include "linden_common.h"
00035 #include "lltut.h"
00036 #include "llxorcipher.h"
00037 #include "llnullcipher.h"
00038 
00039 namespace tut
00040 {
00041         struct cipher
00042         {
00043         };
00044         typedef test_group<cipher> cipher_t;
00045         typedef cipher_t::object cipher_object_t;
00046         tut::cipher_t tut_cipher("cipher");
00047 
00048         //encrypt->decrypt
00049         template<> template<>
00050         void cipher_object_t::test<1>()
00051         {
00052                 const U32 len = 3;
00053                 const U8 pad[] = "abc";
00054                 const char str[] = "SecondLife";
00055                 const S32 str_len = sizeof(str);
00056                 U8 encrypted[str_len];
00057                 U8 decrypted[str_len];
00058                 LLXORCipher xorCipher(pad, len);
00059                 LLXORCipher xorCipher1(pad, len);
00060 
00061                 U32 length = xorCipher.requiredEncryptionSpace(50);
00062                 ensure("requiredEncryptionSpace() function failed", (length == 50));
00063 
00064                 U32 lenEncrypted = xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
00065                 ensure("Encryption failed", (lenEncrypted == str_len));
00066                 U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len);
00067                 ensure("Decryption failed", (lenDecrypted == str_len));
00068                 ensure_memory_matches("LLXORCipher Encrypt/Decrypt failed", str, str_len, decrypted, lenDecrypted);     
00069         }
00070 
00071         // operator=
00072         template<> template<>
00073         void cipher_object_t::test<2>()
00074         {
00075                 const U8 pad[] = "ABCDEFGHIJKLMNOPQ"; // pad len longer than data to be ciphered
00076                 const U32 pad_len = sizeof(pad);
00077                 const U8 pad1[] = "SecondLife";
00078                 const U32 pad_len1 = sizeof(pad1);
00079                 const char str[] = "To Be Ciphered";
00080                 const S32 str_len = sizeof(str);
00081                 U8 encrypted[str_len];
00082                 U8 decrypted[str_len];
00083 
00084                 LLXORCipher xorCipher(pad, pad_len);
00085                 LLXORCipher xorCipher1(pad1, pad_len1);
00086 
00087                 xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
00088                 // make xorCipher1 same as xorCipher..so that xorCipher1 can decrypt what was 
00089                 // encrypted using xorCipher
00090                 xorCipher1 = xorCipher;
00091                 U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len);
00092                 ensure_memory_matches("LLXORCipher operator= failed", str, str_len, decrypted, lenDecrypted);   
00093         }
00094         
00095         //in place encrypt->decrypt
00096         template<> template<>
00097         void cipher_object_t::test<3>()
00098         {
00099                 U32 padNum = 0x12349087;
00100                 const U8* pad = (U8*) &padNum;
00101                 const U32 pad_len = sizeof(U32);
00102                 char str[] = "To Be Ciphered a long string.........!!!.";
00103                 char str1[] = "To Be Ciphered a long string.........!!!."; // same as str
00104                 const S32 str_len = sizeof(str);
00105 
00106                 LLXORCipher xorCipher(pad, pad_len);
00107                 LLXORCipher xorCipher1(pad, pad_len);
00108                 xorCipher.encrypt((U8 *) str, str_len);
00109                 // it should not be the same as original data!
00110                 ensure("LLXORCipher: In Place encrypt failed", memcmp(str, str1, str_len) != 0);
00111                 xorCipher1.decrypt((U8 *) str, str_len);
00112                 // it should not be the same as original data!
00113                 ensure_memory_matches("LLXORCipher: In Place decrypt failed", str, str_len, str1, str_len);
00114         }
00115 
00116         //LLNullCipher encrypt->decrypt
00117         template<> template<>
00118         void cipher_object_t::test<4>()
00119         {
00120                 const char str[] = "SecondLife";
00121                 const S32 str_len = sizeof(str);
00122                 U8 encrypted[str_len];
00123                 U8 decrypted[str_len];
00124                 LLNullCipher nullCipher;
00125                 LLNullCipher nullCipher1;
00126 
00127                 U32 length = nullCipher.requiredEncryptionSpace(50);
00128                 ensure("LLNullCipher::requiredEncryptionSpace() function failed", (length == 50));
00129 
00130                 U32 len1 = nullCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
00131                 ensure_memory_matches("LLNullCipher - Source transformed during encryption.", encrypted, len1, str, str_len);
00132                 
00133                 U32 len2 = nullCipher1.decrypt(encrypted, str_len, decrypted, str_len);
00134                 ensure_memory_matches("LLNullCipher - Decryption failed", decrypted, len2, str, str_len);
00135         }
00136 }

Generated on Fri May 16 08:34:31 2008 for SecondLife by  doxygen 1.5.5