Changeset View
Changeset View
Standalone View
Standalone View
autotests/bolt/fakeserver/fakemanager.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 - 2019 Daniel Vrátil <dvratil@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "fakemanager.h" | ||||
22 | #include "fakemanageradaptor.h" | ||||
23 | #include "fakedevice.h" | ||||
24 | | ||||
25 | #include <QDBusConnection> | ||||
26 | #include <QDBusError> | ||||
27 | #include <QJsonArray> | ||||
28 | | ||||
29 | #include <QDebug> | ||||
30 | | ||||
31 | #include <chrono> | ||||
32 | | ||||
33 | using namespace std::chrono_literals; | ||||
34 | | ||||
35 | namespace { | ||||
36 | static const QString kManagerDBusPath = QStringLiteral("/org/freedesktop/bolt"); | ||||
37 | } | ||||
38 | | ||||
39 | FakeManager::FakeManager(const QJsonObject &json, QObject *parent) | ||||
40 | : QObject(parent) | ||||
41 | , mProbing(json[QStringLiteral("Probing")].toBool()) | ||||
42 | , mDefaultPolicy(json[QStringLiteral("DefaultPolicy")].toString()) | ||||
43 | , mSecurityLevel(json[QStringLiteral("SecurityLevel")].toString()) | ||||
44 | , mAuthMode(json[QStringLiteral("AuthMode")].toString()) | ||||
45 | { | ||||
46 | new FakeManagerAdaptor(this); | ||||
47 | if (!QDBusConnection::sessionBus().registerObject(kManagerDBusPath, this)) { | ||||
48 | throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1") | ||||
49 | .arg(QDBusConnection::sessionBus().lastError().message())); | ||||
50 | } | ||||
51 | | ||||
52 | const auto jsonDevices = json[QStringLiteral("Devices")].toArray(); | ||||
53 | for (const auto &jsonDevice : jsonDevices) { | ||||
54 | auto device = new FakeDevice(jsonDevice.toObject(), this); | ||||
55 | mDevices.insert(device->uid(), device); | ||||
56 | } | ||||
57 | } | ||||
58 | | ||||
59 | FakeManager::FakeManager(QObject *parent) | ||||
60 | : QObject(parent) | ||||
61 | { | ||||
62 | new FakeManagerAdaptor(this); | ||||
63 | if (!QDBusConnection::sessionBus().registerObject(kManagerDBusPath, this)) { | ||||
64 | throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1") | ||||
65 | .arg(QDBusConnection::sessionBus().lastError().message())); | ||||
66 | } | ||||
67 | } | ||||
68 | | ||||
69 | FakeManager::~FakeManager() | ||||
70 | { | ||||
71 | QDBusConnection::sessionBus().unregisterObject(kManagerDBusPath); | ||||
72 | qDeleteAll(mDevices); | ||||
73 | } | ||||
74 | | ||||
75 | FakeDevice *FakeManager::addDevice(std::unique_ptr<FakeDevice> device) | ||||
76 | { | ||||
77 | auto ptr = device.release(); | ||||
78 | mDevices.insert(ptr->uid(), ptr); | ||||
79 | Q_EMIT DeviceAdded(ptr->dbusPath()); | ||||
80 | return ptr; | ||||
81 | } | ||||
82 | | ||||
83 | void FakeManager::removeDevice(const QString &uid) | ||||
84 | { | ||||
85 | auto deviceIt = mDevices.find(uid); | ||||
86 | if (deviceIt == mDevices.end()) { | ||||
87 | return; | ||||
88 | } | ||||
89 | auto device = *deviceIt; | ||||
90 | mDevices.erase(deviceIt); | ||||
91 | Q_EMIT DeviceRemoved(device->dbusPath()); | ||||
92 | device->deleteLater(); | ||||
93 | } | ||||
94 | | ||||
95 | QList<FakeDevice*> FakeManager::devices() const | ||||
96 | { | ||||
97 | return mDevices.values(); | ||||
98 | } | ||||
99 | | ||||
100 | unsigned int FakeManager::version() const | ||||
101 | { | ||||
102 | return 1; | ||||
103 | } | ||||
104 | | ||||
105 | bool FakeManager::isProbing() const | ||||
106 | { | ||||
107 | return mProbing; | ||||
108 | } | ||||
109 | | ||||
110 | QString FakeManager::defaultPolicy() const | ||||
111 | { | ||||
112 | return mDefaultPolicy; | ||||
113 | } | ||||
114 | | ||||
115 | QString FakeManager::securityLevel() const | ||||
116 | { | ||||
117 | return mSecurityLevel; | ||||
118 | } | ||||
119 | | ||||
120 | QString FakeManager::authMode() const | ||||
121 | { | ||||
122 | return mAuthMode; | ||||
123 | } | ||||
124 | | ||||
125 | void FakeManager::setAuthMode(const QString &authMode) | ||||
126 | { | ||||
127 | qDebug("Manager: authMode changed to %s", qUtf8Printable(authMode)); | ||||
128 | mAuthMode = authMode; | ||||
129 | } | ||||
130 | | ||||
131 | QList<QDBusObjectPath> FakeManager::ListDevices() const | ||||
132 | { | ||||
133 | QList<QDBusObjectPath> rv; | ||||
134 | rv.reserve(mDevices.size()); | ||||
135 | for (const auto device : mDevices) { | ||||
136 | rv.push_back(device->dbusPath()); | ||||
137 | } | ||||
138 | return rv; | ||||
139 | } | ||||
140 | | ||||
141 | QDBusObjectPath FakeManager::DeviceByUid(const QString &uid) const | ||||
142 | { | ||||
143 | auto device = mDevices.value(uid); | ||||
144 | return device ? device->dbusPath() : QDBusObjectPath(); | ||||
145 | } | ||||
146 | | ||||
147 | QDBusObjectPath FakeManager::EnrollDevice(const QString &uid, | ||||
148 | const QString &policy, | ||||
149 | const QString &flags) | ||||
150 | { | ||||
151 | std::this_thread::sleep_for(1s); // simulate this operation taking time | ||||
152 | | ||||
153 | auto device = mDevices.value(uid); | ||||
154 | if (policy == QLatin1Literal("default")) { | ||||
155 | device->setPolicy(defaultPolicy()); | ||||
156 | } else { | ||||
157 | device->setPolicy(policy); | ||||
158 | } | ||||
159 | device->setAuthFlags(flags); | ||||
160 | device->setStored(true); | ||||
161 | device->setStatus(QLatin1Literal("authorized")); | ||||
162 | | ||||
163 | return device->dbusPath(); | ||||
164 | } | ||||
165 | | ||||
166 | void FakeManager::ForgetDevice(const QString &uid) | ||||
167 | { | ||||
168 | std::this_thread::sleep_for(1s); // simulate this operation taking time | ||||
169 | | ||||
170 | auto device = mDevices.value(uid); | ||||
171 | device->setStored(false); | ||||
172 | device->setStatus(QLatin1Literal("connected")); | ||||
173 | } | ||||
174 | |