summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io/qlockfile
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@theqtcompany.com>2015-04-29 14:36:24 +0200
committerJoerg Bornemann <joerg.bornemann@theqtcompany.com>2015-05-26 11:01:14 +0000
commit80c8d324b335753d5b1758f29775b844904bb2c6 (patch)
tree8865d7629eba535fbf9564d15f15025641375a8e /tests/auto/corelib/io/qlockfile
parenta0e5210d8b6b21d33800e1fac30efbf2868f9f5b (diff)
take process name into account for QLockFile's pid clash resolution
To cover the situation that the process ID got reused, the current process name is compared to the name of the process that corresponds to the process ID from the lock file. If the process names differ, the lock file is considered stale. [ChangeLog][QtCore][QLockFile] Detection of stale lock files got more robust and takes the name of the process that belongs to the stored PID into account. Task-number: QTBUG-45497 Change-Id: Ic3c0d7e066435451203e77b9b9ce2d70bfb9c570 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'tests/auto/corelib/io/qlockfile')
-rw-r--r--tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
index 8d890e81fa..27614e0eb8 100644
--- a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
+++ b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
@@ -57,6 +57,7 @@ private slots:
void waitForLock();
void staleLockFromCrashedProcess_data();
void staleLockFromCrashedProcess();
+ void staleLockFromCrashedProcessReusedPid();
void staleShortLockFromBusyProcess();
void staleLongLockFromBusyProcess();
void staleLockRace();
@@ -64,6 +65,9 @@ private slots:
void noPermissionsWindows();
void corruptedLockFile();
+private:
+ static bool overwritePidInLockFile(const QString &filePath, qint64 pid);
+
public:
QString m_helperApp;
QTemporaryDir dir;
@@ -277,6 +281,30 @@ void tst_QLockFile::staleLockFromCrashedProcess()
#endif // !QT_NO_PROCESS
}
+void tst_QLockFile::staleLockFromCrashedProcessReusedPid()
+{
+#if defined(QT_NO_PROCESS)
+ QSKIP("This test requires QProcess support");
+#elif defined(Q_OS_WINRT) || defined(Q_OS_WINCE) || defined(Q_OS_IOS)
+ QSKIP("We cannot retrieve information about other processes on this platform.");
+#else
+ const QString fileName = dir.path() + "/staleLockFromCrashedProcessReusedPid";
+
+ int ret = QProcess::execute(m_helperApp, QStringList() << fileName << "-crash");
+ QCOMPARE(ret, int(QLockFile::NoError));
+ QVERIFY(QFile::exists(fileName));
+ QVERIFY(overwritePidInLockFile(fileName, QCoreApplication::applicationPid()));
+
+ QLockFile secondLock(fileName);
+ qint64 pid = 0;
+ secondLock.getLockInfo(&pid, 0, 0);
+ QCOMPARE(pid, QCoreApplication::applicationPid());
+ secondLock.setStaleLockTime(0);
+ QVERIFY(secondLock.tryLock());
+ QCOMPARE(int(secondLock.error()), int(QLockFile::NoError));
+#endif // !QT_NO_PROCESS
+}
+
void tst_QLockFile::staleShortLockFromBusyProcess()
{
#ifdef QT_NO_PROCESS
@@ -497,5 +525,25 @@ void tst_QLockFile::corruptedLockFile()
QCOMPARE(int(secondLock.error()), int(QLockFile::NoError));
}
+bool tst_QLockFile::overwritePidInLockFile(const QString &filePath, qint64 pid)
+{
+ QFile f(filePath);
+ if (!f.open(QFile::ReadWrite)) {
+ qWarning("Cannot open %s.", qPrintable(filePath));
+ return false;
+ }
+ QByteArray buf = f.readAll();
+ int i = buf.indexOf('\n');
+ if (i < 0) {
+ qWarning("Unexpected lockfile content.");
+ return false;
+ }
+ buf.remove(0, i);
+ buf.prepend(QByteArray::number(pid));
+ f.seek(0);
+ f.resize(buf.size());
+ return f.write(buf) == buf.size();
+}
+
QTEST_MAIN(tst_QLockFile)
#include "tst_qlockfile.moc"