~online-accounts/signon/packaging

« back to all changes in this revision

Viewing changes to tests/signond-tests/session_tool.cpp

  • Committer: Alberto Mardegan
  • Date: 2016-08-24 13:13:57 UTC
  • mto: This revision was merged to the branch mainline in revision 637.
  • Revision ID: git-v1:b01d837c3e0f0b65586f9cbf514b8fa020c6d672
Tests: fix the AuthSession "test from another process"

This test is failing with Qt 5.6, getting stuck when calling
QDBusConnection::connectTo() from the child process. Given that calling
fork() from a test function is generally not a good idea, we refactor
this test to avoid calling fork() directly, and use a separate QProcess
instead.

While doing this, we move the refactored test to the signond-tests
directory, since this test is not testing the client library at all, but
only signond's functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of signond
 
3
 *
 
4
 * Copyright (C) 2016 Canonical Ltd.
 
5
 *
 
6
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public License
 
10
 * version 2.1 as published by the Free Software Foundation.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
 * 02110-1301 USA
 
21
 */
 
22
 
 
23
#include <QCoreApplication>
 
24
#include <QDBusConnection>
 
25
#include <QDBusError>
 
26
#include <QDBusInterface>
 
27
#include <QDBusMessage>
 
28
#include <QTextStream>
 
29
#include <QTimer>
 
30
 
 
31
#include "signond/signoncommon.h"
 
32
 
 
33
class AuthSession: public QObject
 
34
{
 
35
    Q_OBJECT
 
36
 
 
37
public:
 
38
    AuthSession(const QString &path);
 
39
    ~AuthSession();
 
40
 
 
41
public Q_SLOTS:
 
42
    void process(const QVariantMap &sessionData);
 
43
    void onResponse(const QVariantMap &response);
 
44
    void onError(const QDBusError &error);
 
45
 
 
46
private:
 
47
    QTextStream m_out;
 
48
    QDBusInterface *m_dbus;
 
49
};
 
50
 
 
51
AuthSession::AuthSession(const QString &path):
 
52
    QObject(),
 
53
    m_out(stdout)
 
54
{
 
55
    m_dbus = new QDBusInterface(SIGNOND_SERVICE,
 
56
                                path,
 
57
                                QLatin1String(SIGNOND_AUTH_SESSION_INTERFACE),
 
58
                                QDBusConnection::sessionBus());
 
59
}
 
60
 
 
61
AuthSession::~AuthSession()
 
62
{
 
63
    delete m_dbus;
 
64
}
 
65
 
 
66
void AuthSession::process(const QVariantMap &sessionData)
 
67
{
 
68
    QVariantList arguments;
 
69
    arguments += sessionData;
 
70
    arguments += QStringLiteral("mech1");
 
71
 
 
72
    QDBusMessage msg = QDBusMessage::createMethodCall(m_dbus->service(),
 
73
                                                      m_dbus->path(),
 
74
                                                      m_dbus->interface(),
 
75
                                                      QStringLiteral("process"));
 
76
    msg.setArguments(arguments);
 
77
 
 
78
    m_dbus->connection().callWithCallback(msg, this,
 
79
                                          SLOT(onResponse(const QVariantMap&)),
 
80
                                          SLOT(onError(const QDBusError&)),
 
81
                                          SIGNOND_MAX_TIMEOUT);
 
82
}
 
83
 
 
84
void AuthSession::onResponse(const QVariantMap &response)
 
85
{
 
86
    // The called doesn't really care about the response value
 
87
    Q_UNUSED(response);
 
88
    m_out << "Response:";
 
89
    QCoreApplication::quit();
 
90
}
 
91
 
 
92
void AuthSession::onError(const QDBusError &error)
 
93
{
 
94
    m_out << "Error:" << error.name();
 
95
    QCoreApplication::quit();
 
96
}
 
97
 
 
98
int main(int argc, char **argv)
 
99
{
 
100
    QCoreApplication app(argc, argv);
 
101
 
 
102
    QString sessionPath;
 
103
 
 
104
    QStringList args = QCoreApplication::arguments();
 
105
    for (int i = 1; i < args.count(); i++) {
 
106
        if (args[i] == "--sessionPath") {
 
107
            sessionPath = args[++i];
 
108
        }
 
109
    }
 
110
 
 
111
    AuthSession authSession(sessionPath);
 
112
 
 
113
    QVariantMap sessionData {
 
114
        { "Secret", QStringLiteral("testSecret") },
 
115
        { "UserName", QStringLiteral("testUsername") },
 
116
    };
 
117
    authSession.process(sessionData);
 
118
 
 
119
    QTimer::singleShot(10*1000, &app, SLOT(quit()));
 
120
    app.exec();
 
121
 
 
122
    return 0;
 
123
}
 
124
 
 
125
#include "session_tool.moc"