diff options
author | Aleksi Lindeman <0xdec05eba@gmail.com> | 2018-02-17 14:26:29 +0100 |
---|---|---|
committer | Aleksi Lindeman <0xdec05eba@gmail.com> | 2018-03-05 22:48:26 +0100 |
commit | 66661e47dc826f50b690e080057f47a0ea27016c (patch) | |
tree | 63f0dfd6ed01843daaf78c96ea304d27f4bd2169 /tests | |
parent | 67957afb6ba01bcd85f1abd1a50ad2c1aa813c7c (diff) |
Add encryption functions (xchacha20)
Changed license to GPL 3.0 because of incompatible license with opendht.
Should odhtdb stay GPL 3.0 or should opendht be replaced with libdht
so license can be changed back to MIT?
Diffstat (limited to 'tests')
-rw-r--r-- | tests/main.cpp | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/tests/main.cpp b/tests/main.cpp index 1940c1a..57b6cbd 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,7 +1,8 @@ -#include <vector> #include "../include/Database.hpp" #include "../include/Group.hpp" #include "../include/LocalUser.hpp" +#include "../include/Encryption.hpp" +#include <vector> #include <chrono> #include <thread> @@ -9,7 +10,7 @@ using namespace std; using namespace chrono_literals; using namespace odhtdb; -#define assertEquals(a, b) do { if((a) != (b)) { fprintf(stderr, "Assert failed:\n%s != %s\n", #a, #b); exit(1); } } while(0) +#define assertEquals(expected, actual) do { if((expected) != (actual)) { fprintf(stderr, "Assert failed:\nExpected: %s, actual: %s\n", #expected, #actual); exit(1); } } while(0) void fail(const string &errMsg) { fprintf(stderr, "Fail:\n%.*s\n", errMsg.size(), errMsg.c_str()); } void testSignData(LocalUser *localUser) @@ -62,10 +63,32 @@ void testSignData(LocalUser *localUser) } } +void testEncryption() +{ + EncryptionKey encryptionKey; + generateEncryptionKey(&encryptionKey); + + const char *message = "hello, world!"; + const unsigned long long messageLength = 13; + EncryptedData encryptedData; + int encrypted = encrypt(&encryptedData, &encryptionKey, message, messageLength); + if(encrypted != 0) + fail("Failed to encrypt data"); + + std::string decryptedText; + int decrypted = decrypt(&decryptedText, &encryptionKey, &encryptedData); + if(decrypted != 0) + fail("Failed to decrypt data"); + + assertEquals(messageLength, decryptedText.size()); + assertEquals(0, strncmp(message, decryptedText.c_str(), messageLength)); +} + int main() { LocalUser *localUser = LocalUser::create(Signature::KeyPair(), "dec05eba"); testSignData(localUser); + testEncryption(); // TODO: For tests, dont run against bootstrap.ring.cx. // Run against a bootstrap node made only for testing which doesn't persist added data. |