aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-05-14 00:20:11 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-05-14 00:27:29 +0200
commit9af086151e6d9d3fe88f9e3e21797812a3e701ba (patch)
treea11889f81b8f929c11dccbd2c1c3b3cd74fbb740 /tests
parentebff7aeafded4dd9d245dbcfc80d9c8d83fe1242 (diff)
Replace files with sqlite
Using sqlite because sqlite has transactions, storing/loading from files automatically, unloading data that is not accessed often. Removed cosmetic data (node name, username). They can be added using addData by the application that uses odhtdb instead. Database callback functions can now be called with stored data using database.loadNode function. TODO: Add local user storage (with password) back, it has been temorary disabled
Diffstat (limited to 'tests')
-rw-r--r--tests/assert.hpp12
-rw-r--r--tests/main.cpp86
2 files changed, 62 insertions, 36 deletions
diff --git a/tests/assert.hpp b/tests/assert.hpp
index 86f74f2..03157c6 100644
--- a/tests/assert.hpp
+++ b/tests/assert.hpp
@@ -2,19 +2,23 @@
#include <cstdlib>
#include <iostream>
+#include <sstream>
+#include <stdexcept>
template <typename T>
static void assertEquals(const T &expected, const T &actual)
{
if(expected != actual)
{
- std::cerr << "Assertion failed!\nExpected: " << expected << ", actual: " << actual << std::endl;
- exit(1);
+ std::stringstream ss;
+ ss << "Assertion failed!\nExpected: " << expected << ", actual: " << actual << std::endl;
+ throw std::runtime_error(ss.str());
}
}
static void fail(const std::string &errMsg)
{
- fprintf(stderr, "Fail:\n%.*s\n", errMsg.size(), errMsg.c_str());
- exit(1);
+ std::stringstream ss;
+ ss << "Fail:\n" << errMsg << std::endl;
+ throw std::runtime_error(ss.str());
}
diff --git a/tests/main.cpp b/tests/main.cpp
index 8f19838..6e1a72f 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -12,6 +12,7 @@
#include <chrono>
#include <thread>
#include <opendht.h>
+#include <boost/filesystem.hpp>
using namespace std;
using namespace chrono_literals;
@@ -144,59 +145,80 @@ void testTimestamp(const Database &database)
int main()
{
Log::debug("Starting tests...");
+ boost::filesystem::path storagePath("/tmp/odhtdbTest");
+ boost::filesystem::remove_all(storagePath);
+ boost::filesystem::create_directory(storagePath);
+
testCachedIdentity();
testBinHexConvert();
testHash();
testEncryption();
- LocalUser *localUser = LocalUser::create(Signature::KeyPair(), "dec05eba", nullptr);
- testSignData(localUser);
-
- // TODO: Setup local bootstrap node for tests
- Database database("bootstrap.ring.cx", 4222, "storage");
-
- testTimestamp(database);
-
int createNodeCounter = 0;
int addDataCounter = 0;
int addUserCounter = 0;
- database.setOnCreateNodeCallback([&createNodeCounter](const DatabaseCreateNodeRequest &request)
+ auto createNodeCallback = [&createNodeCounter](const DatabaseCreateNodeRequest &request)
{
Log::debug("Create node callback");
++createNodeCounter;
- });
-
- database.setOnAddNodeCallback([&addDataCounter](const DatabaseAddNodeRequest &request)
+ };
+
+ auto addNodeCallback = [&addDataCounter](const DatabaseAddNodeRequest &request)
{
Log::debug("Add node callback");
++addDataCounter;
- });
-
- database.setOnAddUserCallback([&addUserCounter](const DatabaseAddUserRequest &request)
+ };
+
+ auto addUserCallback = [&addUserCounter](const DatabaseAddUserRequest &request)
{
Log::debug("Add user callback");
++addUserCounter;
- });
+ };
- auto databaseCreateResponse = database.create("adminUserName", "latenight");
- DatabaseNode databaseNode(databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash());
- auto adminUser = (LocalUser*)databaseCreateResponse->getNodeAdminUser();
- database.addData(databaseNode, adminUser, DataView{ (void*)"hello, world!", 13 });
- database.addUser(databaseNode, adminUser, localUser->getName(), localUser->getPublicKey(), adminUser->getGroups()[0]);
- localUser->addToGroup(adminUser->getGroups()[0]);
- database.addData(databaseNode, localUser, DataView{ (void*)"hello, aaald!", 13 });
-
- database.seed(databaseNode);
- auto start = chrono::high_resolution_clock::now();
- while(chrono::high_resolution_clock::now() - start < 3s)
+ DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback };
+ DatabaseNode databaseNode;
+
{
- this_thread::sleep_for(10ms);
+ LocalUser *localUser = LocalUser::create(Signature::KeyPair(), nullptr);
+ testSignData(localUser);
+
+ // TODO: Setup local bootstrap node for tests
+ Database database("bootstrap.ring.cx", 4222, storagePath, callbackFuncs);
+ testTimestamp(database);
+
+ auto databaseCreateResponse = database.create();
+ databaseNode = { databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash() };
+ auto adminUser = (LocalUser*)databaseCreateResponse->getNodeAdminUser();
+ database.addData(databaseNode, adminUser, DataView{ (void*)"hello, world!", 13 });
+ database.addUser(databaseNode, adminUser, localUser->getPublicKey(), adminUser->getGroups()[0]);
+ localUser->addToGroup(adminUser->getGroups()[0]);
+ database.addData(databaseNode, localUser, DataView{ (void*)"hello, aaald!", 13 });
+
+ database.seed(databaseNode);
+ this_thread::sleep_for(chrono::seconds(3));
+
+ assertEquals(1, createNodeCounter);
+ assertEquals(2, addDataCounter);
+ assertEquals(1, addUserCounter);
+ }
+ Log::debug("Callback works when adding data while connected, now testing to reconnect and check if data remains...");
+ {
+ createNodeCounter = 0;
+ addDataCounter = 0;
+ addUserCounter = 0;
+
+ // TODO: Setup local bootstrap node for tests
+ Database database("bootstrap.ring.cx", 4222, storagePath, callbackFuncs);
+ database.loadNode(*databaseNode.getRequestHash());
+
+ database.seed(databaseNode);
+ this_thread::sleep_for(chrono::seconds(3));
+
+ assertEquals(1, createNodeCounter);
+ assertEquals(2, addDataCounter);
+ assertEquals(1, addUserCounter);
}
-
- assertEquals(1, createNodeCounter);
- assertEquals(2, addDataCounter);
- assertEquals(1, addUserCounter);
return 0;
}