void tst_QDnsLookup::lookupAbortRetry() { QDnsLookup lookup; // try and abort the lookup lookup.setType(QDnsLookup::A); lookup.setName(domainName("a-single")); lookup.lookup(); lookup.abort(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); QCOMPARE(int(lookup.error()), int(QDnsLookup::OperationCancelledError)); QVERIFY(lookup.hostAddressRecords().isEmpty()); // retry a different lookup lookup.setType(QDnsLookup::AAAA); lookup.setName(domainName("aaaa-single")); lookup.lookup(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); #if defined(Q_OS_ANDROID) if (lookup.errorString() == QStringLiteral("Not yet supported on Android")) QEXPECT_FAIL("", "Not yet supported on Android", Abort); #endif QCOMPARE(int(lookup.error()), int(QDnsLookup::NoError)); QVERIFY(!lookup.hostAddressRecords().isEmpty()); QCOMPARE(lookup.hostAddressRecords().first().name(), domainName("aaaa-single")); QCOMPARE(lookup.hostAddressRecords().first().value(), QHostAddress("2001:db8::1")); }
void tst_QDnsLookup::lookupAbortRetry() { QDnsLookup lookup; // try and abort the lookup lookup.setType(QDnsLookup::A); lookup.setName(domainName("a-single")); lookup.lookup(); lookup.abort(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); QCOMPARE(int(lookup.error()), int(QDnsLookup::OperationCancelledError)); QVERIFY(lookup.hostAddressRecords().isEmpty()); // retry a different lookup lookup.setType(QDnsLookup::AAAA); lookup.setName(domainName("aaaa-single")); lookup.lookup(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); QCOMPARE(int(lookup.error()), int(QDnsLookup::NoError)); QVERIFY(!lookup.hostAddressRecords().isEmpty()); QCOMPARE(lookup.hostAddressRecords().first().name(), domainName("aaaa-single")); QCOMPARE(lookup.hostAddressRecords().first().value(), QHostAddress("2001:db8::1")); }
void tst_QDnsLookup::lookupReuse() { QDnsLookup lookup; // first lookup lookup.setType(QDnsLookup::A); lookup.setName(domainName("a-single")); lookup.lookup(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); QCOMPARE(int(lookup.error()), int(QDnsLookup::NoError)); QVERIFY(!lookup.hostAddressRecords().isEmpty()); QCOMPARE(lookup.hostAddressRecords().first().name(), domainName("a-single")); QCOMPARE(lookup.hostAddressRecords().first().value(), QHostAddress("192.0.2.1")); // second lookup lookup.setType(QDnsLookup::AAAA); lookup.setName(domainName("aaaa-single")); lookup.lookup(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); QCOMPARE(int(lookup.error()), int(QDnsLookup::NoError)); QVERIFY(!lookup.hostAddressRecords().isEmpty()); QCOMPARE(lookup.hostAddressRecords().first().name(), domainName("aaaa-single")); QCOMPARE(lookup.hostAddressRecords().first().value(), QHostAddress("2001:db8::1")); }
static bool currentProcessIsService() { typedef BOOL (WINAPI *PtrGetUserName)(LPTSTR lpBuffer, LPDWORD lpnSize); typedef BOOL (WINAPI *PtrLookupAccountName)(LPCTSTR lpSystemName, LPCTSTR lpAccountName, PSID Sid, LPDWORD cbSid, LPTSTR ReferencedDomainName, LPDWORD cchReferencedDomainName, PSID_NAME_USE peUse); static PtrGetUserName ptrGetUserName = (PtrGetUserName)QSystemLibrary::resolve(QLatin1String("Advapi32"), "GetUserNameW"); static PtrLookupAccountName ptrLookupAccountName = (PtrLookupAccountName)QSystemLibrary::resolve(QLatin1String("Advapi32"), "LookupAccountNameW"); if (ptrGetUserName && ptrLookupAccountName) { wchar_t userName[UNLEN + 1] = L""; DWORD size = UNLEN; if (ptrGetUserName(userName, &size)) { SID_NAME_USE type = SidTypeUser; DWORD sidSize = 0; DWORD domainSize = 0; // first call is to get the correct size bool bRet = ptrLookupAccountName(NULL, userName, NULL, &sidSize, NULL, &domainSize, &type); if (bRet == FALSE && ERROR_INSUFFICIENT_BUFFER != GetLastError()) return false; QVarLengthArray<BYTE, 68> buff(sidSize); QVarLengthArray<wchar_t, MAX_PATH> domainName(domainSize); // second call to LookupAccountNameW actually gets the SID // both the pointer to the buffer and the pointer to the domain name should not be NULL if (ptrLookupAccountName(NULL, userName, buff.data(), &sidSize, domainName.data(), &domainSize, &type)) return type != SidTypeUser; //returns true if the current user is not a user } } return false; }
QString tst_QDnsLookup::domainNameList(const QString &input) { QStringList list = input.split(QLatin1Char(';')); QString result; foreach (const QString &s, list) { if (!result.isEmpty()) result += ';'; result += domainName(s); } return result; }
void tst_QDnsLookup::lookup() { QFETCH(int, type); QFETCH(QString, domain); QFETCH(int, error); QFETCH(QString, cname); QFETCH(QString, host); QFETCH(QString, mx); QFETCH(QString, ns); QFETCH(QString, ptr); QFETCH(QString, srv); QFETCH(QString, txt); // transform the inputs domain = domainName(domain); cname = domainName(cname); ns = domainNameList(ns); ptr = domainNameList(ptr); // SRV and MX have reply entries that can change order // and we can't sort QStringList mx_alternatives = domainNameListAlternatives(mx); QStringList srv_alternatives = domainNameListAlternatives(srv); QDnsLookup lookup; lookup.setType(static_cast<QDnsLookup::Type>(type)); lookup.setName(domain); lookup.lookup(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); #if defined(Q_OS_ANDROID) if (lookup.errorString() == QStringLiteral("Not yet supported on Android")) QEXPECT_FAIL("", "Not yet supported on Android", Abort); #endif QVERIFY2(int(lookup.error()) == error, msgDnsLookup(lookup.error(), error, domain, cname, host, srv, mx, ns, ptr, lookup.errorString())); if (error == QDnsLookup::NoError) QVERIFY(lookup.errorString().isEmpty()); QCOMPARE(int(lookup.type()), type); QCOMPARE(lookup.name(), domain); // canonical names if (!cname.isEmpty()) { QVERIFY(!lookup.canonicalNameRecords().isEmpty()); const QDnsDomainNameRecord cnameRecord = lookup.canonicalNameRecords().first(); QCOMPARE(cnameRecord.name(), domain); QCOMPARE(cnameRecord.value(), cname); } else { QVERIFY(lookup.canonicalNameRecords().isEmpty()); } // host addresses const QString hostName = cname.isEmpty() ? domain : cname; QStringList addresses; foreach (const QDnsHostAddressRecord &record, lookup.hostAddressRecords()) { //reply may include A & AAAA records for nameservers, ignore them and only look at records matching the query if (record.name() == hostName) addresses << record.value().toString().toLower(); } addresses.sort(); QCOMPARE(addresses.join(';'), host); // mail exchanges QStringList mailExchanges; foreach (const QDnsMailExchangeRecord &record, lookup.mailExchangeRecords()) { QCOMPARE(record.name(), domain); mailExchanges << QString("%1 %2").arg(QString::number(record.preference()), record.exchange()); } QVERIFY2(mx_alternatives.contains(mailExchanges.join(';')), qPrintable("Actual: " + mailExchanges.join(';') + "\nExpected one of:\n" + mx_alternatives.join('\n'))); // name servers QStringList nameServers; foreach (const QDnsDomainNameRecord &record, lookup.nameServerRecords()) { //reply may include NS records for authoritative nameservers, ignore them and only look at records matching the query if (record.name() == domain) nameServers << record.value(); } nameServers.sort(); QCOMPARE(nameServers.join(';'), ns); // pointers if (!ptr.isEmpty()) { QVERIFY(!lookup.pointerRecords().isEmpty()); const QDnsDomainNameRecord ptrRecord = lookup.pointerRecords().first(); QCOMPARE(ptrRecord.name(), domain); QCOMPARE(ptrRecord.value(), ptr); } else { QVERIFY(lookup.pointerRecords().isEmpty()); } // services QStringList services; foreach (const QDnsServiceRecord &record, lookup.serviceRecords()) { QCOMPARE(record.name(), domain); services << QString("%1 %2 %3 %4").arg( QString::number(record.priority()), QString::number(record.weight()), QString::number(record.port()), record.target()); } QVERIFY2(srv_alternatives.contains(services.join(';')), qPrintable("Actual: " + services.join(';') + "\nExpected one of:\n" + srv_alternatives.join('\n'))); // text QStringList texts; foreach (const QDnsTextRecord &record, lookup.textRecords()) { QCOMPARE(record.name(), domain); QString text; foreach (const QByteArray &ba, record.values()) { if (!text.isEmpty()) text += '\0'; text += QString::fromLatin1(ba); } texts << text; } texts.sort(); QCOMPARE(texts.join(';'), txt); }
void tst_QDnsLookup::lookup() { QFETCH(int, type); QFETCH(QString, domain); QFETCH(int, error); QFETCH(QString, cname); QFETCH(QString, host); QFETCH(QString, mx); QFETCH(QString, ns); QFETCH(QString, ptr); QFETCH(QString, srv); QFETCH(QByteArray, txt); // transform the inputs domain = domainName(domain); cname = domainName(cname); mx = domainNameList(mx); ns = domainNameList(ns); ptr = domainNameList(ptr); srv = domainNameList(srv); QDnsLookup lookup; lookup.setType(static_cast<QDnsLookup::Type>(type)); lookup.setName(domain); lookup.lookup(); QVERIFY(waitForDone(&lookup)); QVERIFY(lookup.isFinished()); QVERIFY2(int(lookup.error()) == error, qPrintable(lookup.errorString())); if (error == QDnsLookup::NoError) QVERIFY(lookup.errorString().isEmpty()); QCOMPARE(int(lookup.type()), type); QCOMPARE(lookup.name(), domain); // canonical names if (!cname.isEmpty()) { QVERIFY(!lookup.canonicalNameRecords().isEmpty()); const QDnsDomainNameRecord cnameRecord = lookup.canonicalNameRecords().first(); QCOMPARE(cnameRecord.name(), domain); QCOMPARE(cnameRecord.value(), cname); } else { QVERIFY(lookup.canonicalNameRecords().isEmpty()); } // host addresses const QString hostName = cname.isEmpty() ? domain : cname; QStringList addresses; foreach (const QDnsHostAddressRecord &record, lookup.hostAddressRecords()) { //reply may include A & AAAA records for nameservers, ignore them and only look at records matching the query if (record.name() == hostName) addresses << record.value().toString().toLower(); } addresses.sort(); QCOMPARE(addresses.join(';'), host); // mail exchanges QStringList mailExchanges; foreach (const QDnsMailExchangeRecord &record, lookup.mailExchangeRecords()) { QCOMPARE(record.name(), domain); mailExchanges << QString("%1 %2").arg(QString::number(record.preference()), record.exchange()); } QCOMPARE(mailExchanges.join(';'), mx); // name servers QStringList nameServers; foreach (const QDnsDomainNameRecord &record, lookup.nameServerRecords()) { //reply may include NS records for authoritative nameservers, ignore them and only look at records matching the query if (record.name() == domain) nameServers << record.value(); } nameServers.sort(); QCOMPARE(nameServers.join(';'), ns); // pointers if (!ptr.isEmpty()) { QVERIFY(!lookup.pointerRecords().isEmpty()); const QDnsDomainNameRecord ptrRecord = lookup.pointerRecords().first(); QCOMPARE(ptrRecord.name(), domain); QCOMPARE(ptrRecord.value(), ptr); } else { QVERIFY(lookup.pointerRecords().isEmpty()); } // services QStringList services; foreach (const QDnsServiceRecord &record, lookup.serviceRecords()) { QCOMPARE(record.name(), domain); services << QString("%1 %2 %3 %4").arg( QString::number(record.priority()), QString::number(record.weight()), QString::number(record.port()), record.target()); } QCOMPARE(services.join(';'), srv); // text if (!txt.isEmpty()) { QVERIFY(!lookup.textRecords().isEmpty()); const QDnsTextRecord firstRecord = lookup.textRecords().first(); QCOMPARE(firstRecord.name(), domain); QCOMPARE(firstRecord.values().size(), 1); QCOMPARE(firstRecord.values().first(), txt); } else { QVERIFY(lookup.textRecords().isEmpty()); } }