qi3pc(1.0.0-rc1) (Debug GNU 15.2.1)


./
File: tests/mock_i3ipc.cpp
Date: 2025-12-19 02:36:39
Lines:
143 of 170, 0 excluded
84.1%
Functions:
19 of 19, 0 excluded
100.0%
Branches:
108 of 304, 0 excluded
35.5%

Line Branch Exec Source
1 /* \author Hantz Vius
2 *
3 * \copyright Copyright (C) 2019-2025 Hantz Vius
4 *
5 * \license{
6 * This file is part of qi3pc.
7 *
8 * qi3pc is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.}
20 */
21
22 #include "mock_i3ipc.h"
23 #include "data/i3_messages.h"
24
25 #include <ranges>
26
27
3/8
✓ Branch 2 → 3 taken 5 times.
✗ Branch 2 → 8 not taken.
✓ Branch 4 → 5 taken 5 times.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 5 times.
✗ Branch 5 → 10 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
5 Q_LOGGING_CATEGORY(MockI3ServerLogger, "qi3pc.test.i3wm.mock", QtMsgType::QtDebugMsg);
28
29 5 MockI3Server::MockI3Server(const QString& socketPath, QObject* parent)
30 5 : QLocalServer(parent), m_socketPath(socketPath)
31 {
32
3/6
✓ Branch 6 → 7 taken 5 times.
✗ Branch 6 → 17 not taken.
✓ Branch 7 → 8 taken 5 times.
✗ Branch 7 → 15 not taken.
✓ Branch 8 → 9 taken 5 times.
✗ Branch 8 → 15 not taken.
5 qDebug(MockI3ServerLogger) << "Creating mock i3wm server reachable at" << socketPath;
33
1/2
✓ Branch 10 → 11 taken 5 times.
✗ Branch 10 → 19 not taken.
5 connect(this, &QLocalServer::newConnection, this, &MockI3Server::handleNewConnection);
34
1/2
✓ Branch 12 → 13 taken 5 times.
✗ Branch 12 → 21 not taken.
5 QFile::remove(m_socketPath);
35
1/2
✓ Branch 13 → 14 taken 5 times.
✗ Branch 13 → 21 not taken.
5 listen(m_socketPath);
36 5 }
37
38 void
39 140 MockI3Server::handleNewConnection()
40 {
41
1/2
✓ Branch 2 → 3 taken 140 times.
✗ Branch 2 → 39 not taken.
140 auto socket = nextPendingConnection();
42
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 9 taken 140 times.
140 if (socket == nullptr) {
43 qWarning(MockI3ServerLogger) << "New connection returned a nullptr client!";
44 return;
45 }
46
47
2/4
✓ Branch 10 → 11 taken 140 times.
✗ Branch 10 → 28 not taken.
✓ Branch 11 → 12 taken 140 times.
✗ Branch 11 → 26 not taken.
140 m_clients.push_back({socket, {}});
48
49 auto connection = connect(socket, &QLocalSocket::readyRead, this, [this, socket]() {
50 80 handleClientMessage(socket);
51
1/2
✓ Branch 14 → 15 taken 140 times.
✗ Branch 14 → 32 not taken.
140 });
52
53
2/4
✓ Branch 15 → 16 taken 140 times.
✗ Branch 15 → 35 not taken.
✓ Branch 16 → 17 taken 140 times.
✗ Branch 16 → 33 not taken.
140 connect(socket, &QLocalSocket::disconnected, this, [this, socket, connection]() {
54 130 disconnect(connection);
55 130 std::erase_if(m_clients, [&socket](const auto& client) {
56 320 return client.first == socket;
57 });
58 130 });
59 140 }
60
61 void
62 80 MockI3Server::handleClientMessage(QLocalSocket* socket)
63 {
64
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 14 taken 80 times.
80 if (socket == nullptr) {
65 qCWarning(MockI3ServerLogger) << "Can't read message from nullptr socket.";
66 return;
67 }
68
69 char c[qi3pc::IpcMagicLength];
70
1/2
✓ Branch 14 → 15 taken 80 times.
✗ Branch 14 → 154 not taken.
80 socket->read(c, qi3pc::IpcMagicLength);
71
2/4
✓ Branch 17 → 18 taken 80 times.
✗ Branch 17 → 126 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 36 taken 80 times.
160 if (auto s = std::string(c); s != qi3pc::IpcMagicString) {
72 qCWarning(MockI3ServerLogger) << "Unexpected magic string in message. Expected"
73 << qi3pc::IpcMagicString << ". - Found" << s << ".";
74 return;
75
1/2
✓ Branch 38 → 39 taken 80 times.
✗ Branch 38 → 41 not taken.
80 }
76
77 quint32 size;
78
2/4
✓ Branch 40 → 42 taken 80 times.
✗ Branch 40 → 154 not taken.
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 58 taken 80 times.
80 if (auto read_size = socket->read(reinterpret_cast<char*>(&size), sizeof size);
79 read_size != sizeof size) {
80 qCWarning(MockI3ServerLogger) << "Insufficient data read for message size. Seeked"
81 << sizeof size <<"bytes. - Read" << read_size << "bytes.";
82 return;
83 }
84
85 quint32 type;
86
2/4
✓ Branch 58 → 59 taken 80 times.
✗ Branch 58 → 154 not taken.
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 75 taken 80 times.
80 if (auto read_size = socket->read(reinterpret_cast<char*>(&type), sizeof type);
87 read_size != sizeof type) {
88 qCWarning(MockI3ServerLogger) << "Insufficient data read for message type. Seeked"
89 << sizeof type <<"bytes. - Read" << read_size << "bytes.";
90 return;
91 }
92
93
1/2
✓ Branch 75 → 76 taken 80 times.
✗ Branch 75 → 154 not taken.
80 QByteArray bytes = socket->read(size);
94
95
2/4
✓ Branch 76 → 77 taken 80 times.
✗ Branch 76 → 152 not taken.
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 79 taken 80 times.
80 if(socket->bytesAvailable() > 0) {
96 emit socket->readyRead();
97 }
98
99
12/14
✓ Branch 79 → 80 taken 15 times.
✓ Branch 79 → 82 taken 5 times.
✓ Branch 79 → 84 taken 5 times.
✓ Branch 79 → 86 taken 5 times.
✓ Branch 79 → 88 taken 5 times.
✓ Branch 79 → 90 taken 5 times.
✓ Branch 79 → 92 taken 10 times.
✓ Branch 79 → 94 taken 5 times.
✓ Branch 79 → 96 taken 5 times.
✓ Branch 79 → 98 taken 5 times.
✓ Branch 79 → 100 taken 10 times.
✗ Branch 79 → 102 not taken.
✓ Branch 79 → 103 taken 5 times.
✗ Branch 79 → 105 not taken.
80 switch(static_cast<qi3pc::IpcType>(type)) {
100 15 case qi3pc::IpcType::Command:
101
1/2
✓ Branch 80 → 81 taken 15 times.
✗ Branch 80 → 152 not taken.
15 processCommand(socket, bytes);
102 15 break;
103 5 case qi3pc::IpcType::Workspaces:
104
1/2
✓ Branch 82 → 83 taken 5 times.
✗ Branch 82 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::workspaces, qi3pc::IpcType::Workspaces);
105 5 break;
106 5 case qi3pc::IpcType::Subscribe:
107
1/2
✓ Branch 84 → 85 taken 5 times.
✗ Branch 84 → 152 not taken.
5 processSubscription(socket, bytes);
108 5 break;
109 5 case qi3pc::IpcType::Outputs:
110
1/2
✓ Branch 86 → 87 taken 5 times.
✗ Branch 86 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::outputs, qi3pc::IpcType::Outputs);
111 5 break;
112 5 case qi3pc::IpcType::Tree:
113
1/2
✓ Branch 88 → 89 taken 5 times.
✗ Branch 88 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::tree, qi3pc::IpcType::Tree);
114 5 break;
115 5 case qi3pc::IpcType::Marks:
116
1/2
✓ Branch 90 → 91 taken 5 times.
✗ Branch 90 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::marks, qi3pc::IpcType::Marks);
117 5 break;
118 10 case qi3pc::IpcType::BarConfig:
119
1/2
✓ Branch 92 → 93 taken 10 times.
✗ Branch 92 → 152 not taken.
10 sendBarConfigReply(socket, bytes);
120 10 break;
121 5 case qi3pc::IpcType::Version:
122
1/2
✓ Branch 94 → 95 taken 5 times.
✗ Branch 94 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::version, qi3pc::IpcType::Version);
123 5 break;
124 5 case qi3pc::IpcType::BindingModes:
125
1/2
✓ Branch 96 → 97 taken 5 times.
✗ Branch 96 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::bindingModes, qi3pc::IpcType::BindingModes);
126 5 break;
127 5 case qi3pc::IpcType::Config:
128
1/2
✓ Branch 98 → 99 taken 5 times.
✗ Branch 98 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::config, qi3pc::IpcType::Config);
129 5 break;
130 10 case qi3pc::IpcType::Tick:
131
1/2
✓ Branch 100 → 101 taken 10 times.
✗ Branch 100 → 152 not taken.
10 processTick(socket, bytes);
132 10 break;
133 case qi3pc::IpcType::Sync:
134 break;
135 5 case qi3pc::IpcType::BindingState:
136
1/2
✓ Branch 103 → 104 taken 5 times.
✗ Branch 103 → 152 not taken.
5 send(socket, test_qi3pc::data::i3messages::bindingState, qi3pc::IpcType::BindingState);
137 5 break;
138 default:
139 qCWarning(MockI3ServerLogger) << "Received message of unknown type:" << type;
140 break;
141 }
142 80 }
143
144 void
145 90 MockI3Server::send(QLocalSocket* socket, const QByteArray& payload, const char* type, qint64 len)
146 {
147 90 QByteArray message;
148
1/2
✓ Branch 3 → 4 taken 90 times.
✗ Branch 3 → 16 not taken.
90 QDataStream stream(&message, QIODevice::WriteOnly);
149
1/2
✓ Branch 5 → 6 taken 90 times.
✗ Branch 5 → 17 not taken.
90 stream.writeRawData(qi3pc::IpcMagicString.data(), qi3pc::IpcMagicLength);
150
151 90 qint32 size = payload.size();
152
1/2
✓ Branch 7 → 8 taken 90 times.
✗ Branch 7 → 17 not taken.
90 stream.writeRawData(reinterpret_cast<const char*>(&size), sizeof size);
153
1/2
✓ Branch 8 → 9 taken 90 times.
✗ Branch 8 → 17 not taken.
90 stream.writeRawData(type, len);
154
155
1/2
✓ Branch 9 → 10 taken 90 times.
✗ Branch 9 → 12 not taken.
90 if (size > 0) {
156
1/2
✓ Branch 11 → 12 taken 90 times.
✗ Branch 11 → 17 not taken.
90 stream.writeRawData(payload.constData(), size);
157 }
158
159
1/2
✓ Branch 12 → 13 taken 90 times.
✗ Branch 12 → 17 not taken.
90 socket->write(message);
160 90 }
161
162 void
163 80 MockI3Server::send(QLocalSocket* socket, const QByteArray& payload, qi3pc::IpcType type)
164 {
165 80 send(socket, payload, reinterpret_cast<const char*>(&type), sizeof type);
166 80 }
167
168 void
169 10 MockI3Server::send(QLocalSocket* socket, const QByteArray& payload, qi3pc::IpcEvent event)
170 {
171 10 send(socket, payload, reinterpret_cast<const char*>(&event), sizeof event);
172 10 }
173
174 void
175 10 MockI3Server::sendBarConfigReply(QLocalSocket* socket, const QByteArray& message)
176 {
177
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 8 taken 5 times.
10 if (message.isEmpty()) {
178
2/4
✓ Branch 4 → 5 taken 5 times.
✗ Branch 4 → 20 not taken.
✓ Branch 5 → 6 taken 5 times.
✗ Branch 5 → 18 not taken.
5 send(socket, test_qi3pc::data::i3messages::barIds(), qi3pc::IpcType::BarConfig);
179 } else {
180 5 auto configs = test_qi3pc::data::i3messages::barConfigs;
181
3/6
✓ Branch 9 → 10 taken 5 times.
✗ Branch 9 → 23 not taken.
✓ Branch 10 → 11 taken 5 times.
✗ Branch 10 → 21 not taken.
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 14 not taken.
5 if(auto id = QString::fromUtf8(message); configs.contains(id)) {
182
2/4
✓ Branch 12 → 13 taken 5 times.
✗ Branch 12 → 21 not taken.
✓ Branch 13 → 14 taken 5 times.
✗ Branch 13 → 21 not taken.
5 send(socket, configs[id], qi3pc::IpcType::BarConfig);
183 5 }
184 5 }
185 10 }
186
187 void
188 15 MockI3Server::processCommand(QLocalSocket* socket, const QByteArray& message)
189 {
190
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 10 taken 15 times.
15 if (message.isEmpty()) {
191 send(socket, QByteArrayLiteral(R"([])"), qi3pc::IpcType::Command);
192 } else {
193 15 auto commands = test_qi3pc::data::i3messages::commands;
194
3/6
✓ Branch 11 → 12 taken 15 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 15 times.
✗ Branch 12 → 26 not taken.
✓ Branch 13 → 14 taken 15 times.
✗ Branch 13 → 16 not taken.
15 if(auto command = QString::fromUtf8(message); commands.contains(command)) {
195
2/4
✓ Branch 14 → 15 taken 15 times.
✗ Branch 14 → 26 not taken.
✓ Branch 15 → 16 taken 15 times.
✗ Branch 15 → 26 not taken.
15 send(socket, commands[command].reply, qi3pc::IpcType::Command);
196 15 }
197 15 }
198 15 }
199
200 void
201 5 MockI3Server::processSubscription(QLocalSocket* socket, const QByteArray& message)
202 {
203 5 QJsonParseError parseError;
204
1/2
✓ Branch 2 → 3 taken 5 times.
✗ Branch 2 → 117 not taken.
5 QJsonDocument payload = QJsonDocument::fromJson(message, &parseError);
205
206
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 18 taken 5 times.
5 if (parseError.error != QJsonParseError::NoError) {
207 qCWarning(MockI3ServerLogger) << "Parsing subscription message failed - JSON parse error:"
208 << parseError.errorString();
209 return;
210 }
211
212
1/2
✓ Branch 18 → 19 taken 5 times.
✗ Branch 18 → 115 not taken.
5 auto events = payload.array();
213
2/4
✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 113 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 32 taken 5 times.
5 if (events.isEmpty()) {
214 qCWarning(MockI3ServerLogger) << "Subscribe message received with no payload";
215 return;
216 }
217
218 5 auto sendTick = false;
219
1/2
✓ Branch 34 → 35 taken 5 times.
✗ Branch 34 → 112 not taken.
5 if (auto client = std::find_if(m_clients.begin(), m_clients.end(),
220 10 [&socket](const auto& client){
221 10 return client.first == socket;});
222
1/2
✓ Branch 42 → 43 taken 5 times.
✗ Branch 42 → 56 not taken.
10 client != m_clients.end()) {
223
224 auto supportedEventsView = events
225
1/2
✓ Branch 45 → 46 taken 5 times.
✗ Branch 45 → 91 not taken.
5 | std::views::transform([] (const QJsonValue& v) {
226
2/4
✓ Branch 2 → 3 taken 15 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 15 times.
✗ Branch 3 → 8 not taken.
15 return v.toString().toStdString();
227
1/2
✓ Branch 46 → 47 taken 5 times.
✗ Branch 46 → 91 not taken.
5 })
228
1/2
✓ Branch 44 → 45 taken 5 times.
✗ Branch 44 → 94 not taken.
5 | std::views::filter([&client] (const std::string& event) {
229
1/2
✓ Branch 7 → 8 taken 5 times.
✗ Branch 7 → 11 not taken.
15 return std::ranges::find(client->second, event) == client->second.end();
230
1/2
✓ Branch 47 → 48 taken 5 times.
✗ Branch 47 → 91 not taken.
5 })
231
1/2
✓ Branch 43 → 44 taken 5 times.
✗ Branch 43 → 97 not taken.
5 | std::views::filter([] (const std::string& event) {
232 5 return std::ranges::find(m_supportedEvents, event) != m_supportedEvents.end();
233
1/2
✓ Branch 48 → 49 taken 5 times.
✗ Branch 48 → 91 not taken.
5 });
234
235
1/2
✓ Branch 49 → 50 taken 5 times.
✗ Branch 49 → 99 not taken.
5 std::ranges::for_each(supportedEventsView, [&] (const std::string& event) {
236 10 client->second.insert(event);
237
238
1/2
✓ Branch 6 → 7 taken 5 times.
✗ Branch 6 → 14 not taken.
5 if(event == "tick") {
239 5 socket->flush();
240
1/2
✓ Branch 10 → 11 taken 5 times.
✗ Branch 10 → 15 not taken.
5 send(socket, QByteArrayLiteral(R"({"first": true, "payload": ""})"), qi3pc::IpcEvent::Tick);
241 5 socket->flush();
242 }
243 5 });
244
245
1/2
✓ Branch 52 → 53 taken 5 times.
✗ Branch 52 → 100 not taken.
5 send(socket, QByteArrayLiteral(R"({"success": true})"), qi3pc::IpcType::Subscribe);
246 } else {
247 qCWarning(MockI3ServerLogger) << "Received subscription request from unknown client";
248 }
249
2/4
✓ Branch 69 → 70 taken 5 times.
✗ Branch 69 → 71 not taken.
✓ Branch 73 → 74 taken 5 times.
✗ Branch 73 → 76 not taken.
5 }
250
251 void
252 10 MockI3Server::processTick(QLocalSocket* socket, const QByteArray& message)
253 {
254
2/2
✓ Branch 38 → 4 taken 20 times.
✓ Branch 38 → 39 taken 10 times.
40 for (auto& client: m_clients) {
255
4/6
✓ Branch 8 → 9 taken 20 times.
✗ Branch 8 → 48 not taken.
✓ Branch 9 → 10 taken 20 times.
✗ Branch 9 → 46 not taken.
✓ Branch 12 → 13 taken 5 times.
✓ Branch 12 → 29 taken 15 times.
60 if (client.second.contains("tick")) {
256
1/2
✓ Branch 13 → 14 taken 5 times.
✗ Branch 13 → 69 not taken.
5 client.first->flush();
257 10 QByteArray payload = QByteArrayLiteral(R"({"first": false, "payload": ")")
258
1/2
✓ Branch 18 → 19 taken 5 times.
✗ Branch 18 → 54 not taken.
10 + message
259
1/2
✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 52 not taken.
15 + QByteArrayLiteral(R"("})");
260
1/2
✓ Branch 25 → 26 taken 5 times.
✗ Branch 25 → 67 not taken.
5 send(client.first, payload, qi3pc::IpcEvent::Tick);
261
1/2
✓ Branch 26 → 27 taken 5 times.
✗ Branch 26 → 67 not taken.
5 client.first->flush();
262 5 }
263 }
264
265
1/2
✓ Branch 41 → 42 taken 10 times.
✗ Branch 41 → 71 not taken.
10 send(socket, QByteArrayLiteral(R"({"success": true})"), qi3pc::IpcType::Tick);
266 10 socket->flush();
267 10 }
268