ACMX 2.136.0
Dual-Backend Real-Time GPU Video Synthesis
Loading...
Searching...
No Matches
input_validation_tests.cpp
Go to the documentation of this file.
2
3#include <functional>
4#include <iostream>
5#include <sstream>
6#include <stdexcept>
7#include <string>
8
9namespace {
10
11 void expectAccepted(std::string_view value, acmxvk::input::StringKind kind) {
12 acmxvk::input::validate_string(value, kind, "test value");
13 }
14
15 void expectRejected(const std::function<void()> &operation) {
16 try {
17 operation();
18 } catch (const std::runtime_error &) {
19 return;
20 }
21 throw std::runtime_error("malformed input was unexpectedly accepted");
22 }
23
24} // namespace
25
26int main() {
29
30 expectAccepted("video files/input 01.mp4", StringKind::Path);
31 expectAccepted("Watermark \xF0\x9F\x8C\x88", StringKind::DisplayText);
32 expectAccepted("custom_uniform_1", StringKind::Identifier);
33 expectAccepted("h264_nvenc", StringKind::Token);
34 expectAccepted("preset=p7:rc=vbr_hq", StringKind::StructuredValue);
35 expectAccepted("https://example.com/audio.ogg", StringKind::Url);
36
38 validate_string(std::string("bad\0path", 8), StringKind::Path,
39 "test path");
40 });
42 validate_string("bad\npath", StringKind::Path, "test path");
43 });
45 validate_string("\xC0\xAF", StringKind::Path, "test path");
46 });
48 validate_string("bad-name", StringKind::Identifier, "test identifier");
49 });
51 validate_string("value;command", StringKind::StructuredValue,
52 "test value");
53 });
55 validate_string("javascript://example", StringKind::Url, "test URL");
56 });
58 validate_string("https://example.com/bad path", StringKind::Url,
59 "test URL");
60 });
62 validate_string("hidden\xE2\x80\xAEtext", StringKind::DisplayText,
63 "test text");
64 });
65 if (acmxvk::input::truncate_utf8("abc\xF0\x9F\x8C\x88xyz", 8) !=
66 "abc...") {
67 throw std::runtime_error("UTF-8 truncation split a codepoint");
68 }
69
70 std::istringstream valid_lines("\xEF\xBB\xBF"
71 "first\r\nsecond\tvalue\n");
72 std::string line;
73 if (!acmxvk::input::read_bounded_line(valid_lines, line, "test input", 1,
74 16) ||
75 line != "first") {
76 throw std::runtime_error("bounded line reader rejected a valid line");
77 }
78 if (!acmxvk::input::read_bounded_line(valid_lines, line, "test input", 2,
79 16) ||
80 line != "second\tvalue") {
81 throw std::runtime_error(
82 "bounded line reader rejected valid configuration whitespace");
83 }
85 std::istringstream long_line("123456789\n");
86 std::string value;
87 static_cast<void>(acmxvk::input::read_bounded_line(
88 long_line, value, "test input", 1, 8));
89 });
90
91 std::cout << "ACMXVK input validation tests passed\n";
92 return 0;
93}
int main(int argc, char **argv)
Application entry point.
bool read_bounded_line(std::istream &input, std::string &line, std::string_view context, std::size_t line_number, std::size_t maximum_bytes)
std::string truncate_utf8(std::string_view value, std::size_t maximum_bytes, std::string_view suffix)
void validate_string(std::string_view value, StringKind kind, std::string_view context, bool allow_empty)
void expectRejected(const std::function< void()> &operation)
void expectAccepted(std::string_view value, acmxvk::input::StringKind kind)