From 33e823ddddddd4a13b1a05b90ae5b419b89bcb1d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 17 Feb 2018 14:26:29 +0100 Subject: 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? --- tests/main.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'tests') 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 #include "../include/Database.hpp" #include "../include/Group.hpp" #include "../include/LocalUser.hpp" +#include "../include/Encryption.hpp" +#include #include #include @@ -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. -- cgit v1.2.3