feat(plugins): add a plugin shop fed by a JSON catalogue
Installing a plugin meant compiling a shared library and dropping it in a directory by hand. The shop reads a catalogue (plugins/catalog.json, one entry per plugin with its description and download URL), lists what can be installed next to what already is, and installs an entry into ~/.local/share/PacManager/plugins — the directory the registry already scans — so a plugin becomes usable without root rights and without a restart. Installing runs third-party code in this process, so the shop only fetches what the user picked, checks the announced sha256 before writing anything, and a removal deletes only the files the shop wrote itself. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
1 parent
b26e907221
commit
5033c4e940
9 files changed
+872
-6
No files matched your search
@@ -0,0 +1,181 @@
|
||||
// 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 <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QSettings>
|
||||
#include <QString>
|
||||
#include <QTemporaryDir>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
// 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<PluginShop::CatalogEntry> 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<PluginShop::CatalogEntry> 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;
|
||||
}
|
||||
Reference in new issue
Block a user