From 7647521500b0d1c1e382fff0d1a0f497f1fac70c Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 17 Feb 2011 17:01:14 +0100 Subject: Autotest: really ensure that two fds are equal Instead of checking that they are both valid or both invalid, test that they point to the same file on the filesystem. So we create a QTemporaryFile and pass its file descriptor to the remote side and back. If the two fds point to the same file on disk later (same st_dev and st_ino), they are equal. This probably works on non-Unix too, but I can't test and there's no point anyway. Task-number: QTBUG-17477 --- tests/auto/qdbusmarshall/common.h | 26 +++++++++++++++++++++++++- tests/auto/qdbusmarshall/tst_qdbusmarshall.cpp | 22 +++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/tests/auto/qdbusmarshall/common.h b/tests/auto/qdbusmarshall/common.h index 53bd0e5..8f7f3c3 100644 --- a/tests/auto/qdbusmarshall/common.h +++ b/tests/auto/qdbusmarshall/common.h @@ -39,6 +39,22 @@ ** ****************************************************************************/ #include // isnan +#include + +#ifdef Q_OS_UNIX +# include + +static bool compareFileDescriptors(int fd1, int fd2) +{ + QT_STATBUF st1, st2; + if (QT_FSTAT(fd1, &st1) == -1 || QT_FSTAT(fd2, &st2) == -1) { + perror("fstat"); + return false; + } + + return (st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino); +} +#endif Q_DECLARE_METATYPE(QVariant) Q_DECLARE_METATYPE(QList) @@ -81,8 +97,16 @@ static bool compare(const QDBusUnixFileDescriptor &t1, const QDBusUnixFileDescri { int fd1 = t1.fileDescriptor(); int fd2 = t2.fileDescriptor(); + if ((fd1 == -1 || fd2 == -1) && fd1 != fd2) { + // one is valid, the other isn't + return false; + } - return (fd1 == -1) == (fd2 == -1); +#ifdef Q_OS_UNIX + return compareFileDescriptors(fd1, fd2); +#else + return true; +#endif } struct MyStruct diff --git a/tests/auto/qdbusmarshall/tst_qdbusmarshall.cpp b/tests/auto/qdbusmarshall/tst_qdbusmarshall.cpp index b7cec3e..737f0cf 100644 --- a/tests/auto/qdbusmarshall/tst_qdbusmarshall.cpp +++ b/tests/auto/qdbusmarshall/tst_qdbusmarshall.cpp @@ -94,7 +94,10 @@ private slots: void receiveUnknownType(); private: + int fileDescriptorForTest(); + QProcess proc; + QTemporaryFile tempFile; bool fileDescriptorPassing; }; @@ -147,6 +150,15 @@ void tst_QDBusMarshall::cleanupTestCase() proc.waitForFinished(200); } +int tst_QDBusMarshall::fileDescriptorForTest() +{ + if (!tempFile.isOpen()) { + tempFile.setFileTemplate(QDir::tempPath() + "/qdbusmarshalltestXXXXXX.tmp"); + tempFile.open(); + } + return tempFile.handle(); +} + void tst_QDBusMarshall::sendBasic_data() { QTest::addColumn("value"); @@ -172,7 +184,7 @@ void tst_QDBusMarshall::sendBasic_data() QTest::newRow("nullstring") << QVariant(QString()) << "s" << "\"\""; if (fileDescriptorPassing) - QTest::newRow("file-descriptor") << qVariantFromValue(QDBusUnixFileDescriptor(0)) << "h" << "[Unix FD: valid]"; + QTest::newRow("file-descriptor") << qVariantFromValue(QDBusUnixFileDescriptor(fileDescriptorForTest())) << "h" << "[Unix FD: valid]"; #endif } @@ -269,7 +281,7 @@ void tst_QDBusMarshall::sendArrays_data() if (fileDescriptorPassing) { QList fileDescriptors; QTest::newRow("emptyfiledescriptorlist") << qVariantFromValue(fileDescriptors) << "ah" << "[Argument: ah {}]"; - fileDescriptors << QDBusUnixFileDescriptor(0) << QDBusUnixFileDescriptor(1); + fileDescriptors << QDBusUnixFileDescriptor(fileDescriptorForTest()) << QDBusUnixFileDescriptor(1); QTest::newRow("filedescriptorlist") << qVariantFromValue(fileDescriptors) << "ah" << "[Argument: ah {[Unix FD: valid], [Unix FD: valid]}]"; } @@ -475,7 +487,7 @@ void tst_QDBusMarshall::sendMaps_data() << "[Argument: a{gs} {[Signature: a{gs}] = \"array of dict_entry of (signature, string)\", [Signature: i] = \"int32\", [Signature: s] = \"string\"}]"; if (fileDescriptorPassing) { - svmap["zzfiledescriptor"] = qVariantFromValue(QDBusUnixFileDescriptor(0)); + svmap["zzfiledescriptor"] = qVariantFromValue(QDBusUnixFileDescriptor(fileDescriptorForTest())); QTest::newRow("sv-map1-fd") << qVariantFromValue(svmap) << "a{sv}" << "[Argument: a{sv} {\"a\" = [Variant(int): 1], \"b\" = [Variant(QByteArray): {99}], \"c\" = [Variant(QString): \"b\"], \"d\" = [Variant(uint): 42], \"e\" = [Variant(short): -47], \"f\" = [Variant: [Variant(int): 0]], \"zzfiledescriptor\" = [Variant(QDBusUnixFileDescriptor): [Unix FD: valid]]}]"; } @@ -536,7 +548,7 @@ void tst_QDBusMarshall::sendStructs_data() if (fileDescriptorPassing) { MyFileDescriptorStruct fds; - fds.fd = QDBusUnixFileDescriptor(0); + fds.fd = QDBusUnixFileDescriptor(fileDescriptorForTest()); QTest::newRow("fdstruct") << qVariantFromValue(fds) << "(h)" << "[Argument: (h) [Unix FD: valid]]"; QList fdlist; @@ -680,7 +692,7 @@ void tst_QDBusMarshall::sendArgument_data() if (fileDescriptorPassing) { arg = QDBusArgument(); - arg << QDBusUnixFileDescriptor(0); + arg << QDBusUnixFileDescriptor(fileDescriptorForTest()); QTest::newRow("filedescriptor") << qVariantFromValue(arg) << "h" << int(QDBusArgument::BasicType); } -- cgit v0.12