on ebd7b5b help sign in
unity8/tests/mocks/liblightdm/MockGreeter.cpp
270 lines 6.9 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 MockController::instance()->hasGuestAccountHint();
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 MockController::instance()->hideUsersHint();
73bool Greeter::showManualLoginHint() const
75 return MockController::instance()->showManualLoginHint();
78bool Greeter::showRemoteLoginHint() const
80 return false;
83QString Greeter::selectUserHint() const
85 return MockController::instance()->selectUserHint();
88bool Greeter::selectGuestHint() const
90 return MockController::instance()->selectGuestHint();
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 if (d->authenticationUser.isEmpty()) {
145 Q_EMIT showPrompt("Username: ", Greeter::PromptTypeQuestion);
146 return;
147 }
149 // Send out any messages we need to
150 if (d->authenticationUser == "info-prompt")
151 Q_EMIT showMessage("Welcome to Unity Greeter", Greeter::MessageTypeInfo);
152 else if (d->authenticationUser == "wide-info-prompt")
153 Q_EMIT showMessage("Welcome to Unity Greeter, the greeteriest greeter that ever did appear in these fine lands", Greeter::MessageTypeInfo);
154 else if (d->authenticationUser == "html-info-prompt")
155 Q_EMIT showMessage("<b>&</b>", Greeter::MessageTypeInfo);
156 else if (d->authenticationUser == "long-info-prompt")
157 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);
158 else if (d->authenticationUser == "multi-info-prompt") {
159 Q_EMIT showMessage("Welcome to Unity Greeter", Greeter::MessageTypeInfo);
160 Q_EMIT showMessage("This is an error", Greeter::MessageTypeError);
161 Q_EMIT showMessage("You should have seen three messages", Greeter::MessageTypeInfo);
162 }
164 // OK, now actually do the prompt
165 if (d->authenticationUser == "no-password") {
166 d->authenticated = true;
167 Q_EMIT authenticationComplete();
168 } else if (d->authenticationUser == "has-pin"){
169 Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
170 } else if (d->authenticationUser == "auth-error") {
171 d->authenticated = false;
172 Q_EMIT authenticationComplete();
173 } else if (d->authenticationUser == "different-prompt") {
174 Q_EMIT showPrompt("Secret word: ", Greeter::PromptTypeSecret);
175 } else {
176 Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
177 }
180void Greeter::authenticateAsGuest()
182 Q_D(Greeter);
184 d->authenticated = true;
185 d->authenticationUser = QString(); // this is what the real liblightdm does
186 d->twoFactorDone = false;
187 QTimer::singleShot(0, this, &Greeter::authenticationComplete);
190void Greeter::authenticateAutologin()
191{}
193void Greeter::authenticateRemote(const QString &session, const QString &username)
195 Q_UNUSED(session)
196 Q_UNUSED(username)
199void Greeter::cancelAuthentication()
200{}
202void Greeter::setLanguage (const QString &language)
204 Q_UNUSED(language)
207bool Greeter::startSessionSync(const QString &session)
209 Q_UNUSED(session)
211 // Send a request to hide the greeter. This is normally done by logind,
212 // but when testing, we don't want the bother of mocking that out. Instead,
213 // just send the request directly ourselves.
214 QDBusInterface iface("com.canonical.UnityGreeter",
215 "/",
216 "com.canonical.UnityGreeter",
217 QDBusConnection::sessionBus());
218 iface.asyncCall("HideGreeter");
220 return true;
223void Greeter::respond(const QString &response)
225 Q_D(Greeter);
227 if (d->authenticationUser.isEmpty()) {
228 // A manual login, our first question was which username
229 d->authenticationUser = response;
230 handleAuthenticate();
231 return;
232 }
234 if (d->authenticationUser == "no-response")
235 return;
236 else if (d->authenticationUser == "two-factor") {
237 if (!d->twoFactorDone) {
238 if (response == "password") {
239 d->twoFactorDone = true;
240 Q_EMIT showPrompt("otp", Greeter::PromptTypeQuestion);
241 } else {
242 d->authenticated = false;
243 sendAuthenticationComplete();
244 }
245 } else {
246 d->authenticated = (response == "otp");
247 sendAuthenticationComplete();
248 }
249 return;
250 }
252 if (d->authenticationUser == "has-pin") {
253 d->authenticated = (response == "1234");
254 } else {
255 d->authenticated = (response == "password");
256 }
257 sendAuthenticationComplete();
260void Greeter::sendAuthenticationComplete()
262 if (qgetenv("UNITY_TESTING").isEmpty()) {
263 // simulate PAM's delay
264 QTimer::singleShot(1000, this, &Greeter::authenticationComplete);
265 } else {
266 Q_EMIT authenticationComplete();
267 }