ARC4Test.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "ARC4Encryptor.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "ARC4Decryptor.h"
  5. #include "Exception.h"
  6. #include "util.h"
  7. namespace aria2 {
  8. class ARC4Test:public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(ARC4Test);
  10. CPPUNIT_TEST(testEncryptDecrypt);
  11. CPPUNIT_TEST_SUITE_END();
  12. public:
  13. void setUp() {}
  14. void tearDown() {}
  15. void testEncryptDecrypt();
  16. };
  17. CPPUNIT_TEST_SUITE_REGISTRATION(ARC4Test);
  18. void ARC4Test::testEncryptDecrypt()
  19. {
  20. ARC4Encryptor enc;
  21. ARC4Decryptor dec;
  22. const size_t LEN = 20;
  23. unsigned char key[LEN];
  24. memset(key, 0, LEN);
  25. util::generateRandomData(key, sizeof(key));
  26. enc.init(key, sizeof(key));
  27. dec.init(key, sizeof(key));
  28. unsigned char encrypted[LEN];
  29. unsigned char decrypted[LEN];
  30. enc.encrypt(encrypted, LEN, key, LEN);
  31. dec.decrypt(decrypted, LEN, encrypted, LEN);
  32. CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
  33. // once more
  34. enc.encrypt(encrypted, LEN, key, LEN);
  35. dec.decrypt(decrypted, LEN, encrypted, LEN);
  36. CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
  37. }
  38. } // namespace aria2