From 22abf1aa83b2668f918556491a55947be798f89f Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Sun, 21 Oct 2018 14:31:28 +0200 Subject: Async connect --- tests/main.cpp | 75 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) (limited to 'tests') diff --git a/tests/main.cpp b/tests/main.cpp index 988309c..760e7a9 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -112,11 +112,11 @@ static void testEncryption() assertEquals(0, strncmp(message, (const char*)decryption.getDecryptedText().data, messageLength)); } -static void testTimestamp(const Database &database) +static void testTimestamp(const std::unique_ptr &database) { - auto timestamp1 = database.getSyncedTimestampUtc(); + auto timestamp1 = database->getSyncedTimestampUtc(); this_thread::sleep_for(chrono::milliseconds(100)); - auto timestamp2 = database.getSyncedTimestampUtc(); + auto timestamp2 = database->getSyncedTimestampUtc(); if(timestamp2.getCombined() > timestamp1.getCombined()) Log::debug("Second timestamp is more than first one, as expected"); @@ -163,15 +163,15 @@ static void testStandard() Signature::KeyPair localUserKeyPair; testSignData(localUserKeyPair); - Database database("127.0.0.1", PORT, storagePath, callbackFuncs); + std::unique_ptr database = Database::connect("127.0.0.1", PORT, storagePath, callbackFuncs).get(); testTimestamp(database); - auto databaseCreateResponse = database.create(); + auto databaseCreateResponse = database->create(); databaseNode = { databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash() }; auto adminUserKey = databaseCreateResponse->getNodeAdminKeyPair(); - database.addData(databaseNode, *adminUserKey, DataView{ (void*)"hello, world!", 13 }); - database.addUser(databaseNode, *adminUserKey, localUserKeyPair.getPublicKey(), databaseCreateResponse->getNodeAdminGroupId()->getView()); - database.addData(databaseNode, localUserKeyPair, DataView{ (void*)"hello, aaald!", 13 }); + database->addData(databaseNode, *adminUserKey, DataView{ (void*)"hello, world!", 13 }); + database->addUser(databaseNode, *adminUserKey, localUserKeyPair.getPublicKey(), databaseCreateResponse->getNodeAdminGroupId()->getView()); + database->addData(databaseNode, localUserKeyPair, DataView{ (void*)"hello, aaald!", 13 }); this_thread::sleep_for(chrono::seconds(3)); assertEquals(1, createNodeCounter); @@ -180,13 +180,13 @@ static void testStandard() string username = "dec05eba"; string password = "secretPassword"; - if(database.doesStoredUserExist(username)) + if(database->doesStoredUserExist(username)) fail("Expected stored to not exist until it has been added"); - database.storeNodeInfoForUserEncrypted(databaseNode, username, password, *adminUserKey); + database->storeNodeInfoForUserEncrypted(databaseNode, username, password, *adminUserKey); try { - database.storeNodeInfoForUserEncrypted(databaseNode, username, password, localUserKeyPair); + database->storeNodeInfoForUserEncrypted(databaseNode, username, password, localUserKeyPair); fail("Expected store user password to fail since we have already stored an user in the node"); } catch(SqlExecException &e) @@ -194,10 +194,10 @@ static void testStandard() Log::debug("Failed with sql exception as expected, since we already have an user in the node: %s", e.what()); } - if(!database.doesStoredUserExist(username)) + if(!database->doesStoredUserExist(username)) fail("Expected stored to exist after it has been added"); - auto nodeUserData = database.getStoredNodeUserInfoDecrypted(username, password); + auto nodeUserData = database->getStoredNodeUserInfoDecrypted(username, password); assertEquals((size_t)1, nodeUserData.size()); auto userDataIt = nodeUserData.find(*databaseNode.getRequestHash()); if(userDataIt == nodeUserData.end()) @@ -211,7 +211,7 @@ static void testStandard() try { - database.storeUserWithoutNodes(username, password); + database->storeUserWithoutNodes(username, password); fail("Expected store user to fail since the user already exists in database"); } catch(SqlExecException &e) @@ -219,17 +219,20 @@ static void testStandard() } - database.storeUserWithoutNodes("anotherUser", password); - if(!database.doesStoredUserExist("anotherUser")) + database->storeUserWithoutNodes("anotherUser", password); + if(!database->doesStoredUserExist("anotherUser")) fail("Added user 'anotherUser' to database without any nodes, but it doesn't seem to be stored"); - auto adminUserGroups = database.getUserGroups(*databaseNode.getRequestHash(), adminUserKey->getPublicKey()); + auto adminUserGroups = database->getUserGroups(*databaseNode.getRequestHash(), adminUserKey->getPublicKey()); if(adminUserGroups.size() != 1 || adminUserGroups[0].getView() != databaseCreateResponse->getNodeAdminGroupId()->getView()) fail("Admin group doesn't match group stored in database"); - auto userGroups = database.getUserGroups(*databaseNode.getRequestHash(), localUserKeyPair.getPublicKey()); + auto userGroups = database->getUserGroups(*databaseNode.getRequestHash(), localUserKeyPair.getPublicKey()); if(userGroups.size() != 1 || userGroups[0].getView() != databaseCreateResponse->getNodeAdminGroupId()->getView()) fail("User group doesn't match group stored in database"); + + // Give time for us to disconnect + std::this_thread::sleep_for(std::chrono::seconds(5)); } Log::debug("Callback works when adding data while connected, now testing to reconnect and check if data remains..."); { @@ -237,10 +240,10 @@ static void testStandard() addDataCounter = 0; addUserCounter = 0; - Database database("127.0.0.1", PORT, storagePath, callbackFuncs); - database.loadNode(*databaseNode.getRequestHash()); + std::unique_ptr database = Database::connect("127.0.0.1", PORT, storagePath, callbackFuncs).get(); + database->loadNode(*databaseNode.getRequestHash()); - database.seed(databaseNode, DatabaseFetchOrder::OLDEST_FIRST); + database->seed(databaseNode, DatabaseFetchOrder::OLDEST_FIRST); this_thread::sleep_for(chrono::seconds(3)); assertEquals(1, createNodeCounter); @@ -252,7 +255,7 @@ static void testStandard() messageToSendSerializer.add((u32)10); u32 receivedNumber = 0; - database.receiveCustomMessage(customMessageKey, [&receivedNumber](const void *data, usize size) + database->receiveCustomMessage(customMessageKey, [&receivedNumber](const void *data, usize size) { sibs::SafeDeserializer deserializer((const u8*)data, size); receivedNumber = deserializer.extract(); @@ -262,7 +265,7 @@ static void testStandard() }); u32 sendCustomMessageResponseNumber = 0; - database.sendCustomMessage(customMessageKey, messageToSendSerializer.getBuffer().data(), messageToSendSerializer.getBuffer().size(), [&sendCustomMessageResponseNumber](bool gotResponse, const void *data, usize size) + database->sendCustomMessage(customMessageKey, messageToSendSerializer.getBuffer().data(), messageToSendSerializer.getBuffer().size(), [&sendCustomMessageResponseNumber](bool gotResponse, const void *data, usize size) { if(!gotResponse) { @@ -309,14 +312,14 @@ static void testTwoLocalNodes() DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback }; - Database database1("127.0.0.1", PORT, storagePath1, callbackFuncs); - auto databaseCreateResponse = database1.create(); + std::unique_ptr database1 = Database::connect("127.0.0.1", PORT, storagePath1, callbackFuncs).get(); + auto databaseCreateResponse = database1->create(); DatabaseNode databaseNode = { databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash() }; auto adminUserKey = databaseCreateResponse->getNodeAdminKeyPair(); - database1.addData(databaseNode, *adminUserKey, DataView{ (void*)"hello, world!", 13 }); + database1->addData(databaseNode, *adminUserKey, DataView{ (void*)"hello, world!", 13 }); - Database database2("127.0.0.1", PORT, storagePath2, callbackFuncs); - database2.seed(databaseNode); + std::unique_ptr database2 = Database::connect("127.0.0.1", PORT, storagePath2, callbackFuncs).get(); + database2->seed(databaseNode); this_thread::sleep_for(chrono::seconds(5)); } @@ -344,8 +347,8 @@ static void testMemoryUsage() DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback }; - Database database("127.0.0.1", PORT, storagePath, callbackFuncs); - auto databaseCreateResponse = database.create(); + std::unique_ptr database = Database::connect("127.0.0.1", PORT, storagePath, callbackFuncs).get(); + auto databaseCreateResponse = database->create(); DatabaseNode databaseNode = { databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash() }; auto adminUserKey = databaseCreateResponse->getNodeAdminKeyPair(); @@ -356,7 +359,7 @@ static void testMemoryUsage() for(int i = 0; i < iterations; ++i) { Log::debug("Memory usage test %d/%d", 1 + i, iterations); - database.addData(databaseNode, *adminUserKey, DataView{ (void*)msg, msgLength }); + database->addData(databaseNode, *adminUserKey, DataView{ (void*)msg, msgLength }); this_thread::sleep_for(chrono::milliseconds(250)); } } @@ -400,22 +403,22 @@ static void testStoreAccount() { DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback }; - Database database("127.0.0.1", PORT, storagePath, callbackFuncs); + std::unique_ptr database = Database::connect("127.0.0.1", PORT, storagePath, callbackFuncs).get(); std::string username = "username"; std::string password = "password"; - database.storeUserWithoutNodes(username, password); - database.getStoredNodeUserInfoDecrypted(username, password); + database->storeUserWithoutNodes(username, password); + database->getStoredNodeUserInfoDecrypted(username, password); } { DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback }; - Database database("127.0.0.1", PORT, storagePath, callbackFuncs); + std::unique_ptr database = Database::connect("127.0.0.1", PORT, storagePath, callbackFuncs).get(); std::string username = "username"; std::string password = "password"; - database.getStoredNodeUserInfoDecrypted(username, password); + database->getStoredNodeUserInfoDecrypted(username, password); } } -- cgit v1.2.3