Skip to content

Commit e68ae6d

Browse files
committed
Fix MSFTS test fixtures on Windows
1 parent a8300a1 commit e68ae6d

3 files changed

Lines changed: 73 additions & 38 deletions

File tree

examples/msfts-publisher/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ if(OPENMOQ_BUILD_TESTS)
1313
)
1414
target_link_libraries(openmoq-publisher-msfts-tests PRIVATE openmoq_publisher_lib)
1515
add_test(NAME openmoq-publisher-msfts-tests COMMAND openmoq-publisher-msfts-tests)
16+
set_tests_properties(openmoq-publisher-msfts-tests PROPERTIES TIMEOUT 60)
1617
endif()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include <atomic>
4+
#include <chrono>
5+
#include <cstdint>
6+
#include <filesystem>
7+
#include <fstream>
8+
#include <stdexcept>
9+
#include <string>
10+
#include <vector>
11+
12+
class FixtureFile {
13+
public:
14+
FixtureFile(const std::vector<std::uint8_t>& bytes,
15+
const std::string& suffix) {
16+
static std::atomic<std::uint64_t> next_id = 0;
17+
const auto timestamp =
18+
std::chrono::steady_clock::now().time_since_epoch().count();
19+
path_ = std::filesystem::temp_directory_path() /
20+
("openmoq-msfts-" + suffix + "-" +
21+
std::to_string(timestamp) + "-" +
22+
std::to_string(next_id.fetch_add(1)) + ".bin");
23+
24+
std::ofstream output(path_, std::ios::binary | std::ios::trunc);
25+
output.write(reinterpret_cast<const char*>(bytes.data()),
26+
static_cast<std::streamsize>(bytes.size()));
27+
if (!output) {
28+
throw std::runtime_error("failed to write MSFTS test fixture");
29+
}
30+
}
31+
32+
~FixtureFile() {
33+
std::error_code error;
34+
std::filesystem::remove(path_, error);
35+
}
36+
37+
FixtureFile(const FixtureFile&) = delete;
38+
FixtureFile& operator=(const FixtureFile&) = delete;
39+
40+
const std::filesystem::path& path() const noexcept {
41+
return path_;
42+
}
43+
44+
private:
45+
std::filesystem::path path_;
46+
};

examples/msfts-publisher/tests/msfts_source_test.cpp

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "../msfts_source.h"
22
#include "../msfts_options.h"
3+
#include "fixture_file.h"
34

45
#include <cstdint>
5-
#include <filesystem>
6-
#include <fstream>
76
#include <iostream>
87
#include <string>
98
#include <vector>
@@ -82,17 +81,6 @@ void append_packet(std::vector<std::uint8_t>& bytes,
8281
bytes.insert(bytes.end(), packet.begin(), packet.end());
8382
}
8483

