Cover file snapshots, cancellation ownership and browser retries
This commit is contained in:
1 parent
c996af5b5e
commit
942247a4dc
1 file changed
+166
@@ -5,6 +5,10 @@
|
||||
#include "../db/db_manager.h"
|
||||
#include <QtTest>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QTreeWidget>
|
||||
#include <QFileDialog>
|
||||
#include <QFile>
|
||||
#include <QTemporaryDir>
|
||||
#include <QScopeGuard>
|
||||
extern "C" {
|
||||
@@ -55,6 +59,7 @@ class FileLifecycleTest : public QObject {
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void initTestCase() {
|
||||
QCoreApplication::setAttribute(Qt::AA_DontUseNativeDialogs);
|
||||
debug_config_init(); debug_set_level(DEBUG_LEVEL_WARN);
|
||||
debug_set_category_level(DEBUG_CATEGORY_MEDIA, DEBUG_LEVEL_TRACE);
|
||||
debug_set_category_level(DEBUG_CATEGORY_GENERAL, DEBUG_LEVEL_DEBUG);
|
||||
@@ -165,6 +170,167 @@ private slots:
|
||||
}
|
||||
QCOMPARE(u_get_allocated_count(), before);
|
||||
}
|
||||
|
||||
void snapshotsSurviveCallerChangesAndDoNotUseSuppliedInstance() {
|
||||
const size_t before = u_get_allocated_count();
|
||||
{
|
||||
FileCore core; QVERIFY(core.ua); core.attach();
|
||||
mf_message message{}; message.kind = MF_REQUEST; message.command = MF_STAT;
|
||||
message.request_id = UINT64_C(0x0102030405060708); message.owner = 22; strcpy(message.path, "original.txt");
|
||||
QCOMPARE(gui_bridge_member_files_request(42, 33, &message), 0);
|
||||
message = {};
|
||||
mf_transfer_request transfer{}; transfer.id = 123; transfer.group = 42; transfer.server = 33; transfer.owner = 22;
|
||||
transfer.inst = reinterpret_cast<UTUN_INSTANCE*>(quintptr(1)); // Bridge обязан заменить этот указатель.
|
||||
strcpy(transfer.remote, "original.txt"); strcpy(transfer.local, "/unused/local.txt");
|
||||
QCOMPARE(gui_bridge_member_files_transfer(&transfer), 0); transfer = {};
|
||||
gui_bridge_set_inst(nullptr); QCoreApplication::processEvents();
|
||||
QCOMPARE(responses.size(), 1); QCOMPARE(transfers.size(), 1); mf_message result{};
|
||||
const auto& bytes = responses[0];
|
||||
QCOMPARE(mf_get64((const uint8_t*)bytes.constData()), uint64_t(42));
|
||||
QCOMPARE(mf_get64((const uint8_t*)bytes.constData() + 8), uint64_t(33));
|
||||
QCOMPARE(mf_decode((const uint8_t*)bytes.constData() + 16, bytes.size() - 16, &result), 0);
|
||||
QCOMPARE(result.request_id, UINT64_C(0x0102030405060708)); QCOMPARE(result.owner, uint64_t(22));
|
||||
QCOMPARE(result.status, uint16_t(MF_CANCELLED)); QCOMPARE(QString::fromUtf8(result.path), QString("original.txt"));
|
||||
QCOMPARE(mf_get64((const uint8_t*)transfers[0].constData()), uint64_t(123));
|
||||
responses.clear(); transfers.clear(); core.attach();
|
||||
transfer.id = 124; transfer.group = 42; transfer.server = 33; transfer.owner = 22; transfer.upload = 1;
|
||||
transfer.inst = reinterpret_cast<UTUN_INSTANCE*>(quintptr(1));
|
||||
strcpy(transfer.remote, "original.txt"); strcpy(transfer.local, "/unused/local.txt");
|
||||
QCOMPARE(gui_bridge_member_files_transfer(&transfer), 0);
|
||||
uasync_poll(core.ua, 0); QCoreApplication::processEvents();
|
||||
QCOMPARE(transfers.size(), 1); QCOMPARE(mf_get64((const uint8_t*)transfers[0].constData()), uint64_t(124));
|
||||
QCOMPARE(transfers[0][50], char(2));
|
||||
QCOMPARE(transfer.inst, reinterpret_cast<UTUN_INSTANCE*>(quintptr(1)));
|
||||
}
|
||||
QCOMPARE(u_get_allocated_count(), before);
|
||||
}
|
||||
|
||||
void cancelCommandsReleaseMemoryOnExecutionDetachAndRejection() {
|
||||
const size_t before = u_get_allocated_count();
|
||||
{
|
||||
FileCore core; QVERIFY(core.ua); core.attach(); const size_t baseline = u_get_allocated_count();
|
||||
QCOMPARE(gui_bridge_member_files_cancel(99), 0); uasync_poll(core.ua, 0);
|
||||
QCOMPARE(u_get_allocated_count(), baseline);
|
||||
QCOMPARE(gui_bridge_member_files_cancel(100), 0); gui_bridge_set_inst(nullptr);
|
||||
QCOMPARE(u_get_allocated_count(), baseline); QVERIFY(gui_bridge_member_files_cancel(101) < 0);
|
||||
core.attach(); QVERIFY(gui_bridge_member_files_cancel(0) < 0);
|
||||
#ifdef UTUN_TEST_FILE_ALLOC_FAILURES
|
||||
for (int point : {1, 2}) {
|
||||
failAllocation = point; QVERIFY(gui_bridge_member_files_cancel(102) < 0); QCOMPARE(failAllocation, 0);
|
||||
QCOMPARE(u_get_allocated_count(), baseline); QCOMPARE(pending(core.ua), 0);
|
||||
}
|
||||
#endif
|
||||
QCoreApplication::processEvents(); QVERIFY(responses.isEmpty()); QVERIFY(transfers.isEmpty());
|
||||
}
|
||||
QCOMPARE(u_get_allocated_count(), before);
|
||||
}
|
||||
|
||||
void browserRejectsOrCancelsListAndCanRetry_data() { batchRequest_data(); }
|
||||
void browserRejectsOrCancelsListAndCanRetry() {
|
||||
QFETCH(QString, scenario);
|
||||
const size_t before = u_get_allocated_count();
|
||||
{
|
||||
FileCore core; QVERIFY(core.ua); core.attach(); DbManager db; sqlite3* sql = nullptr;
|
||||
QCOMPARE(sqlite3_open(":memory:", &sql), SQLITE_OK);
|
||||
const auto close = qScopeGuard([&] { db.setDb(nullptr); sqlite3_close(sql); });
|
||||
QCOMPARE(sqlite3_exec(sql, "CREATE TABLE accounts(node_id INTEGER,display_name TEXT);"
|
||||
"INSERT INTO accounts VALUES(22,'Me'),(33,'Storage');"
|
||||
"CREATE TABLE peers_42(node_id INTEGER,adm_tags TEXT,adm_tags_sig BLOB,deleted INTEGER,source INTEGER);"
|
||||
"INSERT INTO peers_42 VALUES(22,'{}',NULL,0,0),(33,'{\"storage\":\"yes\"}',zeroblob(64),0,0);",
|
||||
nullptr, nullptr, nullptr), SQLITE_OK);
|
||||
db.setDb(sql); db.setMyNodeId(22);
|
||||
MemberFileBrowser browser(&db); activeBrowser = &browser;
|
||||
if (scenario == "unavailable") gui_bridge_set_inst(nullptr);
|
||||
if (scenario == "stopped") uasync_mark_stopped(core.ua);
|
||||
#ifdef UTUN_TEST_FILE_ALLOC_FAILURES
|
||||
if (scenario == "request-allocation") failAllocation = 1;
|
||||
if (scenario == "post-allocation") failAllocation = 2;
|
||||
#endif
|
||||
browser.setChannel("42");
|
||||
if (scenario == "detach") gui_bridge_set_inst(nullptr);
|
||||
if (scenario == "executed-error") uasync_poll(core.ua, 0);
|
||||
QCoreApplication::processEvents(); QCOMPARE(pending(core.ua), 0);
|
||||
QStringList labels; for (auto* label : browser.findChildren<QLabel*>()) labels.append(label->text());
|
||||
const QString expected = scenario == "detach" ? QString::fromUtf8(mf_status_text(MF_CANCELLED)) :
|
||||
scenario == "executed-error" ? QString::fromUtf8(mf_status_text(MF_ACCESS_DENIED)) : "Unable to request directory";
|
||||
QVERIFY2(labels.contains(expected), qPrintable(labels.join('\n')));
|
||||
#ifdef UTUN_TEST_FILE_ALLOC_FAILURES
|
||||
QCOMPARE(failAllocation, 0);
|
||||
#endif
|
||||
core.attach();
|
||||
QPushButton* refresh = nullptr;
|
||||
for (auto* button : browser.findChildren<QPushButton*>()) if (button->text() == "Refresh") refresh = button;
|
||||
QVERIFY(refresh); refresh->click(); QCOMPARE(pending(core.ua), 1);
|
||||
uasync_poll(core.ua, 0); QCoreApplication::processEvents(); QCOMPARE(pending(core.ua), 0);
|
||||
QVERIFY(browser.findChild<QTreeWidget*>()->isEnabled());
|
||||
}
|
||||
QCOMPARE(u_get_allocated_count(), before);
|
||||
}
|
||||
|
||||
void browserSingleDownload_data() {
|
||||
QTest::addColumn<int>("failure");
|
||||
QTest::newRow("detach") << 0;
|
||||
#ifdef UTUN_TEST_FILE_ALLOC_FAILURES
|
||||
QTest::newRow("request-allocation") << 1;
|
||||
QTest::newRow("post-allocation") << 2;
|
||||
#endif
|
||||
}
|
||||
void browserSingleDownload() {
|
||||
QFETCH(int, failure);
|
||||
const size_t before = u_get_allocated_count();
|
||||
{
|
||||
FileCore core; QVERIFY(core.ua); core.attach(); QTemporaryDir root; QVERIFY(root.isValid());
|
||||
DbManager db; sqlite3* sql = nullptr; QCOMPARE(sqlite3_open(":memory:", &sql), SQLITE_OK);
|
||||
const auto close = qScopeGuard([&] { db.setDb(nullptr); sqlite3_close(sql); });
|
||||
QCOMPARE(sqlite3_exec(sql, "CREATE TABLE accounts(node_id INTEGER,display_name TEXT);"
|
||||
"INSERT INTO accounts VALUES(22,'Me'),(33,'Storage');"
|
||||
"CREATE TABLE peers_42(node_id INTEGER,adm_tags TEXT,adm_tags_sig BLOB,deleted INTEGER,source INTEGER);"
|
||||
"INSERT INTO peers_42 VALUES(22,'{}',NULL,0,0),(33,'{\"storage\":\"yes\"}',zeroblob(64),0,0);",
|
||||
nullptr, nullptr, nullptr), SQLITE_OK);
|
||||
db.setDb(sql); db.setMyNodeId(22);
|
||||
MemberFileBrowser browser(&db); activeBrowser = &browser; browser.setChannel("42", 22);
|
||||
uasync_poll(core.ua, 0); QCoreApplication::processEvents();
|
||||
auto* tree = browser.findChild<QTreeWidget*>(); QVERIFY(tree);
|
||||
auto* item = new QTreeWidgetItem(tree, {"selected.txt"}); item->setData(0, Qt::UserRole, "selected.txt");
|
||||
item->setData(0, Qt::UserRole + 1, MF_FILE); item->setData(0, Qt::UserRole + 2, QByteArray(16, '\7'));
|
||||
tree->setCurrentItem(item); browser.show();
|
||||
QPushButton* download = nullptr; QPushButton* cancel = nullptr;
|
||||
for (auto* button : browser.findChildren<QPushButton*>()) {
|
||||
if (button->text() == "Download…") download = button;
|
||||
if (button->text() == "Cancel transfer") cancel = button;
|
||||
}
|
||||
QVERIFY(download); QVERIFY(cancel); QVERIFY(download->isEnabled());
|
||||
bool accepted = false;
|
||||
const QString destination = root.filePath("download.txt");
|
||||
QTimer::singleShot(0, [&] {
|
||||
auto* dialog = qobject_cast<QFileDialog*>(QApplication::activeModalWidget());
|
||||
if (!dialog) return;
|
||||
dialog->selectFile(destination); accepted = QMetaObject::invokeMethod(dialog, "accept");
|
||||
});
|
||||
#ifdef UTUN_TEST_FILE_ALLOC_FAILURES
|
||||
failAllocation = failure;
|
||||
#endif
|
||||
download->click(); QVERIFY(accepted);
|
||||
if (!failure) {
|
||||
QVERIFY(cancel->isEnabled()); QVERIFY(!tree->isEnabled());
|
||||
gui_bridge_set_inst(nullptr); QCoreApplication::processEvents();
|
||||
}
|
||||
QVERIFY(!cancel->isEnabled()); QVERIFY(tree->isEnabled()); QVERIFY(!QFile::exists(destination));
|
||||
QCOMPARE(pending(core.ua), 0);
|
||||
#ifdef UTUN_TEST_FILE_ALLOC_FAILURES
|
||||
QCOMPARE(failAllocation, 0);
|
||||
#endif
|
||||
core.attach(); QVERIFY(download->isEnabled());
|
||||
QTimer::singleShot(0, [&] {
|
||||
auto* dialog = qobject_cast<QFileDialog*>(QApplication::activeModalWidget());
|
||||
if (dialog) { dialog->selectFile(destination); QMetaObject::invokeMethod(dialog, "accept"); }
|
||||
});
|
||||
download->click(); QVERIFY(cancel->isEnabled());
|
||||
uasync_poll(core.ua, 0); QCoreApplication::processEvents();
|
||||
QVERIFY(!cancel->isEnabled()); QVERIFY(tree->isEnabled()); QVERIFY(!QFile::exists(destination));
|
||||
}
|
||||
QCOMPARE(u_get_allocated_count(), before);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(FileLifecycleTest)
|
||||
|
||||
Reference in new issue
Block a user