// Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include #include #include #include #include #include #include #include #include #include #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 typedef QMap IntStringMap; typedef QMap StringStringMap; typedef QMap ObjectPathStringMap; typedef QMap LLDateTimeMap; typedef QMap SignatureStringMap; Q_DECLARE_METATYPE(StringStringMap) Q_DECLARE_METATYPE(LLDateTimeMap) static bool compare(const QDBusUnixFileDescriptor &t1, const QDBusUnixFileDescriptor &t2) { int fd1 = t1.fileDescriptor(); int fd2 = t2.fileDescriptor(); if ((fd1 == -1 || fd2 == -1) && fd1 != fd2) { // one is valid, the other isn't return false; } #ifdef Q_OS_UNIX return compareFileDescriptors(fd1, fd2); #else return true; #endif } struct MyStruct { int i; QString s; inline bool operator==(const MyStruct &other) const { return i == other.i && s == other.s; } }; Q_DECLARE_METATYPE(MyStruct) QDBusArgument &operator<<(QDBusArgument &arg, const MyStruct &ms) { arg.beginStructure(); arg << ms.i << ms.s; arg.endStructure(); return arg; } const QDBusArgument &operator>>(const QDBusArgument &arg, MyStruct &ms) { arg.beginStructure(); arg >> ms.i >> ms.s; arg.endStructure(); return arg; } struct MyVariantMapStruct { QString s; QVariantMap map; inline bool operator==(const MyVariantMapStruct &other) const { return s == other.s && map == other.map; } }; Q_DECLARE_METATYPE(MyVariantMapStruct) QDBusArgument &operator<<(QDBusArgument &arg, const MyVariantMapStruct &ms) { arg.beginStructure(); arg << ms.s << ms.map; arg.endStructure(); return arg; } const QDBusArgument &operator>>(const QDBusArgument &arg, MyVariantMapStruct &ms) { arg.beginStructure(); arg >> ms.s >> ms.map; arg.endStructure(); return arg; } struct MyFileDescriptorStruct { QDBusUnixFileDescriptor fd; inline bool operator==(const MyFileDescriptorStruct &other) const { return compare(fd, other.fd); } }; Q_DECLARE_METATYPE(MyFileDescriptorStruct) QDBusArgument &operator<<(QDBusArgument &arg, const MyFileDescriptorStruct &ms) { arg.beginStructure(); arg << ms.fd; arg.endStructure(); return arg; } const QDBusArgument &operator>>(const QDBusArgument &arg, MyFileDescriptorStruct &ms) { arg.beginStructure(); arg >> ms.fd; arg.endStructure(); return arg; } void commonInit() { qDBusRegisterMetaType >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType > >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType >(); qDBusRegisterMetaType(); qDBusRegisterMetaType(); qDBusRegisterMetaType >(); qDBusRegisterMetaType(); qDBusRegisterMetaType >(); } #ifdef USE_PRIVATE_CODE #include "private/qdbusintrospection_p.h" // just to make it easier: typedef QDBusIntrospection::Interfaces InterfaceMap; typedef QDBusIntrospection::Objects ObjectMap; typedef QDBusIntrospection::Arguments ArgumentList; typedef QDBusIntrospection::Annotations AnnotationsMap; typedef QDBusIntrospection::Methods MethodMap; typedef QDBusIntrospection::Signals SignalMap; typedef QDBusIntrospection::Properties PropertyMap; Q_DECLARE_METATYPE(QDBusIntrospection::Method) Q_DECLARE_METATYPE(QDBusIntrospection::Signal) Q_DECLARE_METATYPE(QDBusIntrospection::Property) Q_DECLARE_METATYPE(MethodMap) Q_DECLARE_METATYPE(SignalMap) Q_DECLARE_METATYPE(PropertyMap) inline QDBusIntrospection::Argument arg(const char* type, const char *name = 0) { QDBusIntrospection::Argument retval; retval.type = QLatin1String(type); retval.name = QLatin1String(name); return retval; } template inline QMap& operator<<(QMap& map, const T& m) { map.insert(m.name, m); return map; } template inline QMultiMap& operator<<(QMultiMap& map, const T& m) { map.insert(m.name, m); return map; } inline const char* mapName(const MethodMap&) { return "MethodMap"; } inline const char* mapName(const SignalMap&) { return "SignalMap"; } inline const char* mapName(const PropertyMap&) { return "PropertyMap"; } QString printable(const QDBusIntrospection::Method& m) { QString result = "method " + m.name + "("; foreach (QDBusIntrospection::Argument arg, m.inputArgs) result += QString("in %1 %2, ") .arg(arg.type, arg.name); foreach (QDBusIntrospection::Argument arg, m.outputArgs) result += QString("out %1 %2, ") .arg(arg.type, arg.name); AnnotationsMap::const_iterator it = m.annotations.begin(); for ( ; it != m.annotations.end(); ++it) result += QString("%1 \"%2\", ").arg(it.key()).arg(it.value().value); result += ")"; return result; } QString printable(const QDBusIntrospection::Signal& s) { QString result = "signal " + s.name + "("; foreach (QDBusIntrospection::Argument arg, s.outputArgs) result += QString("out %1 %2, ") .arg(arg.type, arg.name); AnnotationsMap::const_iterator it = s.annotations.begin(); for ( ; it != s.annotations.end(); ++it) result += QString("%1 \"%2\", ").arg(it.key()).arg(it.value().value); result += ")"; return result; } QString printable(const QDBusIntrospection::Property& p) { QString result; if (p.access == QDBusIntrospection::Property::Read) result = "property read %1 %2, "; else if (p.access == QDBusIntrospection::Property::Write) result = "property write %1 %2, "; else result = "property readwrite %1 %2, "; result = result.arg(p.type, p.name); AnnotationsMap::const_iterator it = p.annotations.begin(); for ( ; it != p.annotations.end(); ++it) result += QString("%1 \"%2\", ").arg(it.key()).arg(it.value().value); return result; } template char* printableMap(const Map& map) { QString contents = "\n"; auto it = map.begin(); for ( ; it != map.end(); ++it) { if (it.key() != it.value().name) contents += it.value().name + ":"; contents += printable(it.value()); contents += ";\n"; } QString result("%1(size = %2): {%3}"); return qstrdup(qPrintable(result .arg(mapName(map)) .arg(map.size()) .arg(contents))); } QT_BEGIN_NAMESPACE namespace QTest { template<> inline char* toString(const MethodMap& map) { return printableMap(map); } template<> inline char* toString(const SignalMap& map) { return printableMap(map); } template<> inline char* toString(const PropertyMap& map) { return printableMap(map); } } QT_END_NAMESPACE #endif template bool compare(const T &t1, const T &t2) { return t1 == t2; } template<> bool compare(const QVariant &v1, const QVariant &v2); bool compare(double d1, double d2) { if (qIsNaN(d1) && qIsNaN(d2)) return true; return d1 == d2; } template<> bool compare(const QString &s1, const QString &s2) { if (s1.isEmpty() && s2.isEmpty()) return true; // regardless of whether one of them is null return s1 == s2; } template<> bool compare(const QByteArray &ba1, const QByteArray &ba2) { if (ba1.isEmpty() && ba2.isEmpty()) return true; // regardless of whether one of them is null return ba1 == ba2; } template<> bool compare(const QDBusVariant &s1, const QDBusVariant &s2) { return compare(s1.variant(), s2.variant()); } template bool compare(const QList &l1, const QList &l2) { if (l1.size() != l2.size()) return false; typename QList::ConstIterator it1 = l1.constBegin(); typename QList::ConstIterator it2 = l2.constBegin(); typename QList::ConstIterator end = l1.constEnd(); for ( ; it1 != end; ++it1, ++it2) if (!compare(*it1, *it2)) return false; return true; } template bool compare(const QMap &m1, const QMap &m2) { if (m1.size() != m2.size()) return false; typename QMap::ConstIterator i1 = m1.constBegin(); typename QMap::ConstIterator end = m1.constEnd(); for ( ; i1 != end; ++i1) { typename QMap::ConstIterator i2 = m2.find(i1.key()); if (i2 == m2.constEnd()) return false; if (!compare(*i1, *i2)) return false; } return true; } template inline bool compare(const QDBusArgument &arg, const QVariant &v2, T * = 0) { return compare(qdbus_cast(arg), qvariant_cast(v2)); } bool compareToArgument(const QDBusArgument &arg, const QVariant &v2) { if (arg.currentSignature() != QDBusMetaType::typeToSignature(v2.metaType())) return false; // try to demarshall the arg according to v2 switch (v2.userType()) { case QMetaType::Bool: return compare(arg, v2); case QMetaType::UChar: return compare(arg, v2); case QMetaType::Short: return compare(arg, v2); case QMetaType::UShort: return compare(arg, v2); case QMetaType::Int: return compare(arg, v2); case QMetaType::UInt: return compare(arg, v2); case QMetaType::LongLong: return compare(arg, v2); case QMetaType::ULongLong: return compare(arg, v2); case QMetaType::Double: return compare(arg, v2); case QMetaType::QString: return compare(arg, v2); case QMetaType::QByteArray: return compare(arg, v2); case QMetaType::QVariantList: return compare(arg, v2); case QMetaType::QVariantMap: return compare(arg, v2); case QMetaType::QPoint: return compare(arg, v2); case QMetaType::QPointF: return compare(arg, v2); case QMetaType::QSize: return compare(arg, v2); case QMetaType::QSizeF: return compare(arg, v2); case QMetaType::QLine: return compare(arg, v2); case QMetaType::QLineF: return compare(arg, v2); case QMetaType::QRect: return compare(arg, v2); case QMetaType::QRectF: return compare(arg, v2); case QMetaType::QDate: return compare(arg, v2); case QMetaType::QTime: return compare(arg, v2); case QMetaType::QDateTime: return compare(arg, v2); default: int id = v2.userType(); if (id == qMetaTypeId()) return compare(arg, v2); else if (id == qMetaTypeId()) return compare(arg, v2); else if (id == qMetaTypeId()) return compare(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId > >()) return compare > >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId()) return compare(arg, v2); else if (id == qMetaTypeId()) return compare(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); else if (id == qMetaTypeId()) return compare(arg, v2); else if (id == qMetaTypeId >()) return compare >(arg, v2); } qWarning() << "Unexpected QVariant type" << v2.userType() << QByteArray(QDBusMetaType::typeToSignature(v2.metaType())) << v2.metaType().name(); return false; } template<> bool compare(const QVariant &v1, const QVariant &v2) { // v1 is the one that came from the network // v2 is the one that we sent if (v1.metaType() == QMetaType::fromType()) // this argument has been left un-demarshalled return compareToArgument(qvariant_cast(v1), v2); if (v1.userType() != v2.userType()) return false; int id = v1.userType(); if (id == QMetaType::QVariantList) return compare(v1.toList(), v2.toList()); else if (id == QMetaType::QVariantMap) return compare(v1.toMap(), v2.toMap()); else if (id == QMetaType::QString) return compare(v1.toString(), v2.toString()); else if (id == QMetaType::QByteArray) return compare(v1.toByteArray(), v2.toByteArray()); else if (id == QMetaType::UChar) return qvariant_cast(v1) == qvariant_cast(v2); else if (id == QMetaType::Short) return qvariant_cast(v1) == qvariant_cast(v2); else if (id == QMetaType::UShort) return qvariant_cast(v1) == qvariant_cast(v2); else if (id == qMetaTypeId()) return qvariant_cast(v1).path() == qvariant_cast(v2).path(); else if (id == qMetaTypeId()) return qvariant_cast(v1).signature() == qvariant_cast(v2).signature(); else if (id == qMetaTypeId()) return compare(qvariant_cast(v1), qvariant_cast(v2)); else if (id == qMetaTypeId()) return compare(qvariant_cast(v1).variant(), qvariant_cast(v2).variant()); else if (id == qMetaTypeId()) return compare(qvariant_cast(v1), qvariant_cast(v2)); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return qvariant_cast >(v2) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return compare(qvariant_cast >(v1), qvariant_cast >(v2)); else if (id == qMetaTypeId()) return compare(qvariant_cast(v1), qvariant_cast(v2)); else if (id == qMetaTypeId > >()) return qvariant_cast > >(v1) == qvariant_cast > >(v2); else if (id == qMetaTypeId > >()) return qvariant_cast > >(v1) == qvariant_cast > >(v2); else if (id == qMetaTypeId > >()) return qvariant_cast > >(v1) == qvariant_cast > >(v2); else if (id == qMetaTypeId > >()) return qvariant_cast > >(v1) == qvariant_cast > >(v2); else if (id == qMetaTypeId > >()) return qvariant_cast > >(v1) == qvariant_cast > >(v2); else if (id == qMetaTypeId > >()) return qvariant_cast > >(v1) == qvariant_cast > >(v2); else if (id == qMetaTypeId > >()) return qvariant_cast > >(v1) == qvariant_cast > >(v2); else if (id == qMetaTypeId > >()) return compare(qvariant_cast > >(v1), qvariant_cast > >(v2)); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return qvariant_cast >(v1) == qvariant_cast >(v2); else if (id == qMetaTypeId >()) return compare(qvariant_cast >(v1), qvariant_cast >(v2)); else if (id == qMetaTypeId >()) return compare(qvariant_cast >(v1), qvariant_cast >(v2)); else if (id == qMetaTypeId >()) // ssmap return compare(qvariant_cast >(v1), qvariant_cast >(v2)); else if (id == qMetaTypeId >()) return compare(qvariant_cast >(v1), qvariant_cast >(v2)); else if (id == qMetaTypeId >()) // lldtmap return compare(qvariant_cast >(v1), qvariant_cast >(v2)); else if (id == qMetaTypeId >()) return compare(qvariant_cast >(v1), qvariant_cast >(v2)); else if (id == qMetaTypeId()) // (is) return qvariant_cast(v1) == qvariant_cast(v2); else if (id < int(QMetaType::User)) // yes, v1.type() // QVariant can compare return v1 == v2; else { qWarning() << "Please write a comparison case for type" << v1.typeName(); return false; // unknown type } }