summaryrefslogtreecommitdiff
path: root/test/ARC4Test.cc
blob: 1bab1163b92fa58acae2647861411e0a509c14df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "ARC4Encryptor.h"
#include "ARC4Decryptor.h"
#include "Exception.h"
#include "Util.h"
#include "DHTUtil.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

class ARC4Test:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(ARC4Test);
  CPPUNIT_TEST(testEncryptDecrypt);
  CPPUNIT_TEST_SUITE_END();
public:
  void setUp() {}

  void tearDown() {}

  void testEncryptDecrypt();
};


CPPUNIT_TEST_SUITE_REGISTRATION(ARC4Test);

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

  unsigned char encrypted[LEN];
  unsigned char decrypted[LEN];
  enc.encrypt(encrypted, LEN, key, LEN);
  dec.decrypt(decrypted, LEN, encrypted, LEN);

  CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
  // once more
  enc.encrypt(encrypted, LEN, key, LEN);
  dec.decrypt(decrypted, LEN, encrypted, LEN);

  CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0);
}

} // namespace aria2