File: test_Docker__x11_passthrough.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (676 lines) | stat: -rw-r--r-- 24,038 bytes parent folder | download | duplicates (2)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import socket
import subprocess
import sys
from pathlib import Path, PosixPath, PurePosixPath
from unittest.mock import MagicMock, call

import pytest

from briefcase.exceptions import BriefcaseCommandError
from briefcase.integrations.docker import XauthDatabaseCreationFailure

XAUTH_LIST_RET_1 = (
    "0000 0004 7f000101 0002 3132 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 1b185a208e4600e717f3b8903fc35141\n"
    "0000 0004 7f000101 0002 3130 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 da9667aa96def95dfa44e01f1b13e55b\n"
    "0100 0006 70692d372d35 0002 3133 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 72e93a771996c40f8465a7aa9add67e0\n"
    "0100 0006 70692d372d35 0002 3134 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 870c9e1d92ee663f93955d7fc27a3641\n"
    "0100 0006 70692d372d35 0002 3135 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 17d837a0890cdf5588bd793ef3410357\n"
    "0100 0006 70692d372d35 0002 3132 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 cb329aff1071b8bd66f24af31c8460cc\n"
    "0100 0006 70692d372d35 0002 3130 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 187dcffa78c868e5c5f67b6640c299f5\n"
    "0100 0006 70692d372d35 0002 3131 0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 f22b85acf66a5819f5e15c2ac5af25b2"
)

XAUTH_LIST_RET_2 = (
    "0100 0007 6a757069746572 0000  0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 fa4b61837675f1581427e0c937701439\n"
    "ffff 0007 6a757069746572 0000  0012 "
    "4d49542d4d414749432d434f4f4b49452d31 0010 fa4b61837675f1581427e0c937701439"
)


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.parametrize(
    "display_num, expected_socket_path",
    [("1", "/tmp/.X11-unix/X1"), (2, "/tmp/.X11-unix/X2")],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_display_socket_path(mock_tools, display_num, expected_socket_path):
    """Path for socket is returned for X11 display."""
    socket_path = mock_tools.docker._x11_display_unix_socket_path(display_num)
    assert socket_path == PosixPath(expected_socket_path)


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.parametrize(
    "path_is_socket_outcome, expected_outcome",
    [(True, True), (OSError, False), (False, False)],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_is_display_socket(mock_tools, path_is_socket_outcome, expected_outcome):
    """X11 display UNIX socket is validated for X display number."""
    # Mock the Path for the X display socket
    mock_socket_path = MagicMock(spec_set=PosixPath)
    mock_socket_path.is_socket.side_effect = [path_is_socket_outcome]
    mock_tools.docker._x11_display_unix_socket_path = MagicMock(
        return_value=mock_socket_path
    )

    assert mock_tools.docker._x11_is_display_unix(1) is expected_outcome


@pytest.mark.parametrize(
    "connect_ret, display_num, connect_port, expected_outcome",
    [
        (0, 0, 6000, True),
        (0, 100, 6100, True),
        (1, 42, 6042, False),
        (OSError, 1, 6001, False),
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_is_display_tcp(
    mock_tools,
    connect_ret,
    display_num,
    connect_port,
    expected_outcome,
    monkeypatch,
):
    """X11 display TCP socket is validated for X display number."""
    # Mock the socket manager
    mock_sock = MagicMock()
    mock_sock.connect_ex.side_effect = [connect_ret]
    mock_socket_manager = MagicMock()
    mock_socket_manager.__enter__.return_value = mock_sock
    mock_socket = MagicMock(return_value=mock_socket_manager)
    monkeypatch.setattr("socket.socket", mock_socket)

    assert mock_tools.docker._x11_is_display_tcp(display_num) is expected_outcome

    # Socket connection timeout is set
    mock_sock.settimeout.assert_called_once_with(3)
    # Connection type for socket is correct
    mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
    # Connection attempt to the proper port
    mock_sock.connect_ex.assert_called_once_with(("localhost", connect_port))


@pytest.mark.parametrize(
    "is_socket_outcomes, is_tcp_outcomes, expected_display_num",
    [
        ([False], [False], 50),
        # Due to short-circuiting, only the first iterator is consumed if it returns False
        ([True, False], [False], 51),
        ([True, True, False, True, False], [False, False, False], 52),
        ([True] * 248 + [False, False], [True, False], 299),
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_allocate_display_success(
    mock_tools,
    is_socket_outcomes,
    is_tcp_outcomes,
    expected_display_num,
):
    """Allocating an X display number succeeds for a qualifying candidate."""
    # Mock X display socket checks
    mock_tools.docker._x11_is_display_unix = MagicMock(side_effect=is_socket_outcomes)
    mock_tools.docker._x11_is_display_tcp = MagicMock(side_effect=is_tcp_outcomes)

    assert mock_tools.docker._x11_allocate_display() == expected_display_num


@pytest.mark.parametrize(
    "is_socket_outcomes, is_tcp_outcomes",
    [
        ([True] * 250, [False] * 250),
        ([False] * 250, [True] * 250),
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_allocate_display_failure(mock_tools, is_socket_outcomes, is_tcp_outcomes):
    """Allocating an X display number fails if the range is exhausted."""
    # Mock X display socket checks
    mock_tools.docker._x11_is_display_unix = MagicMock(side_effect=is_socket_outcomes)
    mock_tools.docker._x11_is_display_tcp = MagicMock(side_effect=is_tcp_outcomes)

    with pytest.raises(BriefcaseCommandError, match="Failed to allocate X11 display"):
        mock_tools.docker._x11_allocate_display()


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.parametrize(
    "display_number, expected_xauth_path",
    [
        (1, Path.cwd() / "build/.briefcase.docker.xauth.1"),
        (50, Path.cwd() / "build/.briefcase.docker.xauth.50"),
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_proxy_display_xauth_file_path(
    mock_tools,
    display_number,
    expected_xauth_path,
):
    """The xauth database file path is generated correctly for the display number."""
    xauth_path = mock_tools.docker._x11_proxy_display_xauth_file_path(display_number)
    assert xauth_path == PosixPath(expected_xauth_path)


@pytest.mark.usefixtures("mock_docker")
def test_x11_write_xauth_missing_xauth_bin(mock_tools, tmp_path):
    """When the xauth program cannot be found, an error is raised."""
    mock_tools.shutil.which.return_value = ""

    with pytest.raises(BriefcaseCommandError, match="Install xauth to run an app"):
        mock_tools.docker._x11_write_xauth_file(
            DISPLAY=":66",
            xauth_file_path=tmp_path / "test_xauth_file",
            target_display_num=42,
        )


@pytest.mark.parametrize(
    "xauth_nlist_outcome",
    [
        subprocess.CalledProcessError(
            returncode=1,
            cmd=["xauth", "-i", "nlist", ":66"],
            output="xauth failure",
        ),
        "not found",
        "",
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_write_xauth_get_cookie_fails(mock_tools, xauth_nlist_outcome, tmp_path):
    """If the attempt to retrieve the current display's cookie fails, an error is
    raised."""

    mock_tools.subprocess._subprocess.check_output.side_effect = [xauth_nlist_outcome]

    with pytest.raises(
        XauthDatabaseCreationFailure, match="Failed to retrieve xauth cookie"
    ):
        mock_tools.docker._x11_write_xauth_file(
            DISPLAY=":66",
            xauth_file_path=tmp_path / "test_xauth_file",
            target_display_num=42,
        )


@pytest.mark.usefixtures("mock_docker")
def test_x11_write_xauth_add_new_xauth_fails(mock_tools, tmp_path):
    """If the attempt to retrieve the current display's cookie fails, an error is
    raised."""

    mock_tools.subprocess._subprocess.check_output.side_effect = [
        # xauth -i nlist :66
        XAUTH_LIST_RET_1,
        subprocess.CalledProcessError(
            returncode=1,
            cmd=[
                "xauth",
                "-i",
                "-f",
                str(tmp_path / "test_xauth_file"),
                "add",
                ":66",
                "MIT-MAGIC-COOKIE-1",
                "cookie",
            ],
            output="xauth failure",
        ),
    ]

    with pytest.raises(
        XauthDatabaseCreationFailure,
        match="Failed to add a xauth entry for existing cookie",
    ):
        mock_tools.docker._x11_write_xauth_file(
            DISPLAY=":66",
            xauth_file_path=tmp_path / "test_xauth_file",
            target_display_num=42,
        )


@pytest.mark.usefixtures("mock_docker")
def test_x11_write_xauth_retrieve_xauth_fails(mock_tools, tmp_path):
    """If the attempt to retrieve the target display's xauth list, an error is
    raised."""

    mock_tools.subprocess._subprocess.check_output.side_effect = [
        # xauth -i nlist :66
        XAUTH_LIST_RET_1,
        # xauth -i -f "xauth_file_path" add "MIT-MAGIC-COOKIE-1" <cookie>
        "xauth add success",
        subprocess.CalledProcessError(
            returncode=1,
            cmd=["xauth", "-i", "-f", str(tmp_path / "test_xauth_file"), "nlist"],
            output="xauth failure",
        ),
    ]

    with pytest.raises(
        XauthDatabaseCreationFailure, match="Failed to retrieve xauth list"
    ):
        mock_tools.docker._x11_write_xauth_file(
            DISPLAY=":66",
            xauth_file_path=tmp_path / "test_xauth_file",
            target_display_num=42,
        )


@pytest.mark.usefixtures("mock_docker")
def test_x11_write_xauth_merge_xauth_fails(mock_tools, tmp_path):
    """If the attempt to retrieve the target display's xauth list, an error is
    raised."""

    mock_tools.subprocess._subprocess.check_output.side_effect = [
        # xauth -i nlist :66
        XAUTH_LIST_RET_1,
        # xauth -i -f "xauth_file_path" add "MIT-MAGIC-COOKIE-1" <cookie>
        "xauth add success",
        # xauth -i -f "xauth_file_path" nlist
        XAUTH_LIST_RET_2,
        subprocess.CalledProcessError(
            returncode=1,
            cmd=["xauth", "-f", str(tmp_path / "test_xauth_file"), "nmerge", "-"],
            output="xauth failure",
        ),
    ]

    with pytest.raises(
        XauthDatabaseCreationFailure,
        match="Failed to merge xauth updates for FamilyWild hostname",
    ):
        mock_tools.docker._x11_write_xauth_file(
            DISPLAY=":66",
            xauth_file_path=tmp_path / "test_xauth_file",
            target_display_num=42,
        )


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_write_xauth_success(mock_tools, tmp_path, sub_check_output_kw):
    """The attempt to write the xauth database for the target display succeeds."""

    mock_tools.subprocess._subprocess.check_output.side_effect = [
        # xauth -i nlist :66
        XAUTH_LIST_RET_1,
        # xauth -i -f "xauth_file_path" add "MIT-MAGIC-COOKIE-1" <cookie>
        "xauth add success",
        # xauth -i -f "xauth_file_path" nlist
        XAUTH_LIST_RET_2,
        # xauth -i -f "xauth_file_path" nmerge -
        "xauth nmerge success",
    ]

    mock_tools.docker._x11_write_xauth_file(
        DISPLAY=":66",
        xauth_file_path=tmp_path / "test_xauth_file",
        target_display_num=42,
    )

    mock_tools.subprocess._subprocess.check_output.assert_has_calls(
        [
            call(["xauth", "-i", "nlist", ":66"], **sub_check_output_kw),
            call(
                [
                    "xauth",
                    "-i",
                    "-f",
                    f"{tmp_path}/test_xauth_file",
                    "add",
                    ":42",
                    "MIT-MAGIC-COOKIE-1",
                    "1b185a208e4600e717f3b8903fc35141",
                ],
                **sub_check_output_kw,
            ),
            call(
                [
                    "xauth",
                    "-i",
                    "-f",
                    f"{tmp_path}/test_xauth_file",
                    "nlist",
                ],
                **sub_check_output_kw,
            ),
            call(
                [
                    "xauth",
                    "-i",
                    "-f",
                    f"{tmp_path}/test_xauth_file",
                    "nmerge",
                    "-",
                ],
                input=(
                    "ffff 0007 6a757069746572 0000  0012 4d49542d4d414749432d434f4f4b49452d31 "
                    "0010 fa4b61837675f1581427e0c937701439\n"
                    "ffff 0007 6a757069746572 0000  0012 4d49542d4d414749432d434f4f4b49452d31 "
                    "0010 fa4b61837675f1581427e0c937701439"
                ),
                **sub_check_output_kw,
            ),
        ],
    )


@pytest.mark.usefixtures("mock_docker")
def test_x11_tcp_proxy_missing_socat_bin(mock_tools):
    """When the socat program cannot be found, an error is raised."""
    mock_tools.shutil.which.return_value = ""

    with pytest.raises(
        BriefcaseCommandError,
        match="Install socat to run an app for a targeted Linux distribution",
    ):
        mock_tools.docker._x11_tcp_proxy(DISPLAY=":66")


@pytest.mark.parametrize("DISPLAY", [None, "", "host;42.0", 42, "42"])
@pytest.mark.usefixtures("mock_docker")
def test_x11_tcp_proxy_invalid_DISPLAY(mock_tools, DISPLAY):
    """When the DISPLAY environment variable is invalid, an error is raised."""
    with pytest.raises(
        BriefcaseCommandError,
        match=f"Unsupported value for environment variable DISPLAY: '{DISPLAY}'",
    ):
        mock_tools.docker._x11_tcp_proxy(DISPLAY=DISPLAY)


@pytest.mark.usefixtures("mock_docker")
def test_x11_tcp_proxy_unknown_display_socket(mock_tools):
    """When a socket for the current display cannot be found, an error is raised."""
    # Mock target display allocation
    mock_tools.docker._x11_allocate_display = MagicMock(return_value=42)
    # Mock current display sockets cannot be found
    mock_tools.docker._x11_is_display_tcp = MagicMock(return_value=False)
    mock_tools.docker._x11_is_display_unix = MagicMock(return_value=False)

    with pytest.raises(
        BriefcaseCommandError,
        match="Cannot find X11 display for ':42'",
    ):
        mock_tools.docker._x11_tcp_proxy(DISPLAY=":42")


@pytest.mark.parametrize(
    "is_unix, is_tcp, current_display_num, target_display_num, connect_def",
    [
        (False, True, 142, 42, "TCP:localhost:{tcp_port}"),
        (False, True, 166, 66, "TCP:localhost:{tcp_port}"),
        (True, True, 166, 66, "TCP:localhost:{tcp_port}"),
        (True, False, 142, 42, "UNIX-CONNECT:/tmp/.X11-unix/X{display_num}"),
        (True, False, 166, 66, "UNIX-CONNECT:/tmp/.X11-unix/X{display_num}"),
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_tcp_proxy_create_success(
    mock_tools,
    is_unix,
    is_tcp,
    current_display_num,
    target_display_num,
    connect_def,
    sub_kw,
):
    """When current display has a socket, the proxy is to that TCP socket."""
    # Mock target display allocation
    mock_tools.docker._x11_allocate_display = MagicMock(return_value=target_display_num)
    # Mock current display sockets cannot be found
    mock_tools.docker._x11_is_display_tcp = MagicMock(return_value=is_tcp)
    mock_tools.docker._x11_is_display_unix = MagicMock(return_value=is_unix)
    # Mock proxy process
    mock_proxy_popen = MagicMock(spec_set=subprocess.Popen)
    mock_tools.subprocess._subprocess.Popen.return_value = mock_proxy_popen

    proxy_popen, proxy_display_num = mock_tools.docker._x11_tcp_proxy(
        DISPLAY=f"host:{current_display_num}.0"
    )

    # Proxy and spoofed target display are returned
    assert proxy_popen is mock_proxy_popen
    assert proxy_display_num == target_display_num

    # Proxy created properly
    mock_tools.subprocess._subprocess.Popen.assert_called_with(
        [
            "socat",
            f"TCP-LISTEN:{6000 + target_display_num},reuseaddr,fork,bind=0.0.0.0",
            connect_def.format(
                tcp_port=6000 + current_display_num,
                display_num=current_display_num,
            ),
        ],
        **sub_kw,
    )


@pytest.mark.parametrize("DISPLAY", [None, ""])
@pytest.mark.usefixtures("mock_docker")
def test_x11_passthrough_missing_DISPLAY(mock_tools, DISPLAY):
    """If the DISPLAY variable is not set, an error is raised."""
    # Mock DISPLAY environment variable
    mock_tools.os.getenv.return_value = DISPLAY

    with pytest.raises(
        BriefcaseCommandError,
        match="The DISPLAY environment variable must be set to run an app in Docker",
    ):
        with mock_tools.docker.x11_passthrough({}):
            pass


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_passthrough_fails(mock_tools):
    """Cleanup runs even if passthrough fails."""
    # Mock DISPLAY environment variable
    mock_tools.os.getenv.return_value = "host:42.0"
    # Mock Subprocess.cleanup()
    mock_tools.subprocess.cleanup = MagicMock()
    # Mock the proxy
    mock_proxy_popen = MagicMock(spec_set=subprocess.Popen)
    mock_tools.docker._x11_tcp_proxy = MagicMock(return_value=(mock_proxy_popen, 66))
    # Mock xauth database file path
    mock_xauth_file_path = MagicMock(spec_set=PosixPath)
    mock_tools.docker._x11_proxy_display_xauth_file_path = MagicMock(
        return_value=mock_xauth_file_path
    )
    # Mock the xauth database file write failing (beyond what's expected)
    mock_tools.docker._x11_write_xauth_file = MagicMock(
        side_effect=OSError("write failed")
    )

    with pytest.raises(OSError, match="write failed"):
        with mock_tools.docker.x11_passthrough({}):
            pass

    # Proxy cleanup ran
    mock_tools.subprocess.cleanup.assert_called_once_with(
        "X display proxy", mock_proxy_popen
    )
    mock_xauth_file_path.unlink.assert_called_once_with(missing_ok=True)


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.parametrize(
    "in_kwargs, out_kwargs",
    [
        (
            {
                "input1": "value1",
                "input2": "value2",
                "env": {"existing-var": "existing-val"},
                "mounts": [("/outside", "/inside")],
                "add_hosts": [("source", "target")],
            },
            {
                "input1": "value1",
                "input2": "value2",
                "env": {
                    "existing-var": "existing-val",
                    "DISPLAY": "host.docker.internal:66",
                },
                "mounts": [
                    ("/outside", "/inside"),
                ],
                "add_hosts": [
                    ("source", "target"),
                    ("host.docker.internal", "host-gateway"),
                ],
            },
        ),
        (
            {},
            {
                "env": {
                    "DISPLAY": "host.docker.internal:66",
                },
                "add_hosts": [("host.docker.internal", "host-gateway")],
            },
        ),
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_passthrough_xauth_fails(mock_tools, in_kwargs, out_kwargs, capsys):
    """Passthrough configuration is successful even if xauth fails and cleanup runs."""
    # Mock DISPLAY environment variable
    mock_tools.os.getenv.return_value = "host:42.0"
    # Mock Subprocess.cleanup()
    mock_tools.subprocess.cleanup = MagicMock()
    # Mock the proxy
    mock_proxy_popen = MagicMock(spec_set=subprocess.Popen)
    mock_tools.docker._x11_tcp_proxy = MagicMock(return_value=(mock_proxy_popen, 66))
    # Mock xauth database file path
    mock_xauth_file_path = MagicMock(spec_set=PosixPath)
    mock_tools.docker._x11_proxy_display_xauth_file_path = MagicMock(
        return_value=mock_xauth_file_path
    )
    # Mock the xauth database file write failing
    mock_tools.docker._x11_write_xauth_file = MagicMock(
        side_effect=XauthDatabaseCreationFailure("write failed")
    )

    with mock_tools.docker.x11_passthrough(in_kwargs) as kwargs:
        assert kwargs == out_kwargs

    # Proxy cleanup ran
    mock_tools.subprocess.cleanup.assert_called_once_with(
        "X display proxy", mock_proxy_popen
    )
    mock_xauth_file_path.unlink.assert_called_once_with(missing_ok=True)

    # User is warned that xauth failed
    assert capsys.readouterr().out == (
        "An X11 authentication database could not be created for the display.\n"
        "\n"
        "Briefcase will proceed, but if access to the display is rejected, this may be why.\n"
    )


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.parametrize(
    "in_kwargs, out_kwargs",
    [
        (
            {
                "input1": "value1",
                "input2": "value2",
                "env": {"existing-var": "existing-val"},
                "mounts": [("/outside", "/inside")],
                "add_hosts": [("source", "target")],
            },
            {
                "input1": "value1",
                "input2": "value2",
                "env": {
                    "existing-var": "existing-val",
                    "DISPLAY": "host.docker.internal:66",
                    "XAUTHORITY": "<placeholder>",
                },
                "mounts": [
                    ("/outside", "/inside"),
                ],
                "add_hosts": [
                    ("source", "target"),
                    ("host.docker.internal", "host-gateway"),
                ],
            },
        ),
        (
            {},
            {
                "env": {
                    "DISPLAY": "host.docker.internal:66",
                    "XAUTHORITY": "<placeholder>",
                },
                "mounts": [],
                "add_hosts": [("host.docker.internal", "host-gateway")],
            },
        ),
    ],
)
@pytest.mark.usefixtures("mock_docker")
def test_x11_passthrough_success(mock_tools, in_kwargs, out_kwargs, capsys):
    """Passthrough configuration is successful and cleanup runs."""
    # Mock DISPLAY environment variable
    mock_tools.os.getenv.return_value = "host:42.0"
    # Mock Subprocess.cleanup()
    mock_tools.subprocess.cleanup = MagicMock()
    # Mock the proxy
    mock_proxy_popen = MagicMock(spec_set=subprocess.Popen)
    mock_tools.docker._x11_tcp_proxy = MagicMock(return_value=(mock_proxy_popen, 66))
    # Mock xauth database file path
    mock_xauth_file_path = MagicMock(wraps=PosixPath("/tmp/subdir/xauth_file.db"))
    mock_xauth_file_path.name = "xauth_file.db"
    mock_tools.docker._x11_proxy_display_xauth_file_path = MagicMock(
        return_value=mock_xauth_file_path
    )
    # Mock the xauth database file write
    mock_tools.docker._x11_write_xauth_file = MagicMock()

    # Finish filling out augmented subprocess kwargs
    out_kwargs.setdefault("mounts", []).append(
        (mock_xauth_file_path, PurePosixPath("/tmp/xauth_file.db"))
    )
    out_kwargs.setdefault("env", {})["XAUTHORITY"] = PurePosixPath("/tmp/xauth_file.db")

    with mock_tools.docker.x11_passthrough(subprocess_kwargs=in_kwargs) as kwargs:
        assert kwargs == out_kwargs

    # Proxy cleanup ran
    mock_tools.subprocess.cleanup.assert_called_once_with(
        "X display proxy", mock_proxy_popen
    )
    mock_xauth_file_path.unlink.assert_called_once_with(missing_ok=True)

    # No console output
    assert capsys.readouterr().out == ""