// Unit tests for PluginShop: the catalogue format the shop accepts, and the // bookkeeping that decides what an uninstall is allowed to delete. Parsing is // pure logic and the file half runs against a temporary directory, so no network // access and no real plugin is involved. #include "plugin-shop.h" #include #include #include #include #include #include #include #include // The full field set, in both kinds, with an entry relying on the URL for the // file name. static void testCatalogIsParsed() { const QByteArray json = R"({ "catalog": 1, "plugins": [ { "id": "apt", "name": "APT", "kind": "provider", "version": "1.0.0", "author": "FWS", "description": "Gestionnaire de paquets Debian", "url": "https://example.org/plugins/libapt-provider.so", "sha256": "ABCDEF0123", "file": "libapt-provider.so" }, { "id": "gnome-tweaks", "name": "GNOME Tweaks", "kind": "addon", "url": "https://example.org/plugins/libgnome-tweaks.so" } ] })"; QString error; const std::vector entries = PluginShop::parseCatalog(json, &error); assert(error.isEmpty()); assert(entries.size() == 2); assert(entries[0].id == "apt"); assert(entries[0].kind == "provider"); assert(entries[0].fileName == "libapt-provider.so"); // Checksums are compared case-insensitively, so they are stored lowercased. assert(entries[0].sha256 == "abcdef0123"); assert(entries[1].id == "gnome-tweaks"); assert(entries[1].kind == "addon"); // No "file": the name is taken from the URL when the plugin is installed. assert(entries[1].fileName.isEmpty()); assert(entries[1].sha256.isEmpty()); } // A document that is not a catalogue must be refused as a whole, so the shop // never shows half a catalogue. static void testInvalidDocumentsAreRejected() { QString error; assert(PluginShop::parseCatalog("{ not json", &error).empty()); assert(!error.isEmpty()); error.clear(); assert(PluginShop::parseCatalog("[]", &error).empty()); assert(!error.isEmpty()); error.clear(); assert(PluginShop::parseCatalog("{\"catalog\": 1}", &error).empty()); assert(!error.isEmpty()); } // Entries that cannot be installed are skipped one by one: one broken line in // the catalogue does not hide the plugins that are fine. static void testIncompleteEntriesAreSkipped() { const QByteArray json = R"({ "plugins": [ {"id": "no-name", "url": "https://example.org/a.so"}, {"id": "no-url", "name": "Sans URL"}, {"name": "Sans identifiant", "url": "https://example.org/c.so"}, "not an object", {"id": "ok", "name": "Complet", "url": "https://example.org/d.so", "kind": "autre chose"} ] })"; QString error; const std::vector entries = PluginShop::parseCatalog(json, &error); assert(error.isEmpty()); assert(entries.size() == 1); assert(entries[0].id == "ok"); // An unknown kind is dropped rather than guessed: the loaded library // declares the real one. assert(entries[0].kind.isEmpty()); } // The shop only removes what it installed itself. static void testUninstallOnlyRemovesWhatItInstalled() { QTemporaryDir dir; PluginShop shop; shop.setInstallDirectory(dir.path() + "/plugins"); bool finished = false; bool success = true; QObject::connect(&shop, &PluginShop::removeFinished, &shop, [&](const QString&, bool ok, const QString&) { finished = true; success = ok; }); // Unknown entry: a removal must fail instead of deleting anything. assert(!shop.isInstalled("apt")); shop.remove("apt"); assert(finished && !success); // A plugin the shop installed: recorded path plus a file on disk. The key // is the one the shop writes (documented in doc/plugin-shop.md). const QString pluginsDir = dir.path() + "/plugins"; assert(QDir().mkpath(pluginsDir)); const QString path = pluginsDir + "/libapt-provider.so"; QFile file(path); assert(file.open(QIODevice::WriteOnly)); file.write("not a real shared library"); file.close(); QSettings settings; settings.setValue("shop/installed/apt", path); settings.sync(); assert(shop.isInstalled("apt")); assert(shop.installedPath("apt") == path); success = false; shop.remove("apt"); assert(finished && success); assert(!QFile::exists(path)); assert(!shop.isInstalled("apt")); } // A file deleted by hand is not installed any more, even when the shop still // remembers it: the state follows the disk. static void testMissingFileCountsAsNotInstalled() { QTemporaryDir dir; PluginShop shop; shop.setInstallDirectory(dir.path() + "/plugins"); QSettings settings; settings.setValue("shop/installed/gone", dir.path() + "/plugins/libgone.so"); settings.sync(); assert(!shop.isInstalled("gone")); assert(shop.installedPath("gone").isEmpty()); } int main(int argc, char** argv) { // The tests must not read or write the user's PacManager settings: an INI // file of their own, in a temporary directory, stands in for them. QTemporaryDir settingsDir; QSettings::setDefaultFormat(QSettings::IniFormat); QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, settingsDir.path()); QCoreApplication app(argc, argv); app.setOrganizationName("PacManagerTests"); app.setApplicationName("plugin-shop-test"); testCatalogIsParsed(); testInvalidDocumentsAreRejected(); testIncompleteEntriesAreSkipped(); testUninstallOnlyRemovesWhatItInstalled(); testMissingFileCountsAsNotInstalled(); std::cout << "All plugin-shop tests passed." << std::endl; return 0; }