mirror of
https://github.com/siemens-mobile-hacks/elfloader3-linux.git
synced 2026-11-25 23:57:20 +00:00
258 lines
8.7 KiB
C++
258 lines
8.7 KiB
C++
#include "../src/Launcher.h"
|
|
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QProcess>
|
|
#include <QRegularExpression>
|
|
#include <QTemporaryDir>
|
|
#include <QtTest>
|
|
|
|
class LauncherTest: public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void mapsRootAndBindPaths();
|
|
void rejectsPathOutsideMounts();
|
|
void buildsDockerArguments();
|
|
void initializesMissingRootWithNestedBind();
|
|
void parsesHostCommandLine();
|
|
void findsSdkInParentOrHome();
|
|
};
|
|
|
|
static void createFile(const QString &path) {
|
|
QFile file(path);
|
|
QVERIFY(file.open(QIODevice::WriteOnly));
|
|
}
|
|
|
|
static LaunchOptions makeOptions(const QString &root, const QString &external) {
|
|
LaunchOptions options;
|
|
options.root = QFileInfo(root).canonicalFilePath();
|
|
options.platform = "elka";
|
|
options.sdk = external;
|
|
options.image = "elf-emu:test";
|
|
options.container_name = "elf-emu-test";
|
|
options.binds.push_back({
|
|
.source = QFileInfo(external).canonicalFilePath(),
|
|
.ffs_path = "4:/home",
|
|
.container_path = "/ffs/MMCard/home",
|
|
.read_only = true,
|
|
});
|
|
return options;
|
|
}
|
|
|
|
void LauncherTest::mapsRootAndBindPaths() {
|
|
QTemporaryDir temp;
|
|
QVERIFY(temp.isValid());
|
|
QString root = temp.path() + "/root";
|
|
QString data = root + "/Data/ZBin";
|
|
QString external = temp.path() + "/external";
|
|
QVERIFY(QDir().mkpath(data));
|
|
QVERIFY(QDir().mkpath(external));
|
|
|
|
QString root_elf = data + "/test.elf";
|
|
QString bind_elf = external + "/bind.elf";
|
|
createFile(root_elf);
|
|
createFile(bind_elf);
|
|
LaunchOptions options = makeOptions(root, external);
|
|
|
|
QCOMPARE(Launcher::mapPath(root_elf, options), QString("0:/ZBin/test.elf"));
|
|
QCOMPARE(Launcher::mapPath(bind_elf, options), QString("4:/home/bind.elf"));
|
|
}
|
|
|
|
void LauncherTest::rejectsPathOutsideMounts() {
|
|
QTemporaryDir temp;
|
|
QVERIFY(temp.isValid());
|
|
QString root = temp.path() + "/root";
|
|
QString external = temp.path() + "/external";
|
|
QVERIFY(QDir().mkpath(root));
|
|
QVERIFY(QDir().mkpath(external));
|
|
|
|
QString outside_elf = temp.path() + "/outside.elf";
|
|
createFile(outside_elf);
|
|
LaunchOptions options = makeOptions(root, external);
|
|
|
|
QVERIFY_EXCEPTION_THROWN(Launcher::mapPath(outside_elf, options), std::runtime_error);
|
|
}
|
|
|
|
void LauncherTest::buildsDockerArguments() {
|
|
QTemporaryDir temp;
|
|
QVERIFY(temp.isValid());
|
|
QString root = temp.path() + "/root";
|
|
QVERIFY(QDir().mkpath(root));
|
|
|
|
LaunchOptions options;
|
|
options.root = root;
|
|
options.platform = "elka";
|
|
options.sdk = "/tmp/sdk";
|
|
options.elf = "0:/ZBin/test.elf";
|
|
options.image = "elf-emu:test";
|
|
options.container_name = "elf-emu-test";
|
|
options.gdb_port = 34567;
|
|
options.binds.push_back({
|
|
.source = "/tmp/home",
|
|
.ffs_path = "4:/home",
|
|
.container_path = "/ffs/MMCard/home",
|
|
.read_only = true,
|
|
});
|
|
|
|
QStringList args = Launcher::dockerArguments(options, 23456);
|
|
QCOMPARE(args.mid(0, 3), QStringList({ "run", "--platform", "linux/arm/v7" }));
|
|
QVERIFY(args.contains("127.0.0.1:23456:23456"));
|
|
QVERIFY(args.contains("127.0.0.1:34567:34567"));
|
|
QVERIFY(args.contains("QEMU_GDB=34567"));
|
|
QCOMPARE(args.at(args.indexOf("--ipc-port") + 1), QString("23456"));
|
|
QVERIFY(args.join('\n').contains("target=/ffs/MMCard/home"));
|
|
QVERIFY(args.contains("/app/elka/elfloader3"));
|
|
}
|
|
|
|
void LauncherTest::initializesMissingRootWithNestedBind() {
|
|
QTemporaryDir temp;
|
|
QVERIFY(temp.isValid());
|
|
QString root = temp.path() + "/root";
|
|
QString external = temp.path() + "/external";
|
|
QVERIFY(QDir().mkpath(root));
|
|
QVERIFY(QDir().mkpath(external));
|
|
QVERIFY(QDir().mkpath(root + "/Config"));
|
|
|
|
LaunchOptions options = makeOptions(root, external);
|
|
options.binds[0].ffs_path = "0:/ZBin";
|
|
options.binds[0].container_path = "/ffs/Data/ZBin";
|
|
QStringList args = Launcher::dockerArguments(options, 0);
|
|
|
|
QVERIFY(args.contains("--init-missing"));
|
|
QVERIFY(args.contains("Data"));
|
|
QVERIFY(args.contains("Cache"));
|
|
QVERIFY(args.contains("MMCard"));
|
|
QVERIFY(!args.contains("Config"));
|
|
QVERIFY(args.contains("--init-exclude"));
|
|
QVERIFY(args.contains("/ffs/Data/ZBin"));
|
|
QVERIFY(!QFileInfo(root + "/Data").exists());
|
|
QVERIFY(!QFileInfo(root + "/Cache").exists());
|
|
QVERIFY(!QFileInfo(root + "/MMCard").exists());
|
|
|
|
Launcher::prepareRoot(options);
|
|
QVERIFY(QFileInfo(root + "/Data/ZBin").isDir());
|
|
QVERIFY(QFileInfo(root + "/Cache").isDir());
|
|
QVERIFY(QFileInfo(root + "/MMCard").isDir());
|
|
QVERIFY(QFileInfo(root + "/.elf-emu-init-Data").exists());
|
|
QVERIFY(QFileInfo(root + "/.elf-emu-init-Cache").exists());
|
|
QVERIFY(!QFileInfo(root + "/.elf-emu-init-Config").exists());
|
|
QVERIFY(Launcher::dockerArguments(options, 0).contains("Data"));
|
|
}
|
|
|
|
void LauncherTest::parsesHostCommandLine() {
|
|
QTemporaryDir temp;
|
|
QVERIFY(temp.isValid());
|
|
QString root = temp.path() + "/root";
|
|
QString sdk = temp.path() + "/sdk";
|
|
QString elf = root + "/Data/test.elf";
|
|
QVERIFY(QDir().mkpath(root + "/Data"));
|
|
QVERIFY(QDir().mkpath(sdk + "/swilib/include"));
|
|
createFile(elf);
|
|
|
|
QProcess process;
|
|
process.setWorkingDirectory(temp.path());
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
env.insert("ELF_EMU_SDK", sdk);
|
|
env.insert("QT_QPA_PLATFORM", "elf-emu-invalid");
|
|
process.setProcessEnvironment(env);
|
|
QString command = QCoreApplication::applicationDirPath() + "/elf-emu";
|
|
QStringList args = { "-d", "-n", "-r", root, "-f", elf, "-V" };
|
|
|
|
process.start(command, args);
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
QString output = process.readAll();
|
|
QVERIFY(output.contains("'--file' '0:/test.elf'"));
|
|
QVERIFY(output.contains("'--verbose'"));
|
|
QVERIFY(output.contains("source=" + sdk));
|
|
|
|
process.start(command, { "--dry-run", "--no-gui", "--root", root, "--file", elf, "--verbose" });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
output = process.readAll();
|
|
QVERIFY(output.contains("'--file' '0:/test.elf'"));
|
|
QVERIFY(output.contains("'--verbose'"));
|
|
|
|
process.start(command, { "-d", "-r", root, "-f", elf });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
output = process.readAll();
|
|
QRegularExpression ipc_port_re("'--publish' '127\\.0\\.0\\.1:(\\d+):\\1'.*'--ipc-port' '\\1'");
|
|
QVERIFY(ipc_port_re.match(output).hasMatch());
|
|
|
|
process.start(command, args + QStringList({ elf }));
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 1);
|
|
QVERIFY(process.readAll().contains("Unexpected positional arguments."));
|
|
|
|
process.start(command, { "-d", "-n", "-r", root });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 1);
|
|
QVERIFY(process.readAll().contains("Missing required --file option."));
|
|
|
|
QString external = temp.path() + "/external";
|
|
QString external_elf = external + "/test.elf";
|
|
QVERIFY(QDir().mkpath(external));
|
|
createFile(external_elf);
|
|
for (QString platform: { "sg", "nsg" }) {
|
|
QString drive = platform == "sg" ? "1:" : "a:";
|
|
process.start(command, { "-d", "-n", "-r", root, "-p", platform,
|
|
"-B", external + "=" + drive + "/ZBin", "-f", external_elf });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
output = process.readAll();
|
|
QVERIFY(output.contains("'--file' '" + drive + "/ZBin/test.elf'"));
|
|
}
|
|
|
|
process.start(command, { "-v" });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
QVERIFY(process.readAll().contains("1.0.0"));
|
|
|
|
process.start(command, { "--help" });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
QVERIFY(process.readAll().contains("--file"));
|
|
}
|
|
|
|
void LauncherTest::findsSdkInParentOrHome() {
|
|
QTemporaryDir temp;
|
|
QVERIFY(temp.isValid());
|
|
QString bin = temp.path() + "/apps/nested/bin";
|
|
QString sdk = temp.path() + "/apps/sdk";
|
|
QString home = temp.path() + "/home";
|
|
QString root = temp.path() + "/root";
|
|
QString elf = root + "/Data/test.elf";
|
|
QVERIFY(QDir().mkpath(bin));
|
|
QVERIFY(QDir().mkpath(sdk + "/swilib/include"));
|
|
QVERIFY(QDir().mkpath(root + "/Data"));
|
|
createFile(elf);
|
|
|
|
QString command = bin + "/elf-emu";
|
|
QVERIFY(QFile::copy(QCoreApplication::applicationDirPath() + "/elf-emu", command));
|
|
QProcess process;
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
env.remove("ELF_EMU_SDK");
|
|
process.setProcessEnvironment(env);
|
|
process.start(command, { "-d", "-n", "-r", root, "-f", elf });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
QVERIFY(QString::fromLocal8Bit(process.readAll()).contains("source=" + sdk));
|
|
|
|
QString home_sdk = home + "/dev/sie/sdk";
|
|
QVERIFY(QDir().mkpath(home + "/dev/sie"));
|
|
QVERIFY(QDir().rename(sdk, home_sdk));
|
|
env.insert("HOME", home);
|
|
process.setProcessEnvironment(env);
|
|
process.start(command, { "-d", "-n", "-r", root, "-f", elf });
|
|
QVERIFY(process.waitForFinished(10000));
|
|
QCOMPARE(process.exitCode(), 0);
|
|
QVERIFY(QString::fromLocal8Bit(process.readAll()).contains("source=" + home_sdk));
|
|
}
|
|
|
|
QTEST_GUILESS_MAIN(LauncherTest)
|
|
|
|
#include "LauncherTest.moc"
|