diff --git a/libobs/build/libobs.core-output+.h.c b/libobs/build/libobs.core-output+.h.c index 6abc60d7eb..256b3742f0 100644 --- a/libobs/build/libobs.core-output+.h.c +++ b/libobs/build/libobs.core-output+.h.c @@ -75,6 +75,7 @@ #include "libobs/obs-output-video-encoder-getter-fields.c.h" #include "libobs/obs-output.c.h" #include "libobs/obs-service-context-getter-fields.c.h" +#include "libobs/obs-service-find.c.h" #include "libobs/obs-service-handler-getters-fields.c.h" #include "libobs/obs-service-state-adapter.c.h" #include "libobs/obs-service.c.h" diff --git a/libobs/obs-service-find.c.h b/libobs/obs-service-find.c.h new file mode 100644 index 0000000000..5f85c9b741 --- /dev/null +++ b/libobs/obs-service-find.c.h @@ -0,0 +1,11 @@ +#include "libobs/build/libobs.pch.h" + +const struct obs_service_info *find_service(const char *id) +{ + size_t i; + for (i = 0; i < obs->service_types.num; i++) + if (strcmp(obs->service_types.array[i].id, id) == 0) + return obs->service_types.array + i; + + return NULL; +} diff --git a/libobs/obs-service.c.h b/libobs/obs-service.c.h index 189b10c607..c56d154769 100644 --- a/libobs/obs-service.c.h +++ b/libobs/obs-service.c.h @@ -19,16 +19,6 @@ #define get_weak(service) ((obs_weak_service_t *)service->context.control) -const struct obs_service_info *find_service(const char *id) -{ - size_t i; - for (i = 0; i < obs->service_types.num; i++) - if (strcmp(obs->service_types.array[i].id, id) == 0) - return obs->service_types.array + i; - - return NULL; -} - /* The service's lifecycle, references, module lookup and activation moved * to obs-service-getters.c (#1920). These are the steps they take on struct * obs_service, its context and its reference counts, which stay here with diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index b1e12a5040..9457d82c33 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -15305,6 +15305,21 @@ "OBS::native-test-cmocka" ] }, + "test_service_find": { + "sources": [ + "test/cmocka/test_service_find.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ] + }, "test_service_getters": { "sources": [ "test/cmocka/test_service_getters.c" @@ -27246,6 +27261,12 @@ "test_serializer": { "target": "test_serializer" }, + "test_service_find": { + "target": "test_service_find", + "command": [ + "${target}" + ] + }, "test_service_getters": { "target": "test_service_getters", "command": [ diff --git a/test/cmocka/test_service_find.c b/test/cmocka/test_service_find.c new file mode 100644 index 0000000000..22541ada45 --- /dev/null +++ b/test/cmocka/test_service_find.c @@ -0,0 +1,130 @@ +/* C-source characterization of hidden find_service (#5267). + * Real private layouts and controlled C-owned storage pin first-match order. + * A future Rust swap must relink these unchanged assertions through a real + * registry adapter; this source-only mode does not exercise a Rust swap. */ +#include +#include +#include +#include +#include +#include "obs-internal.h" + +static struct obs_core service_find_core; +struct obs_core *service_find_test_core = &service_find_core; + +#ifndef SERVICE_FIND_EXTERNAL +#define obs service_find_test_core +#include "libobs/obs-service-find.c.h" +#undef obs +#endif + +static unsigned callback_count; + +static const char *service_find_name(void *data) +{ + (void)data; + callback_count++; + return "unused"; +} + +static void *service_find_create(obs_data_t *settings, obs_service_t *service) +{ + (void)settings; + (void)service; + callback_count++; + return NULL; +} + +static void service_find_destroy(void *data) +{ + (void)data; + callback_count++; +} + +static void check_lookup(const char *id, const struct obs_service_info *expected) +{ + struct obs_service_info before[8]; + unsigned char core_before[sizeof(service_find_core)]; + assert_true(service_find_core.service_types.capacity <= 8); + size_t bytes = service_find_core.service_types.capacity * sizeof(before[0]); + if (bytes) + memcpy(before, service_find_core.service_types.array, bytes); + memcpy(core_before, &service_find_core, sizeof(core_before)); + long allocations = bnum_allocs(); + callback_count = 0; + + const struct obs_service_info *actual = find_service(id); + + assert_ptr_equal(actual, expected); + assert_int_equal(callback_count, 0); + assert_int_equal(bnum_allocs(), allocations); + assert_memory_equal(&service_find_core, core_before, sizeof(core_before)); + if (bytes) + assert_memory_equal(service_find_core.service_types.array, before, bytes); + if (expected) { + assert_ptr_equal(actual->id, expected->id); + assert_ptr_equal(actual->type_data, expected->type_data); + } +} + +static void registry_lookup(void **state) +{ + (void)state; + memset(&service_find_core, 0, sizeof(service_find_core)); + check_lookup("", NULL); + check_lookup("missing", NULL); + + char first_id[] = "svc"; + char second_id[] = "svc"; + char query[] = "svc"; + char binary_id[] = {(char)0xff, (char)0x80, 'x', 0}; + char binary_query[] = {(char)0xff, (char)0x80, 'x', 0}; + char binary_miss[] = {(char)0xff, (char)0x81, 'x', 0}; + int owners[8] = {0}; + struct obs_service_info entries[8] = {0}; + const char *ids[] = {first_id, "svc.suffix", "SVC", "", binary_id, second_id, "last", "outside"}; + for (size_t i = 0; i < 8; i++) { + entries[i].id = ids[i]; + entries[i].get_name = service_find_name; + entries[i].create = service_find_create; + entries[i].destroy = service_find_destroy; + entries[i].type_data = &owners[i]; + entries[i].free_type_data = service_find_destroy; + } + service_find_core.service_types.array = entries; + service_find_core.service_types.capacity = 8; + /* Allocated storage is not a logical entry when num is zero. */ + check_lookup(query, NULL); + service_find_core.service_types.num = 7; + check_lookup(query, &entries[0]); + check_lookup("sv", NULL); + check_lookup("svc.suffix", &entries[1]); + check_lookup("svc.suffix.more", NULL); + check_lookup("SVC", &entries[2]); + check_lookup("Svc", NULL); + check_lookup("", &entries[3]); + check_lookup(binary_query, &entries[4]); + check_lookup(binary_miss, NULL); + check_lookup("last", &entries[6]); + check_lookup("outside", NULL); + check_lookup("missing", NULL); + + /* Characterized, not endorsed: duplicate IDs select the FIRST entry. + * Moving/removing that entry must immediately affect the borrowed result. */ + struct obs_service_info swap = entries[0]; + entries[0] = entries[5]; + entries[5] = swap; + check_lookup(query, &entries[0]); + entries[0].id = "replaced"; + check_lookup(query, &entries[5]); + assert_memory_equal(first_id, "svc", sizeof(first_id)); + assert_memory_equal(second_id, "svc", sizeof(second_id)); + assert_memory_equal(owners, (int[8]){0}, sizeof(owners)); + memset(&service_find_core, 0, sizeof(service_find_core)); +} + +int main(void) +{ + const struct CMUnitTest tests[] = {cmocka_unit_test(registry_lookup)}; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_service_find.cmake b/test/cmocka/tests.d/test_service_find.cmake new file mode 100644 index 0000000000..d85a840326 --- /dev/null +++ b/test/cmocka/tests.d/test_service_find.cmake @@ -0,0 +1 @@ +# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).