aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-03-05 22:45:56 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commiteda9a7bbefc5587bf1ff895a9214f450e64575fa (patch)
tree0f968fb7373a29cf116b4b6d473a966e28e62825 /tests
parent33e823ddddddd4a13b1a05b90ae5b419b89bcb1d (diff)
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/assert.hpp20
-rw-r--r--tests/main.cpp48
2 files changed, 45 insertions, 23 deletions
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 <cstdlib>
+#include <iostream>
+
+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);
+ }
+}
+
+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 <vector>
#include <chrono>
#include <thread>
@@ -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<string>("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<unsigned long long>(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);
}