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
|
#include <QtTest>
#include <algorithm>
#include <array>
#include <cstring>
#include <memory>
#include <new>
#include <vector>
#include "DecoderIpc.hpp"
#include "lib/decoder_ipc_layout.h"
class TestDecoderIpcLayout final : public QObject
{
Q_OBJECT
private Q_SLOTS:
void compiledLayoutsAgree ();
void everyDescriptorWordIsRequired ();
void equalSizeParameterSwapIsRejected ();
void malformedStorageIsRejected ();
void truncatedStorageIsRejectedBeforePayloadAccess ();
void allocationPaddingIsAccepted ();
void publicationAndClaimRejectIncompatibleLayout ();
};
void TestDecoderIpcLayout::compiledLayoutsAgree ()
{
decoder_ipc_layout_t cpp {};
decoder_ipc_layout_t fortran {};
decoder_ipc_expected_layout (&cpp);
decoder_ipc_fortran_layout (&fortran);
QCOMPARE (std::memcmp (&cpp, &fortran, sizeof cpp), 0);
QCOMPARE (cpp.field_count, DECODER_IPC_LAYOUT_FIELDS);
QCOMPARE (cpp.payload_offset, static_cast<int> (offsetof (shared_dec_data_t, payload)));
QCOMPARE (cpp.payload_bytes, static_cast<int> (sizeof (dec_data_t)));
QCOMPARE (cpp.logical_bytes, static_cast<int> (sizeof (shared_dec_data_t)));
std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
DecoderIpc::initialize (*shared);
QCOMPARE (decoder_ipc_validate_layout (shared.get (), sizeof *shared, &fortran),
int {DECODER_IPC_LAYOUT_OK});
QCOMPARE (decoder_ipc_fortran_validate (shared.get (), sizeof *shared),
int {DECODER_IPC_LAYOUT_OK});
}
void TestDecoderIpcLayout::everyDescriptorWordIsRequired ()
{
std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
DecoderIpc::initialize (*shared);
auto const valid = shared->layout;
std::array<int, sizeof (decoder_ipc_layout_t) / sizeof (int)> words;
std::memcpy (words.data (), &valid, sizeof valid);
for (std::size_t index = 0; index < words.size (); ++index)
{
words[index] ^= 1;
std::memcpy (&shared->layout, words.data (), sizeof valid);
auto const before = shared->layout;
auto const message = QByteArray::number (static_cast<qulonglong> (index));
QVERIFY2 (decoder_ipc_validate_layout (shared.get (), sizeof *shared, nullptr)
!= DECODER_IPC_LAYOUT_OK, message.constData ());
QVERIFY2 (decoder_ipc_fortran_validate (shared.get (), sizeof *shared)
!= DECODER_IPC_LAYOUT_OK, message.constData ());
QCOMPARE (std::memcmp (&before, &shared->layout, sizeof before), 0);
shared->layout = valid;
QCOMPARE (decoder_ipc_validate_layout (shared.get (), sizeof *shared, &before),
int {DECODER_IPC_LAYOUT_MISMATCH});
words[index] ^= 1;
}
}
void TestDecoderIpcLayout::equalSizeParameterSwapIsRejected ()
{
std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
DecoderIpc::initialize (*shared);
auto& first = shared->layout.fields[9]; // params.nutc
auto& second = shared->layout.fields[11]; // params.ntrperiod
QCOMPARE (first.bytes, second.bytes);
std::swap (first.offset, second.offset);
QCOMPARE (decoder_ipc_fortran_validate (shared.get (), sizeof *shared),
int {DECODER_IPC_LAYOUT_MISMATCH});
}
void TestDecoderIpcLayout::malformedStorageIsRejected ()
{
QCOMPARE (decoder_ipc_validate_layout (nullptr, sizeof (shared_dec_data_t), nullptr),
int {DECODER_IPC_LAYOUT_NULL});
QCOMPARE (decoder_ipc_fortran_validate (nullptr, sizeof (shared_dec_data_t)),
int {DECODER_IPC_LAYOUT_NULL});
std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
DecoderIpc::initialize (*shared);
auto const* unaligned = reinterpret_cast<unsigned char const *> (shared.get ()) + 1;
QCOMPARE (decoder_ipc_validate_layout (unaligned, sizeof *shared - 1, nullptr),
int {DECODER_IPC_LAYOUT_ALIGNMENT});
QCOMPARE (decoder_ipc_fortran_validate (unaligned, sizeof *shared - 1),
int {DECODER_IPC_LAYOUT_ALIGNMENT});
for (auto const version : {0, 1, 2, 3, DECODER_IPC_VERSION + 1})
{
shared->control.version = version;
QCOMPARE (decoder_ipc_fortran_validate (shared.get (), sizeof *shared),
int {DECODER_IPC_LAYOUT_VERSION});
QCOMPARE (shared->control.version, version);
}
shared->control.version = DECODER_IPC_VERSION;
shared->control.state = -1;
QCOMPARE (decoder_ipc_fortran_validate (shared.get (), sizeof *shared),
int {DECODER_IPC_LAYOUT_STATE});
QCOMPARE (shared->control.state, -1);
shared->control.state = DECODER_IPC_IDLE;
for (auto const capabilities : {0, DECODER_IPC_CAPABILITIES & ~DECODER_IPC_CAP_HANDSHAKE,
DECODER_IPC_CAPABILITIES | 8})
{
shared->layout.capabilities = capabilities;
QCOMPARE (decoder_ipc_fortran_validate (shared.get (), sizeof *shared),
int {DECODER_IPC_LAYOUT_CAPABILITIES});
QCOMPARE (shared->layout.capabilities, capabilities);
}
}
void TestDecoderIpcLayout::truncatedStorageIsRejectedBeforePayloadAccess ()
{
alignas(shared_dec_data_t) std::array<unsigned char,
offsetof (shared_dec_data_t, payload)> header {};
decoder_ipc_control_t const control {0, DECODER_IPC_IDLE, DECODER_IPC_VERSION, 0};
decoder_ipc_layout_t layout;
decoder_ipc_expected_layout (&layout);
std::memcpy (header.data (), &control, sizeof control);
std::memcpy (header.data () + offsetof (shared_dec_data_t, layout), &layout, sizeof layout);
auto const before = header;
for (auto const size : {std::size_t {0}, sizeof control - 1,
sizeof control, header.size () - 1, header.size ()})
{
QCOMPARE (decoder_ipc_validate_layout (header.data (), size, nullptr),
int {DECODER_IPC_LAYOUT_TRUNCATED});
QCOMPARE (decoder_ipc_fortran_validate (header.data (), size),
int {DECODER_IPC_LAYOUT_TRUNCATED});
QVERIFY (header == before);
}
layout.header_bytes = -1;
std::memcpy (header.data () + offsetof (shared_dec_data_t, layout), &layout, sizeof layout);
QCOMPARE (decoder_ipc_fortran_validate (header.data (), header.size ()),
int {DECODER_IPC_LAYOUT_HEADER_SIZE});
decoder_ipc_expected_layout (&layout);
layout.payload_bytes = -1;
std::memcpy (header.data () + offsetof (shared_dec_data_t, layout), &layout, sizeof layout);
QCOMPARE (decoder_ipc_fortran_validate (header.data (), header.size ()),
int {DECODER_IPC_LAYOUT_PAYLOAD_SIZE});
}
void TestDecoderIpcLayout::allocationPaddingIsAccepted ()
{
std::vector<int> storage ((sizeof (shared_dec_data_t) + 4096) / sizeof (int));
auto* shared = new (storage.data ()) shared_dec_data_t;
DecoderIpc::initialize (*shared);
QCOMPARE (decoder_ipc_fortran_validate (shared, storage.size () * sizeof (int)),
int {DECODER_IPC_LAYOUT_OK});
shared->layout.logical_bytes += 4;
QCOMPARE (decoder_ipc_fortran_validate (shared, storage.size () * sizeof (int)),
int {DECODER_IPC_LAYOUT_LOGICAL_SIZE});
shared->~shared_dec_data_t ();
}
void TestDecoderIpcLayout::publicationAndClaimRejectIncompatibleLayout ()
{
std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
std::unique_ptr<dec_data_t> payload {new dec_data_t {}};
DecoderIpc::initialize (*shared);
shared->payload.d2[0] = 42;
payload->d2[0] = 17;
payload->params.nmode = 8;
shared->layout.payload_bytes -= 4;
QByteArray before (reinterpret_cast<char const *> (shared.get ()), sizeof *shared);
QVERIFY (!DecoderIpc::publish (*shared, *payload, true, 1));
QCOMPARE (std::memcmp (shared.get (), before.constData (), sizeof *shared), 0);
shared->control.state = DECODER_IPC_READY;
shared->control.generation = 1;
before = QByteArray (reinterpret_cast<char const *> (shared.get ()), sizeof *shared);
qint32 generation {99};
QVERIFY (!DecoderIpc::claim (*shared, generation));
QCOMPARE (generation, qint32 {99});
QCOMPARE (std::memcmp (shared.get (), before.constData (), sizeof *shared), 0);
}
QTEST_GUILESS_MAIN (TestDecoderIpcLayout)
#include "test_decoder_ipc_layout.moc"
|