-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathCryptoTests.cpp
90 lines (69 loc) · 2.38 KB
/
CryptoTests.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "CryptoTests.h"
CPPUNIT_TEST_SUITE_REGISTRATION(CryptoTest);
CryptoTest::CryptoTest() {
}
CryptoTest::~CryptoTest() {
}
void CryptoTest::setUp() {
crypto = new CryptoKernel::Crypto(true);
}
void CryptoTest::tearDown() {
delete crypto;
}
/**
* Tests that the Crypto module initialised correctly
*/
void CryptoTest::testInit() {
CPPUNIT_ASSERT(crypto->getStatus());
}
/**
* Tests that a keypair has been generated
*/
void CryptoTest::testKeygen() {
const std::string privateKey = crypto->getPrivateKey();
const std::string publicKey = crypto->getPublicKey();
CPPUNIT_ASSERT(privateKey.size() > 0);
CPPUNIT_ASSERT(publicKey.size() > 0);
}
/**
* Tests that signing and verifying works
*/
void CryptoTest::testSignVerify() {
const std::string privateKey = crypto->getPrivateKey();
const std::string publicKey = crypto->getPublicKey();
const std::string signature = crypto->sign(plainText);
CPPUNIT_ASSERT(signature.size() > 0);
CPPUNIT_ASSERT(crypto->verify(plainText, signature));
}
/**
* Tests that verifying invalid texts / signatures / malformed signatures fail
*/
void CryptoTest::testSignVerifyInvalid() {
const std::string privateKey = crypto->getPrivateKey();
const std::string publicKey = crypto->getPublicKey();
const std::string signature = crypto->sign(plainText);
CPPUNIT_ASSERT(signature.size() > 0);
// Valid sig, not matching the input
CPPUNIT_ASSERT(!crypto->verify("invalid plain text", signature));
// Valid text, invalid sig
CPPUNIT_ASSERT(!crypto->verify(plainText, "MzA0NTAyMjEwMGVlYmQyOGRmMjljZGYwNGUwODI5ZmZiZmU2ZmFlZTg1YTUxNGI3MjE4YmYxM2RlNjhkM2E0OGMzNGFmMmQxNGUwMjIwMWY2ZDBhN2Y0ZWNmYmUyMjRlMjI1YmFjYTYyOWFhYzYwMDk3MWQwNmQ3Y2Q2ZDNjYzkwZDVkZDExZGFlZTB3OQ=="));
}
/**
* Tests passing key to class
*/
void CryptoTest::testPassingKeys() {
CryptoKernel::Crypto *tempCrypto = new CryptoKernel::Crypto(true);
const std::string signature = tempCrypto->sign(plainText);
CPPUNIT_ASSERT(signature.size() > 0);
CPPUNIT_ASSERT(crypto->setPublicKey(tempCrypto->getPublicKey()));
CPPUNIT_ASSERT(crypto->verify(plainText, signature));
delete tempCrypto;
}
/**
* Tests hashing with SHA256
*/
void CryptoTest::testSHA256Hash() {
const std::string hash =
"b6dc933311bc2357cc5fc636a4dbe41a01b7a33b583d043a7f870f3440697e27";
CPPUNIT_ASSERT_EQUAL(hash, CryptoKernel::Crypto::sha256("wow"));
}