diff options
author | Jacek Antonelli | 2008-08-15 23:45:02 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:45:02 -0500 |
commit | d644fc64407dcd14ffcee6a0e9fbe28ee3a4e9bd (patch) | |
tree | 7ed0c2c27d717801238a2e6b5749cd5bf88c3059 /linden/indra/test/llxorcipher_tut.cpp | |
parent | Second Life viewer sources 1.17.3.0 (diff) | |
download | meta-impy-d644fc64407dcd14ffcee6a0e9fbe28ee3a4e9bd.zip meta-impy-d644fc64407dcd14ffcee6a0e9fbe28ee3a4e9bd.tar.gz meta-impy-d644fc64407dcd14ffcee6a0e9fbe28ee3a4e9bd.tar.bz2 meta-impy-d644fc64407dcd14ffcee6a0e9fbe28ee3a4e9bd.tar.xz |
Second Life viewer sources 1.18.0.6
Diffstat (limited to 'linden/indra/test/llxorcipher_tut.cpp')
-rw-r--r-- | linden/indra/test/llxorcipher_tut.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/linden/indra/test/llxorcipher_tut.cpp b/linden/indra/test/llxorcipher_tut.cpp new file mode 100644 index 0000000..07b33b6 --- /dev/null +++ b/linden/indra/test/llxorcipher_tut.cpp | |||
@@ -0,0 +1,133 @@ | |||
1 | /** | ||
2 | * @file llxorcipher_tut.cpp | ||
3 | * @author Adroit | ||
4 | * @date 2007-03 | ||
5 | * @brief llxorcipher, llnullcipher test cases. | ||
6 | * | ||
7 | * Copyright (c) 2007-2007, Linden Research, Inc. | ||
8 | * | ||
9 | * Second Life Viewer Source Code | ||
10 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
11 | * to you under the terms of the GNU General Public License, version 2.0 | ||
12 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
13 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
14 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
15 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
16 | * | ||
17 | * There are special exceptions to the terms and conditions of the GPL as | ||
18 | * it is applied to this Source Code. View the full text of the exception | ||
19 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
20 | * online at http://secondlife.com/developers/opensource/flossexception | ||
21 | * | ||
22 | * By copying, modifying or distributing this software, you acknowledge | ||
23 | * that you have read and understood your obligations described above, | ||
24 | * and agree to abide by those obligations. | ||
25 | * | ||
26 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
27 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
28 | * COMPLETENESS OR PERFORMANCE. | ||
29 | */ | ||
30 | |||
31 | #include <tut/tut.h> | ||
32 | #include "lltut.h" | ||
33 | #include "llxorcipher.h" | ||
34 | #include "llnullcipher.h" | ||
35 | |||
36 | namespace tut | ||
37 | { | ||
38 | struct cipher | ||
39 | { | ||
40 | }; | ||
41 | typedef test_group<cipher> cipher_t; | ||
42 | typedef cipher_t::object cipher_object_t; | ||
43 | tut::cipher_t tut_cipher("cipher"); | ||
44 | |||
45 | //encrypt->decrypt | ||
46 | template<> template<> | ||
47 | void cipher_object_t::test<1>() | ||
48 | { | ||
49 | const U32 len = 3; | ||
50 | const U8 pad[] = "abc"; | ||
51 | const char str[] = "SecondLife"; | ||
52 | const S32 str_len = sizeof(str); | ||
53 | U8 encrypted[str_len]; | ||
54 | U8 decrypted[str_len]; | ||
55 | LLXORCipher xorCipher(pad, len); | ||
56 | LLXORCipher xorCipher1(pad, len); | ||
57 | |||
58 | U32 length = xorCipher.requiredEncryptionSpace(50); | ||
59 | ensure("requiredEncryptionSpace() function failed", (length == 50)); | ||
60 | |||
61 | U32 lenEncrypted = xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len); | ||
62 | ensure("Encryption failed", (lenEncrypted == str_len)); | ||
63 | U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len); | ||
64 | ensure("Decryption failed", (lenDecrypted == str_len)); | ||
65 | ensure_memory_matches("LLXORCipher Encrypt/Decrypt failed", str, str_len, decrypted, lenDecrypted); | ||
66 | } | ||
67 | |||
68 | // operator= | ||
69 | template<> template<> | ||
70 | void cipher_object_t::test<2>() | ||
71 | { | ||
72 | const U8 pad[] = "ABCDEFGHIJKLMNOPQ"; // pad len longer than data to be ciphered | ||
73 | const U32 pad_len = sizeof(pad); | ||
74 | const U8 pad1[] = "SecondLife"; | ||
75 | const U32 pad_len1 = sizeof(pad1); | ||
76 | const char str[] = "To Be Ciphered"; | ||
77 | const S32 str_len = sizeof(str); | ||
78 | U8 encrypted[str_len]; | ||
79 | U8 decrypted[str_len]; | ||
80 | |||
81 | LLXORCipher xorCipher(pad, pad_len); | ||
82 | LLXORCipher xorCipher1(pad1, pad_len1); | ||
83 | |||
84 | xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len); | ||
85 | // make xorCipher1 same as xorCipher..so that xorCipher1 can decrypt what was | ||
86 | // encrypted using xorCipher | ||
87 | xorCipher1 = xorCipher; | ||
88 | U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len); | ||
89 | ensure_memory_matches("LLXORCipher operator= failed", str, str_len, decrypted, lenDecrypted); | ||
90 | } | ||
91 | |||
92 | //in place encrypt->decrypt | ||
93 | template<> template<> | ||
94 | void cipher_object_t::test<3>() | ||
95 | { | ||
96 | U32 padNum = 0x12349087; | ||
97 | const U8* pad = (U8*) &padNum; | ||
98 | const U32 pad_len = sizeof(U32); | ||
99 | char str[] = "To Be Ciphered a long string.........!!!."; | ||
100 | char str1[] = "To Be Ciphered a long string.........!!!."; // same as str | ||
101 | const S32 str_len = sizeof(str); | ||
102 | |||
103 | LLXORCipher xorCipher(pad, pad_len); | ||
104 | LLXORCipher xorCipher1(pad, pad_len); | ||
105 | xorCipher.encrypt((U8 *) str, str_len); | ||
106 | // it should not be the same as original data! | ||
107 | ensure("LLXORCipher: In Place encrypt failed", memcmp(str, str1, str_len) != 0); | ||
108 | xorCipher1.decrypt((U8 *) str, str_len); | ||
109 | // it should not be the same as original data! | ||
110 | ensure_memory_matches("LLXORCipher: In Place decrypt failed", str, str_len, str1, str_len); | ||
111 | } | ||
112 | |||
113 | //LLNullCipher encrypt->decrypt | ||
114 | template<> template<> | ||
115 | void cipher_object_t::test<4>() | ||
116 | { | ||
117 | const char str[] = "SecondLife"; | ||
118 | const S32 str_len = sizeof(str); | ||
119 | U8 encrypted[str_len]; | ||
120 | U8 decrypted[str_len]; | ||
121 | LLNullCipher nullCipher; | ||
122 | LLNullCipher nullCipher1; | ||
123 | |||
124 | U32 length = nullCipher.requiredEncryptionSpace(50); | ||
125 | ensure("LLNullCipher::requiredEncryptionSpace() function failed", (length == 50)); | ||
126 | |||
127 | U32 len1 = nullCipher.encrypt((U8 *) str, str_len, encrypted, str_len); | ||
128 | ensure_memory_matches("LLNullCipher - Source transformed during encryption.", encrypted, len1, str, str_len); | ||
129 | |||
130 | U32 len2 = nullCipher1.decrypt(encrypted, str_len, decrypted, str_len); | ||
131 | ensure_memory_matches("LLNullCipher - Decryption failed", decrypted, len2, str, str_len); | ||
132 | } | ||
133 | } | ||