Skip to content

Commit e46e3c2

Browse files
author
Ondřej Majerech
committed
Unify TempYamlFile and TempCertDir in single helper class
1 parent d942ea1 commit e46e3c2

8 files changed

Lines changed: 119 additions & 115 deletions

File tree

test/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ target_link_libraries(moqx_config_test PRIVATE
4040
GTest::gtest_main
4141
GTest::gmock
4242
)
43+
target_include_directories(moqx_config_test PRIVATE
44+
${PROJECT_SOURCE_DIR}/test)
4345
target_compile_definitions(moqx_config_test PRIVATE
4446
CONFIG_EXAMPLE_PATH="${PROJECT_SOURCE_DIR}/config.example.yaml"
4547
)
@@ -131,11 +133,12 @@ target_link_libraries(moqx_tls_test PRIVATE
131133
moqx_core
132134
moqx_config
133135
moqx_config_loader
134-
Folly::folly_testing_test_util
135136
GTest::gtest_main
136137
GTest::gmock
137138
"$<LINK_LIBRARY:WHOLE_ARCHIVE,gflags_nothreads_static>"
138139
)
140+
target_include_directories(moqx_tls_test PRIVATE
141+
${PROJECT_SOURCE_DIR}/test)
139142
set_property(TARGET moqx_tls_test PROPERTY
140143
LINK_LIBRARY_OVERRIDE "WHOLE_ARCHIVE,gflags_nothreads_static"
141144
)

test/config/loader_test.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
#include <gtest/gtest.h>
1111
#include <rfl/json.hpp>
1212

13-
#include "test_utils.h"
13+
#include "util/TempDir.h"
1414

