| 1 | /* |
| 2 | * Copyright (C) 2014 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-service |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <online-accounts-common/globals.h> |
| 22 | #include <online-accounts-common/ipc.h> |
| 23 | #include "request-mock.h" |
| 24 | #include "ui-proxy.h" |
| 25 | |
| 26 | #include <QByteArray> |
| 27 | #include <QDataStream> |
| 28 | #include <QDebug> |
| 29 | #include <QDBusConnection> |
| 30 | #include <QDBusMessage> |
| 31 | #include <QLocalSocket> |
| 32 | #include <QProcess> |
| 33 | #include <QSignalSpy> |
| 34 | #include <QString> |
| 35 | #include <QTemporaryDir> |
| 36 | #include <QTest> |
| 37 | #include <SignOn/uisessiondata_priv.h> |
| 38 | |
| 39 | using namespace OnlineAccountsUi; |
| 40 | |
| 41 | /* mocking the remote process { */ |
| 42 | class RemoteProcess: public QObject { |
| 43 | Q_OBJECT |
| 44 | public: |
| 45 | RemoteProcess(const QString &program, const QStringList &arguments, |
| 46 | QProcess *process); |
| 47 | ~RemoteProcess(); |
| 48 | |
| 49 | bool run(); |
| 50 | void setDelay(int delay) { m_delay = delay; } |
| 51 | void setResult(const QVariantMap &result); |
| 52 | void fail(const QString &errorName, const QString &errorMessage); |
| 53 | void registerHandler(const QString &matchId); |
| 54 | |
| 55 | const QVariantMap &lastReceived() const { return m_lastData; } |
| 56 | QString programName() const { return m_program; } |
| 57 | QStringList arguments() const { return m_arguments; } |
| 58 | void sendOperation(const QVariantMap &data); |
| 59 | QProcessEnvironment environment() const { return m_process->processEnvironment(); } |
| 60 | |
| 61 | private Q_SLOTS: |
| 62 | void onDataReady(QByteArray &data); |
| 63 | |
| 64 | Q_SIGNALS: |
| 65 | void dataReceived(QVariantMap data); |
| 66 | |
| 67 | private: |
| 68 | QString m_program; |
| 69 | QStringList m_arguments; |
| 70 | QProcess *m_process; |
| 71 | QVariantMap m_lastData; |
| 72 | int m_requestId; |
| 73 | int m_delay; |
| 74 | QString m_requestInterface; |
| 75 | QLocalSocket m_socket; |
| 76 | Ipc m_ipc; |
| 77 | }; |
| 78 | |
| 79 | static QMap<QProcess *, RemoteProcess *> remoteProcesses; |
| 80 | |
| 81 | RemoteProcess::RemoteProcess(const QString &program, const QStringList &arguments, |
| 82 | QProcess *process): |
| 83 | QObject(process), |
| 84 | m_program(program), |
| 85 | m_arguments(arguments), |
| 86 | m_process(process), |
| 87 | m_delay(0) |
| 88 | { |
| 89 | QObject::connect(&m_ipc, SIGNAL(dataReady(QByteArray &)), |
| 90 | this, SLOT(onDataReady(QByteArray &))); |
| 91 | QObject::connect(&m_socket, SIGNAL(disconnected()), |
| 92 | this, SLOT(deleteLater())); |
| 93 | } |
| 94 | |
| 95 | RemoteProcess::~RemoteProcess() |
| 96 | { |
| 97 | remoteProcesses.remove(m_process); |
| 98 | } |
| 99 | |
| 100 | bool RemoteProcess::run() |
| 101 | { |
| 102 | int i = m_arguments.indexOf("--socket"); |
| 103 | if (i < 0) return false; |
| 104 | |
| 105 | m_socket.connectToServer(m_arguments[i + 1]); |
| 106 | if (Q_UNLIKELY(!m_socket.waitForConnected())) return false; |
| 107 | |
| 108 | m_ipc.setChannels(&m_socket, &m_socket); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | void RemoteProcess::setResult(const QVariantMap &result) |
| 113 | { |
| 114 | QVariantMap operation; |
| 115 | operation.insert(OAU_OPERATION_CODE, |
| 116 | OAU_OPERATION_CODE_REQUEST_FINISHED); |
| 117 | operation.insert(OAU_OPERATION_ID, m_requestId); |
| 118 | operation.insert(OAU_OPERATION_INTERFACE, m_requestInterface); |
| 119 | operation.insert(OAU_OPERATION_DATA, result); |
| 120 | operation.insert(OAU_OPERATION_DELAY, m_delay); |
| 121 | sendOperation(operation); |
| 122 | deleteLater(); |
| 123 | } |
| 124 | |
| 125 | void RemoteProcess::fail(const QString &errorName, const QString &errorMessage) |
| 126 | { |
| 127 | QVariantMap operation; |
| 128 | operation.insert(OAU_OPERATION_CODE, |
| 129 | OAU_OPERATION_CODE_REQUEST_FAILED); |
| 130 | operation.insert(OAU_OPERATION_ID, m_requestId); |
| 131 | operation.insert(OAU_OPERATION_INTERFACE, m_requestInterface); |
| 132 | operation.insert(OAU_OPERATION_ERROR_NAME, errorName); |
| 133 | operation.insert(OAU_OPERATION_ERROR_MESSAGE, errorMessage); |
| 134 | sendOperation(operation); |
| 135 | deleteLater(); |
| 136 | } |
| 137 | |
| 138 | void RemoteProcess::registerHandler(const QString &matchId) |
| 139 | { |
| 140 | QVariantMap operation; |
| 141 | operation.insert(OAU_OPERATION_CODE, |
| 142 | OAU_OPERATION_CODE_REGISTER_HANDLER); |
| 143 | operation.insert(OAU_OPERATION_HANDLER_ID, matchId); |
| 144 | sendOperation(operation); |
| 145 | } |
| 146 | |
| 147 | void RemoteProcess::sendOperation(const QVariantMap &data) |
| 148 | { |
| 149 | QByteArray ba; |
| 150 | QDataStream stream(&ba, QIODevice::WriteOnly); |
| 151 | stream << data; |
| 152 | m_ipc.write(ba); |
| 153 | } |
| 154 | |
| 155 | void RemoteProcess::onDataReady(QByteArray &data) |
| 156 | { |
| 157 | QVariantMap map; |
| 158 | QDataStream stream(&data, QIODevice::ReadOnly); |
| 159 | stream >> map; |
| 160 | |
| 161 | m_lastData = map; |
| 162 | if (map[OAU_OPERATION_CODE].toString() == OAU_OPERATION_CODE_PROCESS) { |
| 163 | m_requestInterface = map[OAU_OPERATION_INTERFACE].toString(); |
| 164 | m_requestId = map[OAU_OPERATION_ID].toInt(); |
| 165 | } |
| 166 | Q_EMIT dataReceived(map); |
| 167 | } |
| 168 | /* } mocking the remote process */ |
| 169 | |
| 170 | /* mocking QProcess { */ |
| 171 | void QProcess::start(const QString &program, const QStringList &arguments, |
| 172 | OpenMode mode) |
| 173 | { |
| 174 | Q_UNUSED(mode); |
| 175 | RemoteProcess *process = new RemoteProcess(program, arguments, this); |
| 176 | remoteProcesses.insert(this, process); |
| 177 | } |
| 178 | |
| 179 | bool QProcess::waitForStarted(int msecs) |
| 180 | { |
| 181 | Q_UNUSED(msecs); |
| 182 | RemoteProcess *process = remoteProcesses.value(this); |
| 183 | if (!process) return false; |
| 184 | |
| 185 | return process->run(); |
| 186 | } |
| 187 | /* } mocking QProcess */ |
| 188 | |
| 189 | class UiProxyTest: public QObject |
| 190 | { |
| 191 | Q_OBJECT |
| 192 | |
| 193 | public: |
| 194 | UiProxyTest(); |
| 195 | |
| 196 | private: |
| 197 | Request *createRequest(const QString &interface, |
| 198 | const QString &method, |
| 199 | const QString &clientApparmorProfile, |
| 200 | const QVariantMap ¶meters); |
| 201 | |
| 202 | private Q_SLOTS: |
| 203 | void initTestCase(); |
| 204 | void testInit(); |
| 205 | void testRequest_data(); |
| 206 | void testRequest(); |
| 207 | void testRequestDelay_data(); |
| 208 | void testRequestDelay(); |
| 209 | void testHandler(); |
| 210 | void testWrapper(); |
| 211 | void testTrustSessionError_data(); |
| 212 | void testTrustSessionError(); |
| 213 | void testConfinedPlugin_data(); |
| 214 | void testConfinedPlugin(); |
| 215 | |
| 216 | private: |
| 217 | QDBusConnection m_connection; |
| 218 | }; |
| 219 | |
| 220 | UiProxyTest::UiProxyTest(): |
| 221 | QObject(0), |
| 222 | m_connection(QStringLiteral("uninitialized")) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | Request *UiProxyTest::createRequest(const QString &interface, |
| 227 | const QString &method, |
| 228 | const QString &clientApparmorProfile, |
| 229 | const QVariantMap ¶meters) |
| 230 | { |
| 231 | QDBusMessage message = |
| 232 | QDBusMessage::createMethodCall(OAU_SERVICE_NAME, |
| 233 | OAU_OBJECT_PATH, |
| 234 | interface, |
| 235 | method); |
| 236 | Request *request = new Request(m_connection, message, parameters); |
| 237 | RequestPrivate *r = RequestPrivate::mocked(request); |
| 238 | |
| 239 | r->setClientApparmorProfile(clientApparmorProfile); |
| 240 | return request; |
| 241 | } |
| 242 | |
| 243 | void UiProxyTest::initTestCase() |
| 244 | { |
| 245 | qputenv("ACCOUNTS", TMP_DIR "/"); |
| 246 | qputenv("AG_APPLICATIONS", TEST_DATA_DIR); |
| 247 | qputenv("AG_SERVICES", TEST_DATA_DIR); |
| 248 | qputenv("AG_SERVICE_TYPES", TEST_DATA_DIR); |
| 249 | qputenv("AG_PROVIDERS", TEST_DATA_DIR); |
| 250 | qputenv("XDG_DATA_HOME", TEST_DATA_DIR); |
| 251 | QDir runtimeDir(TMP_DIR "/loa-runtime/"); |
| 252 | runtimeDir.mkpath("."); |
| 253 | QFile::setPermissions(TMP_DIR "/loa-runtime/", QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner); |
| 254 | qputenv("XDG_RUNTIME_DIR", runtimeDir.path().toUtf8()); |
| 255 | } |
| 256 | |
| 257 | void UiProxyTest::testInit() |
| 258 | { |
| 259 | /* By passing pid = 0, we disable the prompt session code */ |
| 260 | UiProxy *proxy = new UiProxy(0, this); |
| 261 | QVERIFY(proxy->init()); |
| 262 | delete proxy; |
| 263 | |
| 264 | proxy = new UiProxy(10, this); |
| 265 | QVERIFY(proxy->init()); |
| 266 | delete proxy; |
| 267 | } |
| 268 | |
| 269 | void UiProxyTest::testRequest_data() |
| 270 | { |
| 271 | QTest::addColumn<QString>("interface"); |
| 272 | QTest::addColumn<QString>("method"); |
| 273 | QTest::addColumn<QString>("clientApparmorProfile"); |
| 274 | QTest::addColumn<QVariantMap>("parameters"); |
| 275 | QTest::addColumn<QVariantMap>("expectedResult"); |
| 276 | QTest::addColumn<QString>("expectedError"); |
| 277 | |
| 278 | QVariantMap parameters; |
| 279 | QVariantMap result; |
| 280 | |
| 281 | parameters.insert("greeting", "Hello!"); |
| 282 | result.insert("response", "How do you do?"); |
| 283 | QTest::newRow("success") << |
| 284 | OAU_INTERFACE << |
| 285 | "doSomething" << |
| 286 | "com.lomiri.package_app_0.1" << |
| 287 | parameters << |
| 288 | result << ""; |
| 289 | parameters.clear(); |
| 290 | result.clear(); |
| 291 | |
| 292 | parameters.insert("greeting", "Hi!"); |
| 293 | QTest::newRow("failure") << |
| 294 | OAU_INTERFACE << |
| 295 | "doSomethingElse" << |
| 296 | "com.lomiri.package_app_0.2" << |
| 297 | parameters << |
| 298 | result << "Error code"; |
| 299 | parameters.clear(); |
| 300 | } |
| 301 | |
| 302 | void UiProxyTest::testRequest() |
| 303 | { |
| 304 | QFETCH(QString, interface); |
| 305 | QFETCH(QString, method); |
| 306 | QFETCH(QString, clientApparmorProfile); |
| 307 | QFETCH(QVariantMap, parameters); |
| 308 | QFETCH(QVariantMap, expectedResult); |
| 309 | QFETCH(QString, expectedError); |
| 310 | |
| 311 | Request *request = createRequest(interface, method, |
| 312 | clientApparmorProfile, parameters); |
| 313 | RequestPrivate *r = RequestPrivate::mocked(request); |
| 314 | QSignalSpy requestFailCalled(r, SIGNAL(failCalled(QString,QString))); |
| 315 | QSignalSpy requestSetResultCalled(r, SIGNAL(setResultCalled(QVariantMap))); |
| 316 | |
| 317 | UiProxy *proxy = new UiProxy(0, this); |
| 318 | QVERIFY(proxy->init()); |
| 319 | |
| 320 | QSignalSpy finished(proxy, SIGNAL(finished())); |
| 321 | proxy->handleRequest(request); |
| 322 | |
| 323 | QTRY_VERIFY(!remoteProcesses.isEmpty()); |
| 324 | QCOMPARE(remoteProcesses.count(), 1); |
| 325 | |
| 326 | RemoteProcess *process = remoteProcesses.values().first(); |
| 327 | QVERIFY(process); |
| 328 | QSignalSpy dataReceived(process, SIGNAL(dataReceived(QVariantMap))); |
| 329 | QCOMPARE(process->programName(), |
| 330 | QString(INSTALL_BIN_DIR "/" LOA_UI)); |
| 331 | |
| 332 | /* Check the received data */ |
| 333 | if (process->lastReceived().isEmpty()) { |
| 334 | QVERIFY(dataReceived.wait()); |
| 335 | } |
| 336 | |
| 337 | QVariantMap data = process->lastReceived(); |
| 338 | QCOMPARE(data.value(OAU_OPERATION_CODE).toString(), |
| 339 | QStringLiteral(OAU_OPERATION_CODE_PROCESS)); |
| 340 | QVERIFY(data.contains(OAU_OPERATION_ID)); |
| 341 | QCOMPARE(data.value(OAU_OPERATION_DATA).toMap(), parameters); |
| 342 | QCOMPARE(data.value(OAU_OPERATION_INTERFACE).toString(), interface); |
| 343 | QCOMPARE(data.value(OAU_OPERATION_CLIENT_PROFILE).toString(), |
| 344 | clientApparmorProfile); |
| 345 | |
| 346 | if (expectedError.isEmpty()) { |
| 347 | process->setResult(expectedResult); |
| 348 | QVERIFY(requestSetResultCalled.wait()); |
| 349 | QCOMPARE(requestSetResultCalled.count(), 1); |
| 350 | QCOMPARE(requestSetResultCalled.at(0).at(0).toMap(), expectedResult); |
| 351 | } else { |
| 352 | process->fail(expectedError, "some error"); |
| 353 | QVERIFY(requestFailCalled.wait()); |
| 354 | QCOMPARE(requestFailCalled.count(), 1); |
| 355 | QCOMPARE(requestFailCalled.at(0).at(0).toString(), expectedError); |
| 356 | } |
| 357 | |
| 358 | if (finished.count() == 0) { |
| 359 | QVERIFY(finished.wait()); |
| 360 | } |
| 361 | QCOMPARE(finished.count(), 1); |
| 362 | |
| 363 | delete proxy; |
| 364 | } |
| 365 | |
| 366 | void UiProxyTest::testRequestDelay_data() |
| 367 | { |
| 368 | QTest::addColumn<int>("delay"); |
| 369 | |
| 370 | QTest::newRow("no delay") << 0; |
| 371 | |
| 372 | QTest::newRow("300 ms") << 300; |
| 373 | |
| 374 | QTest::newRow("half second") << 500; |
| 375 | } |
| 376 | |
| 377 | void UiProxyTest::testRequestDelay() |
| 378 | { |
| 379 | QFETCH(int, delay); |
| 380 | |
| 381 | QVariantMap parameters; |
| 382 | parameters.insert("greeting", "Hello!"); |
| 383 | Request *request = createRequest(OAU_INTERFACE, "hello", |
| 384 | "unconfined", parameters); |
| 385 | RequestPrivate *r = RequestPrivate::mocked(request); |
| 386 | QSignalSpy requestFailCalled(r, SIGNAL(failCalled(QString,QString))); |
| 387 | QSignalSpy requestSetResultCalled(r, SIGNAL(setResultCalled(QVariantMap))); |
| 388 | |
| 389 | UiProxy *proxy = new UiProxy(0, this); |
| 390 | QVERIFY(proxy->init()); |
| 391 | |
| 392 | QSignalSpy finished(proxy, SIGNAL(finished())); |
| 393 | proxy->handleRequest(request); |
| 394 | |
| 395 | QTRY_VERIFY(!remoteProcesses.isEmpty()); |
| 396 | QCOMPARE(remoteProcesses.count(), 1); |
| 397 | |
| 398 | RemoteProcess *process = remoteProcesses.values().first(); |
| 399 | QVERIFY(process); |
| 400 | QSignalSpy dataReceived(process, SIGNAL(dataReceived(QVariantMap))); |
| 401 | |
| 402 | /* Check the received data */ |
| 403 | if (process->lastReceived().isEmpty()) { |
| 404 | QVERIFY(dataReceived.wait()); |
| 405 | } |
| 406 | |
| 407 | QVariantMap result; |
| 408 | result.insert("response", "OK"); |
| 409 | process->setDelay(delay); |
| 410 | process->setResult(result); |
| 411 | QVERIFY(requestSetResultCalled.wait()); |
| 412 | QCOMPARE(requestSetResultCalled.count(), 1); |
| 413 | QCOMPARE(requestSetResultCalled.at(0).at(0).toMap(), result); |
| 414 | |
| 415 | QCOMPARE(request->delay(), delay); |
| 416 | |
| 417 | int wontFinishBefore = qMax(delay - 200, 0); |
| 418 | if (wontFinishBefore > 0) { |
| 419 | QTest::qWait(wontFinishBefore); |
| 420 | QCOMPARE(finished.count(), 0); |
| 421 | QTest::qWait(250); |
| 422 | } else { |
| 423 | QTest::qWait(10); |
| 424 | } |
| 425 | QCOMPARE(finished.count(), 1); |
| 426 | |
| 427 | delete proxy; |
| 428 | } |
| 429 | |
| 430 | void UiProxyTest::testHandler() |
| 431 | { |
| 432 | UiProxy *proxy = new UiProxy(0, this); |
| 433 | QVERIFY(proxy->init()); |
| 434 | |
| 435 | /* First, try with empty match parameters */ |
| 436 | QVariantMap matchParams; |
| 437 | QVERIFY(!proxy->hasHandlerFor(matchParams)); |
| 438 | |
| 439 | /* Valid parameters, but without having registered a handler */ |
| 440 | QString match("something unique"); |
| 441 | QVariantMap clientData; |
| 442 | clientData.insert(OAU_REQUEST_MATCH_KEY, match); |
| 443 | matchParams.insert(SSOUI_KEY_CLIENT_DATA, clientData); |
| 444 | QVERIFY(!proxy->hasHandlerFor(matchParams)); |
| 445 | |
| 446 | /* Then, really register a handler */ |
| 447 | QVariantMap parameters; |
| 448 | parameters.insert("greeting", "hi!"); |
| 449 | Request *request = createRequest("iface", "doSomething", |
| 450 | "com.lomiri.package_app_0.1", parameters); |
| 451 | proxy->handleRequest(request); |
| 452 | |
| 453 | QTRY_VERIFY(!remoteProcesses.isEmpty()); |
| 454 | QCOMPARE(remoteProcesses.count(), 1); |
| 455 | |
| 456 | RemoteProcess *process = remoteProcesses.values().first(); |
| 457 | QVERIFY(process); |
| 458 | QSignalSpy dataReceived(process, SIGNAL(dataReceived(QVariantMap))); |
| 459 | |
| 460 | /* Wait for the request to arrive */ |
| 461 | if (process->lastReceived().isEmpty()) { |
| 462 | QVERIFY(dataReceived.wait()); |
| 463 | } |
| 464 | |
| 465 | /* Register a handler */ |
| 466 | process->registerHandler(match); |
| 467 | QTRY_VERIFY(proxy->hasHandlerFor(matchParams)); |
| 468 | |
| 469 | /* make sure that a different key doesn't match */ |
| 470 | clientData.insert(OAU_REQUEST_MATCH_KEY, QString("Won't match")); |
| 471 | matchParams.insert(SSOUI_KEY_CLIENT_DATA, clientData); |
| 472 | QVERIFY(!proxy->hasHandlerFor(matchParams)); |
| 473 | |
| 474 | delete proxy; |
| 475 | } |
| 476 | |
| 477 | void UiProxyTest::testWrapper() |
| 478 | { |
| 479 | QString wrapper("valgrind-deluxe"); |
| 480 | qputenv("OAU_WRAPPER", wrapper.toUtf8()); |
| 481 | |
| 482 | UiProxy *proxy = new UiProxy(0, this); |
| 483 | QVERIFY(proxy->init()); |
| 484 | |
| 485 | QVariantMap parameters; |
| 486 | Request *request = createRequest("iface", "doSomething", |
| 487 | "com.lomiri.package_app_0.1", parameters); |
| 488 | proxy->handleRequest(request); |
| 489 | |
| 490 | QTRY_VERIFY(!remoteProcesses.isEmpty()); |
| 491 | QCOMPARE(remoteProcesses.count(), 1); |
| 492 | |
| 493 | RemoteProcess *process = remoteProcesses.values().first(); |
| 494 | QVERIFY(process); |
| 495 | QCOMPARE(process->programName(), wrapper); |
| 496 | QCOMPARE(process->arguments().at(0), |
| 497 | QString(INSTALL_BIN_DIR "/" LOA_UI)); |
| 498 | |
| 499 | delete proxy; |
| 500 | } |
| 501 | |
| 502 | void UiProxyTest::testTrustSessionError_data() |
| 503 | { |
| 504 | QTest::addColumn<int>("clientPid"); |
| 505 | QTest::addColumn<QString>("envVar"); |
| 506 | QTest::addColumn<bool>("expectSuccess"); |
| 507 | |
| 508 | QTest::newRow("PID 0") << 0 << "" << false; |
| 509 | |
| 510 | QTest::newRow("fail creation") << 4 << |
| 511 | "TEST_MIR_HELPER_FAIL_CREATE=1" << false; |
| 512 | |
| 513 | QTest::newRow("return empty socket") << 4 << |
| 514 | "" << false; |
| 515 | |
| 516 | QTest::newRow("success") << 4 << |
| 517 | "TEST_MIR_HELPER_SOCKET=something" << true; |
| 518 | } |
| 519 | |
| 520 | void UiProxyTest::testTrustSessionError() |
| 521 | { |
| 522 | QFETCH(int, clientPid); |
| 523 | QFETCH(QString, envVar); |
| 524 | QFETCH(bool, expectSuccess); |
| 525 | |
| 526 | qputenv("QT_QPA_PLATFORM", "ubuntu-something"); |
| 527 | |
| 528 | QStringList envVarSplit = envVar.split('='); |
| 529 | QByteArray envVarKey = envVarSplit.value(0, "").toUtf8(); |
| 530 | QByteArray envVarValue = envVarSplit.value(1, "").toUtf8(); |
| 531 | qputenv(envVarKey.constData(), envVarValue); |
| 532 | UiProxy *proxy = new UiProxy(clientPid, this); |
| 533 | QCOMPARE(proxy->init(), expectSuccess); |
| 534 | delete proxy; |
| 535 | |
| 536 | qunsetenv(envVarKey.constData()); |
| 537 | qunsetenv("QT_QPA_PLATFORM"); |
| 538 | } |
| 539 | |
| 540 | void UiProxyTest::testConfinedPlugin_data() |
| 541 | { |
| 542 | QTest::addColumn<QString>("providerId"); |
| 543 | QTest::addColumn<QString>("expectedProfile"); |
| 544 | QTest::addColumn<QString>("expectedAppId"); |
| 545 | |
| 546 | QTest::newRow("unconfined") << |
| 547 | QString() << |
| 548 | "unconfined" << |
| 549 | QString(); |
| 550 | |
| 551 | QTest::newRow("confined") << |
| 552 | "com.lomiri.test_confined" << |
| 553 | "com.lomiri.test_confined_0.2" << |
| 554 | "com.lomiri.test_confined_0.2"; |
| 555 | } |
| 556 | |
| 557 | void UiProxyTest::testConfinedPlugin() |
| 558 | { |
| 559 | QFETCH(QString, providerId); |
| 560 | QFETCH(QString, expectedProfile); |
| 561 | QFETCH(QString, expectedAppId); |
| 562 | |
| 563 | Request *request = createRequest(OAU_INTERFACE, "doSomething", |
| 564 | "unconfined", QVariantMap()); |
| 565 | RequestPrivate *r = RequestPrivate::mocked(request); |
| 566 | r->setProviderId(providerId); |
| 567 | |
| 568 | UiProxy *proxy = new UiProxy(0, this); |
| 569 | QVERIFY(proxy->init()); |
| 570 | |
| 571 | QSignalSpy finished(proxy, SIGNAL(finished())); |
| 572 | proxy->handleRequest(request); |
| 573 | |
| 574 | QTRY_VERIFY(!remoteProcesses.isEmpty()); |
| 575 | QCOMPARE(remoteProcesses.count(), 1); |
| 576 | |
| 577 | RemoteProcess *process = remoteProcesses.values().first(); |
| 578 | QVERIFY(process); |
| 579 | |
| 580 | QStringList args = process->arguments(); |
| 581 | int option = args.indexOf("--profile"); |
| 582 | QVERIFY(option > 0); |
| 583 | |
| 584 | QCOMPARE(args.at(option + 1), expectedProfile); |
| 585 | |
| 586 | QProcessEnvironment env = process->environment(); |
| 587 | QCOMPARE(env.value("APP_ID"), expectedAppId); |
| 588 | |
| 589 | delete proxy; |
| 590 | } |
| 591 | |
| 592 | QTEST_MAIN(UiProxyTest); |
| 593 | |
| 594 | #include "tst_ui_proxy.moc" |