ARC4Test.cc 1.1 KB

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