1 /*!
2  * \copyright Copyright (c) 2018-2021 Governikus GmbH & Co. KG, Germany
3  */
4 
5 #include "context/WorkflowContext.h"
6 
7 #include "MockCardConnection.h"
8 #include "MockCardConnectionWorker.h"
9 #include "MockReader.h"
10 
11 #include <QtTest>
12 
13 
14 using namespace governikus;
15 
16 
17 class test_WorkflowContext
18 	: public QObject
19 {
20 	Q_OBJECT
21 	QSharedPointer<WorkflowContext> mContext;
22 
23 	private Q_SLOTS:
init()24 		void init()
25 		{
26 			mContext.reset(new WorkflowContext());
27 		}
28 
29 
cleanup()30 		void cleanup()
31 		{
32 			mContext.clear();
33 		}
34 
35 
test_WorkflowFinished()36 		void test_WorkflowFinished()
37 		{
38 			mContext->setWorkflowFinished(true);
39 			QVERIFY(mContext->isWorkflowFinished());
40 
41 			mContext->setWorkflowFinished(false);
42 			QVERIFY(!mContext->isWorkflowFinished());
43 		}
44 
45 
test_Can()46 		void test_Can()
47 		{
48 			const QString can1 = QStringLiteral("123256");
49 			const QString can2 = QStringLiteral("222222");
50 			const QString can3 = QStringLiteral("222222");
51 
52 			QSignalSpy spy(mContext.data(), &WorkflowContext::fireCanChanged);
53 
54 			mContext->setCan(can1);
55 			QCOMPARE(mContext->getCan(), can1);
56 			QCOMPARE(spy.count(), 1);
57 
58 			mContext->setCan(can2);
59 			QCOMPARE(mContext->getCan(), can2);
60 			QCOMPARE(spy.count(), 2);
61 
62 			mContext->setCan(can3);
63 			QCOMPARE(mContext->getCan(), can2);
64 			QCOMPARE(spy.count(), 2);
65 		}
66 
67 
test_Pin()68 		void test_Pin()
69 		{
70 			const QString pin1 = QStringLiteral("123256");
71 			const QString pin2 = QStringLiteral("222222");
72 			const QString pin3 = QStringLiteral("222222");
73 			QSignalSpy spy(mContext.data(), &WorkflowContext::firePinChanged);
74 
75 			mContext->setPin(pin1);
76 			QCOMPARE(mContext->getPin(), pin1);
77 			QCOMPARE(spy.count(), 1);
78 
79 			mContext->setPin(pin2);
80 			QCOMPARE(mContext->getPin(), pin2);
81 			QCOMPARE(spy.count(), 2);
82 
83 			mContext->setPin(pin3);
84 			QCOMPARE(mContext->getPin(), pin2);
85 			QCOMPARE(spy.count(), 2);
86 		}
87 
88 
test_Puk()89 		void test_Puk()
90 		{
91 			const QString puk1 = QStringLiteral("123256789");
92 			const QString puk2 = QStringLiteral("222222222");
93 			const QString puk3 = QStringLiteral("222222222");
94 			QSignalSpy spy(mContext.data(), &WorkflowContext::firePukChanged);
95 
96 			mContext->setPuk(puk1);
97 			QCOMPARE(mContext->getPuk(), puk1);
98 			QCOMPARE(spy.count(), 1);
99 
100 			mContext->setPuk(puk2);
101 			QCOMPARE(mContext->getPuk(), puk2);
102 			QCOMPARE(spy.count(), 2);
103 
104 			mContext->setPuk(puk3);
105 			QCOMPARE(mContext->getPuk(), puk2);
106 			QCOMPARE(spy.count(), 2);
107 		}
108 
109 
test_ErrorReportToUser()110 		void test_ErrorReportToUser()
111 		{
112 			mContext->setErrorReportedToUser(true);
113 			QVERIFY(mContext->isErrorReportedToUser());
114 
115 			mContext->setErrorReportedToUser(false);
116 			QVERIFY(!mContext->isErrorReportedToUser());
117 		}
118 
119 
test_CurrentState()120 		void test_CurrentState()
121 		{
122 			const QString state1 = QStringLiteral("state1");
123 			const QString state2 = QStringLiteral("state2");
124 			QSignalSpy spy(mContext.data(), &WorkflowContext::fireStateChanged);
125 
126 			mContext->setCurrentState(state1);
127 			QCOMPARE(mContext->getCurrentState(), state1);
128 			QCOMPARE(spy.count(), 1);
129 			QVERIFY(!mContext->isStateApproved());
130 
131 			mContext->setCurrentState(state2);
132 			QCOMPARE(mContext->getCurrentState(), state2);
133 			QCOMPARE(spy.count(), 2);
134 			QVERIFY(!mContext->isStateApproved());
135 
136 			mContext->killWorkflow();
137 			QCOMPARE(mContext->getCurrentState(), state2);
138 			QCOMPARE(spy.count(), 2);
139 			QVERIFY(mContext->isStateApproved());
140 		}
141 
142 
test_ReaderPlugInTypes()143 		void test_ReaderPlugInTypes()
144 		{
145 			QVector<ReaderManagerPlugInType> vector1({ReaderManagerPlugInType::PCSC});
146 			QVector<ReaderManagerPlugInType> vector2({ReaderManagerPlugInType::REMOTE});
147 			QSignalSpy spy(mContext.data(), &WorkflowContext::fireReaderPlugInTypesChanged);
148 
149 			mContext->setReaderPlugInTypes(vector1);
150 			QCOMPARE(mContext->getReaderPlugInTypes(), vector1);
151 			QCOMPARE(spy.count(), 1);
152 			spy.clear();
153 
154 			mContext->setReaderPlugInTypes(vector2);
155 			QCOMPARE(mContext->getReaderPlugInTypes(), vector2);
156 			QCOMPARE(spy.count(), 1);
157 		}
158 
159 
test_LastPaceAndResult()160 		void test_LastPaceAndResult()
161 		{
162 			QSignalSpy spy(mContext.data(), &WorkflowContext::firePaceResultUpdated);
163 
164 			mContext->setLastPaceResult(CardReturnCode::COMMAND_FAILED);
165 			QCOMPARE(mContext->getLastPaceResult(), CardReturnCode::COMMAND_FAILED);
166 			QCOMPARE(spy.count(), 1);
167 
168 			mContext->setLastPaceResult(CardReturnCode::OK);
169 			QCOMPARE(mContext->getLastPaceResult(), CardReturnCode::OK);
170 			QCOMPARE(spy.count(), 2);
171 		}
172 
173 
test_CardConnection()174 		void test_CardConnection()
175 		{
176 			QSignalSpy spy(mContext.data(), &WorkflowContext::fireCardConnectionChanged);
177 
178 			QSharedPointer<CardConnection> cardConnection1(new MockCardConnection());
179 			mContext->setCardConnection(cardConnection1);
180 			QCOMPARE(mContext->getCardConnection(), cardConnection1);
181 			QCOMPARE(spy.count(), 1);
182 
183 			QSharedPointer<CardConnection> cardConnection2(new MockCardConnection());
184 			mContext->setCardConnection(cardConnection2);
185 			QCOMPARE(mContext->getCardConnection(), cardConnection2);
186 			QCOMPARE(spy.count(), 2);
187 
188 			mContext->setCardConnection(cardConnection2);
189 			QCOMPARE(mContext->getCardConnection(), cardConnection2);
190 			QCOMPARE(spy.count(), 2);
191 		}
192 
193 
test_IsPinBlocked()194 		void test_IsPinBlocked()
195 		{
196 			QThread workerThread;
197 			workerThread.start();
198 
199 			{
200 				const QSharedPointer<MockCardConnectionWorker> worker(new MockCardConnectionWorker());
201 				worker->moveToThread(&workerThread);
202 				mContext->setCardConnection(QSharedPointer<CardConnection>::create(worker));
203 				QVERIFY(!mContext->isPinBlocked());
204 
205 				const CardInfo cardInfo1(CardType::EID_CARD, QSharedPointer<const EFCardAccess>(), 3, false, false);
206 				const ReaderInfo readerInfo1(QString(), ReaderManagerPlugInType::UNKNOWN, cardInfo1);
207 				Q_EMIT worker->fireReaderInfoChanged(readerInfo1);
208 				QVERIFY(!mContext->isPinBlocked());
209 
210 				const CardInfo cardInfo2(CardType::EID_CARD, QSharedPointer<const EFCardAccess>(), 0, false, false);
211 				const ReaderInfo readerInfo2(QString(), ReaderManagerPlugInType::UNKNOWN, cardInfo2);
212 				Q_EMIT worker->fireReaderInfoChanged(readerInfo2);
213 				QVERIFY(mContext->isPinBlocked());
214 			}
215 
216 			workerThread.quit();
217 			workerThread.wait();
218 		}
219 
220 
test_WorkflowKilled()221 		void test_WorkflowKilled()
222 		{
223 			QSignalSpy spy(mContext.data(), &WorkflowContext::fireCancelWorkflow);
224 
225 			QVERIFY(!mContext->isWorkflowKilled());
226 
227 			QTest::ignoreMessage(QtWarningMsg, "Killing the current workflow.");
228 			mContext->killWorkflow();
229 			QVERIFY(mContext->isWorkflowKilled());
230 			QCOMPARE(mContext->getStatus().getStatusCode(), GlobalStatus::Code::Card_Cancellation_By_User);
231 			QVERIFY(mContext->isStateApproved());
232 			QCOMPARE(spy.count(), 1);
233 		}
234 
235 
test_IsWorkflowCancelled()236 		void test_IsWorkflowCancelled()
237 		{
238 			QVERIFY(!mContext->isWorkflowCancelled());
239 
240 			Q_EMIT mContext->fireCancelWorkflow();
241 			QVERIFY(mContext->isWorkflowCancelled());
242 		}
243 
244 
245 };
246 
247 QTEST_GUILESS_MAIN(test_WorkflowContext)
248 #include "test_WorkflowContext.moc"
249