tst_maintests.cpp
10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QtTest>
#include <QtQmqtt/qmqtt.h>
#include <QScopedPointer>
#include <QHostInfo>
#include "cirbuf.h"
#include "mainapp.h"
#include "mainappthread.h"
#include "twoclienttestcontext.h"
// Dumb Qt version gives warnings when comparing uint with number literal.
template <typename T1, typename T2>
inline bool myCastCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected,
const char *file, int line)
{
T1 t2_ = static_cast<T1>(t2);
return QTest::compare_helper(t1 == t2_, "Compared values are not the same",
QTest::toString(t1), QTest::toString(t2), actual, expected, file, line);
}
#define MYCASTCOMPARE(actual, expected) \
do {\
if (!myCastCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
return;\
} while (false)
class MainTests : public QObject
{
Q_OBJECT
MainAppThread mainApp;
public:
MainTests();
~MainTests();
private slots:
void cleanupTestCase();
void test_circbuf();
void test_circbuf_unwrapped_doubling();
void test_circbuf_wrapped_doubling();
void test_circbuf_full_wrapped_buffer_doubling();
void test_validSubscribePath();
void test_retained();
void test_retained_changed();
void test_retained_removed();
void test_packet_bigger_than_one_doubling();
void test_very_big_packet();
};
MainTests::MainTests()
{
mainApp.start();
mainApp.waitForStarted();
}
MainTests::~MainTests()
{
}
void MainTests::cleanupTestCase()
{
mainApp.stopApp();
}
void MainTests::test_circbuf()
{
CirBuf buf(64);
MYCASTCOMPARE(buf.freeSpace(), 63);
uint write_n = 40;
char *head = buf.headPtr();
for (uint i = 0; i < write_n; i++)
{
head[i] = i+1;
}
buf.advanceHead(write_n);
QCOMPARE(buf.head, write_n);
MYCASTCOMPARE(buf.tail, 0);
QCOMPARE(buf.maxReadSize(), write_n);
QCOMPARE(buf.maxWriteSize(), (64 - write_n - 1));
QCOMPARE(buf.freeSpace(), 64 - write_n - 1);
for (uint i = 0; i < write_n; i++)
{
MYCASTCOMPARE(buf.tailPtr()[i], i+1);
}
buf.advanceTail(write_n);
QVERIFY(buf.tail == buf.head);
QCOMPARE(buf.tail, write_n);
MYCASTCOMPARE(buf.maxReadSize(), 0);
QCOMPARE(buf.maxWriteSize(), (64 - write_n)); // no longer -1, because the head can point to 0 afterwards
MYCASTCOMPARE(buf.freeSpace(), 63);
write_n = buf.maxWriteSize();
head = buf.headPtr();
for (uint i = 0; i < write_n; i++)
{
head[i] = i+1;
}
buf.advanceHead(write_n);
MYCASTCOMPARE(buf.head, 0);
// Now write more, starting at the beginning.
write_n = buf.maxWriteSize();
head = buf.headPtr();
for (uint i = 0; i < write_n; i++)
{
head[i] = i+100; // Offset by 100 so we can see if we overwrite the tail
}
buf.advanceHead(write_n);
QCOMPARE(buf.tailPtr()[0], 1); // Did we not overwrite the tail?
QCOMPARE(buf.head, buf.tail - 1);
}
void MainTests::test_circbuf_unwrapped_doubling()
{
CirBuf buf(64);
int w = 63;
char *head = buf.headPtr();
for (int i = 0; i < w; i++)
{
head[i] = i+1;
}
buf.advanceHead(63);
char *tail = buf.tailPtr();
for (int i = 0; i < w; i++)
{
QCOMPARE(tail[i], i+1);
}
QCOMPARE(buf.buf[64], 0); // Vacant place, because of the circulerness.
MYCASTCOMPARE(buf.head, 63);
buf.doubleSize();
tail = buf.tailPtr();
for (int i = 0; i < w; i++)
{
QCOMPARE(tail[i], i+1);
}
for (int i = 63; i < 128; i++)
{
QCOMPARE(tail[i], 5);
}
MYCASTCOMPARE(buf.tail, 0);
MYCASTCOMPARE(buf.head, 63);
MYCASTCOMPARE(buf.maxWriteSize(), 64);
MYCASTCOMPARE(buf.maxReadSize(), 63);
}
void MainTests::test_circbuf_wrapped_doubling()
{
CirBuf buf(64);
int w = 40;
char *head = buf.headPtr();
for (int i = 0; i < w; i++)
{
head[i] = i+1;
}
buf.advanceHead(w);
MYCASTCOMPARE(buf.tail, 0);
MYCASTCOMPARE(buf.head, w);
MYCASTCOMPARE(buf.maxReadSize(), 40);
MYCASTCOMPARE(buf.maxWriteSize(), 23);
buf.advanceTail(40);
MYCASTCOMPARE(buf.maxWriteSize(), 24);
head = buf.headPtr();
for (int i = 0; i < 24; i++)
{
head[i] = 99;
}
buf.advanceHead(24);
MYCASTCOMPARE(buf.tail, 40);
MYCASTCOMPARE(buf.head, 0);
MYCASTCOMPARE(buf.maxReadSize(), 24);
MYCASTCOMPARE(buf.maxWriteSize(), 39);
// Now write a little more, which starts at the start
head = buf.headPtr();
for (int i = 0; i < 10; i++)
{
head[i] = 88;
}
buf.advanceHead(10);
MYCASTCOMPARE(buf.head, 10);
buf.doubleSize();
// The 88's that were appended at the start, should now appear at the end;
for (int i = 64; i < 74; i++)
{
MYCASTCOMPARE(buf.buf[i], 88);
}
MYCASTCOMPARE(buf.tail, 40);
MYCASTCOMPARE(buf.head, 74);
}
void MainTests::test_circbuf_full_wrapped_buffer_doubling()
{
CirBuf buf(64);
buf.head = 10;
buf.tail = 10;
memset(buf.headPtr(), 1, buf.maxWriteSize());
buf.advanceHead(buf.maxWriteSize());
memset(buf.headPtr(), 2, buf.maxWriteSize());
buf.advanceHead(buf.maxWriteSize());
for (int i = 0; i < 9; i++)
{
QCOMPARE(buf.buf[i], 2);
}
QCOMPARE(buf.buf[9], 0);
for (int i = 10; i < 64; i++)
{
QCOMPARE(buf.buf[i], 1);
}
QVERIFY(true);
buf.doubleSize();
// The places where value was 1 are the same
for (int i = 10; i < 64; i++)
{
QCOMPARE(buf.buf[i], 1);
}
// The nine 2's have been moved to the end
for (int i = 64; i < 73; i++)
{
QCOMPARE(buf.buf[i], 2);
}
// The rest are our debug 5.
for (int i = 73; i < 128; i++)
{
QCOMPARE(buf.buf[i], 5);
}
QVERIFY(true);
}
void MainTests::test_validSubscribePath()
{
QVERIFY(isValidSubscribePath("one/two/three"));
QVERIFY(isValidSubscribePath("one//three"));
QVERIFY(isValidSubscribePath("one/+/three"));
QVERIFY(isValidSubscribePath("one/+/#"));
QVERIFY(isValidSubscribePath("#"));
QVERIFY(isValidSubscribePath("///"));
QVERIFY(isValidSubscribePath("//#"));
QVERIFY(isValidSubscribePath("+"));
QVERIFY(isValidSubscribePath(""));
QVERIFY(!isValidSubscribePath("one/tw+o/three"));
QVERIFY(!isValidSubscribePath("one/+o/three"));
QVERIFY(!isValidSubscribePath("one/a+/three"));
QVERIFY(!isValidSubscribePath("#//three"));
QVERIFY(!isValidSubscribePath("#//+"));
QVERIFY(!isValidSubscribePath("one/#/+"));
}
void MainTests::test_retained()
{
TwoClientTestContext testContext;
QByteArray payload = "We are testing";
QString topic = "retaintopic";
testContext.connectSender();
testContext.publish(topic, payload, true);
testContext.publish("dummy2", "Nobody sees this", true);
testContext.connectReceiver();
testContext.subscribeReceiver("dummy");
testContext.subscribeReceiver(topic);
testContext.waitReceiverReceived();
QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list");
QMQTT::Message msg = testContext.receivedMessages.first();
QCOMPARE(msg.payload(), payload);
QVERIFY(msg.retain());
testContext.receivedMessages.clear();
testContext.publish(topic, payload, true);
testContext.waitReceiverReceived();
QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list");
QMQTT::Message msg2 = testContext.receivedMessages.first();
QCOMPARE(msg2.payload(), payload);
QVERIFY2(!msg2.retain(), "Getting a retained message while already being subscribed must be marked as normal, not retain.");
}
void MainTests::test_retained_changed()
{
TwoClientTestContext testContext;
QByteArray payload = "We are testing";
QString topic = "retaintopic";
testContext.connectSender();
testContext.publish(topic, payload, true);
payload = "Changed payload";
testContext.publish(topic, payload, true);
testContext.connectReceiver();
testContext.subscribeReceiver(topic);
testContext.waitReceiverReceived();
QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list");
QMQTT::Message msg = testContext.receivedMessages.first();
QCOMPARE(msg.payload(), payload);
QVERIFY(msg.retain());
}
void MainTests::test_retained_removed()
{
TwoClientTestContext testContext;
QByteArray payload = "We are testing";
QString topic = "retaintopic";
testContext.connectSender();
testContext.publish(topic, payload, true);
payload = "";
testContext.publish(topic, payload, true);
testContext.connectReceiver();
testContext.subscribeReceiver(topic);
testContext.waitReceiverReceived();
QVERIFY2(testContext.receivedMessages.empty(), "We erased the retained message. We shouldn't have received any.");
}
void MainTests::test_packet_bigger_than_one_doubling()
{
TwoClientTestContext testContext;
QByteArray payload(8000, 3);
QString topic = "hugepacket";
testContext.connectSender();
testContext.connectReceiver();
testContext.subscribeReceiver(topic);
testContext.publish(topic, payload);
testContext.waitReceiverReceived();
QVERIFY2(testContext.receivedMessages.count() == 1, "There must be one message in the received list");
QMQTT::Message msg = testContext.receivedMessages.first();
QCOMPARE(msg.payload(), payload);
QVERIFY(!msg.retain());
}
// This tests our write buffer, and that it's emptied during writing already.
void MainTests::test_very_big_packet()
{
TwoClientTestContext testContext;
QByteArray payload(10*1024*1024, 3);
QString topic = "hugepacket";
testContext.connectSender();
testContext.connectReceiver();
testContext.subscribeReceiver(topic);
testContext.publish(topic, payload);
testContext.waitReceiverReceived();
QCOMPARE(testContext.receivedMessages.count(), 1);
QMQTT::Message msg = testContext.receivedMessages.first();
QCOMPARE(msg.payload(), payload);
QVERIFY(!msg.retain());
}
QTEST_GUILESS_MAIN(MainTests)
#include "tst_maintests.moc"