summaryrefslogtreecommitdiff
path: root/tests/unit/decoder_ipc/test_decoder_ipc_handshake.cpp
blob: d1ab0dbdca439ff371ffed11bad664ba89021cf6 (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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include <QtTest>
#include <QProcess>
#include <QFile>
#include <QScopeGuard>
#include <QTemporaryDir>
#include <QUuid>

#include <cerrno>
#include <cstddef>
#include <cstring>
#include <memory>
#include <cstdio>

#include "DecoderIpc.hpp"
#include "lib/NativeSharedMemory.hpp"
#include "lib/DecoderWorkerLock.hpp"

#ifndef Q_OS_WIN
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
#ifdef Q_OS_LINUX
#include <sys/statvfs.h>
#endif

namespace
{
  QString lockPath (QString const& directory)
  {
    return directory + "/worker.lock";
  }

  class DecoderProcess : public QProcess
  {
  public:
    ~DecoderProcess () override
    {
      if (state () != NotRunning)
        {
          kill ();
          waitForFinished (5000);
        }
    }
    void launch (QString const& key, QString const& directory)
    {
      start (QStringLiteral (DECODER_IPC_JT9),
             {"--shmem", key, "--ipc-lock", lockPath (directory), "-w", "1", "-m", "1", "-e", directory,
              "-a", directory, "-t", directory, "-r", QStringLiteral (DECODER_IPC_SOURCE)});
    }
    bool waitForOutput (QByteArray const& marker, QByteArray& output)
    {
      auto completeLine = [&] {
        auto const position = output.indexOf (marker);
        return position >= 0 && output.indexOf ('\n', position) >= 0;
      };
      QElapsedTimer timer;
      timer.start ();
      while (timer.elapsed () < 5000)
        {
          output += readAllStandardOutput ();
          if (completeLine ()) return true;
          if (state () == NotRunning) break;
          waitForReadyRead (100);
        }
      output += readAllStandardOutput ();
      return completeLine ();
    }
  };

  QString uniqueKey ()
  {
    return DecoderIpc::memoryName (QUuid::createUuid ().toString ());
  }
}

class TestDecoderIpcHandshake final : public QObject
{
  Q_OBJECT
private Q_SLOTS:
  void readyBeforePublication ();
  void rejectsSegment_data ();
  void rejectsSegment ();
  void staleNameRecovery_data ();
  void staleNameRecovery ();
  void mappingOwnership ();
  void missingMappingFails ();
  void replacementWorkerCompletesRequest ();
  void simultaneousSessionsStayIsolated ();
  void orphanLockLifetime_data ();
  void orphanLockLifetime ();
  void blockedWorkerPreservesFiles ();
  void insufficientLinuxSharedMemory ();
};

void TestDecoderIpcHandshake::readyBeforePublication ()
{
  using namespace DecoderIpc;
  auto const key = uniqueKey ();
  QTemporaryDir directory;
  QVERIFY (directory.isValid ());
  Session session;
  QCOMPARE (session.open (key, lockPath (directory.path ())), Status::Ok);
  auto source = std::make_unique<dec_data_t> ();
  QCOMPARE (session.submit (Request::snapshot (*source)).status, Status::Unavailable);
  DecoderProcess child;
  child.launch (key, directory.path ());
  QVERIFY (child.waitForStarted (5000));
  QByteArray output;
  QElapsedTimer timer;
  timer.start ();
  while (!output.contains ('\n') && timer.elapsed () < 5000)
    {
      child.waitForReadyRead (100);
      output += child.readAllStandardOutput ();
      if (child.state () == QProcess::NotRunning) break;
    }
  auto const ready = QByteArray {"<DecoderReady> version="} + QByteArray::number (DECODER_IPC_VERSION);
  QVERIFY2 (output.startsWith (ready + "\r\n")
            || output.startsWith (ready + "\n"), output.constData ());
  QVERIFY (!output.contains ("<DecodeStarted>"));
  QCOMPARE (session.acceptReady (DECODER_IPC_VERSION), Status::Ok);
  QVERIFY (session.ready ());
  session.shutdown ();
  QVERIFY (child.waitForFinished (5000));
  output += child.readAllStandardOutput ();
  QCOMPARE (child.exitStatus (), QProcess::NormalExit);
  QCOMPARE (child.exitCode (), 0);
  QVERIFY2 (!output.contains ("<DecoderError>"), output.constData ());
  QFile wisdom {directory.filePath ("jt9_wisdom.dat")};
  QVERIFY (wisdom.open (QIODevice::ReadOnly));
  auto const savedWisdom = wisdom.readAll ();
  QVERIFY (savedWisdom.contains ("fftwf_wisdom"));
  wisdom.close ();
  QVERIFY (QFile::exists (directory.filePath ("timer.out")));

  QVERIFY (session.reset ());
  child.launch (key, directory.path ());
  QVERIFY (child.waitForStarted (5000));
  output.clear ();
  QVERIFY2 (child.waitForOutput ("<DecoderReady>", output), output.constData ());
  session.shutdown ();
  QVERIFY (child.waitForFinished (5000));
  QCOMPARE (child.exitStatus (), QProcess::NormalExit);
  QCOMPARE (child.exitCode (), 0);
  QVERIFY (wisdom.open (QIODevice::ReadOnly));
  QCOMPARE (wisdom.readAll (), savedWisdom);
}

void TestDecoderIpcHandshake::rejectsSegment_data ()
{
  QTest::addColumn<QString> ("mutation");
  QTest::addColumn<QByteArray> ("status");
  QTest::newRow ("v1") << QString {"v1"} << QByteArray {"unsupported-version"};
  QTest::newRow ("v2") << QString {"v2"} << QByteArray {"unsupported-version"};
  QTest::newRow ("v3") << QString {"v3"} << QByteArray {"unsupported-version"};
  QTest::newRow ("future-version") << QString {"v%1"}.arg (DECODER_IPC_VERSION + 1)
                                  << QByteArray {"unsupported-version"};
  QTest::newRow ("header-size") << QString {"header"} << QByteArray {"wrong-header-size"};
  QTest::newRow ("payload-size") << QString {"payload"} << QByteArray {"wrong-payload-size"};
  QTest::newRow ("field-offset") << QString {"offset"} << QByteArray {"incompatible-layout"};
  QTest::newRow ("capabilities") << QString {"caps"} << QByteArray {"unsupported-capabilities"};
  QTest::newRow ("truncated") << QString {"truncated"} << QByteArray {"truncated-segment"};
}

void TestDecoderIpcHandshake::rejectsSegment ()
{
  QFETCH (QString, mutation);
  QFETCH (QByteArray, status);
  auto const key = uniqueKey ();
  QTemporaryDir directory;
  QVERIFY (directory.isValid ());
  QByteArray const sentinel {"existing decoder output\n"};
  for (auto const& name : {"timer.out", "jt9_wisdom.dat"})
    {
      QFile file {directory.filePath (name)};
      QVERIFY (file.open (QIODevice::WriteOnly));
      QCOMPARE (file.write (sentinel), qint64 {sentinel.size ()});
    }
  NativeSharedMemory memory;
  bool const truncated = mutation == "truncated";
  QVERIFY2 (memory.create (key.toStdString (), truncated ? offsetof (shared_dec_data_t, payload)
                                    : sizeof (shared_dec_data_t)),
            memory.errorString ().c_str ());
  if (truncated)
    {
      auto * control = static_cast<decoder_ipc_control_t *> (memory.data ());
      *control = {1, DECODER_IPC_READY, DECODER_IPC_VERSION, 0};
      decoder_ipc_layout_t layout;
      decoder_ipc_expected_layout (&layout);
      std::memcpy (static_cast<unsigned char *> (memory.data ())
                   + offsetof (shared_dec_data_t, layout),
                   &layout, sizeof layout);
    }
  else
    {
      auto& shared = *static_cast<shared_dec_data_t *> (memory.data ());
      DecoderIpc::initialize (shared);
      shared.control.state = DECODER_IPC_READY;
      shared.control.generation = 1;
      if (mutation.startsWith ('v')) shared.control.version = mutation.mid (1).toInt ();
      if (mutation == "header") ++shared.layout.header_bytes;
      if (mutation == "payload") --shared.layout.payload_bytes;
      if (mutation == "offset") ++shared.layout.fields[7].offset;
      if (mutation == "caps") shared.layout.capabilities = 0;
    }
  QByteArray const before {static_cast<char const *> (memory.data ()), static_cast<int> (memory.size ())};
  DecoderProcess child;
  child.launch (key, directory.path ());
  QVERIFY (child.waitForStarted (5000));
  QVERIFY (child.waitForFinished (5000));
  auto const output = child.readAllStandardOutput ();
  QCOMPARE (child.exitStatus (), QProcess::NormalExit);
  QCOMPARE (child.exitCode (), 1);
  QVERIFY2 (output.contains ("<DecoderError> status=" + status), output.constData ());
  QVERIFY (!output.contains ("<DecoderReady>"));
  QVERIFY (!output.contains ("<DecodeStarted>"));
  QCOMPARE (std::memcmp (before.constData (), memory.data (), before.size ()), 0);
  for (auto const& name : {"timer.out", "jt9_wisdom.dat"})
    {
      QFile file {directory.filePath (name)};
      QVERIFY (file.open (QIODevice::ReadOnly));
      QCOMPARE (file.readAll (), sentinel);
    }
}

void TestDecoderIpcHandshake::staleNameRecovery_data ()
{
  QTest::addColumn<bool> ("compatible");
  QTest::newRow ("compatible") << true;
  QTest::newRow ("incompatible") << false;
}

void TestDecoderIpcHandshake::staleNameRecovery ()
{
  QFETCH (bool, compatible);
  QTemporaryDir directory;
  QVERIFY (directory.isValid ());
  auto const key = uniqueKey ();
  NativeSharedMemory memory;
#ifdef Q_OS_WIN
  QVERIFY (memory.create (key.toStdString (), sizeof (shared_dec_data_t)));
#else
  // A crashed creator leaves its name behind while decoder attachments stay alive.
  auto const descriptor = shm_open (key.toStdString ().c_str (), O_CREAT | O_EXCL | O_RDWR, 0600);
  QVERIFY (descriptor >= 0);
  auto cleanup = qScopeGuard ([&] { NativeSharedMemory::remove (key.toStdString ()); });
  auto const resized = ftruncate (descriptor, sizeof (shared_dec_data_t));
  auto const closed = close (descriptor);
  QCOMPARE (resized, 0);
  QCOMPARE (closed, 0);
  auto const attached = memory.attach (key.toStdString ());
  QVERIFY2 (attached, memory.errorString ().c_str ());
#endif
  auto& shared = *static_cast<shared_dec_data_t *> (memory.data ());
  DecoderIpc::initialize (shared);
  shared.payload.d2[0] = 41;
  if (!compatible) shared.control.version = 2;
  QByteArray const before {static_cast<char const *> (memory.data ()), static_cast<int> (memory.size ())};
  DecoderIpc::Session session;
#ifdef Q_OS_WIN
  QCOMPARE (session.open (key, lockPath (directory.path ())), DecoderIpc::Status::Orphaned);
  QVERIFY (!session.errorString ().isEmpty ());
#else
  QCOMPARE (session.open (key, lockPath (directory.path ())), DecoderIpc::Status::Ok);
#endif
  if (compatible)
    {
      QCOMPARE (DecoderIpc::state (shared), int {DECODER_IPC_SHUTDOWN});
      QCOMPARE (shared.payload.d2[0], short {41});
    }
  else
    {
      QCOMPARE (std::memcmp (before.constData (), memory.data (), before.size ()), 0);
    }
  QVERIFY (memory.detach ());
#ifdef Q_OS_WIN
  QCOMPARE (session.open (key, lockPath (directory.path ())), DecoderIpc::Status::Ok);
#endif
  NativeSharedMemory replacement;
  QVERIFY2 (replacement.attach (key.toStdString ()), replacement.errorString ().c_str ());
  auto const& fresh = *static_cast<shared_dec_data_t const *> (replacement.data ());
  QCOMPARE (DecoderIpc::state (fresh), int {DECODER_IPC_IDLE});
  QCOMPARE (fresh.control.version, int {DECODER_IPC_VERSION});
  QCOMPARE (fresh.payload.d2[0], short {0});
}

void TestDecoderIpcHandshake::mappingOwnership ()
{
  auto const name = uniqueKey ().toStdString ();
  NativeSharedMemory owner;
  QVERIFY2 (owner.create (name, 4096), owner.errorString ().c_str ());
  std::memset (owner.data (), 0x5a, 4096);
  QByteArray const before {static_cast<char const *> (owner.data ()), 4096};
  {
    NativeSharedMemory duplicate;
    QVERIFY (!duplicate.create (name, 4096));
    QVERIFY (duplicate.alreadyExists ());
    QVERIFY (!duplicate.isAttached ());
    QVERIFY (!duplicate.errorString ().empty ());
  }
  QCOMPARE (std::memcmp (owner.data (), before.constData (), before.size ()), 0);
  NativeSharedMemory peer;
  QVERIFY2 (peer.attach (name), peer.errorString ().c_str ());
  QVERIFY (peer.size () >= 4096);
  QCOMPARE (std::memcmp (owner.data (), peer.data (), 4096), 0);
  QCOMPARE (static_cast<unsigned char *> (owner.data ())[0], static_cast<unsigned char> (0x5a));
  QVERIFY (peer.detach ());
  QVERIFY2 (peer.attach (name), peer.errorString ().c_str ());
  QVERIFY (owner.detach ());
  QCOMPARE (static_cast<unsigned char *> (peer.data ())[0], static_cast<unsigned char> (0x5a));
#ifndef Q_OS_WIN
  NativeSharedMemory retired;
  QVERIFY (!retired.attach (name));
#endif
  QVERIFY (peer.detach ());
  NativeSharedMemory retiredAfterLastPeer;
  QVERIFY (!retiredAfterLastPeer.attach (name));
}

void TestDecoderIpcHandshake::missingMappingFails ()
{
  auto const key = uniqueKey ();
  NativeSharedMemory memory;
  QVERIFY (!memory.attach (key.toStdString ()));
  QVERIFY (memory.notFound ());
  QVERIFY (!memory.isAttached ());
  QVERIFY (!memory.errorString ().empty ());
  QTemporaryDir directory;
  QVERIFY (directory.isValid ());
  DecoderProcess child;
  child.launch (key, directory.path ());
  QVERIFY (child.waitForStarted (5000));
  QVERIFY (child.waitForFinished (5000));
  auto const output = child.readAllStandardOutput ();
  QCOMPARE (child.exitStatus (), QProcess::NormalExit);
  QCOMPARE (child.exitCode (), 1);
  QVERIFY2 (output.contains ("<DecoderError>"), output.constData ());
  QVERIFY (!output.contains ("<DecoderReady>"));
}

void TestDecoderIpcHandshake::replacementWorkerCompletesRequest ()
{
  using namespace DecoderIpc;
  auto const key = uniqueKey ();
  QTemporaryDir directory;
  QVERIFY (directory.isValid ());
  Session session;
  QCOMPARE (session.open (key, lockPath (directory.path ())), Status::Ok);
  NativeSharedMemory peer;
  QVERIFY (peer.attach (key.toStdString ()));
  auto const* retained = peer.data ();
  auto payload = std::make_unique<dec_data_t> ();
  payload->params.nmode = 8;
  payload->params.ntrperiod = 15;
  payload->params.nzhsym = 50;
  payload->params.newdat = true;
  payload->params.nfa = 200;
  payload->params.nfb = 4000;
  qint32 previousGeneration {0};
  DecoderProcess child;
  for (int worker = 0; worker < 2; ++worker)
    {
      child.launch (key, directory.path ());
      QVERIFY (child.waitForStarted (5000));
      QByteArray output;
      QVERIFY2 (child.waitForOutput ("<DecoderReady>", output), output.constData ());
      QCOMPARE (session.acceptReady (DECODER_IPC_VERSION), Status::Ok);
      auto const submission = session.submit (Request::snapshot (*payload, {11, 12, worker + 1, 180000}));
      QVERIFY (submission);
      QVERIFY (submission.generation.value () > previousGeneration);
      previousGeneration = submission.generation.value ();
      QVERIFY2 (child.waitForOutput ("<DecodeFinished>", output), output.constData ());
      Completion completion {};
      bool completed {false};
      for (auto const& line : output.split ('\n'))
        if (parseCompletion (line, &completion)) completed = true;
      QVERIFY2 (completed, output.constData ());
      QCOMPARE (completion.generation, submission.generation.value ());
      QCOMPARE (session.complete (completion), Status::Ok);
      QVERIFY2 (!output.contains ("<DecodeRejected>"), output.constData ());
      QVERIFY2 (!output.contains ("<DecoderError>"), output.constData ());
      if (worker == 0)
        {
          child.kill ();
          QVERIFY (child.waitForFinished (5000));
          QCOMPARE (child.state (), QProcess::NotRunning);
          QVERIFY (session.reset ());
          QVERIFY (!session.ready ());
          QVERIFY (peer.data () == retained);
          QCOMPARE (state (*static_cast<shared_dec_data_t const *> (retained)), int {DECODER_IPC_IDLE});
        }
    }
  session.shutdown ();
  QVERIFY (child.waitForFinished (5000));
  QCOMPARE (child.exitStatus (), QProcess::NormalExit);
  QCOMPARE (child.exitCode (), 0);
}

void TestDecoderIpcHandshake::simultaneousSessionsStayIsolated ()
{
  using namespace DecoderIpc;
  auto const firstKey = uniqueKey ();
  auto const secondKey = uniqueKey ();
  QTemporaryDir firstDirectory;
  QTemporaryDir secondDirectory;
  QVERIFY (firstDirectory.isValid ());
  QVERIFY (secondDirectory.isValid ());
  Session first;
  Session second;
  QCOMPARE (first.open (firstKey, lockPath (firstDirectory.path ())), Status::Ok);
  QCOMPARE (second.open (secondKey, lockPath (secondDirectory.path ())), Status::Ok);
  DecoderProcess firstChild;
  DecoderProcess secondChild;
  firstChild.launch (firstKey, firstDirectory.path ());
  secondChild.launch (secondKey, secondDirectory.path ());
  QVERIFY (firstChild.waitForStarted (5000));
  QVERIFY (secondChild.waitForStarted (5000));
  QByteArray firstOutput;
  QByteArray secondOutput;
  QVERIFY2 (firstChild.waitForOutput ("<DecoderReady>", firstOutput), firstOutput.constData ());
  QVERIFY2 (secondChild.waitForOutput ("<DecoderReady>", secondOutput), secondOutput.constData ());
  QCOMPARE (first.acceptReady (DECODER_IPC_VERSION), Status::Ok);
  QCOMPARE (second.acceptReady (DECODER_IPC_VERSION), Status::Ok);
  first.shutdown ();
  QVERIFY (firstChild.waitForFinished (5000));
  QCOMPARE (firstChild.exitCode (), 0);
  first.detach ();
  QCOMPARE (secondChild.state (), QProcess::Running);
  auto payload = std::make_unique<dec_data_t> ();
  payload->params.nmode = 8;
  payload->params.ntrperiod = 15;
  payload->params.nzhsym = 50;
  payload->params.newdat = true;
  payload->params.nfa = 200;
  payload->params.nfb = 4000;
  auto const submission = second.submit (Request::snapshot (*payload, {21, 22, 1, 180000}));
  QVERIFY (submission);
  QVERIFY2 (secondChild.waitForOutput ("<DecodeFinished>", secondOutput), secondOutput.constData ());
  Completion completion {};
  bool completed {false};
  for (auto const& line : secondOutput.split ('\n'))
    if (parseCompletion (line, &completion)) completed = true;
  QVERIFY2 (completed, secondOutput.constData ());
  QCOMPARE (second.complete (completion), Status::Ok);
  QVERIFY2 (!secondOutput.contains ("<DecodeRejected>"), secondOutput.constData ());
  QVERIFY2 (!secondOutput.contains ("<DecoderError>"), secondOutput.constData ());
  second.shutdown ();
  QVERIFY (secondChild.waitForFinished (5000));
  QCOMPARE (secondChild.exitStatus (), QProcess::NormalExit);
  QCOMPARE (secondChild.exitCode (), 0);
}

void TestDecoderIpcHandshake::orphanLockLifetime_data ()
{
  QTest::addColumn<bool> ("killed");
  QTest::newRow ("normal-exit") << false;
  QTest::newRow ("process-death") << true;
}

void TestDecoderIpcHandshake::orphanLockLifetime ()
{
#ifdef Q_OS_WIN
  QSKIP ("POSIX worker locks protect cleanup after mapping detach");
#else
  QFETCH (bool, killed);
  QTemporaryDir directory;
  QVERIFY (directory.isValid ());
  auto const key = uniqueKey ();
  auto const nativeName = key.toStdString ();
  auto const descriptor = shm_open (nativeName.c_str (), O_CREAT | O_EXCL | O_RDWR, 0600);
  QVERIFY (descriptor >= 0);
  auto cleanup = qScopeGuard ([&] { NativeSharedMemory::remove (key.toStdString ()); });
  auto const resized = ftruncate (descriptor, sizeof (shared_dec_data_t));
  auto const closed = close (descriptor);
  QCOMPARE (resized, 0);
  QCOMPARE (closed, 0);
  NativeSharedMemory memory;
  QVERIFY (memory.attach (nativeName));
  auto& shared = *static_cast<shared_dec_data_t *> (memory.data ());
  DecoderIpc::initialize (shared);
  shared.payload.d2[0] = 41;

  DecoderProcess holder;
  holder.start (QCoreApplication::applicationFilePath (),
                {"--hold-worker-lock", lockPath (directory.path ()), key});
  QVERIFY (holder.waitForStarted (5000));
  QByteArray output;
  QVERIFY2 (holder.waitForOutput ("<WorkerLocked>", output), output.constData ());
  QCOMPARE (holder.write ("d", 1), qint64 {1});
  QVERIFY2 (holder.waitForOutput ("<MappingDetached>", output), output.constData ());

  DecoderIpc::Session session;
  QElapsedTimer timer;
  timer.start ();
  QCOMPARE (session.open (key, lockPath (directory.path ())), DecoderIpc::Status::Orphaned);
  QVERIFY (timer.elapsed () >= 2900);
  QVERIFY (!session.errorString ().isEmpty ());
  QCOMPARE (holder.state (), QProcess::Running);
  QCOMPARE (DecoderIpc::state (shared), int {DECODER_IPC_SHUTDOWN});
  NativeSharedMemory retained;
  QVERIFY (retained.attach (nativeName));
  auto const& stillOld = *static_cast<shared_dec_data_t const *> (retained.data ());
  QCOMPARE (stillOld.payload.d2[0], short {41});
  QCOMPARE (DecoderIpc::state (stillOld), int {DECODER_IPC_SHUTDOWN});
  QVERIFY (retained.detach ());

  if (killed) holder.kill ();
  else QCOMPARE (holder.write ("r", 1), qint64 {1});
  QVERIFY (holder.waitForFinished (5000));
  if (!killed) QCOMPARE (holder.exitCode (), 0);
  QCOMPARE (session.open (key, lockPath (directory.path ())), DecoderIpc::Status::Ok);
  NativeSharedMemory replacement;
  QVERIFY (replacement.attach (nativeName));
  auto const& fresh = *static_cast<shared_dec_data_t const *> (replacement.data ());
  QCOMPARE (fresh.payload.d2[0], short {0});
  QCOMPARE (DecoderIpc::state (fresh), int {DECODER_IPC_IDLE});
  QCOMPARE (shared.payload.d2[0], short {41});
#endif
}

void TestDecoderIpcHandshake::blockedWorkerPreservesFiles ()
{
#ifdef Q_OS_WIN
  QSKIP ("The worker lifetime lock is POSIX-only");
#else
  QTemporaryDir directory;
  QVERIFY (directory.isValid ());
  auto const key = uniqueKey ();
  DecoderIpc::Session session;
  QCOMPARE (session.open (key, lockPath (directory.path ())), DecoderIpc::Status::Ok);
  DecoderWorkerLock holder {QFile::encodeName (lockPath (directory.path ())).constData ()};
  QVERIFY2 (holder.tryLock (), holder.errorString ().c_str ());
  QByteArray const sentinel {"existing decoder output\n"};
  for (auto const& name : {"timer.out", "jt9_wisdom.dat"})
    {
      QFile file {directory.filePath (name)};
      QVERIFY (file.open (QIODevice::WriteOnly));
      QCOMPARE (file.write (sentinel), qint64 {sentinel.size ()});
    }
  DecoderProcess child;
  child.launch (key, directory.path ());
  QVERIFY (child.waitForStarted (5000));
  QVERIFY (child.waitForFinished (5000));
  QCOMPARE (child.exitStatus (), QProcess::NormalExit);
  QCOMPARE (child.exitCode (), 1);
  auto const output = child.readAllStandardOutput ();
  QVERIFY2 (output.contains ("<DecoderError>"), output.constData ());
  QVERIFY (!output.contains ("<DecoderReady>"));
  for (auto const& name : {"timer.out", "jt9_wisdom.dat"})
    {
      QFile file {directory.filePath (name)};
      QVERIFY (file.open (QIODevice::ReadOnly));
      QCOMPARE (file.readAll (), sentinel);
    }
#endif
}

void TestDecoderIpcHandshake::insufficientLinuxSharedMemory ()
{
#ifdef Q_OS_LINUX
  if (qgetenv ("WSJTX_TEST_SMALL_SHM") != "1")
    QSKIP ("Run alone in a container with --shm-size=16m and WSJTX_TEST_SMALL_SHM=1");
  struct statvfs capacity {};
  QCOMPARE (statvfs ("/dev/shm", &capacity), 0);
  QVERIFY2 (capacity.f_blocks * capacity.f_frsize <= 16 * 1024 * 1024,
            "This test requires an isolated /dev/shm mount of at most 16 MiB");
  auto const name = uniqueKey ().toStdString ();
  NativeSharedMemory memory;
  QVERIFY (!memory.create (name, sizeof (shared_dec_data_t)));
  QVERIFY (!memory.isAttached ());
  auto const error = QString::fromStdString (memory.errorString ());
  QVERIFY2 (error.startsWith ("posix_fallocate:"), qPrintable (error));
  QVERIFY2 (error.endsWith (QString {"(%1)"}.arg (ENOSPC)), qPrintable (error));
  NativeSharedMemory leftover;
  QVERIFY (!leftover.attach (name));
  QVERIFY (leftover.notFound ());
#else
  QSKIP ("Linux allocation reservation regression");
#endif
}

int main (int argc, char ** argv)
{
  QCoreApplication application {argc, argv};
  auto const arguments = application.arguments ();
  if (arguments.size () == 4 && arguments[1] == "--hold-worker-lock")
    {
      DecoderWorkerLock lock {QFile::encodeName (arguments[2]).constData ()};
      if (!lock.tryLock ()) return 1;
      NativeSharedMemory memory;
      if (!memory.attach (arguments[3].toStdString ())) return 2;
      std::puts ("<WorkerLocked>");
      std::fflush (stdout);
      if (std::getchar () != 'd' || !memory.detach ()) return 3;
      std::puts ("<MappingDetached>");
      std::fflush (stdout);
      return std::getchar () == 'r' ? 0 : 4;
    }
  TestDecoderIpcHandshake test;
  return QTest::qExec (&test, argc, argv);
}
#include "test_decoder_ipc_handshake.moc"