Skip to content

Commit

Permalink
Quotest, sendFile(): actually test file contents
Browse files Browse the repository at this point in the history
Also: simplify the expression in FINISH_TEST.
  • Loading branch information
KitsuneRal committed May 27, 2023
1 parent b34e13d commit 6112740
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions quotest/quotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,16 @@ TEST_IMPL(sendReaction)
return false;
}

static constexpr auto fileContent = "Test";

TEST_IMPL(sendFile)
{
auto* tf = new QTemporaryFile;
if (!tf->open()) {
clog << "Failed to create a temporary file" << endl;
FAIL_TEST();
}
tf->write("Test");
tf->write(fileContent);
tf->close();
QFileInfo tfi { *tf };
// QFileInfo::fileName brings only the file name; QFile::fileName brings
Expand Down Expand Up @@ -464,20 +466,23 @@ struct DownloadRunner {

using result_type = QNetworkReply::NetworkError;

QNetworkReply::NetworkError operator()(int) const
result_type operator()(int) const
{
QEventLoop el;
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply {
const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply {
NetworkAccessManager::instance()->get(QNetworkRequest(url))
};
QObject::connect(
reply.data(), &QNetworkReply::finished, &el, [&el] { el.exit(); },
Qt::QueuedConnection);
el.exec();
return reply->error();
return reply->error() != QNetworkReply::NoError ? reply->error()
: reply->readAll() != fileContent
? QNetworkReply::UnknownContentError
: QNetworkReply::NoError;
}

static QVector<QNetworkReply::NetworkError> run(const QUrl& url, int threads)
static QVector<result_type> run(const QUrl& url, int threads)
{
return QtConcurrent::blockingMapped(QVector<int>(threads),
DownloadRunner{ url });
Expand All @@ -492,28 +497,26 @@ bool TestSuite::testDownload(const TestToken& thisTest, const QUrl& mxcUrl)
"Network/allow_direct_media_requests"_ls;
s.setValue(DirectMediaRequestsSetting, true);
if (const auto result = DownloadRunner::run(mxcUrl, 1);
result.back() != QNetworkReply::NoError) {
result.front() != QNetworkReply::NoError) {
clog << "Direct media request to "
<< mxcUrl.toDisplayString().toStdString()
<< " was allowed but failed" << endl;
FAIL_TEST();
}
s.setValue(DirectMediaRequestsSetting, false);
if (const auto result = DownloadRunner::run(mxcUrl, 1);
result.back() == QNetworkReply::NoError) {
result.front() == QNetworkReply::NoError) {
clog << "Direct media request to "
<< mxcUrl.toDisplayString().toStdString()
<< " was disallowed but succeeded" << endl;
FAIL_TEST();
}

static constexpr auto ThreadsCount = 3;
const auto httpUrl = targetRoom->connection()->makeMediaUrl(mxcUrl);
const auto results = DownloadRunner::run(httpUrl, 3);
const auto results = DownloadRunner::run(httpUrl, ThreadsCount);
// Move out actual test from the multithreaded code to help debugging
FINISH_TEST(std::all_of(results.begin(), results.end(),
[](QNetworkReply::NetworkError ne) {
return ne == QNetworkReply::NoError;
}));
FINISH_TEST(results == QVector(ThreadsCount, QNetworkReply::NoError));
}

bool TestSuite::checkFileSendingOutcome(const TestToken& thisTest,
Expand Down

0 comments on commit 6112740

Please sign in to comment.