diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-02-17 14:26:29 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:25:41 +0200 |
commit | 33e823ddddddd4a13b1a05b90ae5b419b89bcb1d (patch) | |
tree | 7672ca6f2b3c3268decf0b44df1804b10f1a92e1 /tests | |
parent | 40d94ad83f74753b71f33b58be8664bb21200219 (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. |