aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-05-18 04:21:49 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-05-18 04:21:57 +0200
commit5a48f2a4a5cf10c69e75e304a23ed2ca97582a1f (patch)
tree2c7b61739393234039325bc18244e2fec5884362 /tests
parent1fdbbfd98b91cd36374cdac9500bd73e33e54b9b (diff)
Resend data if it fails
Diffstat (limited to 'tests')
-rw-r--r--tests/main.cpp90
1 files changed, 80 insertions, 10 deletions
diff --git a/tests/main.cpp b/tests/main.cpp
index 5d527fe..b5ce639 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -12,6 +12,7 @@
#include <thread>
#include <opendht.h>
#include <boost/filesystem.hpp>
+#include <map>
using namespace std;
using namespace chrono_literals;
@@ -113,20 +114,20 @@ void testCachedIdentity()
{
pair<shared_ptr<dht::crypto::PrivateKey>, shared_ptr<dht::crypto::Certificate>> identity = dht::crypto::generateIdentity();
dht::Blob privateKeyData = identity.first->serialize();
- printf("Private key size: %d, serialized data: %s\n", privateKeyData.size(), Hash(privateKeyData.data(), privateKeyData.size()).toString().c_str());
+ 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();
- printf("Private key size: %d, serialized data: %s\n", privateKeyData.size(), Hash(privateKeyData.data(), privateKeyData.size()).toString().c_str());
+ 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);
- printf("Certificate data size: %d, serialized data: %s\n", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str());
+ 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);
- printf("Certificate data size: %d, serialized data: %s\n", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str());
+ Log::debug("Certificate data size: %d, serialized data: %s", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str());
}
void testTimestamp(const Database &database)
@@ -141,18 +142,17 @@ void testTimestamp(const Database &database)
fail("Second timestamp is not more than first one for some reason");
}
-int main()
+void testStandard()
{
- Log::debug("Starting tests...");
- boost::filesystem::path storagePath("/tmp/odhtdbTest");
- boost::filesystem::remove_all(storagePath);
- boost::filesystem::create_directory(storagePath);
-
testCachedIdentity();
testBinHexConvert();
testHash();
testEncryption();
+ boost::filesystem::path storagePath("/tmp/odhtdbTest");
+ boost::filesystem::remove_all(storagePath);
+ boost::filesystem::create_directory(storagePath);
+
int createNodeCounter = 0;
int addDataCounter = 0;
int addUserCounter = 0;
@@ -303,6 +303,76 @@ int main()
assertEquals((u32)10, receivedNumber);
assertEquals((u32)20, sendCustomMessageResponseNumber);
}
+}
+
+void testTwoLocalNodes()
+{
+ boost::filesystem::path storagePath1("/tmp/odhtdbTest1");
+ boost::filesystem::remove_all(storagePath1);
+ boost::filesystem::create_directory(storagePath1);
+
+ boost::filesystem::path storagePath2("/tmp/odhtdbTest2");
+ boost::filesystem::remove_all(storagePath2);
+ boost::filesystem::create_directory(storagePath2);
+
+ auto createNodeCallback = [](const DatabaseCreateNodeRequest &request)
+ {
+ Log::debug("Create node callback");
+ };
+
+ auto addNodeCallback = [](const DatabaseAddNodeRequest &request)
+ {
+ Log::debug("Add node callback");
+ };
+
+ auto addUserCallback = [](const DatabaseAddUserRequest &request)
+ {
+ Log::debug("Add user callback");
+ };
+
+ DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback };
+
+ Database database1("bootstrap.ring.cx", 4222, 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);
+ database2.seed(databaseNode);
+}
+
+int main(int argc, char **argv)
+{
+ map<string, function<void()>> testByName;
+ testByName["standard"] = testStandard;
+ testByName["two_local_nodes"] = testTwoLocalNodes;
+
+ const char *testName = "all";
+ if(argc > 1)
+ testName = argv[1];
+
+ if(strcmp(testName, "all") == 0)
+ {
+ for(auto &testIt : testByName)
+ {
+ Log::debug("Running test: %s", testIt.first.c_str());
+ testIt.second();
+ }
+ }
+ else
+ {
+ auto testIt = testByName.find(testName);
+ if(testIt == testByName.end())
+ {
+ Log::error("There is no test called %s", testName);
+ exit(1);
+ }
+
+ Log::debug("Running test: %s", testIt->first.c_str());
+ testIt->second();
+ }
return 0;
}