85-
std::filesystem::path write_fixture(const std::vector<std::uint8_t>& bytes,
86-
const std::string& suffix) {
87-
const auto path = std::filesystem::temp_directory_path() /
88-
("openmoq-msfts-" + suffix + ".bin");
89-
std::ofstream output(path, std::ios::binary | std::ios::trunc);
90-
output.write(reinterpret_cast<const char*>(bytes.data()),
91-
static_cast<std::streamsize>(bytes.size()));
92-
output.close();
93-
return path;
94-
}
95-
9684
std::uint16_t packet_pid(const std::uint8_t* packet, std::size_t packet_size) {
9785
const std::uint8_t* ts = packet + (packet_size == 192 ? 4 : 0);
9886
return static_cast<std::uint16_t>(((ts[1] & 0x1f) << 8) | ts[2]);
@@ -106,17 +94,15 @@ void test_source_catalog_and_filtering(std::size_t packet_size) {
10694
append_packet(fixture, make_packet(0x101, 0x11), packet_size);
10795
append_packet(fixture, make_packet(0x111, 0x22), packet_size);
10896
append_packet(fixture, make_packet(0x101, 0x33), packet_size);
109-
const auto path = write_fixture(fixture, std::to_string(packet_size));
97+
const FixtureFile fixture_file(fixture, std::to_string(packet_size));
11098

11199
std::string error;
112100
auto source = MsftsSource::open(MsftsSourceConfig{
113-
.input_path = path,
101+
.input_path = fixture_file.path(),
114102
.track_name = "transport",
115103
.requested_program = 1,
116104
.packets_per_object = 2,
117105
}, error);
118-
std::filesystem::remove(path);
119-
120106
expect(source != nullptr, "expected valid " + std::to_string(packet_size) + "-byte source");
121107
if (source == nullptr) {
122108
std::cerr << error << '\n';
@@ -186,11 +172,11 @@ void test_ambiguous_m2ts_prefix_is_detected_as_192_bytes() {
186172
append_packet(fixture, pat, 192);
187173
append_packet(fixture, make_pmt(0x100, 1, 0x101), 192);
188174
fixture[0] = 0x47;
189-
const auto path = write_fixture(fixture, "ambiguous-192");
175+
const FixtureFile fixture_file(fixture, "ambiguous-192");
190176

191177
std::string error;
192-
auto source = MsftsSource::open(MsftsSourceConfig{.input_path = path}, error);
193-
std::filesystem::remove(path);
178+
auto source = MsftsSource::open(
179+
MsftsSourceConfig{.input_path = fixture_file.path()}, error);
194180
expect(source != nullptr && source->info().packet_size == 192,
195181
"expected file-size and sync alignment to disambiguate M2TS");
196182
}
@@ -200,11 +186,11 @@ void test_partial_packet_is_rejected() {
200186
append_packet(fixture, make_pat(), 188);
201187
append_packet(fixture, make_pmt(0x100, 1, 0x101), 188);
202188
fixture.push_back(0x47);
203-
const auto path = write_fixture(fixture, "partial");
189+
const FixtureFile fixture_file(fixture, "partial");
204190

205191
std::string error;
206-
auto source = MsftsSource::open(MsftsSourceConfig{.input_path = path}, error);
207-
std::filesystem::remove(path);
192+
auto source = MsftsSource::open(
193+
MsftsSourceConfig{.input_path = fixture_file.path()}, error);
208194

209195
expect(source == nullptr, "expected partial source packet to be rejected");
210196
expect(error.find("partial") != std::string::npos,
@@ -218,14 +204,13 @@ void test_corrupt_packet_does_not_publish_partial_object() {
218204
auto corrupt = make_packet(0x101);
219205
corrupt[0] = 0;
220206
append_packet(fixture, corrupt, 188);
221-
const auto path = write_fixture(fixture, "corrupt");
207+
const FixtureFile fixture_file(fixture, "corrupt");
222208

223209
std::string error;
224210
auto source = MsftsSource::open(MsftsSourceConfig{
225-
.input_path = path,
211+
.input_path = fixture_file.path(),
226212
.packets_per_object = 4,
227213
}, error);
228-
std::filesystem::remove(path);
229214
expect(source != nullptr, "expected discovery before the corrupt packet");
230215
if (source == nullptr) {
231216
return;
@@ -244,10 +229,10 @@ void test_program_discovery_errors() {
244229
std::vector<std::uint8_t> fixture;
245230
append_packet(fixture, make_packet(0x101), 188);
246231
append_packet(fixture, make_packet(0x101), 188);
247-
const auto path = write_fixture(fixture, "missing-pat");
232+
const FixtureFile fixture_file(fixture, "missing-pat");
248233
std::string error;
249-
auto source = MsftsSource::open(MsftsSourceConfig{.input_path = path}, error);
250-
std::filesystem::remove(path);
234+
auto source = MsftsSource::open(
235+
MsftsSourceConfig{.input_path = fixture_file.path()}, error);
251236
expect(source == nullptr && error.find("PAT") != std::string::npos,
252237
"expected a missing PAT to be rejected");
253238
}
@@ -256,12 +241,14 @@ void test_program_discovery_errors() {
256241
std::vector<std::uint8_t> fixture;
257242
append_packet(fixture, make_pat(), 188);
258243
append_packet(fixture, make_pmt(0x100, 1, 0x101), 188);
259-
const auto path = write_fixture(fixture, "missing-program");
244+
const FixtureFile fixture_file(fixture, "missing-program");
260245
std::string error;
261246
auto source = MsftsSource::open(
262-
MsftsSourceConfig{.input_path = path, .requested_program = 3},
247+
MsftsSourceConfig{
248+
.input_path = fixture_file.path(),
249+
.requested_program = 3,
250+
},
263251
error);
264-
std::filesystem::remove(path);
265252
expect(source == nullptr && error.find("requested program 3") != std::string::npos,
266253
"expected an absent requested program to be rejected");
267254
}
@@ -270,12 +257,14 @@ void test_program_discovery_errors() {
270257
std::vector<std::uint8_t> fixture;
271258
append_packet(fixture, make_pat(), 188);
272259
append_packet(fixture, make_pmt(0x100, 1, 0x101), 188);
273-
const auto path = write_fixture(fixture, "missing-pmt");
260+
const FixtureFile fixture_file(fixture, "missing-pmt");
274261
std::string error;
275262
auto source = MsftsSource::open(
276-
MsftsSourceConfig{.input_path = path, .requested_program = 2},
263+
MsftsSourceConfig{
264+
.input_path = fixture_file.path(),
265+
.requested_program = 2,
266+
},
277267
error);
278-
std::filesystem::remove(path);
279268
expect(source == nullptr && error.find("PMT") != std::string::npos,
280269
"expected a missing selected-program PMT to be rejected");
281270
}
@@ -290,14 +279,13 @@ void test_declared_psi_interval_is_honored() {
290279
make_packet(0x101, static_cast<std::uint8_t>(index)),
291280
188);
292281
}
293-
const auto path = write_fixture(fixture, "psi-interval");
282+
const FixtureFile fixture_file(fixture, "psi-interval");
294283

295284
std::string error;
296285
auto source = MsftsSource::open(MsftsSourceConfig{
297-
.input_path = path,
286+
.input_path = fixture_file.path(),
298287
.packets_per_object = 2,
299288
}, error);
300-
std::filesystem::remove(path);
301289
expect(source != nullptr, "expected PSI interval fixture to open");
302290
if (source == nullptr) {
303291
return;

0 commit comments

Comments
 (0)