AtlatestRepositorysigil-ui
1
/* Stale/thread guards use the actual bindings without a desktop connection. */2
#include <assert.h>3
#include "../../native/wayland.c"4
#include "standalone-protocols.h"6
typedef struct { SigilVM *vm; Value handle, text; } ServiceFixture;8
static void check_refused(ServiceFixture *fixture)9
{10
Value handle=fixture->handle;11
Value text[]={handle,fixture->text}, fullscreen[]={handle,SIGIL_TRUE};12
assert(native_window_title(fixture->vm,2,text)==SIGIL_FALSE);13
assert(native_window_clipboard_set(fixture->vm,2,text)==SIGIL_FALSE);14
assert(native_window_clipboard_read(fixture->vm,1,&handle)==SIGIL_FALSE);15
assert(native_window_fullscreen_state(fixture->vm,1,&handle)==SIGIL_FALSE);16
assert(native_window_fullscreen(fixture->vm,2,fullscreen)==SIGIL_FALSE);17
assert(sigil_as_fixnum(native_window_fullscreen_status(fixture->vm,1,&handle))==SUI_FULLSCREEN_REFUSED);18
assert(native_wl_activate(fixture->vm,2,text)==SIGIL_FALSE);19
assert(!sigil_error_message(fixture->vm));20
}22
static void check_activation_guards(SigilVM *vm)23
{24
Wl window = {.service_thread = pthread_self()};25
window.display = (struct wl_display *)(uintptr_t)1;26
window.surface = (struct wl_surface *)(uintptr_t)1;27
window.toplevel = (struct xdg_toplevel *)(uintptr_t)1;28
struct xdg_activation_v1 *global = (struct xdg_activation_v1 *)(uintptr_t)1;29
window.activation = global;31
char oversized[1025];32
memset(oversized, 'a', sizeof oversized);33
Value text = sigil_make_string(vm, "token", 5);34
Value handle = sigil_make_foreign(vm, wl_tag, &window, NULL, sizeof window);35
Value rejected[] = {SIGIL_FALSE, sigil_make_string(vm, "", 0),36
sigil_make_string(vm, oversized, sizeof oversized),37
sigil_make_string(vm, "a\0b", 3),38
sigil_make_string(vm, "\xed\xa0\x80", 3)};39
for (size_t i = 0; i < sizeof rejected / sizeof rejected[0]; i++) {40
Value args[] = {handle, rejected[i]};41
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);42
assert(!sigil_error_message(vm));43
}45
Value args[] = {handle, text};46
window.activation = NULL;47
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);48
window.activation = global;49
Wl parent = {0};50
window.parent = &parent;51
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);52
window.parent = NULL;53
window.toplevel = NULL;54
window.layer = (struct zwlr_layer_surface_v1 *)(uintptr_t)1;55
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);56
window.layer = NULL;57
window.toplevel = (struct xdg_toplevel *)(uintptr_t)1;58
window.connection_depth = 1;59
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);60
window.connection_depth = 0;61
window.closed = 1;62
window.display = NULL;63
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);64
assert(!sigil_error_message(vm));65
puts("PASS: activation refuses invalid UTF-8 and other invalid tokens, missing globals, "66
"subsurfaces, layer surfaces, dispatch reentry and closed windows");67
}69
static void *wrong_thread(void *data)70
{71
check_refused(data);72
return NULL;73
}75
int main(void)76
{77
SigilVM *vm=sigil_vm_create(); assert(vm);78
sigil__register_sigil_ui_wayland(vm);79
Wl window={.service_thread=pthread_self(),.closed=1};80
ServiceFixture fixture={vm,sigil_make_foreign(vm,wl_tag,&window,NULL,sizeof window),81
sigil_make_string(vm,"Owned text",10)};82
check_refused(&fixture);83
puts("PASS: closed Wayland bindings return refusal without a bridge error");84
window.closed=0;85
window.fullscreen_request=(SuiFullscreenRequest){0};86
/* Invalid protocol sentinels prove the thread guard runs before access. */87
window.display=(struct wl_display *)(uintptr_t)1;88
window.toplevel=(struct xdg_toplevel *)(uintptr_t)1;89
pthread_t thread;90
assert(!pthread_create(&thread,NULL,wrong_thread,&fixture));91
assert(!pthread_join(thread,NULL));92
assert(window.fullscreen_request.status==SUI_FULLSCREEN_IDLE);93
puts("PASS: Wayland bindings reject the non-owning thread before protocol access");94
Wl child={.service_thread=pthread_self(),.closed=1,.parent=&window};95
fixture.handle=sigil_make_foreign(vm,wl_tag,&child,NULL,sizeof child);96
check_refused(&fixture);97
puts("PASS: closed child services do not fall through to the live parent's clipboard");98
check_activation_guards(vm);99
window.display=NULL; window.toplevel=NULL;100
sigil_vm_destroy(vm);101
return 0;102
}