/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. #include "linden_common.h"
  29. #include "lltut.h"
  30. #include "llxorcipher.h"
  31. #include "llnullcipher.h"
  32. namespace tut
  33. {
  34. struct cipher
  35. {
  36. };
  37. typedef test_group<cipher> cipher_t;
  38. typedef cipher_t::object cipher_object_t;
  39. tut::cipher_t tut_cipher("cipher");
  40. //encrypt->decrypt
  41. template<> template<>
  42. void cipher_object_t::test<1>()
  43. {
  44. const U32 len = 3;
  45. const U8 pad[] = "abc";
  46. const char str[] = "SecondLife";
  47. const S32 str_len = sizeof(str);
  48. U8 encrypted[str_len];
  49. U8 decrypted[str_len];
  50. LLXORCipher xorCipher(pad, len);
  51. LLXORCipher xorCipher1(pad, len);
  52. U32 length = xorCipher.requiredEncryptionSpace(50);
  53. ensure("requiredEncryptionSpace() function failed", (length == 50));
  54. U32 lenEncrypted = xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
  55. ensure("Encryption failed", (lenEncrypted == str_len));
  56. U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len);
  57. ensure("Decryption failed", (lenDecrypted == str_len));
  58. ensure_memory_matches("LLXORCipher Encrypt/Decrypt failed", str, str_len, decrypted, lenDecrypted);
  59. }
  60. // operator=
  61. template<> template<>
  62. void cipher_object_t::test<2>()
  63. {
  64. const U8 pad[] = "ABCDEFGHIJKLMNOPQ"; // pad len longer than data to be ciphered
  65. const U32 pad_len = sizeof(pad);
  66. const U8 pad1[] = "SecondLife";
  67. const U32 pad_len1 = sizeof(pad1);
  68. const char str[] = "To Be Ciphered";
  69. const S32 str_len = sizeof(str);
  70. U8 encrypted[str_len];
  71. U8 decrypted[str_len];
  72. LLXORCipher xorCipher(pad, pad_len);
  73. LLXORCipher xorCipher1(pad1, pad_len1);
  74. xorCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
  75. // make xorCipher1 same as xorCipher..so that xorCipher1 can decrypt what was
  76. // encrypted using xorCipher
  77. xorCipher1 = xorCipher;
  78. U32 lenDecrypted = xorCipher1.decrypt(encrypted, str_len, decrypted, str_len);
  79. ensure_memory_matches("LLXORCipher operator= failed", str, str_len, decrypted, lenDecrypted);
  80. }
  81. //in place encrypt->decrypt
  82. template<> template<>
  83. void cipher_object_t::test<3>()
  84. {
  85. U32 padNum = 0x12349087;
  86. const U8* pad = (U8*) &padNum;
  87. const U32 pad_len = sizeof(U32);
  88. char str[] = "To Be Ciphered a long string.........!!!.";
  89. char str1[] = "To Be Ciphered a long string.........!!!."; // same as str
  90. const S32 str_len = sizeof(str);
  91. LLXORCipher xorCipher(pad, pad_len);
  92. LLXORCipher xorCipher1(pad, pad_len);
  93. xorCipher.encrypt((U8 *) str, str_len);
  94. // it should not be the same as original data!
  95. ensure("LLXORCipher: In Place encrypt failed", memcmp(str, str1, str_len) != 0);
  96. xorCipher1.decrypt((U8 *) str, str_len);
  97. // it should not be the same as original data!
  98. ensure_memory_matches("LLXORCipher: In Place decrypt failed", str, str_len, str1, str_len);
  99. }
  100. //LLNullCipher encrypt->decrypt
  101. template<> template<>
  102. void cipher_object_t::test<4>()
  103. {
  104. const char str[] = "SecondLife";
  105. const S32 str_len = sizeof(str);
  106. U8 encrypted[str_len];
  107. U8 decrypted[str_len];
  108. LLNullCipher nullCipher;
  109. LLNullCipher nullCipher1;
  110. U32 length = nullCipher.requiredEncryptionSpace(50);
  111. ensure("LLNullCipher::requiredEncryptionSpace() function failed", (length == 50));
  112. U32 len1 = nullCipher.encrypt((U8 *) str, str_len, encrypted, str_len);
  113. ensure_memory_matches("LLNullCipher - Source transformed during encryption.", encrypted, len1, str, str_len);
  114. U32 len2 = nullCipher1.decrypt(encrypted, str_len, decrypted, str_len);
  115. ensure_memory_matches("LLNullCipher - Decryption failed", decrypted, len2, str, str_len);
  116. }
  117. }