test: add identify test

fix some bugs found in testing

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe
2023-08-15 14:43:34 +08:00
committed by Comix
parent de09f3dbc2
commit a3dd315e33
15 changed files with 250 additions and 85 deletions

View File

@ -16,14 +16,13 @@ IdentifyRet CGroupsIdentifier::Identify(pid_t pid)
qWarning() << "open " << AppCgroupPath << "failed: " << AppCgroupFile.errorString();
return {};
}
auto appInstance = parseCGroupsPath(
AppCgroupFile.readAll().split(':').last().trimmed()); // FIXME: support CGroup version detection and multi-line parsing
auto appInstanceComponent = appInstance.split('@');
if (appInstanceComponent.count() != 2) {
qWarning() << "Application Instance format is invalid." << appInstanceComponent;
return {};
}
return {appInstanceComponent.first(), appInstanceComponent.last()};
auto UnitStr = parseCGroupsPath(QString::fromLocal8Bit(AppCgroupFile.readAll())
.split(':', Qt::SkipEmptyParts)
.last()
.trimmed()); // FIXME: support CGroup version detection and multi-line parsing
auto [appId, InstanceId] = processUnitName(UnitStr);
return {std::move(appId), std::move(InstanceId)};
}
QString CGroupsIdentifier::parseCGroupsPath(const QString &CGP) noexcept
@ -32,8 +31,7 @@ QString CGroupsIdentifier::parseCGroupsPath(const QString &CGP) noexcept
qWarning() << "CGroupPath is empty.";
return {};
}
auto unescapedCGP = unescapeString(CGP);
auto CGPSlices = unescapedCGP.split('/');
auto CGPSlices = CGP.split('/', Qt::SkipEmptyParts);
if (CGPSlices.first() != "user.slice") {
qWarning() << "unrecognized process.";
@ -51,7 +49,7 @@ QString CGroupsIdentifier::parseCGroupsPath(const QString &CGP) noexcept
return {};
}
auto appInstance = CGPSlices.last().split('.').first();
auto appInstance = CGPSlices.last();
if (appInstance.isEmpty()) {
qWarning() << "get AppId failed.";
return {};

View File

@ -90,41 +90,6 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
});
}
QPair<QString, QString> ApplicationManager1Service::processUnitName(const QString &unitName) noexcept
{
QString instanceId;
QString applicationId;
if (unitName.endsWith(".service")) {
auto lastDotIndex = unitName.lastIndexOf('.');
auto app = unitName.sliced(0, lastDotIndex - 1); // remove suffix
if (app.contains('@')) {
auto atIndex = app.indexOf('@');
instanceId = app.sliced(atIndex + 1);
app.remove(atIndex, instanceId.length() + 1);
}
applicationId = app.split('-').last(); // drop launcher if it exists.
} else if (unitName.endsWith(".scope")) {
auto lastDotIndex = unitName.lastIndexOf('.');
auto app = unitName.sliced(0, lastDotIndex - 1);
auto components = app.split('-');
instanceId = components.takeLast();
applicationId = components.takeLast();
} else {
qDebug() << "it's not service or scope:" << unitName << "ignore.";
return {};
}
if (instanceId.isEmpty()) {
instanceId = QUuid::createUuid().toString(QUuid::Id128);
}
return qMakePair(unescapeApplicationId(applicationId), std::move(instanceId));
}
QList<QDBusObjectPath> ApplicationManager1Service::list() const
{
return m_applicationList.keys();

View File

@ -62,8 +62,6 @@ private:
std::unique_ptr<Identifier> m_identifier;
QScopedPointer<JobManager1Service> m_jobManager{nullptr};
QMap<QDBusObjectPath, QSharedPointer<ApplicationService>> m_applicationList;
static QPair<QString, QString> processUnitName(const QString &serviceName) noexcept;
};
#endif

View File

@ -64,7 +64,7 @@ public Q_SLOTS:
private:
friend class ApplicationManager1Service;
template <typename T>
ApplicationService(T &&source, ApplicationManager1Service *parent)
explicit ApplicationService(T &&source, ApplicationManager1Service *parent = nullptr)
: m_parent(parent)
, m_desktopSource(std::forward<T>(source))
{
@ -84,7 +84,9 @@ private:
m_isPersistence = true;
sourceFile.setFileName(m_desktopSource.m_file.filePath());
if (!sourceFile.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) {
#ifndef DEBUG_MODE
qCritical() << "desktop file can't open:" << m_desktopSource.m_file.filePath() << sourceFile.errorString();
#endif
return;
}
sourceStream.setDevice(&sourceFile);

View File

@ -18,6 +18,7 @@
#include <QRegularExpression>
#include <QDBusObjectPath>
#include <unistd.h>
#include <QUuid>
#include "constant.h"
#include "config.h"
@ -143,7 +144,7 @@ public:
return m_destConnection.value();
}
void setDestBus(const QString &destAddress)
void setDestBus(const QString &destAddress = "")
{
if (m_destConnection) {
m_destConnection->disconnectFromBus(ApplicationManagerDestDBusName);
@ -206,26 +207,6 @@ inline QLocale getUserLocale()
return QLocale::system(); // current use env
}
inline QString unescapeString(const QString &input)
{
QRegularExpression regex("\\\\x([0-9A-Fa-f]{2})");
QRegularExpressionMatchIterator it = regex.globalMatch(input);
QString output{input};
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
bool ok;
// Get the hexadecimal value from the match and convert it to a number.
int asciiValue = match.captured(1).toInt(&ok, 16);
if (ok) {
// Convert the ASCII value to a QChar and perform the replacement.
output.replace(match.capturedStart(), match.capturedLength(), QChar(asciiValue));
}
}
return output;
}
inline QString escapeToObjectPath(const QString &str)
{
if (str.isEmpty()) {
@ -333,4 +314,39 @@ inline QStringList getXDGDataDirs()
return XDGDataDirs;
}
inline QPair<QString, QString> processUnitName(const QString &unitName)
{
QString instanceId;
QString applicationId;
if (unitName.endsWith(".service")) {
auto lastDotIndex = unitName.lastIndexOf('.');
auto app = unitName.sliced(0, lastDotIndex); // remove suffix
if (app.contains('@')) {
auto atIndex = app.indexOf('@');
instanceId = app.sliced(atIndex + 1);
app.remove(atIndex, instanceId.length() + 1);
}
applicationId = app.split('-').last(); // drop launcher if it exists.
} else if (unitName.endsWith(".scope")) {
auto lastDotIndex = unitName.lastIndexOf('.');
auto app = unitName.sliced(0, lastDotIndex);
auto components = app.split('-');
instanceId = components.takeLast();
applicationId = components.takeLast();
} else {
qDebug() << "it's not service or scope:" << unitName << "ignore.";
return {};
}
if (instanceId.isEmpty()) {
instanceId = QUuid::createUuid().toString(QUuid::Id128);
}
return qMakePair(unescapeApplicationId(applicationId), std::move(instanceId));
}
#endif