summaryrefslogtreecommitdiff
path: root/tests/unit/jtty/test_mmttyif.cpp
blob: 8333d6c9acb816c4d42663dd4d203f4f741efc70 (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
#include <QtTest>

#include <memory>

#include <QCoreApplication>
#include <QHostAddress>
#include <QSignalSpy>
#include <QTcpServer>
#include <QTcpSocket>

#include "widgets/MMTTYIF.hpp"

namespace
{
  int constexpr maximumHeaderBytes {64};
  int constexpr maximumPayloadBytes {64 * 1024};
}

class TestMMTTYIF final
  : public QObject
{
  Q_OBJECT

private slots:
  void init ()
  {
    server_.reset (new QTcpServer);
    // MMTTYIF connects to 127.0.0.1 (IPv4). Bind the same family so Windows
    // does not leave the server on ::1 while the client dials IPv4.
    QVERIFY (server_->listen (QHostAddress {QStringLiteral ("127.0.0.1")}, 0));
    QCoreApplication::processEvents ();

    interface_.reset (new MMTTYIF);
    interface_->initialize (server_->serverPort ());

    QTRY_VERIFY_WITH_TIMEOUT (server_->hasPendingConnections (), 5000);
    peer_ = server_->nextPendingConnection ();
    QVERIFY (peer_);
    QTRY_VERIFY_WITH_TIMEOUT (interface_->isConnected (), 5000);
  }

  void cleanup ()
  {
    interface_.reset ();
    peer_ = nullptr;
    server_.reset ();
    QCoreApplication::processEvents ();
  }

  void parsesFragmentedFrame ()
  {
    QSignalSpy text {interface_.get (), &MMTTYIF::app_tx_string};

    QVERIFY (writeToInterface ("<TXTEXT:11>HELLO"));
    QTest::qWait (20);
    QCOMPARE (text.count (), 0);

    QVERIFY (writeToInterface (" WORLD"));
    QTRY_COMPARE_WITH_TIMEOUT (text.count (), 1, 1000);
    QCOMPARE (text.at (0).at (0).toString (), QString {"HELLO WORLD"});
  }

  void dispatchesFragmentedTaggedTextBeforeStart ()
  {
    QStringList events;
    connect (interface_.get (), &MMTTYIF::app_tx_string, this,
             [&events] (QString const& message) { events.append ("text:" + message); });
    connect (interface_.get (), &MMTTYIF::app_start_tx, this,
             [&events] { events.append (QStringLiteral ("start")); });

    QByteArray const payload {"[[JTTY:CALL_EXCH]] W9XYZ 7"};
    QByteArray frame {"<TXTEXT:"};
    frame += QByteArray::number (payload.size ());
    frame += '>';
    frame += payload;
    int const split = frame.size () - 5;

    QVERIFY (writeToInterface (frame.left (split)));
    QTest::qWait (20);
    QVERIFY (events.isEmpty ());

    QVERIFY (writeToInterface (frame.mid (split) + "<XMIT:2>ON"));
    QTRY_COMPARE_WITH_TIMEOUT (events.size (), 2, 1000);
    QCOMPARE (events.at (0), QStringLiteral ("text:[[JTTY:CALL_EXCH]] W9XYZ 7"));
    QCOMPARE (events.at (1), QStringLiteral ("start"));
  }

  void parsesCoalescedFrames ()
  {
    QSignalSpy text {interface_.get (), &MMTTYIF::app_tx_string};
    QSignalSpy start {interface_.get (), &MMTTYIF::app_start_tx};

    QVERIFY (writeToInterface ("<TXTEXT:5>A<B>C<XMIT:2>ON"));

    QTRY_COMPARE_WITH_TIMEOUT (text.count (), 1, 1000);
    QTRY_COMPARE_WITH_TIMEOUT (start.count (), 1, 1000);
    QCOMPARE (text.at (0).at (0).toString (), QString {"A<B>C"});
  }

  void dispatchesControlCommands ()
  {
    QSignalSpy stop {interface_.get (), &MMTTYIF::app_stop_tx};
    QSignalSpy abort {interface_.get (), &MMTTYIF::app_abort_tx};
    QSignalSpy close {interface_.get (), &MMTTYIF::app_is_quitting};

    QVERIFY (writeToInterface ("<XMIT:3>OFF<ABORT>ABORT<CLOSE>CLOSE"));

    QTRY_COMPARE_WITH_TIMEOUT (stop.count (), 1, 1000);
    QTRY_COMPARE_WITH_TIMEOUT (abort.count (), 1, 1000);
    QTRY_COMPARE_WITH_TIMEOUT (close.count (), 1, 1000);
  }

  void delaysOutputCompleteUntilDrainReport ()
  {
    QSignalSpy stop {interface_.get (), &MMTTYIF::app_stop_tx};
    peer_->readAll ();

    QVERIFY (writeToInterface ("<XMIT:3>OFF"));
    QTRY_COMPARE_WITH_TIMEOUT (stop.count (), 1, 1000);
    QTest::qWait (20);
    QCOMPARE (peer_->bytesAvailable (), qint64 {0});

    interface_->report_output_complete ();
    QTRY_VERIFY_WITH_TIMEOUT (peer_->bytesAvailable () > 0, 1000);
    QCOMPARE (peer_->readAll (), QByteArray {"<OUTPUTCOMPLETE>"});
  }

  void parsesFragmentedImplicitControl ()
  {
    QSignalSpy abort {interface_.get (), &MMTTYIF::app_abort_tx};
    QSignalSpy received {interface_.get (), &MMTTYIF::message_received};

    QVERIFY (writeToInterface ("<ABORT>AB"));
    QTRY_VERIFY_WITH_TIMEOUT (received.count () >= 1, 1000);
    QCOMPARE (abort.count (), 0);

    QVERIFY (writeToInterface ("ORT"));
    QTRY_COMPARE_WITH_TIMEOUT (abort.count (), 1, 1000);
  }

  void recoversAfterInvalidLength ()
  {
    QSignalSpy start {interface_.get (), &MMTTYIF::app_start_tx};
    QSignalSpy logs {interface_.get (), &MMTTYIF::log_message};

    QVERIFY (writeToInterface ("<TXTEXT:-1><TXTEXT:nope><XMIT:2>ON"));

    QTRY_COMPARE_WITH_TIMEOUT (start.count (), 1, 1000);
    QTRY_VERIFY_WITH_TIMEOUT (logs.count () >= 2, 1000);
  }

  void acceptsMaximumDeclaredPayload ()
  {
    QSignalSpy text {interface_.get (), &MMTTYIF::app_tx_string};
    QByteArray const payload (maximumPayloadBytes, 'a');
    QByteArray frame {"<TXTEXT:"};
    frame += QByteArray::number (payload.size ());
    frame += '>';
    frame += payload;

    QVERIFY (writeToInterface (frame));

    QTRY_COMPARE_WITH_TIMEOUT (text.count (), 1, 2000);
    QCOMPARE (text.at (0).at (0).toString (), QString (maximumPayloadBytes, 'A'));
  }

  void rejectsOversizedDeclaredPayload ()
  {
    QSignalSpy start {interface_.get (), &MMTTYIF::app_start_tx};
    QSignalSpy logs {interface_.get (), &MMTTYIF::log_message};

    QByteArray frame {"<TXTEXT:"};
    frame += QByteArray::number (maximumPayloadBytes + 1);
    frame += "><XMIT:2>ON";
    QVERIFY (writeToInterface (frame));

    QTRY_COMPARE_WITH_TIMEOUT (start.count (), 1, 1000);
    QTRY_VERIFY_WITH_TIMEOUT (hasLogContaining (logs, "payload length exceeds"), 1000);
  }

  void rejectsSignedMaximumLength ()
  {
    QSignalSpy start {interface_.get (), &MMTTYIF::app_start_tx};
    QSignalSpy logs {interface_.get (), &MMTTYIF::log_message};

    QVERIFY (writeToInterface ("<TXTEXT:2147483647><XMIT:2>ON"));

    QTRY_COMPARE_WITH_TIMEOUT (start.count (), 1, 1000);
    QTRY_VERIFY_WITH_TIMEOUT (hasLogContaining (logs, "payload length exceeds"), 1000);
  }

  void rejectsOversizedUndeclaredPayload ()
  {
    QSignalSpy start {interface_.get (), &MMTTYIF::app_start_tx};
    QSignalSpy abort {interface_.get (), &MMTTYIF::app_abort_tx};
    QSignalSpy logs {interface_.get (), &MMTTYIF::log_message};
    QSignalSpy received {interface_.get (), &MMTTYIF::message_received};

    QByteArray firstFragment {"<ABORT>"};
    firstFragment += QByteArray (maximumPayloadBytes / 2, 'A');
    QVERIFY (writeToInterface (firstFragment));
    QTRY_VERIFY_WITH_TIMEOUT (received.count () >= 1, 1000);
    QCOMPARE (abort.count (), 0);

    QByteArray secondFragment (maximumPayloadBytes / 2 + 1, 'A');
    secondFragment += "<XMIT:2>ON";
    QVERIFY (writeToInterface (secondFragment));

    QTRY_COMPARE_WITH_TIMEOUT (start.count (), 1, 2000);
    QCOMPARE (abort.count (), 0);
    QTRY_VERIFY_WITH_TIMEOUT (hasLogContaining (logs, "payload exceeds"), 1000);
  }

  void rejectsOverlongIncompleteHeader ()
  {
    QSignalSpy start {interface_.get (), &MMTTYIF::app_start_tx};
    QSignalSpy logs {interface_.get (), &MMTTYIF::log_message};

    QByteArray header {"<"};
    header += QByteArray (maximumHeaderBytes, 'A');
    QVERIFY (writeToInterface (header));
    QTRY_VERIFY_WITH_TIMEOUT (hasLogContaining (logs, "header exceeds"), 1000);

    QVERIFY (writeToInterface ("<XMIT:2>ON"));
    QTRY_COMPARE_WITH_TIMEOUT (start.count (), 1, 1000);
  }

  void rejectsOverlongTerminatedHeader ()
  {
    QSignalSpy start {interface_.get (), &MMTTYIF::app_start_tx};
    QSignalSpy logs {interface_.get (), &MMTTYIF::log_message};

    QByteArray header {"<"};
    header += QByteArray (maximumHeaderBytes - 1, 'A');
    header += '>';
    QVERIFY (writeToInterface (header));
    QTRY_VERIFY_WITH_TIMEOUT (hasLogContaining (logs, "header exceeds"), 1000);

    QVERIFY (writeToInterface ("<XMIT:2>ON"));
    QTRY_COMPARE_WITH_TIMEOUT (start.count (), 1, 1000);
  }

private:
  static bool hasLogContaining (QSignalSpy const& logs, QString const& text)
  {
    for (auto const& arguments : logs) {
      if (arguments.at (0).toString ().contains (text)) return true;
    }
    return false;
  }

  bool writeToInterface (QByteArray const& data)
  {
    if (!peer_ || peer_->write (data) != data.size ()) return false;
    return !peer_->bytesToWrite () || peer_->waitForBytesWritten (1000);
  }

  std::unique_ptr<QTcpServer> server_;
  std::unique_ptr<MMTTYIF> interface_;
  QTcpSocket * peer_ {nullptr};
};

QTEST_GUILESS_MAIN (TestMMTTYIF)

#include "test_mmttyif.moc"