summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkh <karsten.heimrich@theqtcompany.com>2014-11-24 17:39:06 +0100
committerKarsten Heimrich <karsten.heimrich@theqtcompany.com>2014-11-26 14:49:23 +0100
commit64d49f097f25dd019345a7e913e1c4cf8188e168 (patch)
treeda6670298b59bd04db7d1f5dc62a9ceb79082b7b
parentd18b9696e573aa7b3f38784f6c5764b9fe6fd81b (diff)
Implement a way to start the server in debug mode and API cleanup.
1; Passing debug as first argument to the starting server does not start the server side so the server keeps running in an endless loop. This makes it far easier to attach a debugger. 2; API cleanup and unify init function to take port, key, and mode. The address was never able to be changed anyway, so stop passing them around. Change-Id: I2a847f009ed1557a5e136e2b0006de5c62426da2 Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com>
-rw-r--r--src/libs/installer/packagemanagercore.cpp8
-rw-r--r--src/libs/installer/packagemanagercore.h6
-rw-r--r--src/libs/installer/packagemanagercore_p.cpp12
-rw-r--r--src/libs/installer/protocol.h4
-rw-r--r--src/libs/installer/remoteclient.cpp43
-rw-r--r--src/libs/installer/remoteclient.h8
-rw-r--r--src/libs/installer/remoteclient_p.h34
-rw-r--r--src/libs/installer/remoteserver.cpp44
-rw-r--r--src/libs/installer/remoteserver.h4
-rw-r--r--src/sdk/commandlineparser.cpp9
-rw-r--r--src/sdk/constants.h1
-rw-r--r--src/sdk/installerbase.cpp21
-rw-r--r--src/sdk/main.cpp37
-rw-r--r--tests/auto/installer/clientserver/tst_clientserver.cpp79
14 files changed, 194 insertions, 116 deletions
diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp
index 494b8b486..abff0bafa 100644
--- a/src/libs/installer/packagemanagercore.cpp
+++ b/src/libs/installer/packagemanagercore.cpp
@@ -690,15 +690,19 @@ PackageManagerCore::PackageManagerCore()
qRegisterMetaType<QInstaller::PackageManagerCore::WizardPage>("QInstaller::PackageManagerCore::WizardPage");
}
-PackageManagerCore::PackageManagerCore(qint64 magicmaker, const QList<OperationBlob> &operations)
+PackageManagerCore::PackageManagerCore(qint64 magicmaker, const QList<OperationBlob> &operations,
+ quint16 port, const QString &key, Protocol::Mode mode)
: d(new PackageManagerCorePrivate(this, magicmaker, operations))
{
Repository::registerMetaType(); // register, cause we stream the type as QVariant
qRegisterMetaType<QInstaller::PackageManagerCore::Status>("QInstaller::PackageManagerCore::Status");
qRegisterMetaType<QInstaller::PackageManagerCore::WizardPage>("QInstaller::PackageManagerCore::WizardPage");
- d->initialize(QHash<QString, QString>());
+ // Creates and initializes a remote client, makes us get admin rights for QFile, QSettings
+ // and QProcess operations. Init needs to called to set the server side authorization key.
+ RemoteClient::instance().init(port, key, mode, Protocol::StartAs::SuperUser);
+ d->initialize(QHash<QString, QString>());
//
// Sanity check to detect a broken installations with missing operations.
diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h
index 10e39f0f8..df7ac5557 100644
--- a/src/libs/installer/packagemanagercore.h
+++ b/src/libs/installer/packagemanagercore.h
@@ -35,6 +35,7 @@
#define PACKAGEMANAGERCORE_H
#include "binaryformat.h"
+#include "protocol.h"
#include "repository.h"
#include "qinstallerglobal.h"
@@ -64,7 +65,10 @@ class INSTALLER_EXPORT PackageManagerCore : public QObject
public:
PackageManagerCore();
- PackageManagerCore(qint64 magicmaker, const QList<OperationBlob> &ops);
+ PackageManagerCore(qint64 magicmaker, const QList<OperationBlob> &ops,
+ quint16 port = Protocol::DefaultPort,
+ const QString &key = QLatin1String(Protocol::DefaultAuthorizationKey),
+ Protocol::Mode mode = Protocol::Mode::Production);
~PackageManagerCore();
// status
diff --git a/src/libs/installer/packagemanagercore_p.cpp b/src/libs/installer/packagemanagercore_p.cpp
index 3c1c8b674..45a41d301 100644
--- a/src/libs/installer/packagemanagercore_p.cpp
+++ b/src/libs/installer/packagemanagercore_p.cpp
@@ -50,7 +50,6 @@
#include "qprocesswrapper.h"
#include "protocol.h"
#include "qsettingswrapper.h"
-#include "remoteclient.h"
#include "installercalculator.h"
#include "uninstallercalculator.h"
@@ -242,17 +241,6 @@ PackageManagerCorePrivate::PackageManagerCorePrivate(PackageManagerCore *core, q
m_performedOperationsOld.append(op.take());
}
- // Creates and initializes a remote client, makes us get admin rights for QFile, QSettings
- // and QProcess operations. Init needs to called before we can get the real authorization key.
- int port = 30000 + qrand() % 1000;
- RemoteClient::instance().init(port,
- QHostAddress(QLatin1String(Protocol::DefaultHostAddress)), Protocol::Mode::Release);
- RemoteClient::instance().setStartServerCommand(QCoreApplication::applicationFilePath(),
- QStringList() << QLatin1String("--startserver") << QString::fromLatin1("%1,%2")
- .arg(port)
- .arg(RemoteClient::instance().authorizationKey()),
- Protocol::StartAs::SuperUser);
-
connect(this, SIGNAL(installationStarted()), m_core, SIGNAL(installationStarted()));
connect(this, SIGNAL(installationFinished()), m_core, SIGNAL(installationFinished()));
connect(this, SIGNAL(uninstallationStarted()), m_core, SIGNAL(uninstallationStarted()));
diff --git a/src/libs/installer/protocol.h b/src/libs/installer/protocol.h
index 2aee34769..a4e58774f 100644
--- a/src/libs/installer/protocol.h
+++ b/src/libs/installer/protocol.h
@@ -40,8 +40,10 @@ namespace Protocol {
enum struct Mode {
Debug,
- Release
+ Production
};
+const char ModeDebug[] = "DEBUG";
+const char ModeProduction[] = "PRODUCTION";
enum struct StartAs {
User,
diff --git a/src/libs/installer/remoteclient.cpp b/src/libs/installer/remoteclient.cpp
index ce317847f..d8c8478b6 100644
--- a/src/libs/installer/remoteclient.cpp
+++ b/src/libs/installer/remoteclient.cpp
@@ -39,14 +39,6 @@
namespace QInstaller {
-class RemoteClientGuard
-{
-public:
- RemoteClient client;
-};
-Q_GLOBAL_STATIC(RemoteClientGuard, remoteClientGuard);
-static RemoteClientGuard *gGuard = remoteClientGuard();
-
RemoteClient::RemoteClient()
: d_ptr(new RemoteClientPrivate(this))
{
@@ -60,7 +52,8 @@ RemoteClient::~RemoteClient()
RemoteClient &RemoteClient::instance()
{
- return gGuard->client;
+ static RemoteClient instance;
+ return instance;
}
quint16 RemoteClient::port() const
@@ -81,18 +74,15 @@ QString RemoteClient::authorizationKey() const
return d->m_key;
}
-void RemoteClient::setAuthorizationKey(const QString &key)
-{
- Q_D(RemoteClient);
- if (d->m_serverStarted)
- return;
- d->m_key = key;
-}
-
-void RemoteClient::init(quint16 port, const QHostAddress &address, Protocol::Mode mode)
+/*!
+ Initializes the client with \a port, the port to write to, with \a key, the key the client
+ sends to authenticate with the server, \a mode and \a startAs.
+*/
+void RemoteClient::init(quint16 port, const QString &key, Protocol::Mode mode,
+ Protocol::StartAs startAs)
{
Q_D(RemoteClient);
- d->init(port, address, mode);
+ d->init(port, key, mode, startAs);
}
void RemoteClient::shutdown()
@@ -155,19 +145,4 @@ void RemoteClient::setActive(bool active)
}
}
-void RemoteClient::setStartServerCommand(const QString &command, Protocol::StartAs startAs)
-{
- setStartServerCommand(command, QStringList(), startAs);
-}
-
-void RemoteClient::setStartServerCommand(const QString &command, const QStringList &arguments,
- Protocol::StartAs startAs)
-{
- Q_D(RemoteClient);
- d->maybeStopServer();
- d->m_serverCommand = command;
- d->m_serverArguments = arguments;
- d->m_startServerAs = startAs;
-}
-
} // namespace QInstaller
diff --git a/src/libs/installer/remoteclient.h b/src/libs/installer/remoteclient.h
index f4bec7534..f8e9faf86 100644
--- a/src/libs/installer/remoteclient.h
+++ b/src/libs/installer/remoteclient.h
@@ -54,11 +54,10 @@ class INSTALLER_EXPORT RemoteClient : public QObject
Q_OBJECT
Q_DISABLE_COPY(RemoteClient)
Q_DECLARE_PRIVATE(RemoteClient)
- friend class RemoteClientGuard;
public:
static RemoteClient &instance();
- void init(quint16 port, const QHostAddress &address, Protocol::Mode mode);
+ void init(quint16 port, const QString &key, Protocol::Mode mode, Protocol::StartAs startAs);
void shutdown();
bool connect(QTcpSocket *socket) const;
@@ -66,15 +65,10 @@ public:
quint16 port() const;
QHostAddress address() const;
QString authorizationKey() const;
- void setAuthorizationKey(const QString &key);
bool isActive() const;
void setActive(bool active);
- void setStartServerCommand(const QString &command, Protocol::StartAs startAs);
- void setStartServerCommand(const QString &command, const QStringList &arguments,
- Protocol::StartAs start);
-
private:
RemoteClient();
~RemoteClient();
diff --git a/src/libs/installer/remoteclient_p.h b/src/libs/installer/remoteclient_p.h
index 6405b5fe8..fdcfabb64 100644
--- a/src/libs/installer/remoteclient_p.h
+++ b/src/libs/installer/remoteclient_p.h
@@ -48,7 +48,6 @@
#include <QTcpSocket>
#include <QThread>
#include <QTimer>
-#include <QUuid>
namespace QInstaller {
@@ -142,17 +141,21 @@ public:
m_thread.wait();
}
- void init(quint16 port, const QHostAddress &address, Protocol::Mode mode)
+ void init(quint16 port, const QString &key, Protocol::Mode mode, Protocol::StartAs startAs)
{
- m_port = port;
m_mode = mode;
- m_address = address;
+ if (mode == Protocol::Mode::Production) {
+ m_key = key;
+ m_port = port;
+ m_mode = mode;
+ m_startServerAs = startAs;
+ m_serverCommand = QCoreApplication::applicationFilePath();
+ m_serverArguments = QStringList() << QLatin1String("--startserver")
+ << QString::fromLatin1("%1,%2,%3")
+ .arg(QLatin1String(Protocol::ModeProduction))
+ .arg(port)
+ .arg(key);
- if (mode == Protocol::Mode::Debug) {
- m_active = true;
- m_serverStarted = true;
- } else if (m_mode == Protocol::Mode::Release) {
- m_key = QUuid::createUuid().toString();
if (!m_object) {
m_object = new KeepAliveObject;
m_object->moveToThread(&m_thread);
@@ -162,13 +165,17 @@ public:
} else {
Q_ASSERT_X(false, Q_FUNC_INFO, "Keep alive thread already started.");
}
- } else {
- Q_ASSERT_X(false, Q_FUNC_INFO, "RemoteClient mode not set properly.");
+ } else if (mode == Protocol::Mode::Debug) {
+ // To be able to debug the client-server connection start and stop the server manually,
+ // e.g. installer --startserver debug. The server is listening on localhost:39999 then.
}
}
void maybeStartServer() {
- if (m_serverStarted || m_serverCommand.isEmpty())
+ if (m_mode == Protocol::Mode::Debug)
+ m_serverStarted = true; // we expect the server to be started by the developer
+
+ if (m_serverStarted)
return;
const QMutexLocker ml(&m_mutex);
@@ -218,6 +225,9 @@ public:
void maybeStopServer()
{
+ if (m_mode == Protocol::Mode::Debug)
+ m_serverStarted = false; // we never started the server in debug mode
+
if (!m_serverStarted)
return;
diff --git a/src/libs/installer/remoteserver.cpp b/src/libs/installer/remoteserver.cpp
index f47cb30ab..c3ec93c85 100644
--- a/src/libs/installer/remoteserver.cpp
+++ b/src/libs/installer/remoteserver.cpp
@@ -39,6 +39,9 @@
namespace QInstaller {
+/*!
+ Constructs an remote server object with \a parent.
+*/
RemoteServer::RemoteServer(QObject *parent)
: QObject(parent)
, d_ptr(new RemoteServerPrivate(this))
@@ -46,6 +49,9 @@ RemoteServer::RemoteServer(QObject *parent)
Repository::registerMetaType(); // register, cause we stream the type as QVariant
}
+/*!
+ Destroys the remote server object.
+*/
RemoteServer::~RemoteServer()
{
Q_D(RemoteServer);
@@ -54,8 +60,10 @@ RemoteServer::~RemoteServer()
}
/*!
- Starts the server. If started in debug mode, the watchdog that kills the server after
- 30 seconds without usage is not started. The authorization key is set to "DebugMode".
+ Starts the server.
+
+ \note If running in debug mode, the timer that kills the server after 30 seconds without
+ usage is not started, so the server runs in an endless loop.
*/
void RemoteServer::start()
{
@@ -69,36 +77,40 @@ void RemoteServer::start()
connect (d->m_tcpServer, SIGNAL(newIncomingConnection()), this, SLOT(restartWatchdog()));
d->m_thread.start();
- if (d->m_mode == Protocol::Mode::Release) {
+ if (d->m_mode == Protocol::Mode::Production) {
connect(d->m_watchdog.data(), SIGNAL(timeout()), this, SLOT(deleteLater()));
d->m_watchdog->start();
}
}
/*!
- Returns the authorization key.
+ Initializes the server with \a port, the port to listen on, with \a key, the key the client
+ needs to send to authenticate with the server, and \a mode.
*/
-QString RemoteServer::authorizationKey() const
+void RemoteServer::init(quint16 port, const QString &key, Protocol::Mode mode)
{
- Q_D(const RemoteServer);
- return d->m_key;
+ Q_D(RemoteServer);
+ d->m_port = port;
+ d->m_key = key;
+ d->m_mode = mode;
}
/*!
- Sets the authorization key \a authorizationKey this server is asking the clients for.
+ Returns the port the server is listening on.
*/
-void RemoteServer::setAuthorizationKey(const QString &authorizationKey)
+quint16 RemoteServer::port() const
{
- Q_D(RemoteServer);
- d->m_key = authorizationKey;
+ Q_D(const RemoteServer);
+ return d->m_port;
}
-void RemoteServer::init(quint16 port, const QHostAddress &address, Protocol::Mode mode)
+/*!
+ Returns the authorization key.
+*/
+QString RemoteServer::authorizationKey() const
{
- Q_D(RemoteServer);
- d->m_port = port;
- d->m_address = address;
- d->m_mode = mode;
+ Q_D(const RemoteServer);
+ return d->m_key;
}
/*!
diff --git a/src/libs/installer/remoteserver.h b/src/libs/installer/remoteserver.h
index c4235a178..9a4fbdd34 100644
--- a/src/libs/installer/remoteserver.h
+++ b/src/libs/installer/remoteserver.h
@@ -59,10 +59,10 @@ public:
~RemoteServer();
void start();
- void init(quint16 port, const QHostAddress &address, Protocol::Mode mode);
+ void init(quint16 port, const QString &authorizationKey, Protocol::Mode mode);
+ quint16 port() const;
QString authorizationKey() const;
- void setAuthorizationKey(const QString &key);
private slots:
void restartWatchdog();
diff --git a/src/sdk/commandlineparser.cpp b/src/sdk/commandlineparser.cpp
index 23d903bd3..9c4200b3e 100644
--- a/src/sdk/commandlineparser.cpp
+++ b/src/sdk/commandlineparser.cpp
@@ -94,7 +94,14 @@ CommandLineParser::CommandLineParser()
"https://, http:// or ftp://."), QLatin1String("URI,...")));
m_parser.addOption(QCommandLineOption(QLatin1String(CommandLineOptions::StartServer),
- QLatin1String("Starts the application as headless process waiting for commands to execute."),
+ QLatin1String("Starts the application as headless process waiting for commands to execute."
+ " Mode can be DEBUG or PRODUCTION. In DEBUG mode, the option values can be omitted."
+ "Note: The server will not shutdown on his own, you need to quit the process by hand."),
+ QLatin1String("mode,port,key")));
+ m_parser.addOption(QCommandLineOption(QLatin1String(CommandLineOptions::StartClient),
+ QString::fromLatin1("Starts the application to debug the client-server communication. If "
+ "a value is omitted, the client will use a default instead. Note: The server process is "
+ "not started by the client application in that case, you need to start it on your own."),
QLatin1String("port,key")));
m_parser.addPositionalArgument(QLatin1String(CommandLineOptions::KeyValue),
diff --git a/src/sdk/constants.h b/src/sdk/constants.h
index 1d4f5b5e8..44fe1f598 100644
--- a/src/sdk/constants.h
+++ b/src/sdk/constants.h
@@ -55,6 +55,7 @@ const char AddRepository[] = "addRepository";
const char AddTmpRepository[] = "addTempRepository";
const char SetTmpRepository[] = "setTempRepository";
const char StartServer[] = "startserver";
+const char StartClient[] = "startclient";
} // namespace CommandLineOptions
diff --git a/src/sdk/installerbase.cpp b/src/sdk/installerbase.cpp
index 306b37223..6253f3e25 100644
--- a/src/sdk/installerbase.cpp
+++ b/src/sdk/installerbase.cpp
@@ -47,6 +47,7 @@
#include <packagemanagercore.h>
#include <packagemanagerproxyfactory.h>
#include <qprocesswrapper.h>
+#include <protocol.h>
#include <productkeycheck.h>
#include <settings.h>
#include <utils.h>
@@ -57,6 +58,7 @@
#include <QDirIterator>
#include <QTemporaryFile>
#include <QTranslator>
+#include <QUuid>
InstallerBase::InstallerBase(int &argc, char *argv[])
: SDKApp<QApplication>(argc, argv)
@@ -109,8 +111,22 @@ int InstallerBase::run()
qDebug() << "Arguments: " << arguments().join(QLatin1String(", ")).toUtf8().constData();
}
+ CommandLineParser parser;
+ parser.parse(arguments());
+
SDKApp::registerMetaResources(manager.collectionByName("QResources"));
- m_core = new QInstaller::PackageManagerCore(magicMarker, oldOperations);
+ if (parser.isSet(QLatin1String(CommandLineOptions::StartClient))) {
+ const QStringList arguments = parser.value(QLatin1String(CommandLineOptions::StartServer))
+ .split(QLatin1Char(','), QString::SkipEmptyParts);
+ m_core = new QInstaller::PackageManagerCore(magicMarker, oldOperations, QString(arguments
+ .value(0, QString::number(QInstaller::Protocol::DefaultPort))).toInt(),
+ arguments.value(1, QLatin1String(QInstaller::Protocol::DefaultAuthorizationKey)),
+ QInstaller::Protocol::Mode::Debug);
+ } else {
+ m_core = new QInstaller::PackageManagerCore(magicMarker, oldOperations,
+ 30000 + qrand() % 100, QUuid::createUuid().toString());
+ }
+
{
using namespace QInstaller;
ProductKeyCheck::instance()->init(m_core);
@@ -120,9 +136,6 @@ int InstallerBase::run()
if (QInstaller::isVerbose())
dumpResourceTree();
- CommandLineParser parser;
- parser.parse(arguments());
-
QString controlScript;
if (parser.isSet(QLatin1String(CommandLineOptions::Script))) {
controlScript = parser.value(QLatin1String(CommandLineOptions::Script));
diff --git a/src/sdk/main.cpp b/src/sdk/main.cpp
index a5565fe5a..e67ee6662 100644
--- a/src/sdk/main.cpp
+++ b/src/sdk/main.cpp
@@ -116,30 +116,39 @@ int main(int argc, char *argv[])
}
if (parser.isSet(QLatin1String(CommandLineOptions::StartServer))) {
- const QString argument = parser.value(QLatin1String(CommandLineOptions::StartServer));
- const QString port = argument.section(QLatin1Char(','), 0, 0);
- const QString key = argument.section(QLatin1Char(','), 1, 1);
+ const QStringList arguments = parser.value(QLatin1String(CommandLineOptions::StartServer))
+ .split(QLatin1Char(','), QString::SkipEmptyParts);
+
+ QString port, key;
+ const QString mode = arguments.value(0);
+ bool argumentsValid = (mode.compare(QLatin1String(QInstaller::Protocol::ModeDebug),
+ Qt::CaseInsensitive) == 0);
+ if (argumentsValid) {
+ port = arguments.value(1, QString::number(QInstaller::Protocol::DefaultPort));
+ key = arguments.value(2, QLatin1String(QInstaller::Protocol::DefaultAuthorizationKey));
+ } else {
+ port = arguments.value(1);
+ key = arguments.value(2);
+ }
- QStringList missing;
- if (port.isEmpty())
- missing << QLatin1String("Port");
- if (key.isEmpty())
- missing << QLatin1String("Key");
+ const bool production = (mode.compare(QLatin1String(QInstaller::Protocol::ModeProduction),
+ Qt::CaseInsensitive) == 0);
+ if (production)
+ argumentsValid = (!key.isEmpty()) && (!port.isEmpty());
SDKApp<QCoreApplication> app(argc, argv);
- if (missing.count()) {
+ if (!argumentsValid) {
Console c;
- std::cerr << qPrintable(QString::fromLatin1("Missing argument(s) for option "
- "'startserver': %2").arg(missing.join(QLatin1String(", ")))) << std::endl;
std::cout << qPrintable(parser.helpText()) << std::endl;
+ std::cerr << "Wrong argument(s) for option --startserver." << std::endl;
return EXIT_FAILURE;
}
QInstaller::RemoteServer *server = new QInstaller::RemoteServer;
QObject::connect(server, SIGNAL(destroyed()), &app, SLOT(quit()));
- server->init(port.toInt(), QHostAddress(QLatin1String(QInstaller::Protocol
- ::DefaultHostAddress)), QInstaller::Protocol::Mode::Release);
- server->setAuthorizationKey(key);
+ server->init(port.toInt(), key, (production ? QInstaller::Protocol::Mode::Production
+ : QInstaller::Protocol::Mode::Debug));
+
server->start();
return app.exec();
}
diff --git a/tests/auto/installer/clientserver/tst_clientserver.cpp b/tests/auto/installer/clientserver/tst_clientserver.cpp
index 7b1764285..1c135c0b5 100644
--- a/tests/auto/installer/clientserver/tst_clientserver.cpp
+++ b/tests/auto/installer/clientserver/tst_clientserver.cpp
@@ -53,32 +53,87 @@ class tst_ClientServer : public QObject
Q_OBJECT
private slots:
- void testServerConnection()
+ void initTestCase()
+ {
+ RemoteClient::instance().setActive(true);
+ }
+
+ void testServerConnectDebug()
{
RemoteServer server;
- server.init(39999, QHostAddress::LocalHost, Protocol::Mode::Debug);
+ server.init(Protocol::DefaultPort, QString(Protocol::DefaultAuthorizationKey),
+ Protocol::Mode::Debug);
server.start();
QTcpSocket socket;
- socket.connectToHost(QHostAddress::LocalHost, 39999);
+ socket.connectToHost(QHostAddress(QLatin1String(Protocol::DefaultHostAddress)),
+ Protocol::DefaultPort);
QVERIFY2(socket.waitForConnected(), "Could not connect to server.");
+ QCOMPARE(socket.state() == QAbstractSocket::ConnectedState, true);
+
+ QDataStream stream;
+ stream.setDevice(&socket);
+ stream << QString::fromLatin1(Protocol::Authorize) << QString(Protocol::DefaultAuthorizationKey);
+
+ socket.waitForBytesWritten(-1);
+ if (!socket.bytesAvailable())
+ socket.waitForReadyRead(-1);
+
+ quint32 size; stream >> size;
+ bool authorized;
+ stream >> authorized;
+ QCOMPARE(authorized, true);
+
+ socket.flush();
+ stream << QString::fromLatin1(Protocol::Authorize) << QString("SomeKey");
+ socket.waitForBytesWritten(-1);
+ if (!socket.bytesAvailable())
+ socket.waitForReadyRead(-1);
+
+ stream >> size;
+ stream >> authorized;
+ QCOMPARE(authorized, false);
}
- void testClientConnection()
+ void testServerConnectRelease()
{
RemoteServer server;
- server.init(39999, QHostAddress::LocalHost, Protocol::Mode::Debug);
+ quint16 port = (30000 + qrand() % 100);
+ server.init(port, QString("SomeKey"), Protocol::Mode::Production);
server.start();
QTcpSocket socket;
- RemoteClient::instance().init(39999, QHostAddress::LocalHost, Protocol::Mode::Debug);
- QVERIFY2(RemoteClient::instance().connect(&socket), "Could not connect to server.");
+ socket.connectToHost(QHostAddress(QLatin1String(Protocol::DefaultHostAddress)), port);
+ QVERIFY2(socket.waitForConnected(), "Could not connect to server.");
+ QCOMPARE(socket.state() == QAbstractSocket::ConnectedState, true);
+
+ QDataStream stream;
+ stream.setDevice(&socket);
+ stream << QString::fromLatin1(Protocol::Authorize) << QString("SomeKey");
+
+ socket.waitForBytesWritten(-1);
+ if (!socket.bytesAvailable())
+ socket.waitForReadyRead(-1);
+
+ quint32 size; stream >> size;
+ bool authorized;
+ stream >> authorized;
+ QCOMPARE(authorized, true);
+
+ socket.flush();
+ stream << QString::fromLatin1(Protocol::Authorize) << QString(Protocol::DefaultAuthorizationKey);
+ socket.waitForBytesWritten(-1);
+ if (!socket.bytesAvailable())
+ socket.waitForReadyRead(-1);
+
+ stream >> size;
+ stream >> authorized;
+ QCOMPARE(authorized, false);
}
void testQSettingsWrapper()
{
RemoteServer server;
- server.init(39999, QHostAddress::LocalHost, Protocol::Mode::Debug);
server.start();
QSettingsWrapper wrapper("digia", "clientserver");
@@ -194,7 +249,6 @@ private slots:
void testQProcessWrapper()
{
RemoteServer server;
- server.init(39999, QHostAddress::LocalHost, Protocol::Mode::Debug);
server.start();
{
@@ -291,7 +345,6 @@ private slots:
void testRemoteFileEngine()
{
RemoteServer server;
- server.init(39999, QHostAddress::LocalHost, Protocol::Mode::Debug);
server.start();
QString filename;
@@ -319,6 +372,12 @@ private slots:
file.write(QProcess::systemEnvironment().join(QLatin1String("\n")).toLocal8Bit());
QCOMPARE(file.atEnd(), true);
}
+
+ void cleanupTestCase()
+ {
+ RemoteClient::instance().setActive(false);
+ RemoteClient::instance().shutdown();
+ }
};
QTEST_MAIN(tst_ClientServer)