From 0e627b69d4d0a8d01a21e4dc9bd7be370c0a1245 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 16 Oct 2018 00:38:01 +0200 Subject: Replace opendht with sibs pubsub This should fix issues with memory usage/leaks and make it easier to get peers subscribed to the same key. It will also be easier to modify and also works easier cross platform because of no additional dependencies. --- tests/main.cpp | 73 ++++++++++++++++++++++------------------------------------ 1 file changed, 28 insertions(+), 45 deletions(-) (limited to 'tests') diff --git a/tests/main.cpp b/tests/main.cpp index 17348c4..1541d2a 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -10,15 +10,17 @@ #include #include #include -#include #include #include +#include using namespace std; using namespace chrono_literals; using namespace odhtdb; -void testBinHexConvert() +static const u16 PORT = 22111; + +static void testBinHexConvert() { DataView input { (void*)"hello", 5 }; @@ -41,7 +43,7 @@ void testBinHexConvert() } } -void testHash() +static void testHash() { Hash hash("odhtdb", 6); string hashHex = hash.toString(); @@ -49,7 +51,7 @@ void testHash() Log::debug("hash of 'odhtdb' is: a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f"); } -void testSignData(const Signature::KeyPair &localUserKeyPair) +static void testSignData(const Signature::KeyPair &localUserKeyPair) { std::string publicKeyStr = localUserKeyPair.getPublicKey().toString(); Log::debug("Local user public key: %s", publicKeyStr.c_str()); @@ -99,7 +101,7 @@ void testSignData(const Signature::KeyPair &localUserKeyPair) } } -void testEncryption() +static void testEncryption() { const char *message = "hello, world!"; const unsigned long long messageLength = 13; @@ -110,27 +112,7 @@ void testEncryption() assertEquals(0, strncmp(message, (const char*)decryption.getDecryptedText().data, messageLength)); } -void testCachedIdentity() -{ - pair, shared_ptr> identity = dht::crypto::generateIdentity(); - dht::Blob privateKeyData = identity.first->serialize(); - Log::debug("Private key size: %d, serialized data: %s", privateKeyData.size(), Hash(privateKeyData.data(), privateKeyData.size()).toString().c_str()); - - dht::crypto::PrivateKey privateKeyDeserialized(privateKeyData); - privateKeyData = identity.first->serialize(); - Log::debug("Private key size: %d, serialized data: %s", privateKeyData.size(), Hash(privateKeyData.data(), privateKeyData.size()).toString().c_str()); - - dht::Blob certificateData; - identity.second->pack(certificateData); - Log::debug("Certificate data size: %d, serialized data: %s", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str()); - - dht::crypto::Certificate certificateDeserialized(certificateData); - certificateData.clear(); - identity.second->pack(certificateData); - Log::debug("Certificate data size: %d, serialized data: %s", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str()); -} - -void testTimestamp(const Database &database) +static void testTimestamp(const Database &database) { auto timestamp1 = database.getSyncedTimestampUtc(); this_thread::sleep_for(chrono::milliseconds(100)); @@ -142,9 +124,8 @@ void testTimestamp(const Database &database) fail("Second timestamp is not more than first one for some reason"); } -void testStandard() +static void testStandard() { - testCachedIdentity(); testBinHexConvert(); testHash(); testEncryption(); @@ -182,8 +163,7 @@ void testStandard() Signature::KeyPair localUserKeyPair; testSignData(localUserKeyPair); - // TODO: Setup local bootstrap node for tests - Database database("bootstrap.ring.cx", 4222, storagePath, callbackFuncs); + Database database("127.0.0.1", PORT, storagePath, callbackFuncs); testTimestamp(database); auto databaseCreateResponse = database.create(); @@ -192,8 +172,6 @@ void testStandard() 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.seed(databaseNode, DatabaseFetchOrder::OLDEST_FIRST); this_thread::sleep_for(chrono::seconds(3)); assertEquals(1, createNodeCounter); @@ -259,8 +237,7 @@ void testStandard() addDataCounter = 0; addUserCounter = 0; - // TODO: Setup local bootstrap node for tests - Database database("bootstrap.ring.cx", 4222, storagePath, callbackFuncs); + Database database("127.0.0.1", PORT, storagePath, callbackFuncs); database.loadNode(*databaseNode.getRequestHash()); database.seed(databaseNode, DatabaseFetchOrder::OLDEST_FIRST); @@ -270,7 +247,7 @@ void testStandard() assertEquals(2, addDataCounter); assertEquals(1, addUserCounter); - dht::InfoHash customMessageKey = Database::getInfoHash("asdf", 4); + InfoHash customMessageKey = Database::getInfoHash("asdf", 4); sibs::SafeSerializer messageToSendSerializer; messageToSendSerializer.add((u32)10); @@ -285,7 +262,7 @@ void testStandard() }); u32 sendCustomMessageResponseNumber = 0; - database.sendCustomMessage(customMessageKey, move(messageToSendSerializer.getBuffer()), [&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) { @@ -305,7 +282,7 @@ void testStandard() } } -void testTwoLocalNodes() +static void testTwoLocalNodes() { boost::filesystem::path storagePath1("/tmp/odhtdbTest1"); boost::filesystem::remove_all(storagePath1); @@ -332,18 +309,19 @@ void testTwoLocalNodes() DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback }; - Database database1("bootstrap.ring.cx", 4222, storagePath1, callbackFuncs); + Database database1("127.0.0.1", PORT, storagePath1, callbackFuncs); auto databaseCreateResponse = database1.create(); DatabaseNode databaseNode = { databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash() }; auto adminUserKey = databaseCreateResponse->getNodeAdminKeyPair(); database1.addData(databaseNode, *adminUserKey, DataView{ (void*)"hello, world!", 13 }); - database1.seed(databaseNode); - Database database2("bootstrap.ring.cx", 4223, storagePath2, callbackFuncs); + Database database2("127.0.0.1", PORT, storagePath2, callbackFuncs); database2.seed(databaseNode); + + this_thread::sleep_for(chrono::seconds(5)); } -void testMemoryUsage() +static void testMemoryUsage() { boost::filesystem::path storagePath("/tmp/odhtdbTestMemoryUsage"); boost::filesystem::remove_all(storagePath); @@ -366,11 +344,10 @@ void testMemoryUsage() DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback }; - Database database("bootstrap.ring.cx", 4222, storagePath, callbackFuncs); + Database database("127.0.0.1", PORT, storagePath, callbackFuncs); auto databaseCreateResponse = database.create(); DatabaseNode databaseNode = { databaseCreateResponse->getNodeEncryptionKey(), databaseCreateResponse->getRequestHash() }; auto adminUserKey = databaseCreateResponse->getNodeAdminKeyPair(); - database.seed(databaseNode); const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; const usize msgLength = strlen(msg); @@ -384,7 +361,7 @@ void testMemoryUsage() } } -pair> __attribute__((optimize("O0"))) getDataNoCopy() +static pair> __attribute__((optimize("O0"))) getDataNoCopy() { u8 *decryptionKeyRawCopy = new u8[1024 * 1024 * 64]; memcpy(decryptionKeyRawCopy, "hello, world!", 14); @@ -392,7 +369,7 @@ pair> __attribute__((optimize("O0"))) getDataNo return make_pair(true, decryptionKey); } -void __attribute__((optimize("O0"))) testMemoryLeak() +static void __attribute__((optimize("O0"))) testMemoryLeak() { { auto data = getDataNoCopy(); @@ -408,6 +385,10 @@ struct Test int main(int argc, char **argv) { + sibs::BootstrapNode bootstrapNode(sibs::Ipv4(nullptr, PORT)); + // Wait until bootstrap node is ready to accept connections + this_thread::sleep_for(chrono::seconds(2)); + map testByName; testByName["standard"] = { testStandard, false }; testByName["two_local_nodes"] = { testTwoLocalNodes, false }; @@ -427,6 +408,7 @@ int main(int argc, char **argv) { Log::debug("Running test: %s", testIt.first.c_str()); testIt.second.testFunc(); + Log::debug("Finished test: %s", testIt.first.c_str()); } } } @@ -441,6 +423,7 @@ int main(int argc, char **argv) Log::debug("Running test: %s", testIt->first.c_str()); testIt->second.testFunc(); + Log::debug("Finished test: %s", testIt->first.c_str()); } return 0; -- cgit v1.2.3