ARC4Test.cc 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. dec.encrypt(LEN, decrypted, encrypted);
  31. CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
  32. // once more
  33. enc.encrypt(LEN, encrypted, key);
  34. dec.encrypt(LEN, decrypted, encrypted);
  35. CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
  36. }
  37. } // namespace aria2