aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-02-14 22:18:48 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:12 +0200
commit40d94ad83f74753b71f33b58be8664bb21200219 (patch)
tree7ebe59b3e9a8872cf4c3c49f14f564827810a1e3 /tests
parent7d9b97b437252df8a481816d50b0fa8587fa69c9 (diff)
Sign messages/verify message signatures
Diffstat (limited to 'tests')
-rw-r--r--tests/main.cpp60
1 files changed, 53 insertions, 7 deletions
diff --git a/tests/main.cpp b/tests/main.cpp
index 8818ff9..1940c1a 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -9,15 +9,63 @@ using namespace std;
using namespace chrono_literals;
using namespace odhtdb;
-int main()
+#define assertEquals(a, b) do { if((a) != (b)) { fprintf(stderr, "Assert failed:\n%s != %s\n", #a, #b); exit(1); } } while(0)
+void fail(const string &errMsg) { fprintf(stderr, "Fail:\n%.*s\n", errMsg.size(), errMsg.c_str()); }
+
+void testSignData(LocalUser *localUser)
{
- LocalUser *localUser = LocalUser::create(Signature::KeyPair(), "dec05eba");
-
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);
+ }
+}
+
+int main()
+{
+ LocalUser *localUser = LocalUser::create(Signature::KeyPair(), "dec05eba");
+ testSignData(localUser);
// TODO: For tests, dont run against bootstrap.ring.cx.
// Run against a bootstrap node made only for testing which doesn't persist added data.
@@ -25,12 +73,10 @@ int main()
Database database("bootstrap.ring.cx", 4222, "storage");
database.seed();
- Group group("admin");
- group.addUser(localUser);
- database.create("galax.channel.latenight.chat", &group);
+ database.create(localUser, "galax.channel.latenight.chat");
const char *data = "hello, world!";
- database.add("galax.channel.latenight.chat", DataView{ (void*)data, strlen(data) }, localUser);
+ 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)