1515
namespace openmoq::moqx::config {
1616
namespace {
1717

18-
using test::TempYamlFile;
18+
using test::util::TempDir;
1919
using ::testing::HasSubstr;
2020

2121
// --- Parse tests ---
2222

2323
TEST(ConfigLoader, MinimalConfig) {
24-
TempYamlFile yaml(R"(
24+
TempDir dir;
25+
auto yaml = dir.writeYaml(R"(
2526
listeners:
2627
- name: main
2728
udp:
@@ -48,7 +49,7 @@ TEST(ConfigLoader, MinimalConfig) {
4849
port: 9669
4950
)");
5051

51-
auto cfg = loadConfig(yaml.path());
52+
auto cfg = loadConfig(yaml);
5253
EXPECT_EQ(cfg.listeners.value().size(), 1);
5354
EXPECT_EQ(cfg.listeners.value()[0].name.value(), "main");
5455

@@ -66,7 +67,8 @@ TEST(ConfigLoader, MinimalConfig) {
6667
}
6768

6869
TEST(ConfigLoader, TlsFileConfig) {
69-
TempYamlFile yaml(R"(
70+
TempDir dir;
71+
auto yaml = dir.writeYaml(R"(
7072
listeners:
7173
- name: production
7274
udp:
@@ -96,7 +98,7 @@ TEST(ConfigLoader, TlsFileConfig) {
9698
port: 9669
9799
)");
98100

99-
auto cfg = loadConfig(yaml.path());
101+
auto cfg = loadConfig(yaml);
100102
const auto& l = cfg.listeners.value()[0];
101103

102104
EXPECT_EQ(l.name.value(), "production");
@@ -118,7 +120,8 @@ TEST(ConfigLoader, TlsFileConfig) {
118120
}
119121

120122
TEST(ConfigLoader, ServicesWithAuthorityAndPath) {
121-
TempYamlFile yaml(R"(
123+
TempDir dir;
124+
auto yaml = dir.writeYaml(R"(
122125
listeners:
123126
- name: main
124127
udp:
@@ -149,7 +152,7 @@ TEST(ConfigLoader, ServicesWithAuthorityAndPath) {
149152
max_groups_per_track: 3
150153
)");
151154

152-
auto cfg = loadConfig(yaml.path());
155+
auto cfg = loadConfig(yaml);
153156
ASSERT_EQ(cfg.services.value().size(), 2);
154157

155158
const auto& svc0 = cfg.services.value().at("live");
@@ -208,7 +211,8 @@ TEST(ConfigLoader, ServicesWithAuthorityAndPath) {
208211
}
209212

210213
TEST(ConfigLoader, ServicesWithAnyAuthority) {
211-
TempYamlFile yaml(R"(
214+
TempDir dir;
215+
auto yaml = dir.writeYaml(R"(
212216
listeners:
213217
- name: main
214218
udp:
@@ -229,7 +233,7 @@ TEST(ConfigLoader, ServicesWithAnyAuthority) {
229233
max_groups_per_track: 3
230234
)");
231235

232-
auto cfg = loadConfig(yaml.path());
236+
auto cfg = loadConfig(yaml);
233237
ASSERT_EQ(cfg.services.value().size(), 1);
234238
const auto& svc = cfg.services.value().at("default");
235239
ASSERT_EQ(svc.match.value().size(), 1);
@@ -256,7 +260,8 @@ TEST(ConfigLoader, ServicesWithAnyAuthority) {
256260
}
257261

258262
TEST(ConfigLoader, ServiceDefaults) {
259-
TempYamlFile yaml(R"(
263+
TempDir dir;
264+
auto yaml = dir.writeYaml(R"(
260265
listeners:
261266
- name: main
262267
udp:
@@ -278,7 +283,7 @@ TEST(ConfigLoader, ServiceDefaults) {
278283
path: {prefix: "/"}
279284
)");
280285

281-
auto cfg = loadConfig(yaml.path());
286+
auto cfg = loadConfig(yaml);
282287
ASSERT_TRUE(cfg.service_defaults.value().has_value());
283288
ASSERT_TRUE(cfg.service_defaults.value()->cache.value().has_value());
284289
EXPECT_EQ(cfg.service_defaults.value()->cache.value()->max_tracks.value(), 50);
@@ -289,7 +294,8 @@ TEST(ConfigLoader, ServiceDefaults) {
289294
}
290295

291296
TEST(ConfigLoader, TlsDirectoryConfig) {
292-
TempYamlFile yaml(R"(
297+
TempDir dir;
298+
auto yaml = dir.writeYaml(R"(
293299
listeners:
294300
- name: multi
295301
udp:
@@ -312,12 +318,13 @@ TEST(ConfigLoader, TlsDirectoryConfig) {
312318
path: {prefix: "/"}
313319
)");
314320

315-
auto cfg = loadConfig(yaml.path());
321+
auto cfg = loadConfig(yaml);
316322
EXPECT_EQ(cfg.listeners.value().size(), 1);
317323
}
318324

319325
TEST(ConfigLoader, TlsDirectoryConfigNoDefault) {
320-
TempYamlFile yaml(R"(
326+
TempDir dir;
327+
auto yaml = dir.writeYaml(R"(
321328
listeners:
322329
- name: multi
323330
udp:
@@ -339,7 +346,7 @@ TEST(ConfigLoader, TlsDirectoryConfigNoDefault) {
339346
path: {prefix: "/"}
340347
)");
341348

342-
auto cfg = loadConfig(yaml.path());
349+
auto cfg = loadConfig(yaml);
343350
EXPECT_EQ(cfg.listeners.value().size(), 1);
344351
}
345352

@@ -369,7 +376,8 @@ TEST(ConfigSchema, GeneratesValidJson) {
369376
// --- Load from file test ---
370377

371378
TEST(ConfigLoader, LoadFromFile) {
372-
TempYamlFile yaml(R"(
379+
TempDir dir;
380+
auto yaml = dir.writeYaml(R"(
373381
listeners:
374382
- name: test
375383
udp:
@@ -396,7 +404,7 @@ TEST(ConfigLoader, LoadFromFile) {
396404
port: 9669
397405
)");
398406

399-
auto cfg = loadConfig(yaml.path());
407+
auto cfg = loadConfig(yaml);
400408
EXPECT_EQ(cfg.listeners.value()[0].name.value(), "test");
401409
ASSERT_TRUE(cfg.services.value().at("default").cache.value().has_value());
402410
EXPECT_EQ(cfg.services.value().at("default").cache.value()->enabled.value(), false);
@@ -407,14 +415,16 @@ TEST(ConfigLoader, LoadFromFileNotFound) {
407415
}
408416

409417
TEST(ConfigLoader, LoadFromFileInvalidYaml) {
410-
TempYamlFile yaml("not: [valid: yaml: config");
411-
EXPECT_THROW(loadConfig(yaml.path()), std::runtime_error);
418+
TempDir dir;
419+
auto yaml = dir.writeYaml("not: [valid: yaml: config");
420+
EXPECT_THROW(loadConfig(yaml), std::runtime_error);
412421
}
413422

414423
// --- Unknown field tests ---
415424

416425
TEST(ConfigLoader, UnknownFieldIgnoredNonStrict) {
417-
TempYamlFile yaml(R"(
426+
TempDir dir;
427+
auto yaml = dir.writeYaml(R"(
418428
listeners:
419429
- name: main
420430
udp:
@@ -442,11 +452,12 @@ TEST(ConfigLoader, UnknownFieldIgnoredNonStrict) {
442452
port: 9669
443453
)");
444454

445-
EXPECT_NO_THROW(loadConfig(yaml.path()));
455+
EXPECT_NO_THROW(loadConfig(yaml));
446456
}
447457

448458
TEST(ConfigLoader, UnknownFieldRejectedStrict) {
449-
TempYamlFile yaml(R"(
459+
TempDir dir;
460+
auto yaml = dir.writeYaml(R"(
450461
listeners:
451462
- name: main
452463
udp:
@@ -474,7 +485,7 @@ TEST(ConfigLoader, UnknownFieldRejectedStrict) {
474485
port: 9669
475486
)");
476487

477-
EXPECT_THROW(loadConfig(yaml.path(), /*strict=*/true), std::runtime_error);
488+
EXPECT_THROW(loadConfig(yaml, /*strict=*/true), std::runtime_error);
478489
}
479490

480491
#ifdef CONFIG_EXAMPLE_PATH

test/config/test_utils.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

test/tls/DirectoryCertLoaderTest.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010
#include <gtest/gtest.h>
1111

1212
#include "TestCertUtils.h"
13+
#include "util/TempDir.h"
1314

1415
namespace openmoq::moqx::tls {
1516
namespace {
1617

18+
using openmoq::moqx::test::util::TempDir;
1719
using test::kTestCert2Pem;
1820
using test::kTestCertPem;
1921
using test::kTestKey2Pem;
2022
using test::kTestKeyPem;
21-
using test::TempCertDir;
2223
using ::testing::HasSubstr;
2324

2425
TEST(DirectoryCertLoader, LoadsSingleCert) {
25-
TempCertDir dir;
26+
TempDir dir;
2627
dir.writeCert("test", kTestCertPem, kTestKeyPem);
2728

2829
DirectoryCertLoader loader(dir.path(), "");
@@ -34,7 +35,7 @@ TEST(DirectoryCertLoader, LoadsSingleCert) {
3435
}
3536

3637
TEST(DirectoryCertLoader, LoadsMultipleCerts) {
37-
TempCertDir dir;
38+
TempDir dir;
3839
dir.writeCert("alpha", kTestCertPem, kTestKeyPem);
3940
dir.writeCert("beta", kTestCert2Pem, kTestKey2Pem);
4041

@@ -48,7 +49,7 @@ TEST(DirectoryCertLoader, LoadsMultipleCerts) {
4849
}
4950

5051
TEST(DirectoryCertLoader, EmptyDirectory) {
51-
TempCertDir dir;
52+
TempDir dir;
5253

5354
DirectoryCertLoader loader(dir.path(), "");
5455
auto result = loader.load();
@@ -64,7 +65,7 @@ TEST(DirectoryCertLoader, NonexistentDirectory) {
6465
}
6566

6667
TEST(DirectoryCertLoader, CrtWithoutMatchingKey) {
67-
TempCertDir dir;
68+
TempDir dir;
6869
dir.writeCertOnly("orphan", kTestCertPem);
6970

7071
DirectoryCertLoader loader(dir.path(), "");
@@ -74,7 +75,7 @@ TEST(DirectoryCertLoader, CrtWithoutMatchingKey) {
7475
}
7576

7677
TEST(DirectoryCertLoader, InvalidDefaultCertIdentity) {
77-
TempCertDir dir;
78+
TempDir dir;
7879
dir.writeCert("test", kTestCertPem, kTestKeyPem);
7980

8081
DirectoryCertLoader loader(dir.path(), "nonexistent.example.com");
@@ -84,7 +85,7 @@ TEST(DirectoryCertLoader, InvalidDefaultCertIdentity) {
8485
}
8586

8687
TEST(DirectoryCertLoader, InvalidPem) {
87-
TempCertDir dir;
88+
TempDir dir;
8889
dir.writeCert("bad", "not a cert", "not a key");
8990

9091
DirectoryCertLoader loader(dir.path(), "");

test/tls/FileCertLoaderTest.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
#include <gtest/gtest.h>
1111

1212
#include "TestCertUtils.h"
13+
#include "util/TempDir.h"
1314

1415
namespace openmoq::moqx::tls {
1516
namespace {
1617

18+
using openmoq::moqx::test::util::TempDir;
1719
using test::kTestCertPem;
1820
using test::kTestKeyPem;
19-
using test::TempCertDir;
2021
using ::testing::HasSubstr;
2122

2223
TEST(FileCertLoader, LoadsValidCertKeyPair) {
23-
TempCertDir dir;
24+
TempDir dir;
2425
dir.writeCert("test", kTestCertPem, kTestKeyPem);
2526

2627
FileCertLoader loader(dir.filePath("test.crt"), dir.filePath("test.key"));
@@ -41,7 +42,7 @@ TEST(FileCertLoader, MissingCertFile) {
4142
}
4243

4344
TEST(FileCertLoader, MissingKeyFile) {
44-
TempCertDir dir;
45+
TempDir dir;
4546
dir.writeCertOnly("test", kTestCertPem);
4647

4748
FileCertLoader loader(dir.filePath("test.crt"), "/nonexistent/key.pem");
@@ -51,7 +52,7 @@ TEST(FileCertLoader, MissingKeyFile) {
5152
}
5253

5354
TEST(FileCertLoader, InvalidPem) {
54-
TempCertDir dir;
55+
TempDir dir;
5556
dir.writeCert("test", "not a cert", "not a key");
5657

5758
FileCertLoader loader(dir.filePath("test.crt"), dir.filePath("test.key"));

0 commit comments

Comments
 (0)