1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
#include "assert.hpp"
#include "../include/odhtdb/Log.hpp"
#include "../include/odhtdb/Database.hpp"
#include "../include/odhtdb/Group.hpp"
#include "../include/odhtdb/Encryption.hpp"
#include "../include/odhtdb/Hash.hpp"
#include "../include/odhtdb/hex2bin.hpp"
#include "../include/odhtdb/bin2hex.hpp"
#include "../include/odhtdb/DataView.hpp"
#include <vector>
#include <chrono>
#include <thread>
#include <opendht.h>
#include <boost/filesystem.hpp>
#include <map>
using namespace std;
using namespace chrono_literals;
using namespace odhtdb;
void testBinHexConvert()
{
DataView input { (void*)"hello", 5 };
string inputHex = bin2hex((const char*)input.data, input.size);
assertEquals<string>("68656c6c6f", inputHex);
string inputBin = hex2bin(inputHex.c_str(), inputHex.size());
if(inputBin.size() != input.size)
{
string errMsg = "Expected input converted to hex and then back to binary size to be 5, was: ";
errMsg += to_string(inputBin.size());
fail(errMsg);
}
if(memcmp(input.data, inputBin.data(), input.size) != 0)
{
string errMsg = "Expected input converted to hex and then back to binary to be same as original input ('hello'), was: ";
errMsg += string(inputBin.c_str(), inputBin.size());
fail(errMsg);
}
}
void testHash()
{
Hash hash("odhtdb", 6);
string hashHex = hash.toString();
assertEquals<string>("a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f", hashHex);
Log::debug("hash of 'odhtdb' is: a7b30ec8ab92de60e551b26bb8f78d315697f84dd7f5549a143477e095ec934f");
}
void testSignData(const Signature::KeyPair &localUserKeyPair)
{
std::string publicKeyStr = localUserKeyPair.getPublicKey().toString();
Log::debug("Local user public key: %s", publicKeyStr.c_str());
std::string privateKeyStr = localUserKeyPair.getPrivateKey().toString();
Log::debug("Local user private key: %s", privateKeyStr.c_str());
string expectedUnsignedData = "hello, world!";
string signedData = localUserKeyPair.getPrivateKey().sign(DataView((void*)expectedUnsignedData.data(), expectedUnsignedData.size()));
assertEquals(SIGNED_HASH_SIZE + expectedUnsignedData.size(), signedData.size());
string unsignedData = localUserKeyPair.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<unsigned long long>(messageLength, decryption.getDecryptedText().size);
assertEquals(0, strncmp(message, (const char*)decryption.getDecryptedText().data, messageLength));
}
void testCachedIdentity()
{
pair<shared_ptr<dht::crypto::PrivateKey>, shared_ptr<dht::crypto::Certificate>> 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)
{
auto timestamp1 = database.getSyncedTimestampUtc();
this_thread::sleep_for(chrono::milliseconds(100));
auto timestamp2 = database.getSyncedTimestampUtc();
if(timestamp2.getCombined() > timestamp1.getCombined())
Log::debug("Second timestamp is more than first one, as expected");
else
fail("Second timestamp is not more than first one for some reason");
}
void testStandard()
{
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;
auto createNodeCallback = [&createNodeCounter](const DatabaseCreateNodeRequest &request)
{
Log::debug("Create node callback");
++createNodeCounter;
};
auto addNodeCallback = [&addDataCounter](const DatabaseAddNodeRequest &request)
{
Log::debug("Add node callback");
++addDataCounter;
};
auto addUserCallback = [&addUserCounter](const DatabaseAddUserRequest &request)
{
Log::debug("Add user callback");
++addUserCounter;
};
DatabaseCallbackFuncs callbackFuncs { createNodeCallback, addNodeCallback, addUserCallback };
DatabaseNode databaseNode;
{
Signature::KeyPair localUserKeyPair;
testSignData(localUserKeyPair);
// TODO: Setup local bootstrap node for tests
Database database("bootstrap.ring.cx", 4222, storagePath, callbackFuncs);
testTimestamp(database);
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.seed(databaseNode, DatabaseFetchOrder::OLDEST_FIRST);
this_thread::sleep_for(chrono::seconds(3));
assertEquals(1, createNodeCounter);
assertEquals(2, addDataCounter);
assertEquals(1, addUserCounter);
string username = "dec05eba";
string password = "secretPassword";
if(database.doesStoredUserExist(username))
fail("Expected stored to not exist until it has been added");
database.storeNodeInfoForUserEncrypted(databaseNode, username, password, *adminUserKey);
try
{
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)
{
Log::debug("Failed with sql exception as expected, since we already have an user in the node: %s", e.what());
}
if(!database.doesStoredUserExist(username))
fail("Expected stored to exist after it has been added");
auto nodeUserData = database.getStoredNodeUserInfoDecrypted(username, password);
assertEquals((size_t)1, nodeUserData.size());
auto userDataIt = nodeUserData.find(*databaseNode.getRequestHash());
if(userDataIt == nodeUserData.end())
fail("Expected stored node hash to match node hash");
if(userDataIt->second.userKeyPair->getPublicKey() != adminUserKey->getPublicKey())
fail("Expected stored public key to match admin user public key");
if(userDataIt->second.userKeyPair->getPrivateKey() != adminUserKey->getPrivateKey())
fail("Expected stored private key to match admin user private key");
if(userDataIt->second.nodeEncryptionKey->getView() != databaseCreateResponse->getNodeEncryptionKey()->getView())
fail("Expected stored node encryption key to match node encryption key");
try
{
database.storeUserWithoutNodes(username, password);
fail("Expected store user to fail since the user already exists in database");
}
catch(SqlExecException &e)
{
}
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());
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());
if(userGroups.size() != 1 || userGroups[0].getView() != databaseCreateResponse->getNodeAdminGroupId()->getView())
fail("User group doesn't match group stored in database");
}
Log::debug("Callback works when adding data while connected, now testing to reconnect and check if data remains...");
{
createNodeCounter = 0;
addDataCounter = 0;
addUserCounter = 0;
// TODO: Setup local bootstrap node for tests
Database database("bootstrap.ring.cx", 4222, storagePath, callbackFuncs);
database.loadNode(*databaseNode.getRequestHash());
database.seed(databaseNode, DatabaseFetchOrder::OLDEST_FIRST);
this_thread::sleep_for(chrono::seconds(3));
assertEquals(1, createNodeCounter);
assertEquals(2, addDataCounter);
assertEquals(1, addUserCounter);
dht::InfoHash customMessageKey = Database::getInfoHash("asdf", 4);
sibs::SafeSerializer messageToSendSerializer;
messageToSendSerializer.add((u32)10);
u32 receivedNumber = 0;
database.receiveCustomMessage(customMessageKey, [&receivedNumber](const void *data, usize size)
{
sibs::SafeDeserializer deserializer((const u8*)data, size);
receivedNumber = deserializer.extract<u32>();
sibs::SafeSerializer serializer;
serializer.add((u32)20);
return serializer;
});
u32 sendCustomMessageResponseNumber = 0;
database.sendCustomMessage(customMessageKey, move(messageToSendSerializer.getBuffer()), [&sendCustomMessageResponseNumber](bool gotResponse, const void *data, usize size)
{
if(!gotResponse)
{
Log::error("Didn't get reponse!");
return true;
}
sibs::SafeDeserializer deserializer((const u8*)data, size);
sendCustomMessageResponseNumber = deserializer.extract<u32>();
return false;
});
this_thread::sleep_for(chrono::seconds(3));
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);
}
void testMemoryUsage()
{
boost::filesystem::path storagePath("/tmp/odhtdbTestMemoryUsage");
boost::filesystem::remove_all(storagePath);
boost::filesystem::create_directory(storagePath);
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 database("bootstrap.ring.cx", 4222, 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);
const int iterations = 500;
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 });
this_thread::sleep_for(chrono::milliseconds(250));
}
}
pair<bool, shared_ptr<OwnedByteArray>> __attribute__((optimize("O0"))) getDataNoCopy()
{
u8 *decryptionKeyRawCopy = new u8[1024 * 1024 * 64];
memcpy(decryptionKeyRawCopy, "hello, world!", 14);
shared_ptr<OwnedByteArray> decryptionKey = make_shared<OwnedByteArray>(decryptionKeyRawCopy, ENCRYPTION_KEY_BYTE_SIZE);
return make_pair(true, decryptionKey);
}
void __attribute__((optimize("O0"))) testMemoryLeak()
{
{
auto data = getDataNoCopy();
printf("data: %s\n", data.second->data);
}
}
struct Test
{
function<void()> testFunc;
bool optional;
};
int main(int argc, char **argv)
{
map<string, Test> testByName;
testByName["standard"] = { testStandard, false };
testByName["two_local_nodes"] = { testTwoLocalNodes, false };
testByName["memory_usage"] = { testMemoryUsage, true };
testByName["test_memory_leak"] = { testMemoryLeak, true };
const char *testName = "all";
if(argc > 1)
testName = argv[1];
if(strcmp(testName, "all") == 0)
{
Log::debug("Running all non-optional tests");
for(auto &testIt : testByName)
{
if(!testIt.second.optional)
{
Log::debug("Running test: %s", testIt.first.c_str());
testIt.second.testFunc();
}
}
}
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.testFunc();
}
return 0;
}
|