on c488c56 help sign in
unity8/tests/mocks/liblightdm/MockGreeter.cpp
256 lines 6.3 kB history raw
1/*
2 * Copyright (C) 2014-2016 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "MockController.h"
18#include "MockGreeter.h"
19#include <QDBusInterface>
20#include <QDBusPendingCall>
21#include <QTimer>
23namespace QLightDM
26class GreeterPrivate
28public:
29 bool authenticated = false;
30 QString authenticationUser;
31 bool twoFactorDone = false;
32};
34Greeter::Greeter(QObject *parent)
35 : QObject(parent)
36 , d_ptr(new GreeterPrivate)
40Greeter::~Greeter()
42 delete d_ptr;
45QString Greeter::authenticationUser() const
47 Q_D(const Greeter);
49 return d->authenticationUser;
52bool Greeter::hasGuestAccountHint() const
54 return true;
57QString Greeter::getHint(const QString &name) const
59 Q_UNUSED(name)
60 return "";
63QString Greeter::defaultSessionHint() const
65 return QStringLiteral("ubuntu");
68bool Greeter::hideUsersHint() const
70 return false;
73bool Greeter::showManualLoginHint() const
75 return true;
78bool Greeter::showRemoteLoginHint() const
80 return true;
83QString Greeter::selectUserHint() const
85 return MockController::instance()->selectUserHint();
88bool Greeter::selectGuestHint() const
90 return false;
93QString Greeter::autologinUserHint() const
95 return "";
98bool Greeter::autologinGuestHint() const
100 return false;
103int Greeter::autologinTimeoutHint() const
105 return 0;
108bool Greeter::inAuthentication() const
110 return false;
113QString Greeter::hostname() const
115 return "hostname1";
118bool Greeter::isAuthenticated() const
120 Q_D(const Greeter);
122 return d->authenticated;
125bool Greeter::connectSync()
127 return true;
130void Greeter::authenticate(const QString &username)
132 Q_D(Greeter);
134 d->authenticated = false;
135 d->authenticationUser = username;
136 d->twoFactorDone = false;
137 QTimer::singleShot(0, this, &Greeter::handleAuthenticate);
140void Greeter::handleAuthenticate()
142 Q_D(Greeter);
144 // Send out any messages we need to
145 if (d->authenticationUser == "info-prompt")
146 Q_EMIT showMessage("Welcome to Unity Greeter", Greeter::MessageTypeInfo);
147 else if (d->authenticationUser == "wide-info-prompt")
148 Q_EMIT showMessage("Welcome to Unity Greeter, the greeteriest greeter that ever did appear in these fine lands", Greeter::MessageTypeInfo);
149 else if (d->authenticationUser == "html-info-prompt")
150 Q_EMIT showMessage("<b>&</b>", Greeter::MessageTypeInfo);
151 else if (d->authenticationUser == "long-info-prompt")
152 Q_EMIT showMessage("Welcome to Unity Greeter\n\nWe like to annoy you with super ridiculously long messages.\nLike this one\n\nThis is the last line of a multiple line message.", Greeter::MessageTypeInfo);
153 else if (d->authenticationUser == "multi-info-prompt") {
154 Q_EMIT showMessage("Welcome to Unity Greeter", Greeter::MessageTypeInfo);
155 Q_EMIT showMessage("This is an error", Greeter::MessageTypeError);
156 Q_EMIT showMessage("You should have seen three messages", Greeter::MessageTypeInfo);
157 }
159 // OK, now actually do the prompt
160 if (d->authenticationUser == "no-password") {
161 d->authenticated = true;
162 Q_EMIT authenticationComplete();
163 } else if (d->authenticationUser == "has-pin"){
164 Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
165 } else if (d->authenticationUser == "auth-error") {
166 d->authenticated = false;
167 Q_EMIT authenticationComplete();
168 } else if (d->authenticationUser == "different-prompt") {
169 Q_EMIT showPrompt("Secret word: ", Greeter::PromptTypeSecret);
170 } else {
171 Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
172 }
175void Greeter::authenticateAsGuest()
176{}
178void Greeter::authenticateAutologin()
179{}
181void Greeter::authenticateRemote(const QString &session, const QString &username)
183 Q_UNUSED(session)
184 Q_UNUSED(username)
187void Greeter::cancelAuthentication()
188{}
190void Greeter::setLanguage (const QString &language)
192 Q_UNUSED(language)
195bool Greeter::startSessionSync(const QString &session)
197 Q_UNUSED(session)
199 // Send a request to hide the greeter. This is normally done by logind,
200 // but when testing, we don't want the bother of mocking that out. Instead,
201 // just send the request directly ourselves.
202 QDBusInterface iface("com.canonical.UnityGreeter",
203 "/",
204 "com.canonical.UnityGreeter",
205 QDBusConnection::sessionBus());
206 iface.asyncCall("HideGreeter");
208 return true;
211void Greeter::respond(const QString &response)
213 Q_D(Greeter);
215 if (d->authenticationUser == "no-response")
216 return;
217 else if (d->authenticationUser == "two-factor") {
218 if (!d->twoFactorDone) {
219 if (response == "password") {
220 d->twoFactorDone = true;
221 Q_EMIT showPrompt("otp", Greeter::PromptTypeQuestion);
222 } else {
223 d->authenticated = false;
224 sendAuthenticationComplete();
225 }
226 } else {
227 d->authenticated = (response == "otp");
228 sendAuthenticationComplete();
229 }
230 return;
231 }
233 if (d->authenticationUser == "has-pin") {
234 d->authenticated = (response == "1234");
235 } else {
236 d->authenticated = (response == "password");
237 }
238 sendAuthenticationComplete();
241void Greeter::sendAuthenticationComplete()
243 if (qgetenv("UNITY_TESTING").isEmpty()) {
244 // simulate PAM's delay
245 QTimer::singleShot(1000, this, &Greeter::authenticationComplete);
246 } else {
247 Q_EMIT authenticationComplete();
248 }
251QObject *Greeter::mock()
253 return MockController::instance();