AusweisApp2/test/qt/core/context/test_WorkflowContext.cpp

271 lines
7.1 KiB
C++
Raw Permalink Normal View History

2019-01-03 15:06:22 +01:00
/*!
2019-05-22 10:08:38 +02:00
* \copyright Copyright (c) 2018-2019 Governikus GmbH & Co. KG, Germany
2019-01-03 15:06:22 +01:00
*/
#include "context/WorkflowContext.h"
#include "MockCardConnectionWorker.h"
#include "MockReader.h"
#include <QtTest>
using namespace governikus;
class test_WorkflowContext
: public QObject
{
Q_OBJECT
2019-09-30 17:22:19 +02:00
QSharedPointer<WorkflowContext> mContext;
2019-01-03 15:06:22 +01:00
private Q_SLOTS:
2019-09-30 17:22:19 +02:00
void init()
{
mContext.reset(new WorkflowContext());
}
void cleanup()
2019-01-03 15:06:22 +01:00
{
2019-09-30 17:22:19 +02:00
mContext.clear();
}
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
void test_WorkflowFinished()
{
mContext->setWorkflowFinished(true);
QVERIFY(mContext->isWorkflowFinished());
mContext->setWorkflowFinished(false);
QVERIFY(!mContext->isWorkflowFinished());
2019-01-03 15:06:22 +01:00
}
void test_CanAllowed()
{
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::fireCanAllowedModeChanged);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setCanAllowedMode(true);
QVERIFY(mContext->isCanAllowedMode());
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
2019-09-30 17:22:19 +02:00
mContext->setCanAllowedMode(false);
QVERIFY(!mContext->isCanAllowedMode());
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
}
void test_Can()
{
const QString can1 = QStringLiteral("123256");
const QString can2 = QStringLiteral("222222");
const QString can3 = QStringLiteral("222222");
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::fireCanChanged);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setCan(can1);
QCOMPARE(mContext->getCan(), can1);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
2019-09-30 17:22:19 +02:00
mContext->setCan(can2);
QCOMPARE(mContext->getCan(), can2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
2019-09-30 17:22:19 +02:00
mContext->setCan(can3);
QCOMPARE(mContext->getCan(), can2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
}
void test_Pin()
{
const QString pin1 = QStringLiteral("123256");
const QString pin2 = QStringLiteral("222222");
const QString pin3 = QStringLiteral("222222");
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::firePinChanged);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setPin(pin1);
QCOMPARE(mContext->getPin(), pin1);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
2019-09-30 17:22:19 +02:00
mContext->setPin(pin2);
QCOMPARE(mContext->getPin(), pin2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
2019-09-30 17:22:19 +02:00
mContext->setPin(pin3);
QCOMPARE(mContext->getPin(), pin2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
}
void test_Puk()
{
const QString puk1 = QStringLiteral("123256789");
const QString puk2 = QStringLiteral("222222222");
const QString puk3 = QStringLiteral("222222222");
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::firePukChanged);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setPuk(puk1);
QCOMPARE(mContext->getPuk(), puk1);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
2019-09-30 17:22:19 +02:00
mContext->setPuk(puk2);
QCOMPARE(mContext->getPuk(), puk2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
2019-09-30 17:22:19 +02:00
mContext->setPuk(puk3);
QCOMPARE(mContext->getPuk(), puk2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
}
void test_ErrorReportToUser()
{
2019-09-30 17:22:19 +02:00
mContext->setErrorReportedToUser(true);
QVERIFY(mContext->isErrorReportedToUser());
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setErrorReportedToUser(false);
QVERIFY(!mContext->isErrorReportedToUser());
2019-01-03 15:06:22 +01:00
}
void test_CurrentState()
{
const QString state1 = QStringLiteral("state1");
const QString state2 = QStringLiteral("state2");
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::fireStateChanged);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setCurrentState(state1);
QCOMPARE(mContext->getCurrentState(), state1);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
2019-09-30 17:22:19 +02:00
QVERIFY(!mContext->isStateApproved());
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setCurrentState(state2);
QCOMPARE(mContext->getCurrentState(), state2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
2019-09-30 17:22:19 +02:00
QVERIFY(!mContext->isStateApproved());
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->killWorkflow();
QCOMPARE(mContext->getCurrentState(), state2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
2019-09-30 17:22:19 +02:00
QVERIFY(mContext->isStateApproved());
2019-01-03 15:06:22 +01:00
}
void test_ReaderPlugInTypes()
{
QVector<ReaderManagerPlugInType> vector1({ReaderManagerPlugInType::PCSC});
QVector<ReaderManagerPlugInType> vector2({ReaderManagerPlugInType::BLUETOOTH});
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::fireReaderPlugInTypesChanged);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setReaderPlugInTypes(vector1);
QCOMPARE(mContext->getReaderPlugInTypes(), vector1);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
spy.clear();
2019-09-30 17:22:19 +02:00
mContext->setReaderPlugInTypes(vector2);
QCOMPARE(mContext->getReaderPlugInTypes(), vector2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
}
void test_LastPaceAndResult()
{
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::firePaceResultUpdated);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setLastPaceResult(CardReturnCode::COMMAND_FAILED);
QCOMPARE(mContext->getLastPaceResult(), CardReturnCode::COMMAND_FAILED);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
2019-09-30 17:22:19 +02:00
mContext->setLastPaceResult(CardReturnCode::OK);
QCOMPARE(mContext->getLastPaceResult(), CardReturnCode::OK);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
}
void test_CardConnection()
{
QThread cardThread;
cardThread.start();
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::fireCardConnectionChanged);
2019-01-03 15:06:22 +01:00
MockReader reader1(QStringLiteral("reader"));
reader1.moveToThread(&cardThread);
QSharedPointer<CardConnectionWorker> worker1 = CardConnectionWorker::create(&reader1);
worker1->moveToThread(&cardThread);
QSharedPointer<CardConnection> cardConnection1(new CardConnection(worker1));
MockReader reader2(QStringLiteral("reader2"));
reader2.moveToThread(&cardThread);
QSharedPointer<CardConnectionWorker> worker2 = CardConnectionWorker::create(&reader2);
worker2->moveToThread(&cardThread);
QSharedPointer<CardConnection> cardConnection2(new CardConnection(worker2));
2019-09-30 17:22:19 +02:00
mContext->setCardConnection(cardConnection1);
QCOMPARE(mContext->getCardConnection(), cardConnection1);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
2019-09-30 17:22:19 +02:00
mContext->setCardConnection(cardConnection2);
QCOMPARE(mContext->getCardConnection(), cardConnection2);
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 2);
cardThread.quit();
cardThread.wait();
}
void test_IsPinBlocked()
{
QThread workerThread;
workerThread.start();
const QSharedPointer<MockCardConnectionWorker> worker(new MockCardConnectionWorker());
worker->moveToThread(&workerThread);
const CardInfo cardInfo1(CardType::EID_CARD, QSharedPointer<const EFCardAccess>(), 3, false, false);
const CardInfo cardInfo2(CardType::EID_CARD, QSharedPointer<const EFCardAccess>(), 0, false, false);
const ReaderInfo readerInfo1(QString(), ReaderManagerPlugInType::UNKNOWN, cardInfo1);
const ReaderInfo readerInfo2(QString(), ReaderManagerPlugInType::UNKNOWN, cardInfo2);
2019-09-30 17:22:19 +02:00
QVERIFY(!mContext->isPinBlocked());
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
mContext->setCardConnection(QSharedPointer<CardConnection>::create(worker));
Q_EMIT worker->fireReaderInfoChanged(readerInfo1);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
Q_EMIT worker->fireReaderInfoChanged(readerInfo2);
QVERIFY(mContext->isPinBlocked());
2019-01-03 15:06:22 +01:00
workerThread.quit();
workerThread.wait();
}
void test_WorkflowKilled()
{
2019-09-30 17:22:19 +02:00
QSignalSpy spy(mContext.data(), &WorkflowContext::fireCancelWorkflow);
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
QVERIFY(!mContext->isWorkflowKilled());
2019-01-03 15:06:22 +01:00
QTest::ignoreMessage(QtWarningMsg, "Killing the current workflow.");
2019-09-30 17:22:19 +02:00
mContext->killWorkflow();
QVERIFY(mContext->isWorkflowKilled());
QCOMPARE(mContext->getStatus().getStatusCode(), GlobalStatus::Code::Card_Cancellation_By_User);
QVERIFY(mContext->isStateApproved());
2019-01-03 15:06:22 +01:00
QCOMPARE(spy.count(), 1);
}
void test_IsWorkflowCancelled()
{
2019-09-30 17:22:19 +02:00
QVERIFY(!mContext->isWorkflowCancelled());
2019-01-03 15:06:22 +01:00
2019-09-30 17:22:19 +02:00
Q_EMIT mContext->fireCancelWorkflow();
QVERIFY(mContext->isWorkflowCancelled());
2019-01-03 15:06:22 +01:00
}
};
QTEST_GUILESS_MAIN(test_WorkflowContext)
#include "test_WorkflowContext.moc"