#include "bioheadexpressionstore.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace BioHeadExpressionStore; const QString real = QStringLiteral("real|192.168.0.234:50051"); const QString demo = QStringLiteral("demo|192.168.0.234:50051"); const QString device = QStringLiteral("usb_head"); void check(bool value, const char *message) { if (!value) throw std::runtime_error(message); } QByteArray contents(const QString &path) { QFile file(path); check(file.open(QIODevice::ReadOnly), "fixture file can be read"); return file.readAll(); } void fixture(const QString &path, const QByteArray &bytes) { QFile file(path); check(file.open(QIODevice::WriteOnly | QIODevice::Truncate), "fixture file can be written"); check(file.write(bytes) == bytes.size(), "fixture write is complete"); } Expression expression(const QString &name = QStringLiteral("微笑")) { return {QUuid::createUuid().toString(QUuid::WithoutBraces), name, {QVariantMap{{QStringLiteral("id"), QStringLiteral("eye_l_up")}, {QStringLiteral("positionRad"), 0.0}}, QVariantMap{{QStringLiteral("id"), QStringLiteral("pwm8_local")}, {QStringLiteral("positionRad"), -.123456789012345}}}, .7}; } QList list(const QString &path, const QString &endpoint = real, const QString &id = device) { QList result; QString error; check(load(path, endpoint, id, &result, &error), "stored expressions reload successfully"); check(error.isEmpty(), "successful operations clear previous error text"); return result; } void lifecycleAndIsolation(const QString &path) { QString error; check(list(path).isEmpty() && !QFile::exists(path), "a missing file is an empty library without creating a file"); Expression first = expression(); check(save(path, real, device, first, false, &error), "new local expression is persisted"); auto reloaded = list(path); check(reloaded.size() == 1 && reloaded[0].id == first.id && reloaded[0].name == first.name, "fresh loads retain IDs and Chinese names without process memory state"); check(reloaded[0].targets[0].toMap().value("positionRad").toDouble() == 0. && reloaded[0].targets[1].toMap().value("positionRad").toDouble() == -.123456789012345 && reloaded[0].transitionDurationS == .7, "explicit zero and double precision survive JSON round trips"); QByteArray original = contents(path); Expression sameName = expression(); check(!save(path, real, device, sameName, false, &error) && !error.isEmpty() && contents(path) == original, "same-scope duplicate names cannot overwrite the library"); check(!save(path, real, device, first, false, &error) && contents(path) == original, "create cannot silently replace an existing UUID"); check(!save(path, real, device, expression(QStringLiteral("不存在")), true, &error) && contents(path) == original, "replace requires an existing local UUID"); first.name = QStringLiteral("眨眼"); first.transitionDurationS = 1.25; first.targets[0] = QVariantMap{{"id", "eye_l_up"}, {"positionRad", .21}}; check(save(path, real, device, first, true, &error) && list(path)[0].name == first.name && list(path)[0].transitionDurationS == 1.25, "replace persists new name, targets and duration"); const Expression alternate = expression(first.name); check(save(path, QStringLiteral("real|192.168.0.235:50051"), device, alternate, false, &error) && save(path, real, QStringLiteral("other_head"), alternate, false, &error) && save(path, demo, device, alternate, false, &error), "endpoint, device and demo scopes are independent"); check(list(path).size() == 1 && list(path, demo)[0].id == alternate.id && list(path, real, "other_head")[0].id == alternate.id, "scope lookups never mix expression records"); check(remove(path, real, device, first.id, &error) && list(path).isEmpty() && list(path, demo).size() == 1 && list(path, real, "other_head").size() == 1, "deleting one local expression preserves all other scopes"); check(!remove(path, real, device, first.id, &error), "delete requires an existing record"); std::puts("PASS local add/replace/delete/reload, exact numeric values and endpoint/device/demo isolation"); } void invalidInputs(const QString &path) { QString error; const Expression valid = expression(); check(save(path, real, device, valid, false, &error), "validation fixture is saved"); const QByteArray original = contents(path); QList bad; Expression value = expression(); value.id = QStringLiteral("ad8f669a78d54d30a2356512b0802c1d"); bad.append(value); value = expression(); value.name = QStringLiteral(" 前导空格"); bad.append(value); value = expression(); value.name = QString(81, 'x'); bad.append(value); value = expression(); value.transitionDurationS = std::numeric_limits::infinity(); bad.append(value); value = expression(); value.transitionDurationS = .1; bad.append(value); value = expression(); value.targets.clear(); bad.append(value); value = expression(); value.targets.append(value.targets[0]); bad.append(value); value = expression(); value.targets[0] = QVariantMap{{"id", "eye_l_up"}, {"positionRad", true}}; bad.append(value); value = expression(); value.targets[0] = QVariantMap{{"id", "eye_l_up"}, {"positionRad", "0.0"}}; bad.append(value); value = expression(); value.targets[0] = QVariantMap{{"id", "eye_l_up"}, {"positionRad", std::numeric_limits::quiet_NaN()}}; bad.append(value); value = expression(); value.targets.clear(); for (int i = 0; i < 65; ++i) value.targets.append(QVariantMap{{"id", QString::number(i)}, {"positionRad", 0.}}); bad.append(value); for (const Expression &invalid : bad) check(!save(path, real, device, invalid, false, &error) && !error.isEmpty() && contents(path) == original, "malformed input must fail clearly and preserve the original file"); check(!save(path, "", device, expression(), false, &error) && contents(path) == original, "missing routing scope never creates a shared fallback profile"); std::puts("PASS local UUID/name/count/duplicate/finite-number/duration validation preserves existing data"); } void corruptFilesAndFailedWrites(const QString &path, const QString &directory) { QString error; const Expression valid = expression(); check(save(path, real, device, valid, false, &error), "corruption fixture is saved"); const QJsonObject good = QJsonDocument::fromJson(contents(path)).object(); QList damaged{QByteArray("{broken json")}; QJsonObject object = good; object.insert("version", 2); damaged.append(QJsonDocument(object).toJson()); object = good; QJsonArray scopes = object.value("scopes").toArray(); scopes.append(scopes[0]); object.insert("scopes", scopes); damaged.append(QJsonDocument(object).toJson()); for (int fault = 0; fault < 3; ++fault) { object = good; scopes = object.value("scopes").toArray(); QJsonObject scope = scopes[0].toObject(); QJsonArray records = scope.value("expressions").toArray(); QJsonObject record = records[0].toObject(); if (fault == 0) records.append(records[0]); else if (fault == 1) { QJsonArray targets = record.value("targets").toArray(); targets.append(targets[0]); record.insert("targets", targets); records[0] = record; } else { QJsonArray targets = record.value("targets").toArray(); QJsonObject target = targets[0].toObject(); target.insert("positionRad", "0"); targets[0] = target; record.insert("targets", targets); records[0] = record; } scope.insert("expressions", records); scopes[0] = scope; object.insert("scopes", scopes); damaged.append(QJsonDocument(object).toJson()); } damaged.append(QByteArray(8 * 1024 * 1024 + 1, ' ')); for (const QByteArray &bytes : damaged) { fixture(path, bytes); QList unchanged{valid}; check(!load(path, real, device, &unchanged, &error) && !error.isEmpty() && unchanged.size() == 1 && unchanged[0].id == valid.id, "failed load leaves caller data intact"); check(!save(path, real, device, expression(), false, &error) && contents(path) == bytes, "corrupt, duplicate, unsupported-version and oversized files are never replaced"); check(!remove(path, real, device, valid.id, &error) && contents(path) == bytes, "delete cannot rewrite a damaged library either"); } fixture(path, QJsonDocument(good).toJson()); const QByteArray original = contents(path); { QLockFile competing(path + ".lock"); check(competing.tryLock(0), "fixture holds a competing instance lock"); QElapsedTimer elapsed; elapsed.start(); check(!save(path, real, device, expression("忙碌"), false, &error) && elapsed.elapsed() < 1500 && contents(path) == original, "competing writes fail promptly and preserve the valid library"); } const QString blocker = QDir(directory).filePath("file_not_directory"); fixture(blocker, "do not replace"); check(!save(QDir(blocker).filePath("expressions.json"), real, device, expression(), false, &error) && !error.isEmpty() && contents(blocker) == "do not replace", "unwritable destination never reports success or destroys an existing file"); #ifdef Q_OS_WIN const QFile::Permissions permissions = QFile::permissions(path); check(QFile::setPermissions(path, QFile::ReadOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther), "existing library can be made read-only for an actual atomic-write failure"); const bool written = save(path, real, device, expression(QStringLiteral("只读失败")), false, &error); const QByteArray afterWrite = contents(path); check(QFile::setPermissions(path, permissions), "test restores original library permissions"); check(!written && !error.isEmpty() && afterWrite == original, "QSaveFile failure on a read-only destination is reported without changing the original library"); #endif std::puts("PASS corruption/schema/duplicate/size refusal, bounded instance lock and failed-write preservation"); } } // namespace int main(int argc, char **argv) { QCoreApplication app(argc, argv); try { QTemporaryDir temporary; check(temporary.isValid(), "temporary test directory exists"); check(defaultPath().endsWith("/data/biohead_expressions.json"), "default path is explicit executable-relative data file"); lifecycleAndIsolation(temporary.filePath("lifecycle.json")); invalidInputs(temporary.filePath("validation.json")); corruptFilesAndFailedWrites(temporary.filePath("damaged.json"), temporary.path()); std::puts("PASS BioHead local expression store; QtCore temporary files only, no RPC or hardware"); return 0; } catch (const std::exception &error) { std::fprintf(stderr, "FAIL BioHead expression store: %s\n", error.what()); return 1; } }