PageRenderTime 32ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 4ms app.codeStats 0ms

/indra/test/llxorcipher_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 131 lines | 82 code | 14 blank | 35 comment | 5 complexity | 6b71f891dcfd307a3b7cfe0ec4f82e89 MD5 | raw file
  1/**
  2 * @file llxorcipher_tut.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief llxorcipher, llnullcipher test cases.
  6 *
  7 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28 
 29#include "linden_common.h"
 30#include "lltut.h"
 31#include "llxorcipher.h"
 32#include "llnullcipher.h"
 33
 34namespace tut
 35{
 36	struct cipher
 37	{
 38	};
 39	typedef test_group<cipher> cipher_t;
 40	typedef cipher_t::object cipher_object_t;
 41	tut::cipher_t tut_cipher("cipher");
 42
 43	//encrypt->decrypt
 44	template<> template<>
 45	void cipher_object_t::test<1>()
 46	{
 47		const U32 len = 3;
 48		const U8 pad[] = "abc";
 49		const char str[] = "SecondLife";
 50		const S32 str_len = sizeof(str);
 51		U8 encrypted[str_len];
 52		U8 decrypted[str_len];
 53		LLXORCipher xorCipher(pad, len);
 54		LLXORCipher xorCipher1(pad, len);
 55
 56		U32 length = xorCipher.requiredEncryptionSpace(50);
 57		ensure("requiredEncryptionSpace() function failed", (length == 50));
 58
 59		U32 lenEncrypted = xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
 60		ensure("Encryption failed", (lenEncrypted == str_len));
 61		U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len);
 62		ensure("Decryption failed", (lenDecrypted == str_len));
 63		ensure_memory_matches("LLXORCipher Encrypt/Decrypt failed", str, str_len, decrypted, lenDecrypted);	
 64	}
 65
 66	// operator=
 67	template<> template<>
 68	void cipher_object_t::test<2>()
 69	{
 70		const U8 pad[] = "ABCDEFGHIJKLMNOPQ"; // pad len longer than data to be ciphered
 71		const U32 pad_len = sizeof(pad);
 72		const U8 pad1[] = "SecondLife";
 73		const U32 pad_len1 = sizeof(pad1);
 74		const char str[] = "To Be Ciphered";
 75		const S32 str_len = sizeof(str);
 76		U8 encrypted[str_len];
 77		U8 decrypted[str_len];
 78
 79		LLXORCipher xorCipher(pad, pad_len);
 80		LLXORCipher xorCipher1(pad1, pad_len1);
 81
 82		xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
 83		// make xorCipher1 same as xorCipher..so that xorCipher1 can decrypt what was 
 84		// encrypted using xorCipher
 85		xorCipher1 = xorCipher;
 86		U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len);
 87		ensure_memory_matches("LLXORCipher operator= failed", str, str_len, decrypted, lenDecrypted);	
 88	}
 89	
 90	//in place encrypt->decrypt
 91	template<> template<>
 92	void cipher_object_t::test<3>()
 93	{
 94		U32 padNum = 0x12349087;
 95		const U8* pad = (U8*) &padNum;
 96		const U32 pad_len = sizeof(U32);
 97		char str[] = "To Be Ciphered a long string.........!!!.";
 98		char str1[] = "To Be Ciphered a long string.........!!!."; // same as str
 99		const S32 str_len = sizeof(str);
100
101		LLXORCipher xorCipher(pad, pad_len);
102		LLXORCipher xorCipher1(pad, pad_len);
103		xorCipher.encrypt((U8 *) str, str_len);
104		// it should not be the same as original data!
105		ensure("LLXORCipher: In Place encrypt failed", memcmp(str, str1, str_len) != 0);
106		xorCipher1.decrypt((U8 *) str, str_len);
107		// it should not be the same as original data!
108		ensure_memory_matches("LLXORCipher: In Place decrypt failed", str, str_len, str1, str_len);
109	}
110
111	//LLNullCipher encrypt->decrypt
112	template<> template<>
113	void cipher_object_t::test<4>()
114	{
115		const char str[] = "SecondLife";
116		const S32 str_len = sizeof(str);
117		U8 encrypted[str_len];
118		U8 decrypted[str_len];
119		LLNullCipher nullCipher;
120		LLNullCipher nullCipher1;
121
122		U32 length = nullCipher.requiredEncryptionSpace(50);
123		ensure("LLNullCipher::requiredEncryptionSpace() function failed", (length == 50));
124
125		U32 len1 = nullCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
126		ensure_memory_matches("LLNullCipher - Source transformed during encryption.", encrypted, len1, str, str_len);
127		
128		U32 len2 = nullCipher1.decrypt(encrypted, str_len, decrypted, str_len);
129		ensure_memory_matches("LLNullCipher - Decryption failed", decrypted, len2, str, str_len);
130	}
131}