ACMX 2.139.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().time_since_epoch().count();
17 path = fs::temp_directory_path() / ("acmxvk-resource-paths-" + std::to_string(suffix));
18 fs::create_directories(path);
19 }
20
22 std::error_code error;
23 fs::remove_all(path, error);
24 }
25
28
29 fs::path path;
30 };
31
32 void create_file(const fs::path &path) {
33 fs::create_directories(path.parent_path());
34 std::ofstream output(path);
35 if (!output) {
36 throw std::runtime_error("could not create test file: " + path.string());
37 }
38 output << "test";
39 }
40
41 void expect_equal(const fs::path &actual, const fs::path &expected, const std::string &label) {
42 if (actual != expected) {
43 throw std::runtime_error(label + ": expected " + expected.string() + ", received " + actual.string());
44 }
45 }
46} // namespace
47
48int main() {
49 try {
50 TemporaryDirectory temporary;
51 const fs::path resource_directory = temporary.path / "share/acmxvk";
52 const fs::path mxvk_shaders = temporary.path / "share/mxvk/shaders";
53 const fs::path flip = resource_directory / "shaders/flip.frag.spv";
54 const fs::path crossfade = resource_directory / "shaders/xfade/xfade_01_linear.frag.spv";
55 const fs::path model = resource_directory / "models/cube.obj";
56 const fs::path font = resource_directory / "data/font.ttf";
57 create_file(flip);
58 create_file(crossfade);
59 create_file(model);
60 create_file(font);
61 create_file(mxvk_shaders / "sprite.frag.spv");
62
63 acmxvk::Options options;
64 options.resource_directory = resource_directory.string();
65 expect_equal(acmxvk::flip_shader_path(options), flip, "user resource shader");
66 expect_equal(acmxvk::crossfade_shader_path(options, 0), crossfade, "user resource crossfade");
67 expect_equal(acmxvk::default_model_path(options), model, "user resource model");
68 expect_equal(acmxvk::overlay_font_path(options), font, "user resource font");
69 expect_equal(acmxvk::mxvk_shader_directory(options), mxvk_shaders, "installed MXVK shader directory");
70
71 if (!acmxvk::find_resource(options, "../outside").empty()) {
72 throw std::runtime_error("parent traversal was accepted");
73 }
74 if (!acmxvk::find_resource(options, flip).empty()) {
75 throw std::runtime_error("absolute resource path was accepted");
76 }
77
78 bool rejected_index = false;
79 try {
80 static_cast<void>(acmxvk::crossfade_shader_path(options, acmxvk::CROSSFADE_NAMES.size()));
81 } catch (const std::out_of_range &) {
82 rejected_index = true;
83 }
84 if (!rejected_index) {
85 throw std::runtime_error("invalid crossfade index was accepted");
86 }
87 } catch (const std::exception &error) {
88 std::cerr << "resource path test failed: " << error.what() << '\n';
89 return 1;
90 }
91 return 0;
92}
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)
fs::path mxvk_shader_directory(const Options &options)
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:206