1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*:*
* ggaze — clipboard URI-list integration test
*
* Verifies that clipboard_build_uri_provider (the helper clipboard_copy_uris
* sets on the GdkClipboard) advertises BOTH text/uri-list and text/plain and
* serializes standards-compliant bytes for each:
* - text/uri-list : RFC 2483 CRLF-terminated URI lines (trailing CRLF).
* - text/plain : newline-joined local PATH list (trailing newline), with
* a URI fallback for non-local files.
*
* It also exercises a negative target (application/x-bogus) to prove the
* provider does not accidentally offer arbitrary MIME types, and an empty
* file list (must not crash and must yield a valid, empty-bytes provider).
*
* The fake-target technique: rather than driving a real clipboard daemon, we
* serialize the provider's content for a chosen MIME type directly through
* gdk_content_provider_write_mime_type_async into a GMemoryOutputStream, then
* compare the captured bytes against the expected payload. Needs a display (the
* GdkContentProvider/GdkContentFormats APIs require Gdk to be initialized;
* integration suite; CI uses xvfb-run).
*
* Copyright (c) 2026 ggaze contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*:*/
#include "clipboard.h"
#include <gdk/gdk.h>
#include <gio/gio.h>
#include <glib.h>
#include <gtk/gtk.h>
/* --- helpers ------------------------------------------------------------ */
static void
drain_main(guint u_ms) {
for (guint u = 0; u < u_ms; u++) {
g_main_context_iteration(g_main_context_default(), FALSE);
g_usleep(1000);
}
}
/* Async-write completion context. */
typedef struct {
GMainLoop *p_loop;
GError *p_err;
gboolean b_ok;
} WriteCtx;
static void
_write_done_cb(GObject *p_src, GAsyncResult *p_res, gpointer p_data) {
WriteCtx *p_ctx = (WriteCtx *)p_data;
p_ctx->b_ok = gdk_content_provider_write_mime_type_finish(
GDK_CONTENT_PROVIDER(p_src), p_res, &p_ctx->p_err);
g_main_loop_quit(p_ctx->p_loop);
}
/* Serialize p_prov's content for c_mime into freshly-allocated bytes.
* Returns NULL (and sets *p_err) if the provider cannot supply c_mime.
* Caller frees with g_free. The provider is ref'd here so the caller need
* not keep it alive across the call. */
static guchar *
serialize_mime(GdkContentProvider *p_prov, const char *c_mime, gsize *p_len,
GError **p_err) {
GMemoryOutputStream *p_mem =
G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
GMainLoop *p_loop = g_main_loop_new(NULL, FALSE);
WriteCtx st_ctx = {.p_loop = p_loop, .p_err = NULL, .b_ok = FALSE};
gdk_content_provider_write_mime_type_async(
p_prov, c_mime, G_OUTPUT_STREAM(p_mem), G_PRIORITY_DEFAULT, NULL,
_write_done_cb, &st_ctx);
g_main_loop_run(p_loop);
/* The content write fills the buffer but does not close the stream;
* g_memory_output_stream_steal_data requires a closed stream. */
g_output_stream_close(G_OUTPUT_STREAM(p_mem), NULL, NULL);
guchar *c_out = NULL;
if (st_ctx.b_ok) {
gsize u_size = 0;
c_out = g_memory_output_stream_steal_data(p_mem);
/* steal_data returns the buffer but not its length; query it. */
u_size = g_memory_output_stream_get_data_size(p_mem);
if (p_len != NULL) {
*p_len = u_size;
}
} else {
if (p_err != NULL) {
*p_err = st_ctx.p_err;
} else {
g_clear_error(&st_ctx.p_err);
}
}
g_object_unref(p_mem);
g_main_loop_unref(p_loop);
return (c_out);
}
/* Build a GList of GFile* from NULL-terminated path arguments. */
static GList *
build_file_list(const char *c_first, ...) {
GList *p_list = NULL;
if (c_first != NULL) {
p_list = g_list_append(p_list, g_file_new_for_path(c_first));
va_list ap;
va_start(ap, c_first);
const char *c_path;
while ((c_path = va_arg(ap, const char *)) != NULL) {
p_list = g_list_append(p_list, g_file_new_for_path(c_path));
}
va_end(ap);
}
return (p_list);
}
static void
free_file_list(GList *p_list) {
g_list_free_full(p_list, (GDestroyNotify)g_object_unref);
}
/* --- subtests ----------------------------------------------------------- */
/* Both MIME types must be in the provider's offered formats. */
static void
test_clipboard_formats(void) {
GList *p_files = build_file_list("/tmp/a.jpg", "/tmp/b.jpg", NULL);
GdkContentProvider *p_prov = clipboard_build_uri_provider(p_files);
g_assert_nonnull(p_prov);
GdkContentFormats *p_fmts = gdk_content_provider_ref_formats(p_prov);
g_assert_nonnull(p_fmts);
g_assert_true(
gdk_content_formats_contain_mime_type(p_fmts, "text/uri-list"));
g_assert_true(gdk_content_formats_contain_mime_type(p_fmts, "text/plain"));
/* Negative: a type we never offered must NOT be present. */
g_assert_false(
gdk_content_formats_contain_mime_type(p_fmts, "application/x-bogus"));
gdk_content_formats_unref(p_fmts);
g_object_unref(p_prov);
free_file_list(p_files);
drain_main(50);
}
/* Single local file: uri-list has one CRLF-terminated line; plain has the
* path followed by a newline. */
static void
test_clipboard_single_file(void) {
GList *p_files = build_file_list("/home/u/pic.jpg", NULL);
GdkContentProvider *p_prov = clipboard_build_uri_provider(p_files);
g_assert_nonnull(p_prov);
/* text/uri-list: file:// URI + trailing CRLF. */
{
gsize u_len = 0;
GError *p_err = NULL;
guchar *c_buf = serialize_mime(p_prov, "text/uri-list", &u_len, &p_err);
g_assert_no_error(p_err);
g_assert_nonnull(c_buf);
char *c_expect = g_strdup_printf("file:///home/u/pic.jpg\r\n");
g_assert_cmpuint(u_len, ==, strlen(c_expect));
g_assert_cmpint(memcmp(c_buf, c_expect, u_len), ==, 0);
g_free(c_expect);
g_free(c_buf);
}
/* text/plain: local path + trailing newline. */
{
gsize u_len = 0;
GError *p_err = NULL;
guchar *c_buf = serialize_mime(p_prov, "text/plain", &u_len, &p_err);
g_assert_no_error(p_err);
g_assert_nonnull(c_buf);
char *c_expect = g_strdup_printf("/home/u/pic.jpg\n");
g_assert_cmpuint(u_len, ==, strlen(c_expect));
g_assert_cmpint(memcmp(c_buf, c_expect, u_len), ==, 0);
g_free(c_expect);
g_free(c_buf);
}
g_object_unref(p_prov);
free_file_list(p_files);
drain_main(50);
}
/* Multiple local files: each line terminated correctly, order preserved. */
static void
test_clipboard_multiple_files(void) {
GList *p_files = build_file_list("/a/1.jpg", "/a/2.png", "/a/3.webp", NULL);
GdkContentProvider *p_prov = clipboard_build_uri_provider(p_files);
g_assert_nonnull(p_prov);
{
gsize u_len = 0;
GError *p_err = NULL;
guchar *c_buf = serialize_mime(p_prov, "text/uri-list", &u_len, &p_err);
g_assert_no_error(p_err);
g_assert_nonnull(c_buf);
const char *c_expect =
"file:///a/1.jpg\r\nfile:///a/2.png\r\nfile:///a/3.webp\r\n";
g_assert_cmpuint(u_len, ==, strlen(c_expect));
g_assert_cmpint(memcmp(c_buf, c_expect, u_len), ==, 0);
g_free(c_buf);
}
{
gsize u_len = 0;
GError *p_err = NULL;
guchar *c_buf = serialize_mime(p_prov, "text/plain", &u_len, &p_err);
g_assert_no_error(p_err);
g_assert_nonnull(c_buf);
const char *c_expect = "/a/1.jpg\n/a/2.png\n/a/3.webp\n";
g_assert_cmpuint(u_len, ==, strlen(c_expect));
g_assert_cmpint(memcmp(c_buf, c_expect, u_len), ==, 0);
g_free(c_buf);
}
g_object_unref(p_prov);
free_file_list(p_files);
drain_main(50);
}
/* Empty list: must not crash. clipboard_build_uri_provider returns NULL
* (leaving the clipboard untouched rather than replacing it with an empty
* payload), and clipboard_copy_uris must handle that without crashing. */
static void
test_clipboard_empty_list(void) {
GdkContentProvider *p_prov = clipboard_build_uri_provider(NULL);
g_assert_null(p_prov);
/* Driving clipboard_copy_uris with an empty list must not crash; it
* should be a no-op against the default display's clipboard. */
GdkDisplay *p_disp = gdk_display_get_default();
if (p_disp != NULL) {
GdkClipboard *p_clip = gdk_display_get_clipboard(p_disp);
clipboard_copy_uris(p_clip, NULL);
}
drain_main(50);
}
/* Negative: requesting a MIME type the provider does NOT offer must fail the
* write (write_finish returns FALSE with a GError), proving the union does
* not accidentally advertise arbitrary content. */
static void
test_clipboard_rejects_unknown_mime(void) {
GList *p_files = build_file_list("/tmp/x.jpg", NULL);
GdkContentProvider *p_prov = clipboard_build_uri_provider(p_files);
g_assert_nonnull(p_prov);
gsize u_len = 0;
GError *p_err = NULL;
guchar *c_buf =
serialize_mime(p_prov, "application/x-bogus", &u_len, &p_err);
g_assert_null(c_buf);
g_assert_nonnull(p_err);
g_clear_error(&p_err);
g_object_unref(p_prov);
free_file_list(p_files);
drain_main(50);
}
int
main(int i_argc, char **c_argv) {
g_test_init(&i_argc, &c_argv, NULL);
g_log_set_always_fatal(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
if (!gtk_init_check()) {
g_test_skip("no display available (run under xvfb)");
return (g_test_run());
}
g_test_add_func("/clipboard/formats", test_clipboard_formats);
g_test_add_func("/clipboard/single_file", test_clipboard_single_file);
g_test_add_func("/clipboard/multiple_files", test_clipboard_multiple_files);
g_test_add_func("/clipboard/empty_list", test_clipboard_empty_list);
g_test_add_func("/clipboard/rejects_unknown_mime",
test_clipboard_rejects_unknown_mime);
return (g_test_run());
}
|