summaryrefslogtreecommitdiff
path: root/tests/unit/network/test_message_client_inhibit_status.cpp
blob: 6dc77367991803ed0a027d78a1d53099b81eb02c (plain)
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
#include <QtTest>

#include <QHostAddress>
#include <QNetworkDatagram>
#include <QUdpSocket>

#include <memory>

#include "Network/MessageClient.hpp"
#include "Network/NetworkMessage.hpp"

class TestMessageClientInhibitStatus final
  : public QObject
{
  Q_OBJECT

private:
  static QByteArray command (quint32 ttl = 1000, QByteArray controller = "key-agent",
                             QByteArray station = "W2SZ", QString id = "test-client",
                             quint32 schema = 2)
  {
    QByteArray message;
    NetworkMessage::Builder out {&message, NetworkMessage::TxInhibit, id, schema};
    out << controller << ttl << station;
    return message;
  }

private Q_SLOTS:
  void acceptUdpRequestsAuthorizesCommands ()
  {
    QUdpSocket receiver;
    QVERIFY (receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));
    MessageClient client {"test-client", "version", "revision", "127.0.0.1",
                          receiver.localPort (), {}, 1};
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    auto const endpoint = receiver.receiveDatagram ();
    QSignalSpy commands {&client, &MessageClient::tx_inhibit_command};
    QSignalSpy clears {&client, &MessageClient::clear_decodes};
    QSignalSpy invalid {&client, &MessageClient::tx_inhibit_invalid};
    QByteArray clear;
    NetworkMessage::Builder out {&clear, NetworkMessage::Clear, "test-client", 2};
    out << quint8 {2};
    auto send = [&] (QByteArray const& bytes) {
      QCOMPARE (receiver.writeDatagram (bytes, endpoint.senderAddress (), endpoint.senderPort ()),
                qint64 (bytes.size ()));
    };
    send (command ());
    send (command (99));
    send (clear);
    QTest::qWait (150);
    QCOMPARE (commands.size (), 0);
    QCOMPARE (invalid.size (), 0);
    QCOMPARE (clears.size (), 0);
    client.enable (true);
    send (command (100));
    send (command (30000, "key-agent", "Priority", "test-client", 3));
    send (command (0, "key-agent", {}));
    QTRY_COMPARE (commands.size (), 3);
    QCOMPARE (commands[0][0].toString (), QString {"key-agent"});
    QCOMPARE (commands[0][1].toUInt (), 100u);
    QCOMPARE (commands[1][1].toUInt (), 30000u);
    QCOMPARE (commands[1][2].toString (), QString {"Priority"});
    QCOMPARE (commands[2][1].toUInt (), 0u);
    QVERIFY (commands[2][2].toString ().isEmpty ());
    send (command (1000, "key-agent", {}, "other-instance"));
    send (clear);
    QTRY_COMPARE (clears.size (), 1);
    QCOMPARE (commands.size (), 3);
    client.enable (false);
    send (command ());
    send (command (99));
    send (clear);
    QTest::qWait (150);
    QCOMPARE (commands.size (), 3);
    QCOMPARE (invalid.size (), 0);
    QCOMPARE (clears.size (), 1);
  }

  void rejectsMalformedCommands ()
  {
    QUdpSocket receiver;
    QVERIFY (receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));
    MessageClient client {"test-client", "version", "revision", "127.0.0.1",
                          receiver.localPort (), {}, 1};
    client.enable (true);
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    auto const endpoint = receiver.receiveDatagram ();
    QSignalSpy commands {&client, &MessageClient::tx_inhibit_command};
    QSignalSpy invalid {&client, &MessageClient::tx_inhibit_invalid};
    QList<QByteArray> bad {
      command (99), command (30001), command (0xffffffff), command (1000, {}),
      command (1000, "two words"), command (1000, QByteArray (129, 'a')),
      command (1000, QByteArray ("\xc0\xaf", 2)), command (1000, "key", QByteArray (129, 'a')),
      command (1000, "key", QByteArray ("\xff", 1)), command (1000, "key", "line\nbreak"),
      command (1000, "key", {}, "test-client", 1), command () + QByteArray (4096, 'a')
    };
    auto const valid = command ();
    for (int length = 12; length < valid.size (); ++length) bad.append (valid.left (length));
    auto corrupt = valid;
    corrupt[0] = 0;
    bad.append (corrupt);
    corrupt = valid;
    corrupt[7] = 99;
    bad.append (corrupt);
    corrupt = valid;
    for (int index = 12; index < 16; ++index) corrupt[index] = char (0xff);
    bad.append (corrupt);
    for (auto const& bytes : bad)
      QCOMPARE (receiver.writeDatagram (bytes, endpoint.senderAddress (), endpoint.senderPort ()),
                qint64 (bytes.size ()));
    auto count = [&] {
      quint64 result {0};
      for (auto const& event : invalid) result += event[0].toULongLong ();
      return result;
    };
    QTRY_COMPARE (count (), quint64 (bad.size ()));
    QCOMPARE (commands.size (), 0);
    QVERIFY (invalid.size () < bad.size ());
    auto extended = command () + QByteArray {"future fields"};
    receiver.writeDatagram (extended, endpoint.senderAddress (), endpoint.senderPort ());
    QTRY_COMPARE (commands.size (), 1);
  }

  void receivesCommandsWhenOutgoingReportingIsDisabled ()
  {
    QUdpSocket receiver;
    QVERIFY (receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));
    MessageClient client {"test-client", "version", "revision", "127.0.0.1",
                          receiver.localPort (), {}, 1};
    client.enable (true);
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    auto const endpoint = receiver.receiveDatagram ();
    QSignalSpy commands {&client, &MessageClient::tx_inhibit_command};
    client.set_server_port (0);
    auto const bytes = command ();
    QCOMPARE (receiver.writeDatagram (bytes, endpoint.senderAddress (), endpoint.senderPort ()),
              qint64 (bytes.size ()));
    QTRY_COMPARE (commands.size (), 1);
  }

  void reportsInvalidServerThroughExistingErrorSignal ()
  {
    MessageClient client {"test-client", "version", "revision", "", 0, {}, 1};
    QSignalSpy errors {&client, &MessageClient::error};
    client.set_server ("255.255.255.255", {});
    QCOMPARE (errors.size (), 1);
    QVERIFY (errors[0][0].toString ().contains ("IPv4 broadcast not supported"));
  }

  void portChangeDuringLookupDoesNotCloseClient ()
  {
    QUdpSocket receiver;
    QVERIFY (receiver.bind (QHostAddress {QHostAddress::Any}, 0));
    MessageClient client {"test-client", "version", "revision", "127.0.0.1", 0, {}, 1};
    QSignalSpy closed {&client, &MessageClient::close};
    client.set_server ("localhost", {});
    client.set_server_port (receiver.localPort ());
    QCOMPARE (closed.size (), 0);
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    QCOMPARE (closed.size (), 0);
  }

  void advertisementTracksRequestAuthorization ()
  {
    QUdpSocket receiver;
    QVERIFY (receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));
    MessageClient client {"test-client", "version", "revision", "127.0.0.1",
                          receiver.localPort (), {}, 1};
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    while (receiver.hasPendingDatagrams ()) receiver.receiveDatagram ();

    client.inhibit_status (true, true, QStringLiteral ("W2SZ"), 11, 12, 13, 14);
    QTest::qWait (50);
    QVERIFY (!receiver.hasPendingDatagrams ());

    client.enable (true);
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    NetworkMessage::Reader available {receiver.receiveDatagram ().data ()};
    bool supported {false};
    bool inhibited {false};
    QByteArray source;
    quint32 hold_rx {0};
    quint32 release_rx {0};
    quint32 expiries {0};
    quint32 invalid {0};
    available >> supported >> inhibited >> source >> hold_rx >> release_rx
              >> expiries >> invalid;
    QCOMPARE (available.type (), NetworkMessage::InhibitStatus);
    QVERIFY (supported);
    QVERIFY (inhibited);
    QCOMPARE (source, QByteArrayLiteral ("W2SZ"));

    client.enable (false);
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    NetworkMessage::Reader withdrawn {receiver.receiveDatagram ().data ()};
    withdrawn >> supported >> inhibited >> source >> hold_rx >> release_rx
              >> expiries >> invalid;
    QCOMPARE (withdrawn.type (), NetworkMessage::InhibitStatus);
    QVERIFY (!supported);
    QVERIFY (inhibited);
    QCOMPARE (source, QByteArrayLiteral ("W2SZ"));
    QCOMPARE (hold_rx, 11u);
    QCOMPARE (release_rx, 12u);
    QCOMPARE (expiries, 13u);
    QCOMPARE (invalid, 14u);

    client.inhibit_status (true, false, QString {}, 15, 16, 17, 18);
    QTest::qWait (50);
    QVERIFY (!receiver.hasPendingDatagrams ());
  }

  void heartbeatRefreshesAvailableStatus ()
  {
    QUdpSocket original_receiver;
    QVERIFY (original_receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));
    MessageClient client {"test-client", "version", "revision", "127.0.0.1",
                          original_receiver.localPort (), {}, 1};
    client.enable (true);
    client.inhibit_status (true, false, QString {}, 1, 2, 3, 4);
    QTRY_VERIFY (original_receiver.hasPendingDatagrams ());
    QTest::qWait (25);
    while (original_receiver.hasPendingDatagrams ()) original_receiver.receiveDatagram ();

    QUdpSocket refreshed_receiver;
    QVERIFY (refreshed_receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));
    client.set_server_port (refreshed_receiver.localPort ());
    QTRY_VERIFY (refreshed_receiver.hasPendingDatagrams ());
    NetworkMessage::Reader heartbeat {refreshed_receiver.receiveDatagram ().data ()};
    QCOMPARE (heartbeat.type (), NetworkMessage::Heartbeat);
    QTRY_VERIFY (refreshed_receiver.hasPendingDatagrams ());
    NetworkMessage::Reader refreshed {refreshed_receiver.receiveDatagram ().data ()};
    QCOMPARE (refreshed.type (), NetworkMessage::InhibitStatus);
    bool supported {false};
    bool inhibited {true};
    QByteArray source;
    quint32 hold_rx {0};
    quint32 release_rx {0};
    quint32 expiries {0};
    quint32 invalid {0};
    refreshed >> supported >> inhibited >> source >> hold_rx >> release_rx
              >> expiries >> invalid;
    QVERIFY (supported);
    QVERIFY (!inhibited);
    QVERIFY (source.isEmpty ());
    QCOMPARE (hold_rx, 1u);
    QCOMPARE (release_rx, 2u);
    QCOMPARE (expiries, 3u);
    QCOMPARE (invalid, 4u);

    client.inhibit_status (false, false, QString {}, 0, 0, 0, 0);
    QTRY_VERIFY (refreshed_receiver.hasPendingDatagrams ());
    NetworkMessage::Reader withdrawn {refreshed_receiver.receiveDatagram ().data ()};
    supported = true;
    withdrawn >> supported;
    QCOMPARE (withdrawn.type (), NetworkMessage::InhibitStatus);
    QVERIFY (!supported);

    QUdpSocket unsupported_receiver;
    QVERIFY (unsupported_receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));
    client.set_server_port (unsupported_receiver.localPort ());
    QTRY_VERIFY (unsupported_receiver.hasPendingDatagrams ());
    NetworkMessage::Reader unsupported_heartbeat {
      unsupported_receiver.receiveDatagram ().data ()};
    QCOMPARE (unsupported_heartbeat.type (), NetworkMessage::Heartbeat);
    QTest::qWait (50);
    QVERIFY (!unsupported_receiver.hasPendingDatagrams ());
  }

  void preservesType17FieldOrder ()
  {
    QUdpSocket receiver;
    QVERIFY (receiver.bind (QHostAddress {QHostAddress::LocalHost}, 0));

    auto client = std::unique_ptr<MessageClient> {new MessageClient {
      "test-client", "test-version", "test-revision",
      "127.0.0.1", receiver.localPort (), {}, 1}};
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    while (receiver.hasPendingDatagrams ())
      {
        receiver.receiveDatagram ();
      }

    client->enable (true);
    client->inhibit_status (true, true, QStringLiteral ("W2SZ"), 11, 12, 13, 14);
    QTRY_VERIFY (receiver.hasPendingDatagrams ());
    auto const datagram = receiver.receiveDatagram ().data ();

    NetworkMessage::Reader reader {datagram};
    QCOMPARE (reader.type (), NetworkMessage::InhibitStatus);
    QCOMPARE (reader.id (), QStringLiteral ("test-client"));

    bool supported {false};
    bool inhibited {false};
    QByteArray source;
    quint32 hold_rx {0};
    quint32 release_rx {0};
    quint32 expiries {0};
    quint32 invalid {0};
    reader >> supported >> inhibited >> source >> hold_rx >> release_rx
           >> expiries >> invalid;

    QCOMPARE (reader.status (), QDataStream::Ok);
    QVERIFY (supported);
    QVERIFY (reader.atEnd ());
    QVERIFY (inhibited);
    QCOMPARE (source, QByteArrayLiteral ("W2SZ"));
    QCOMPARE (hold_rx, 11u);
    QCOMPARE (release_rx, 12u);
    QCOMPARE (expiries, 13u);
    QCOMPARE (invalid, 14u);
  }
};

QTEST_GUILESS_MAIN (TestMessageClientInhibitStatus)
#include "test_message_client_inhibit_status.moc"