ARC4Test.cc 1.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "ARC4Encryptor.h"

#include <cstring>
#include <cppunit/extensions/HelperMacros.h>

#include "Exception.h"
#include "util.h"

namespace aria2 {

11
class ARC4Test : public CppUnit::TestFixture {
12 13 14 15

  CPPUNIT_TEST_SUITE(ARC4Test);
  CPPUNIT_TEST(testEncrypt);
  CPPUNIT_TEST_SUITE_END();
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
public:
  void setUp() {}

  void tearDown() {}

  void testEncrypt();
};

CPPUNIT_TEST_SUITE_REGISTRATION(ARC4Test);

void ARC4Test::testEncrypt()
{
  ARC4Encryptor enc;
  ARC4Encryptor dec;
  const size_t LEN = 20;
  unsigned char key[LEN];
  memset(key, 0, LEN);
  util::generateRandomData(key, sizeof(key));
  enc.init(key, sizeof(key));
  dec.init(key, sizeof(key));

  unsigned char encrypted[LEN];
  unsigned char decrypted[LEN];
40

41
  enc.encrypt(LEN, encrypted, key);
42
  CPPUNIT_ASSERT(memcmp(key, encrypted, LEN) != 0);
43 44
  dec.encrypt(LEN, decrypted, encrypted);
  CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
45

46 47
  // once more
  enc.encrypt(LEN, encrypted, key);
48
  CPPUNIT_ASSERT(memcmp(key, encrypted, LEN) != 0);
49 50 51 52 53
  dec.encrypt(LEN, decrypted, encrypted);
  CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
}

} // namespace aria2