Add autotests for configuration dependent network proxies

1. test that systemProxyForQuery returns something for all configs
2. test that QNetworkAccessManager uses the settings for the
   configuration it was started with.

Task-number: QTBUG-18618
Reviewed-by: Peter Hartmann
This commit is contained in:
Shane Kearns 2011-04-18 15:46:19 +01:00 committed by Markus Goetz
parent a342ba933a
commit 724671feca
2 changed files with 145 additions and 1 deletions

View File

@ -7,5 +7,5 @@ QT = core network
SOURCES += tst_qnetworkproxyfactory.cpp
symbian: TARGET.CAPABILITY = NetworkServices
symbian: TARGET.CAPABILITY = NetworkServices ReadUserData

View File

@ -41,20 +41,60 @@
#include <QtTest/QTest>
#include <QtTest/QTestEventLoop>
#include <qcoreapplication.h>
#include <qdebug.h>
#include <qnetworkproxy.h>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QList>
Q_DECLARE_METATYPE(QNetworkConfiguration);
Q_DECLARE_METATYPE(QList<QNetworkProxy>);
class tst_QNetworkProxyFactory : public QObject {
Q_OBJECT
public:
tst_QNetworkProxyFactory();
class QDebugProxyFactory : public QNetworkProxyFactory
{
public:
virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query = QNetworkProxyQuery())
{
returnedList = QNetworkProxyFactory::systemProxyForQuery(query);
requestCounter++;
return returnedList;
}
QList<QNetworkProxy> returnedList;
int requestCounter;
};
private slots:
void systemProxyForQuery() const;
#ifndef QT_NO_BEARERMANAGEMENT
void fromConfigurations();
void inNetworkAccessManager_data();
void inNetworkAccessManager();
#endif
private:
QString formatProxyName(const QNetworkProxy & proxy) const;
QDebugProxyFactory *factory;
};
tst_QNetworkProxyFactory::tst_QNetworkProxyFactory()
{
factory = new QDebugProxyFactory;
QNetworkProxyFactory::setApplicationProxyFactory(factory);
}
QString tst_QNetworkProxyFactory::formatProxyName(const QNetworkProxy & proxy) const
{
QString proxyName;
@ -96,5 +136,109 @@ void tst_QNetworkProxyFactory::systemProxyForQuery() const
QFAIL("One or more system proxy lookup failures occurred.");
}
#ifndef QT_NO_BEARERMANAGEMENT
//Purpose of this test is just to check systemProxyForQuery doesn't hang or crash
//with any given configuration including no configuration.
//We can't test it returns the right proxies without implementing the native proxy code
//again here, which would be testing our implementation against itself.
//Therefore it's just testing that something valid is returned (at least a NoProxy entry)
void tst_QNetworkProxyFactory::fromConfigurations()
{
QNetworkConfigurationManager manager;
QList<QNetworkProxy> proxies;
QUrl url(QLatin1String("http://qt.nokia.com"));
//get from known configurations
foreach (QNetworkConfiguration config, manager.allConfigurations()) {
QNetworkProxyQuery query(config, url, QNetworkProxyQuery::UrlRequest);
proxies = QNetworkProxyFactory::systemProxyForQuery(query);
QVERIFY(!proxies.isEmpty());
foreach (QNetworkProxy proxy, proxies) {
qDebug() << config.name() << " - " << config.identifier() << " - " << formatProxyName(proxy);
}
}
//get from default configuration
QNetworkProxyQuery defaultquery(url, QNetworkProxyQuery::UrlRequest);
proxies = QNetworkProxyFactory::systemProxyForQuery(defaultquery);
QVERIFY(!proxies.isEmpty());
foreach (QNetworkProxy proxy, proxies) {
qDebug() << "default - " << formatProxyName(proxy);
}
//get from active configuration
QNetworkSession session(manager.defaultConfiguration());
session.open();
QVERIFY(session.waitForOpened(30000));
proxies = QNetworkProxyFactory::systemProxyForQuery(defaultquery);
QVERIFY(!proxies.isEmpty());
foreach (QNetworkProxy proxy, proxies) {
qDebug() << "active - " << formatProxyName(proxy);
}
//get from known configurations while there is one active
foreach (QNetworkConfiguration config, manager.allConfigurations()) {
QNetworkProxyQuery query(config, url, QNetworkProxyQuery::UrlRequest);
proxies = QNetworkProxyFactory::systemProxyForQuery(query);
QVERIFY(!proxies.isEmpty());
foreach (QNetworkProxy proxy, proxies) {
qDebug() << config.name() << " - " << config.identifier() << " - " << formatProxyName(proxy);
}
}
}
void tst_QNetworkProxyFactory::inNetworkAccessManager_data()
{
QTest::addColumn<QNetworkConfiguration>("config");
QTest::addColumn<QList<QNetworkProxy> >("proxies");
QNetworkConfigurationManager manager;
//get from known configurations
foreach (QNetworkConfiguration config, manager.allConfigurations()) {
QNetworkProxyQuery query(config, QUrl(QString("http://qt.nokia.com")), QNetworkProxyQuery::UrlRequest);
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery(query);
QTest::newRow(config.name().toUtf8()) << config << proxies;
}
}
//Purpose of this test is to check that QNetworkAccessManager uses the proxy from the configuration it
//has been given. Needs two or more working configurations to be a good test.
void tst_QNetworkProxyFactory::inNetworkAccessManager()
{
QFETCH(QNetworkConfiguration, config);
QFETCH(QList<QNetworkProxy>, proxies);
int count = factory->requestCounter;
QNetworkAccessManager manager;
manager.setConfiguration(config);
//using an internet server, because cellular APs won't have a route to the test server.
QNetworkRequest req(QUrl(QString("http://qt.nokia.com")));
QNetworkReply *reply = manager.get(req);
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
QTestEventLoop::instance().enterLoop(30);
delete reply;
if (count == factory->requestCounter) {
//RND phones are preconfigured with several test access points which won't work without a matching SIM
//If the network fails to start, QNAM won't ask the factory for proxies so we can't test.
QSKIP("network configuration didn't start", SkipSingle);
}
qDebug() << "testing network configuration for" << config.name();
foreach (QNetworkProxy proxy, factory->returnedList) {
qDebug() << formatProxyName(proxy);
}
qDebug() << " <vs> ";
foreach (QNetworkProxy proxy, proxies) {
qDebug() << formatProxyName(proxy);
}
if (config.type() != QNetworkConfiguration::InternetAccessPoint)
QEXPECT_FAIL("","QNetworkProxyFactory::systemProxyForQuery doesn't work for service networks yet", Continue);
QCOMPARE(factory->returnedList, proxies);
}
#endif //QT_NO_BEARERMANAGEMENT
QTEST_MAIN(tst_QNetworkProxyFactory)
#include "tst_qnetworkproxyfactory.moc"