ACMX 2.139.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) { acmxvk::input::validate_string(value, kind, "test value"); }
12
13 void expectRejected(const std::function<void()> &operation) {
14 try {
15 operation();
16 } catch (const std::runtime_error &) {
17 return;
18 }
19 throw std::runtime_error("malformed input was unexpectedly accepted");
20 }
21
22} // namespace
23
24int main() {
27
28 expectAccepted("video files/input 01.mp4", StringKind::Path);
29 expectAccepted("Watermark \xF0\x9F\x8C\x88", StringKind::DisplayText);
30 expectAccepted("custom_uniform_1", StringKind::Identifier);
31 expectAccepted("h264_nvenc", StringKind::Token);
32 expectAccepted("preset=p7:rc=vbr_hq", StringKind::StructuredValue);
33 expectAccepted("https://example.com/audio.ogg", StringKind::Url);
34
35 expectRejected([] { validate_string(std::string("bad\0path", 8), StringKind::Path, "test path"); });
36 expectRejected([] { validate_string("bad\npath", StringKind::Path, "test path"); });
37 expectRejected([] { validate_string("\xC0\xAF", StringKind::Path, "test path"); });
38 expectRejected([] { validate_string("bad-name", StringKind::Identifier, "test identifier"); });
39 expectRejected([] { validate_string("value;command", StringKind::StructuredValue, "test value"); });
40 expectRejected([] { validate_string("javascript://example", StringKind::Url, "test URL"); });
41 expectRejected([] { validate_string("https://example.com/bad path", StringKind::Url, "test URL"); });
42 expectRejected([] { validate_string("hidden\xE2\x80\xAEtext", StringKind::DisplayText, "test text"); });
43 if (acmxvk::input::truncate_utf8("abc\xF0\x9F\x8C\x88xyz", 8) != "abc...") {
44 throw std::runtime_error("UTF-8 truncation split a codepoint");
45 }
46
47 std::istringstream valid_lines("\xEF\xBB\xBF"
48 "first\r\nsecond\tvalue\n");
49 std::string line;
50 if (!acmxvk::input::read_bounded_line(valid_lines, line, "test input", 1, 16) || line != "first") {
51 throw std::runtime_error("bounded line reader rejected a valid line");
52 }
53 if (!acmxvk::input::read_bounded_line(valid_lines, line, "test input", 2, 16) || line != "second\tvalue") {
54 throw std::runtime_error("bounded line reader rejected valid configuration whitespace");
55 }
57 std::istringstream long_line("123456789\n");
58 std::string value;
59 static_cast<void>(acmxvk::input::read_bounded_line(long_line, value, "test input", 1, 8));
60 });
61
62 std::cout << "ACMXVK input validation tests passed\n";
63 return 0;
64}
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)