1/*
2 * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "agentmanagertest.h"
8#include "autotests.h"
9#include "initmanagerjob.h"
10#include "manager.h"
11#include "pendingcall.h"
12#include "services.h"
13
14#include <QTest>
15
16namespace BluezQt
17{
18extern void bluezqt_initFakeBluezTestRun();
19}
20
21using namespace BluezQt;
22
23// TestAgent
24TestAgent::TestAgent(QObject *parent)
25 : Agent(parent)
26 , m_pinRequested(false)
27 , m_passkeyRequested(false)
28 , m_authorizationRequested(false)
29 , m_cancelCalled(false)
30 , m_releaseCalled(false)
31{
32}
33
34QDBusObjectPath TestAgent::objectPath() const
35{
36 return QDBusObjectPath(QStringLiteral("/testagent"));
37}
38
39void TestAgent::requestPinCode(DevicePtr device, const BluezQt::Request<QString> &request)
40{
41 m_device = device;
42 m_pinRequested = true;
43
44 request.accept(QString());
45}
46
47void TestAgent::displayPinCode(DevicePtr device, const QString &pinCode)
48{
49 m_device = device;
50 m_displayedPinCode = pinCode;
51}
52
53void TestAgent::requestPasskey(DevicePtr device, const BluezQt::Request<quint32> &request)
54{
55 m_device = device;
56 m_passkeyRequested = true;
57
58 request.accept(0);
59}
60
61void TestAgent::displayPasskey(DevicePtr device, const QString &passkey, const QString &entered)
62{
63 m_device = device;
64 m_displayedPasskey = passkey;
65 m_enteredPasskey = entered;
66}
67
68void TestAgent::requestConfirmation(DevicePtr device, const QString &passkey, const BluezQt::Request<> &request)
69{
70 m_device = device;
71 m_requestedPasskey = passkey;
72
73 request.accept();
74}
75
76void TestAgent::requestAuthorization(DevicePtr device, const BluezQt::Request<> &request)
77{
78 m_device = device;
79 m_authorizationRequested = true;
80
81 request.accept();
82}
83
84void TestAgent::authorizeService(DevicePtr device, const QString &uuid, const BluezQt::Request<> &request)
85{
86 m_device = device;
87 m_authorizedUuid = uuid;
88
89 request.accept();
90}
91
92void TestAgent::cancel()
93{
94 m_cancelCalled = true;
95}
96
97void TestAgent::release()
98{
99 m_releaseCalled = true;
100}
101
102// AgentManagerTest
103void AgentManagerTest::initTestCase()
104{
105 bluezqt_initFakeBluezTestRun();
106
107 FakeBluez::start();
108 FakeBluez::runTest(QStringLiteral("bluez-standard"));
109
110 // Create adapter
111 QVariantMap adapterProps;
112 adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(QStringLiteral("/org/bluez/hci0")));
113 adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E");
114 adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter");
115 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps);
116
117 // Create device
118 m_device = QDBusObjectPath(QStringLiteral("/org/bluez/hci0/dev_40_79_6A_0C_39_75"));
119 QVariantMap deviceProps;
120 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(m_device);
121 deviceProps[QStringLiteral("Adapter")] = adapterProps.value(QStringLiteral("Path"));
122 deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75");
123 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice");
124 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
125
126 Manager *manager = new Manager(this);
127 InitManagerJob *job = manager->init();
128 job->exec();
129
130 QVERIFY(!job->error());
131 QCOMPARE(manager->adapters().count(), 1);
132 QCOMPARE(manager->devices().count(), 1);
133
134 m_agent = new TestAgent(this);
135 manager->registerAgent(m_agent)->waitForFinished();
136}
137
138void AgentManagerTest::cleanupTestCase()
139{
140 FakeBluez::stop();
141}
142
143void AgentManagerTest::requestPinCodeTest()
144{
145 QCOMPARE(m_agent->m_pinRequested, false);
146
147 QVariantMap props;
148 props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
149 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-pincode"), props);
150
151 QTRY_COMPARE(m_agent->m_pinRequested, true);
152 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
153}
154
155void AgentManagerTest::displayPinCodeTest()
156{
157 QCOMPARE(m_agent->m_displayedPinCode, QString());
158
159 QVariantMap props;
160 props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
161 props.insert(QStringLiteral("PinCode"), QStringLiteral("123456"));
162 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("display-pincode"), props);
163
164 QTRY_COMPARE(m_agent->m_displayedPinCode, QStringLiteral("123456"));
165 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
166}
167
168void AgentManagerTest::requestPasskeyTest()
169{
170 QCOMPARE(m_agent->m_passkeyRequested, false);
171
172 QVariantMap props;
173 props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
174 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-passkey"), props);
175
176 QTRY_COMPARE(m_agent->m_passkeyRequested, true);
177 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
178}
179
180void AgentManagerTest::displayPasskeyTest()
181{
182 QCOMPARE(m_agent->m_displayedPasskey, QString());
183 QCOMPARE(m_agent->m_enteredPasskey, QString());
184
185 QVariantMap props;
186 props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
187 props.insert(QStringLiteral("Passkey"), QVariant::fromValue(quint32(654321)));
188 props.insert(QStringLiteral("EnteredPasskey"), QVariant::fromValue(quint16(43)));
189 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("display-passkey"), props);
190
191 QTRY_COMPARE(m_agent->m_displayedPasskey, QStringLiteral("654321"));
192 QTRY_COMPARE(m_agent->m_enteredPasskey, QStringLiteral("43"));
193 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
194}
195
196void AgentManagerTest::requestConfirmationTest()
197{
198 QCOMPARE(m_agent->m_requestedPasskey, QString());
199
200 QVariantMap props;
201 props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
202 props.insert(QStringLiteral("Passkey"), QVariant::fromValue(quint32(12)));
203 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-confirmation"), props);
204
205 QTRY_COMPARE(m_agent->m_requestedPasskey, QStringLiteral("000012"));
206 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
207}
208
209void AgentManagerTest::requestAuthorizationTest()
210{
211 QCOMPARE(m_agent->m_authorizationRequested, false);
212
213 QVariantMap props;
214 props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
215 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-authorization"), props);
216
217 QTRY_COMPARE(m_agent->m_authorizationRequested, true);
218 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
219}
220
221void AgentManagerTest::authorizeServiceTest()
222{
223 QCOMPARE(m_agent->m_authorizedUuid, QString());
224
225 QVariantMap props;
226 props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
227 props.insert(QStringLiteral("UUID"), Services::ObexFileTransfer);
228 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("authorize-service"), props);
229
230 QTRY_COMPARE(m_agent->m_authorizedUuid, Services::ObexFileTransfer);
231 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
232}
233
234void AgentManagerTest::cancelTest()
235{
236 QCOMPARE(m_agent->m_cancelCalled, false);
237
238 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("cancel"));
239
240 QTRY_COMPARE(m_agent->m_cancelCalled, true);
241 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
242}
243
244void AgentManagerTest::releaseTest()
245{
246 QCOMPARE(m_agent->m_releaseCalled, false);
247
248 FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("release"));
249
250 QTRY_COMPARE(m_agent->m_releaseCalled, true);
251 QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
252}
253
254QTEST_MAIN(AgentManagerTest)
255
256#include "moc_agentmanagertest.cpp"
257

source code of bluez-qt/autotests/agentmanagertest.cpp