From 2ffb47d0043e57707474e5ae811f97c2e5e93f25 Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Mon, 5 Mar 2018 22:45:56 +0100 Subject: Implement 'create' operation, add seeding Seeding is currently only done on the key you specify, in the future the user should request data that it can seed. --- tests/assert.hpp | 20 ++++++++++++++++++++ tests/main.cpp | 48 +++++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 tests/assert.hpp (limited to 'tests') diff --git a/tests/assert.hpp b/tests/assert.hpp new file mode 100644 index 0000000..86f74f2 --- /dev/null +++ b/tests/assert.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +template +static void assertEquals(const T &expected, const T &actual) +{ + if(expected != actual) + { + std::cerr << "Assertion failed!\nExpected: " << expected << ", actual: " << actual << std::endl; + exit(1); + } +} + +static void fail(const std::string &errMsg) +{ + fprintf(stderr, "Fail:\n%.*s\n", errMsg.size(), errMsg.c_str()); + exit(1); +} diff --git a/tests/main.cpp b/tests/main.cpp index 57b6cbd..3c7e798 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,7 +1,9 @@ +#include "assert.hpp" #include "../include/Database.hpp" #include "../include/Group.hpp" #include "../include/LocalUser.hpp" #include "../include/Encryption.hpp" +#include "../include/Hash.hpp" #include #include #include @@ -10,8 +12,12 @@ using namespace std; using namespace chrono_literals; using namespace odhtdb; -#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 testHash() +{ + Hash hash("odhtdb", 6); + assertEquals("a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f", hash.toString()); + printf("hash of 'odhtdb' is: a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f\n"); +} void testSignData(LocalUser *localUser) { @@ -65,44 +71,40 @@ 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"); + Encryption encryption(DataView((void*)message, messageLength)); - 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)); + Decryption decryption(encryption.getCipherText(), encryption.getNonce(), encryption.getKey()); + assertEquals(messageLength, decryption.getDecryptedText().size); + assertEquals(0, strncmp(message, (const char*)decryption.getDecryptedText().data, messageLength)); } int main() { + printf("Starting tests...\n"); 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. + testHash(); + // TODO: Setup local bootstrap node for tests Database database("bootstrap.ring.cx", 4222, "storage"); - database.seed(); - - database.create(localUser, "galax.channel.latenight.chat"); + auto databaseCreateResponse = database.create(localUser, "latenight"); + /* const char *data = "hello, world!"; database.add(localUser, "galax.channel.latenight.chat", DataView{ (void*)data, strlen(data) }); database.commit(); auto start = chrono::high_resolution_clock::now(); - while(chrono::high_resolution_clock::now() - start < 5s) + while(chrono::high_resolution_clock::now() - start < 3s) + { + this_thread::sleep_for(10ms); + } + */ + database.seed(databaseCreateResponse->getRequestHash(), databaseCreateResponse->getNodeEncryptionKey()); + auto start = chrono::high_resolution_clock::now(); + while(chrono::high_resolution_clock::now() - start < 3s) { this_thread::sleep_for(10ms); } -- cgit v1.2.3