ACMX 2.136.0
Dual-Backend Real-Time GPU Video Synthesis
Loading...
Searching...
No Matches
resource_paths_tests.cpp
Go to the documentation of this file.
2
3#include <chrono>
4#include <fstream>
5#include <iostream>
6#include <stdexcept>
7#include <string>
8#include <system_error>
9
10namespace {
11 namespace fs = std::filesystem;
12
14 public:
16 const auto suffix = std::chrono::steady_clock::now()
17 .time_since_epoch()
18 .count();
19 path = fs::temp_directory_path() /
20 ("acmxvk-resource-paths-" + std::to_string(suffix));
21 fs::create_directories(path);
22 }
23
25 std::error_code error;
26 fs::remove_all(path, error);
27 }
28
31
32 fs::path path;
33 };
34
35 void create_file(const fs::path &path) {
36 fs::create_directories(path.parent_path());
37 std::ofstream output(path);
38 if (!output) {
39 throw std::runtime_error("could not create test file: " +
40 path.string());
41 }
42 output << "test";
43 }
44
45 void expect_equal(const fs::path &actual, const fs::path &expected,
46 const std::string &label) {
47 if (actual != expected) {
48 throw std::runtime_error(label + ": expected " +
49 expected.string() + ", received " +
50 actual.string());
51 }
52 }
53} // namespace
54
55int main() {
56 try {
57 TemporaryDirectory temporary;
58 const fs::path flip = temporary.path / "shaders/flip.frag.spv";
59 const fs::path crossfade =
60 temporary.path / "shaders/xfade/xfade_01_linear.frag.spv";
61 const fs::path model = temporary.path / "models/cube.obj";
62 const fs::path font = temporary.path / "data/font.ttf";
63 create_file(flip);
64 create_file(crossfade);
65 create_file(model);
66 create_file(font);
67
68 acmxvk::Options options;
69 options.resource_directory = temporary.path.string();
71 "user resource shader");
72 expect_equal(acmxvk::crossfade_shader_path(options, 0), crossfade,
73 "user resource crossfade");
75 "user resource model");
77 "user resource font");
78
79 if (!acmxvk::find_resource(options, "../outside").empty()) {
80 throw std::runtime_error("parent traversal was accepted");
81 }
82 if (!acmxvk::find_resource(options, flip).empty()) {
83 throw std::runtime_error("absolute resource path was accepted");
84 }
85
86 bool rejected_index = false;
87 try {
88 static_cast<void>(acmxvk::crossfade_shader_path(
89 options, acmxvk::CROSSFADE_NAMES.size()));
90 } catch (const std::out_of_range &) {
91 rejected_index = true;
92 }
93 if (!rejected_index) {
94 throw std::runtime_error("invalid crossfade index was accepted");
95 }
96 } catch (const std::exception &error) {
97 std::cerr << "resource path test failed: " << error.what() << '\n';
98 return 1;
99 }
100 return 0;
101}
int main(int argc, char **argv)
Application entry point.
TemporaryDirectory & operator=(const TemporaryDirectory &)=delete
fs::path default_model_path(const Options &options)
fs::path crossfade_shader_path(const Options &options, std::size_t shader_index)
constexpr std::array< std::string_view, 35 > CROSSFADE_NAMES
Definition options.hpp:15
fs::path overlay_font_path(const Options &options)
fs::path flip_shader_path(const Options &options)
fs::path find_resource(const Options &options, const fs::path &relative_path)
void expect_equal(const fs::path &actual, const fs::path &expected, const std::string &label)
std::string resource_directory
Definition options.hpp:192