Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlSchwan committed Jan 29, 2021
1 parent 81e2a26 commit af97ef4
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 32 deletions.
53 changes: 53 additions & 0 deletions .ci/adjust-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

CMD=""

$CMD perl -pi -w -e \
's/rc_messages_per_second.*/rc_messages_per_second: 1000/g;' data/homeserver.yaml
$CMD perl -pi -w -e \
's/rc_message_burst_count.*/rc_message_burst_count: 10000/g;' data/homeserver.yaml

(
cat <<HEREDOC
rc_message:
per_second: 10000
burst_count: 100000
rc_registration:
per_second: 10000
burst_count: 30000
rc_login:
address:
per_second: 10000
burst_count: 30000
account:
per_second: 10000
burst_count: 30000
failed_attempts:
per_second: 10000
burst_count: 30000
rc_admin_redaction:
per_second: 1000
burst_count: 5000
rc_joins:
local:
per_second: 10000
burst_count: 100000
remote:
per_second: 10000
burst_count: 100000
HEREDOC
) | $CMD tee -a data/homeserver.yaml

$CMD perl -pi -w -e \
's/#enable_registration: false/enable_registration: true/g;' data/homeserver.yaml
$CMD perl -pi -w -e \
's/tls: false/tls: true/g;' data/homeserver.yaml
$CMD perl -pi -w -e \
's/#tls_certificate_path:/tls_certificate_path:/g;' data/homeserver.yaml
$CMD perl -pi -w -e \
's/#tls_private_key_path:/tls_private_key_path:/g;' data/homeserver.yaml

$CMD openssl req -x509 -newkey rsa:4096 -keyout data/localhost.tls.key -out data/localhost.tls.crt -days 365 -subj '/CN=localhost' -nodes

$CMD chmod 0777 data/localhost.tls.crt
$CMD chmod 0777 data/localhost.tls.key
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
SYNAPSE_IMAGE="matrixdotorg/synapse:v1.24.0"

test: ## Run the tests
@cd build/ && GTEST_COLOR=1 ctest --verbose

synapse: ## Start a synapse instance on docker
@mkdir -p data
@chmod 0777 data
@docker run -v `pwd`/data:/data --rm \
-e SYNAPSE_SERVER_NAME=localhost -e SYNAPSE_REPORT_STATS=no ${SYNAPSE_IMAGE} generate
@./.ci/adjust-config.sh
@docker run -d \
--name synapse \
-p 443:8008 \
-p 8448:8008 \
-p 8008:8008 \
-v `pwd`/data:/data ${SYNAPSE_IMAGE}
@echo Waiting for synapse to start...
@until curl -s -f -k https://localhost:443/_matrix/client/versions; do echo "Checking ..."; sleep 2; done
@echo Register alice
@docker exec synapse /bin/sh -c 'register_new_matrix_user --admin -u alice -p secret -c /data/homeserver.yaml https://localhost:8008'
@echo Register bob
@docker exec synapse /bin/sh -c 'register_new_matrix_user --admin -u bob -p secret -c /data/homeserver.yaml https://localhost:8008'
@echo Register carl
@docker exec synapse /bin/sh -c 'register_new_matrix_user --admin -u carl -p secret -c /data/homeserver.yaml https://localhost:8008'

stop-synapse: ## Stop any running instance of synapse
@rm -rf ./data/*
@docker rm -f synapse 2>&1>/dev/null

restart: stop-synapse synapse
206 changes: 205 additions & 1 deletion autotests/testolmaccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "testolmaccount.h"
#include "crypto/qolmaccount.h"
#include "csapi/definitions/device_keys.h"
#include "connection.h"
#include "events/encryptedfile.h"

using namespace Quotient;
Expand Down Expand Up @@ -162,4 +162,208 @@ void TestOlmAccount::encryptedFile()
QCOMPARE(file.key.keyOps.count(), 2);
QCOMPARE(file.key.kty, "oct");
}

void TestOlmAccount::uploadIdentityKey()
{
auto conn = new Connection();
conn->resolveServer("@alice:localhost:" + QString::number(443));
connect(conn, &Connection::loginFlowsChanged, this, [this, conn]() {
conn->loginWithPassword("alice", "secret", "AlicePhone", "");
connect(conn, &Connection::connected, this, [this, conn] {
auto olmAccount = conn->olmAccount();
auto idKeys = olmAccount->identityKeys();

QVERIFY(idKeys.curve25519.size() > 10);
QVERIFY(idKeys.curve25519.size() > 10);


OneTimeKeys unused;
auto request = olmAccount->createUploadKeyRequest(unused);
connect(request, &BaseJob::result, this, [request, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 0);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
});
connect(conn, &Connection::networkError, [=](QString error, const QString &, int, int) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, [=](QString error, const QString &) {
QFAIL("Login failed");
});
});

connect(conn, &Connection::resolveError, this, [=](QString error) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, this, [=] {
QFAIL("Network error: make sure synapse is running");
});

QSignalSpy spy(conn, &Connection::loginFlowsChanged);
QSignalSpy spy2(conn, &Connection::connected);
QVERIFY(spy.wait(10000));
QVERIFY(spy2.wait(10000));
delete conn;
}

void TestOlmAccount::uploadOneTimeKeys()
{
auto conn = new Connection();
conn->resolveServer("@alice:localhost:" + QString::number(443));
connect(conn, &Connection::loginFlowsChanged, this, [this, conn]() {
conn->loginWithPassword("alice", "secret", "AlicePhone", "");
connect(conn, &Connection::connected, this, [this, conn] {
auto olmAccount = conn->olmAccount();

auto nKeys = olmAccount->generateOneTimeKeys(5);
QCOMPARE(nKeys, 5);

auto oneTimeKeys = olmAccount->oneTimeKeys();

QHash<QString, QVariant> oneTimeKeysHash;
const auto curve = oneTimeKeys.curve25519();
for (const auto &[keyId, key] : asKeyValueRange(curve)) {
oneTimeKeysHash["curve25519:"+keyId] = key;
}
auto request = new UploadKeysJob(none, oneTimeKeysHash);
connect(request, &BaseJob::result, this, [request, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 1);
QCOMPARE(job2->oneTimeKeyCounts()["curve25519"], 5);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
});
connect(conn, &Connection::networkError, [=](QString error, const QString &, int, int) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, [=](QString error, const QString &) {
QFAIL("Login failed");
});
});

connect(conn, &Connection::resolveError, this, [=](QString error) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, this, [=] {
QFAIL("Network error: make sure synapse is running");
});

QSignalSpy spy(conn, &Connection::loginFlowsChanged);
QSignalSpy spy2(conn, &Connection::connected);
QVERIFY(spy.wait(10000));
QVERIFY(spy2.wait(10000));
delete conn;
}

void TestOlmAccount::uploadSignedOneTimeKeys()
{
auto conn = new Connection();
conn->resolveServer("@alice:localhost:" + QString::number(443));
connect(conn, &Connection::loginFlowsChanged, this, [this, conn]() {
conn->loginWithPassword("alice", "secret", "AlicePhone", "");
connect(conn, &Connection::connected, this, [this, conn] {
auto olmAccount = conn->olmAccount();
auto nKeys = olmAccount->generateOneTimeKeys(5);
QCOMPARE(nKeys, 5);

auto oneTimeKeys = olmAccount->oneTimeKeys();
QHash<QString, QVariant> oneTimeKeysHash;
const auto signedKey = olmAccount->signOneTimeKeys(oneTimeKeys);
for (const auto &[keyId, key] : asKeyValueRange(signedKey)) {
QVariant var;
var.setValue(key);
oneTimeKeysHash[keyId] = var;
}
auto request = new UploadKeysJob(none, oneTimeKeysHash);
connect(request, &BaseJob::result, this, [request, nKeys, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 1);
QCOMPARE(job2->oneTimeKeyCounts()["signed_curve25519"], nKeys);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
});
connect(conn, &Connection::networkError, [=](QString error, const QString &, int, int) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, [=](QString error, const QString &) {
QFAIL("Login failed");
});
});

connect(conn, &Connection::resolveError, this, [=](QString error) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, this, [=] {
QFAIL("Network error: make sure synapse is running");
});

QSignalSpy spy(conn, &Connection::loginFlowsChanged);
QSignalSpy spy2(conn, &Connection::connected);
QVERIFY(spy.wait(10000));
QVERIFY(spy2.wait(10000));
delete conn;
}

void TestOlmAccount::uploadKeys()
{
auto conn = new Connection();
conn->resolveServer("@alice:localhost:" + QString::number(443));
connect(conn, &Connection::loginFlowsChanged, this, [this, conn]() {
conn->loginWithPassword("alice", "secret", "AlicePhone", "");
connect(conn, &Connection::connected, this, [this, conn] {
auto olmAccount = conn->olmAccount();
auto idks = olmAccount->identityKeys();
olmAccount->generateOneTimeKeys(1);
auto otks = olmAccount->oneTimeKeys();
auto request = olmAccount->createUploadKeyRequest(otks);
connect(request, &BaseJob::result, this, [request, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 1);
QCOMPARE(job2->oneTimeKeyCounts()["signed_curve25519"], 1);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
});
connect(conn, &Connection::networkError, [=](QString error, const QString &, int, int) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, [=](QString error, const QString &) {
QFAIL("Login failed");
});
});

connect(conn, &Connection::resolveError, this, [=](QString error) {
QFAIL("Network error: make sure synapse is running");
});
connect(conn, &Connection::loginError, this, [=] {
QFAIL("Network error: make sure synapse is running");
});

QSignalSpy spy(conn, &Connection::loginFlowsChanged);
QSignalSpy spy2(conn, &Connection::connected);
QVERIFY(spy.wait(10000));
QVERIFY(spy2.wait(10000));
delete conn;
}

QTEST_MAIN(TestOlmAccount)
4 changes: 4 additions & 0 deletions autotests/testolmaccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ private Q_SLOTS:
//void removeOneTimeKeys();
void deviceKeys();
void encryptedFile();
void uploadIdentityKey();
void uploadOneTimeKeys();
void uploadSignedOneTimeKeys();
void uploadKeys();
};
Loading

0 comments on commit af97ef4

Please sign in to comment.