#include "assert.hpp" #include "../include/Database.hpp" #include "../include/Group.hpp" #include "../include/LocalUser.hpp" #include "../include/Encryption.hpp" #include "../include/Hash.hpp" #include #include #include using namespace std; using namespace chrono_literals; using namespace odhtdb; void testHash() { Hash hash("odhtdb", 6); assertEquals("a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f", hash.toString()); printf("hash of 'odhtdb' is: a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f\n"); } void testSignData(LocalUser *localUser) { std::string publicKeyStr = localUser->getPublicKey().toString(); printf("Local user public key: %s\n", publicKeyStr.c_str()); std::string privateKeyStr = localUser->getPrivateKey().toString(); printf("Local user private key: %s\n", privateKeyStr.c_str()); string expectedUnsignedData = "hello, world!"; string signedData = localUser->getPrivateKey().sign(DataView((void*)expectedUnsignedData.data(), expectedUnsignedData.size())); assertEquals(SIGNED_HASH_SIZE + expectedUnsignedData.size(), signedData.size()); string unsignedData = localUser->getPublicKey().unsign(DataView((void*)signedData.data(), signedData.size())); assertEquals(expectedUnsignedData, unsignedData); try { Signature::KeyPair anotherKeyPair; anotherKeyPair.getPublicKey().unsign(DataView((void*)signedData.data(), signedData.size())); fail("Expected unsign to fail since the data was not signed with the private key matching given public key"); } catch (UnsignWrongKeyException &e) { } catch (exception &e) { string errMsg = "Expected unsign to fail with UnsignWrongKeyException, fail reason: "; errMsg += e.what(); fail(errMsg); } try { Signature::KeyPair anotherKeyPair; anotherKeyPair.getPublicKey().unsign(DataView((void*)signedData.data(), 3)); fail("Expected unsign to fail since the (signed) data is too small to have been signed (signed hash is 64 bytes)"); } catch (UnsignInvalidSizeException &e) { } catch (exception &e) { string errMsg = "Expected unsign to fail with UnsignInvalidSizeException, fail reason: "; errMsg += e.what(); fail(errMsg); } } void testEncryption() { const char *message = "hello, world!"; const unsigned long long messageLength = 13; Encryption encryption(DataView((void*)message, messageLength)); Decryption decryption(encryption.getCipherText(), encryption.getNonce(), encryption.getKey()); assertEquals(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(); testHash(); // TODO: Setup local bootstrap node for tests Database database("bootstrap.ring.cx", 4222, "storage"); auto databaseCreateResponse = database.create("dec05eba", "latenight"); const char *data = "hello, world!"; database.add(databaseCreateResponse, DataView{ (void*)data, strlen(data) }); database.commit(); 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); } return 0; }