Files
wine-access/tests/text_limits_test.cpp

76 lines
3.9 KiB
C++

// SPDX-License-Identifier: LGPL-2.1-or-later
#include "../text_limits.h"
#include "../reader_events.h"
#include <cstdio>
#include <vector>
static bool check(bool value, const char *message) {
if (!value) std::fprintf(stderr, "FAIL: %s\n", message);
return value;
}
static GVariant *event(const std::string &text, const char *kind = "value") {
return g_variant_new("(sssssssiis)", "source", kind, text.c_str(), "edit", "", "", "", 0, 0, "test");
}
int main() {
bool ok = true;
std::vector<WCHAR> large(max_controller_units + 2, L'a');
large.back() = 0;
ok &= check(!controller_text_fits(large.data()), "oversized controller input rejected");
large[max_controller_units] = 0;
ok &= check(controller_text_fits(large.data()), "maximum controller input accepted");
ok &= check(bounded_accessible_text(large.data(), large.size()).size() == max_accessible_units,
"large accessible value is bounded before UTF-8 allocation");
large[max_accessible_units - 1] = 0xd800;
large[max_accessible_units] = 0xdc00;
ok &= check(bounded_accessible_text(large.data(), large.size()).size() == max_accessible_units - 1,
"truncation does not split a surrogate pair");
const WCHAR padded[] = {L'o', L'k', 0, 0xd800};
ok &= check(bounded_accessible_text(padded, 4) == "ok", "malformed BSTR padding excluded");
GError *error = nullptr;
auto *connection = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error);
if (!connection) {
std::fprintf(stderr, "FAIL: private session bus: %s\n", error ? error->message : "unavailable");
if (error) g_error_free(error);
return 1;
}
const char *xml = "<node><interface name='org.stormux.LimitTest'>"
"<method name='PresentAccessibleEvent'>"
"<arg type='s' direction='in'/><arg type='s' direction='in'/><arg type='s' direction='in'/>"
"<arg type='s' direction='in'/><arg type='s' direction='in'/><arg type='s' direction='in'/>"
"<arg type='s' direction='in'/><arg type='i' direction='in'/><arg type='i' direction='in'/>"
"<arg type='s' direction='in'/><arg type='b' direction='out'/></method></interface></node>";
auto *info = g_dbus_node_info_new_for_xml(xml, nullptr);
static const GDBusInterfaceVTable table{
[](GDBusConnection *, const gchar *, const gchar *, const gchar *, const gchar *,
GVariant *, GDBusMethodInvocation *invocation, gpointer) {
g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", TRUE));
}, nullptr, nullptr, {nullptr}};
guint object = g_dbus_connection_register_object(connection, "/test", info->interfaces[0],
&table, nullptr, nullptr, nullptr);
ok &= check(object != 0, "recording reader object registered");
ReaderEndpoint endpoint{g_dbus_connection_get_unique_name(connection), "/test", "org.stormux.LimitTest"};
ReaderEventQueue queue;
ok &= check(queue.start(connection, endpoint), "reader queue starts");
ok &= check(queue.submit(event("focus", "focus")), "focus dependency accepted");
// Do not dispatch this connection's default GLib context: its first method
// call withholds the reply while the event thread fills the pending queue.
Sleep(20);
ok &= check(!queue.submit(event(std::string(max_reader_event_bytes, 'x'))),
"oversized single event rejected");
const std::string payload(100000, 'x');
unsigned accepted = 0;
auto started = GetTickCount64();
for (unsigned i = 0; i < 100; ++i) accepted += queue.submit(event(payload));
ok &= check(GetTickCount64() - started < 400, "flood completed before reader reply timeout");
ok &= check(accepted == 10, "byte budget rejects large events before the 128-event count limit");
queue.stop();
if (object) g_dbus_connection_unregister_object(connection, object);
g_dbus_node_info_unref(info);
g_object_unref(connection);
if (ok) std::puts("PASS: bounded MSAA/controller text, Unicode boundaries and reader queue byte budget");
return ok ? 0 : 1;
}