From 00b1af2c08c77d3ad02574ce53451fb991c12275 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Wed, 25 Nov 2026 06:30:40 -0800 Subject: [PATCH] test: characterize caption text2 API gates and duration bits (Refs #5678) Client-Checks: {"checks":{"base":"1a371e8bc576a915376715bf91ad39d30518b860","coverage":"all-tracked-applicable-files","diff":["test/cmocka/build/test-graph.json","test/cmocka/test_output_caption_text2.c"],"head":"91679ceda97d9a22e18736a1be67369ac49cc22d","tools":{"orchestrator-python":"3.13.15 (main, Aug 14 2026, 15:34:59) [Clang 22.1.3 ]","schema-dependencies":"3.13.15 (main, Aug 14 2026, 15:34:59) [Clang 22.1.3 ]\n{'jsonschema': '4.26.0', 'json_source_map': '1.0.5'}","uv":"uv 0.13.5 (x86_64-unknown-linux-gnu)","xmllint":"xmllint: using libxml version 21503\n compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ISO8859X Regexps Automata RelaxNG Schemas Modules Debug"},"tracked-digest":"77681778082e495a5824e4f9805357bd9fc0e98e0513c7488b020d7957208e18","tree":"1b98951cf3aeccaaa5b9c6d122648f7c98c6feb8"}} Ci-Attestation: {"at":1795673391,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1a371e8bc576a915376715bf91ad39d30518b860"],"secs":80,"stamp":"72c7641b44c15b0e6cc75ac2d9851c17","tree":"1b98951cf3aeccaaa5b9c6d122648f7c98c6feb8","v":1,"via":"run"} Ci-Attestation: {"at":1795673391,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1a371e8bc576a915376715bf91ad39d30518b860"],"secs":80,"stamp":"9245ab6a080ea9597b08a73edbf25942","tree":"1b98951cf3aeccaaa5b9c6d122648f7c98c6feb8","v":1,"via":"run"} Ci-Attestation: {"at":1795673391,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1a371e8bc576a915376715bf91ad39d30518b860"],"secs":80,"stamp":"12d612b667b75851f3504b1de410b4bc","tree":"1b98951cf3aeccaaa5b9c6d122648f7c98c6feb8","v":1,"via":"run"} Ci-Attestation: {"at":1795673391,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1a371e8bc576a915376715bf91ad39d30518b860"],"secs":80,"stamp":"640028af395acff069cdfe4888b2e9f4","tree":"1b98951cf3aeccaaa5b9c6d122648f7c98c6feb8","v":1,"via":"run"} Ci-Attestation: {"at":1796409805,"gate":"general/all/client-lint","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"8c48b0c7c3a0a2fe5b40fb8669265254","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1795673391,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1a371e8bc576a915376715bf91ad39d30518b860"],"secs":80,"stamp":"abb1151f8daa51b4a971ca3fa84c0170","tree":"1b98951cf3aeccaaa5b9c6d122648f7c98c6feb8","v":1,"via":"run"} Ci-Attestation: {"at":1796409807,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"2e5926ec2f45a52e2edf199a2aa30af4","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409807,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"9a2d33f16a1838bbc4ae20326ce566b1","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409807,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"a016f17d4de9652b34789eddaf388dfa","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409807,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"9a6073c5a27ecfc1572cc62168ab1cc1","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409806,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"798d1dbaf6e5b34b9b7a2fe96ccd224f","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409806,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"f4aeed5c4e4f7e00f40d528d3f6c6c61","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409807,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"9ffdf8d2f1ea2008267d824eff329536","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409807,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"75fbc4ebae834581dc45add2ac98d6d9","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409806,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"21f58f7431930c6e568d1202f525d781","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409806,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"59ada2859f3ed7ad1ee492293f3d2523","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409806,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"2b61abb04c0bb6c905b6f3fca81a9d63","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1796409806,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"ed6ff4b27965cc7620bb3ab22b019ab979bf5b55c88d6e72e9e78aa96a12385e","lane":"client-lint","parents":["f4b266b71bab7a58eac7e2cad3f1c1a6e4e0231b"],"secs":511,"stamp":"5c112fdb04cfef84545531d8bc31bc1e","tree":"59913df442417470e92f154f9df750bdbc6796ae","v":1,"via":"run"} Ci-Attestation: {"at":1795673655,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"c9932136e55968d5712695743e3b3f3be5b8e9e1125185f6370d8abe6f6a61f0","lane":"client-test-x86_64-unknown-linux-gnu","parents":["1a371e8bc576a915376715bf91ad39d30518b860"],"secs":263,"stamp":"e92a27dbf3ff0636e6efd87917fec6a6","tree":"1b98951cf3aeccaaa5b9c6d122648f7c98c6feb8","v":1,"via":"run"} --- .../test_output_caption_text2.json | 32 +++ test/cmocka/test_output_caption_text2.c | 208 ++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 test/cmocka/build/test-graph.d/test_output_caption_text2.json create mode 100644 test/cmocka/test_output_caption_text2.c diff --git a/test/cmocka/build/test-graph.d/test_output_caption_text2.json b/test/cmocka/build/test-graph.d/test_output_caption_text2.json new file mode 100644 index 0000000000..8941696a8e --- /dev/null +++ b/test/cmocka/build/test-graph.d/test_output_caption_text2.json @@ -0,0 +1,32 @@ +{ + "schema": 1, + "targets": { + "test_output_caption_text2": { + "sources": [ + "test/cmocka/test_output_caption_text2.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + }, + "SIMDe::SIMDe" + ], + "includes": [ + "libobs", + "deps/libcaption" + ] + } + }, + "tests": { + "test_output_caption_text2": { + "target": "test_output_caption_text2", + "command": [ + "${target}" + ] + } + } +} diff --git a/test/cmocka/test_output_caption_text2.c b/test/cmocka/test_output_caption_text2.c new file mode 100644 index 0000000000..28a4be7c0f --- /dev/null +++ b/test/cmocka/test_output_caption_text2.c @@ -0,0 +1,208 @@ +/* Characterize text2 directly through the exported API and real caption queue. + * Like test_output_caption_append, only the fields used by the API are needed; + * no running core, encoder, video backend or replacement API is involved. */ +#include +#include +#include +#include +#include +#include +#include +#include "obs-internal.h" +#include "util/bmem.h" +#include "util/threading.h" + +static unsigned log_count; +static int log_level; +static char log_message[256]; +static log_handler_t saved_handler; +static void *saved_param; + +#if defined(__GNUC__) || defined(__clang__) +__attribute__((format(printf, 2, 0))) +#endif +static void capture_log(int level, const char *format, va_list args, void *param) +{ + (void)param; + log_count++; + log_level = level; + vsnprintf(log_message, sizeof(log_message), format, args); +} + +struct fixture { + struct obs_output output; + struct caption_track_data tracks[2]; + long allocations; +}; + +static int setup(void **state) +{ + long allocations = bnum_allocs(); + struct fixture *f = bzalloc(sizeof(*f)); + f->allocations = allocations; + for (size_t i = 0; i < 2; i++) + assert_int_equal(pthread_mutex_init(&f->tracks[i].caption_mutex, NULL), 0); + f->output.caption_tracks[0] = &f->tracks[0]; + f->output.caption_tracks[MAX_OUTPUT_VIDEO_ENCODERS - 1] = &f->tracks[1]; + base_get_log_handler(&saved_handler, &saved_param); + base_set_log_handler(capture_log, NULL); + log_count = 0; + *state = f; + return 0; +} + +static int teardown(void **state) +{ + struct fixture *f = *state; + base_set_log_handler(saved_handler, saved_param); + for (size_t i = 0; i < 2; i++) { + struct caption_text *node = f->tracks[i].caption_head; + while (node) { + struct caption_text *next = node->next; + bfree(node); + node = next; + } + assert_int_equal(pthread_mutex_destroy(&f->tracks[i].caption_mutex), 0); + } + long allocations = f->allocations; + bfree(f); + assert_int_equal(bnum_allocs(), allocations); + return 0; +} + +static void null_output_stops_before_text(void **state) +{ + (void)state; + obs_output_output_caption_text2(NULL, NULL, -0.0); + assert_int_equal(log_count, 1); + assert_int_equal(log_level, LOG_DEBUG); + assert_string_equal(log_message, "obs_output_output_caption_text2: Null 'output' parameter"); +} + +static void inactive_output_is_silent(void **state) +{ + struct fixture *f = *state; + /* NULL text is never read while inactive, even when reconnecting. */ + os_atomic_set_bool(&f->output.reconnecting, true); + obs_output_output_caption_text2(&f->output, NULL, 3.25); + assert_int_equal(log_count, 0); + for (size_t i = 0; i < 2; i++) { + assert_null(f->tracks[i].caption_head); + assert_null(f->tracks[i].caption_tail); + } +} + +static void sparse_tracks_append_copied_text(void **state) +{ + struct fixture *f = *state; + os_atomic_set_bool(&f->output.active, true); + char text[] = "hello caption"; + obs_output_output_caption_text2(&f->output, text, 3.25); + memset(text, 'x', sizeof(text) - 1); + obs_output_output_caption_text2(&f->output, "second", 3.25); + /* Characterized, not endorsed: empty input still queues an empty page. */ + obs_output_output_caption_text2(&f->output, "", 3.25); + assert_int_equal(log_count, 3); + assert_int_equal(log_level, LOG_DEBUG); + assert_string_equal(log_message, "Caption text: "); + for (size_t i = 0; i < 2; i++) { + struct caption_text *node = f->tracks[i].caption_head; + const char *expected[] = {"hello caption", "second", ""}; + for (size_t j = 0; j < 3; j++) { + assert_non_null(node); + assert_string_equal(node->text, expected[j]); + assert_true(node->display_duration == 3.25); + if (j == 2) + assert_ptr_equal(node, f->tracks[i].caption_tail); + node = node->next; + } + assert_null(node); + } + assert_ptr_not_equal(f->tracks[0].caption_head, f->tracks[1].caption_head); +} + +static void every_page_gets_caller_duration(void **state) +{ + struct fixture *f = *state; + os_atomic_set_bool(&f->output.active, true); + char text[482]; + memset(text, 'a', 480); + text[480] = 'b'; + text[481] = '\0'; + obs_output_output_caption_text2(&f->output, text, 3.25); + for (size_t i = 0; i < 2; i++) { + struct caption_text *first = f->tracks[i].caption_head; + assert_non_null(first); + /* The page contains fifteen 32-column rows, separated by newlines. */ + assert_int_equal(strlen(first->text), 480 + 14); + for (size_t row = 0; row < 15; row++) { + assert_memory_equal(first->text + row * 33, text, 32); + if (row < 14) + assert_int_equal(first->text[row * 33 + 32], '\n'); + } + assert_true(first->display_duration == 3.25); + struct caption_text *last = first->next; + assert_non_null(last); + assert_string_equal(last->text, "b"); + assert_true(last->display_duration == 3.25); + assert_ptr_equal(last, f->tracks[i].caption_tail); + assert_null(last->next); + } +} + +static void active_without_tracks(void **state) +{ + struct fixture *f = *state; + os_atomic_set_bool(&f->output.active, true); + f->output.caption_tracks[0] = NULL; + f->output.caption_tracks[MAX_OUTPUT_VIDEO_ENCODERS - 1] = NULL; + obs_output_output_caption_text2(&f->output, "no tracks", 3.25); + assert_int_equal(log_count, 1); + assert_string_equal(log_message, "Caption text: no tracks"); + assert_null(f->tracks[0].caption_head); + assert_null(f->tracks[1].caption_head); +} + +/* Characterized, not endorsed: duration is stored without validation, including + * signed zero, negative values, infinities and a quiet NaN payload. */ +static void duration_bits_survive_repeated_appends(void **state) +{ + struct fixture *f = *state; + os_atomic_set_bool(&f->output.active, true); + const uint64_t bits[] = { + UINT64_C(0x0000000000000000), UINT64_C(0x8000000000000000), UINT64_C(0xbff4000000000000), + UINT64_C(0x3fb999999999999a), UINT64_C(0x7ff0000000000000), UINT64_C(0xfff0000000000000), + UINT64_C(0x7ff8000000000123), + }; + _Static_assert(sizeof(double) == sizeof(uint64_t), "IEEE double storage"); + for (size_t j = 0; j < sizeof(bits) / sizeof(bits[0]); j++) { + double duration; + memcpy(&duration, &bits[j], sizeof(duration)); + obs_output_output_caption_text2(&f->output, "duration", duration); + } + for (size_t i = 0; i < 2; i++) { + struct caption_text *node = f->tracks[i].caption_head; + for (size_t j = 0; j < sizeof(bits) / sizeof(bits[0]); j++) { + assert_non_null(node); + assert_string_equal(node->text, "duration"); + assert_memory_equal(&node->display_duration, &bits[j], sizeof(double)); + if (j + 1 == sizeof(bits) / sizeof(bits[0])) + assert_ptr_equal(node, f->tracks[i].caption_tail); + node = node->next; + } + assert_null(node); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(null_output_stops_before_text, setup, teardown), + cmocka_unit_test_setup_teardown(inactive_output_is_silent, setup, teardown), + cmocka_unit_test_setup_teardown(sparse_tracks_append_copied_text, setup, teardown), + cmocka_unit_test_setup_teardown(every_page_gets_caller_duration, setup, teardown), + cmocka_unit_test_setup_teardown(active_without_tracks, setup, teardown), + cmocka_unit_test_setup_teardown(duration_bits_survive_repeated_appends, setup, teardown), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +}