Debian Codegraph @

tests/platforms/windows/app/test_package.py

raw
1 from subprocess import CalledProcessError
2 from unittest import mock
3 from zipfile import ZipFile
4
5 import pytest
6
7 import briefcase.platforms.windows
8 from briefcase.exceptions import BriefcaseCommandError
9 from briefcase.integrations.subprocess import Subprocess
10 from briefcase.integrations.windows_sdk import WindowsSDK
11 from briefcase.integrations.wix import WiX
12 from briefcase.platforms.windows.app import WindowsAppPackageCommand
13
14 from ....integrations.wix.conftest import WIX_EXE_PATH, WIX_UI_PATH
15 from ....utils import create_file
16
17
18 @pytest.fixture
19 def package_command(dummy_console, tmp_path):
20 command = WindowsAppPackageCommand(
21 console=dummy_console,
22 base_path=tmp_path / "base_path",
23 data_path=tmp_path / "briefcase",
24 )
25 command.tools.host_os = "Windows"
26 command.tools.subprocess = mock.MagicMock(spec_set=Subprocess)
27 command.tools.wix = WiX(command.tools, wix_home=tmp_path / "wix")
28 command.tools.windows_sdk = WindowsSDK(
29 tools=command.tools,
30 root_path=tmp_path / "windows_sdk",
31 version="81.2.1.0",
32 arch="groovy",
33 )
34 return command
35
36
37 @pytest.fixture
38 def package_command_with_files(package_command, tmp_path):
39 # Build the paths for the source and the distribution folders:
40 src_path = tmp_path / "base_path/build/first-app/windows/app/src"
41 dist_path = tmp_path / "base_path/dist"
42 src_path.mkdir(parents=True)
43 dist_path.mkdir(parents=True)
44
45 # Mock some typical folders and files in the src folder:
46 files = (
47 src_path / "First App.exe",
48 src_path / "python.exe",
49 src_path / "python3.dll",
50 src_path / "vcruntime140.dll",
51 src_path / "app/first-app/app.py",
52 src_path / "app/first-app/resources/__init__.py",
53 src_path / "app/first-app-0.0.1.dist-info/top_level.txt",
54 src_path / "app_packages/clr.py",
55 src_path / "app_packages/toga_winforms/command.py",
56 )
57 for file in files:
58 create_file(file, "")
59
60 return package_command
61
62
63 def test_package_formats(package_command):
64 """Packaging formats are as expected."""
65 assert package_command.packaging_formats == ["msi", "zip"]
66 assert package_command.default_packaging_format == "msi"
67
68
69 def test_verify(package_command, monkeypatch):
70 """Verifying on Windows creates a WiX wrapper."""
71 # prime Command to _not_ need Windows SDK
72 package_command._windows_sdk_needed = False
73
74 mock_wix_verify = mock.MagicMock(wraps=WiX.verify)
75 monkeypatch.setattr(
76 briefcase.platforms.windows.WiX,
77 "verify",
78 mock_wix_verify,
79 )
80
81 package_command.verify_tools()
82
83 # WiX tool was verified
84 mock_wix_verify.assert_called_once_with(tools=package_command.tools)
85 assert isinstance(package_command.tools.wix, WiX)
86
87
88 def test_verify_with_signing(package_command, monkeypatch):
89 """Verifying on Windows creates WiX and WindowsSDK wrappers when code signing."""
90 # prime Command to need Windows SDK
91 package_command._windows_sdk_needed = True
92
93 mock_windows_sdk_verify = mock.MagicMock(wraps=WindowsSDK.verify)
94 monkeypatch.setattr(
95 briefcase.platforms.windows.WindowsSDK,
96 "verify",
97 mock_windows_sdk_verify,
98 )
99
100 mock_wix_verify = mock.MagicMock(wraps=WiX.verify)
101 monkeypatch.setattr(
102 briefcase.platforms.windows.WiX,
103 "verify",
104 mock_wix_verify,
105 )
106
107 package_command.verify_tools()
108
109 # WiX tool was verified
110 mock_wix_verify.assert_called_once_with(tools=package_command.tools)
111 assert isinstance(package_command.tools.wix, WiX)
112 # WindowsSDK tool was verified
113 mock_windows_sdk_verify.assert_called_once_with(tools=package_command.tools)
114 assert isinstance(package_command.tools.windows_sdk, WindowsSDK)
115
116
117 @pytest.mark.parametrize(
118 ("cli_args", "signing_options", "is_sdk_needed"),
119 [
120 ([], {}, False),
121 (["--adhoc-sign"], {"adhoc_sign": True}, False),
122 (["--file-digest", "sha2000"], {"file_digest": "sha2000"}, False),
123 (["-i", "asdf"], {"identity": "asdf"}, True),
124 (["--identity", "asdf"], {"identity": "asdf"}, True),
125 (
126 [
127 "-i",
128 "asdf",
129 "--file-digest",
130 "sha42",
131 "--use-local-machine-stores",
132 "--cert-store",
133 "mystore",
134 "--timestamp-url",
135 "http://freetimestamps.com",
136 "--timestamp-digest",
137 "sha56",
138 ],
139 {
140 "identity": "asdf",
141 "file_digest": "sha42",
142 "use_local_machine": True,
143 "cert_store": "mystore",
144 "timestamp_url": "http://freetimestamps.com",
145 "timestamp_digest": "sha56",
146 },
147 True,
148 ),
149 (
150 [
151 "-i",
152 "asdf",
153 "--file-digest",
154 "sha42",
155 "--cert-store",
156 "mystore",
157 "--timestamp-url",
158 "http://freetimestamps.com",
159 "--timestamp-digest",
160 "sha56",
161 ],
162 {
163 "identity": "asdf",
164 "file_digest": "sha42",
165 "cert_store": "mystore",
166 "timestamp_url": "http://freetimestamps.com",
167 "timestamp_digest": "sha56",
168 },
169 True,
170 ),
171 ],
172 )
173 def test_parse_options(package_command, cli_args, signing_options, is_sdk_needed):
174 """Command line arguments are parsed as expected; Windows SDK is required if an
175 identity is specified."""
176 default_options = {
177 "identity": None,
178 "file_digest": "sha256",
179 "use_local_machine": False,
180 "cert_store": "My",
181 "timestamp_url": "http://timestamp.digicert.com",
182 "timestamp_digest": "sha256",
183 "adhoc_sign": False,
184 "packaging_format": "msi",
185 "update": False,
186 }
187 expected_options = {**default_options, **signing_options}
188
189 options, overrides = package_command.parse_options(extra=cli_args)
190
191 assert options == expected_options
192 assert overrides == {}
193 assert package_command._windows_sdk_needed is is_sdk_needed
194
195
196 @pytest.mark.parametrize(
197 ("kwargs", "external"),
198 [
199 # Default behavior (adhoc signing, internal app)
200 ({}, False),
201 # External app mode makes no difference when packaging an MSI, because it's
202 # entirely implemented in `briefcase create`.
203 ({}, True),
204 # Explicit adhoc signing
205 ({"adhoc_sign": True}, False),
206 ],
207 )
208 def test_package_msi(
209 package_command, first_app_config, external_first_app, tmp_path, kwargs, external
210 ):
211 """A Windows app can be packaged as an MSI."""
212
213 package_command.package_app(
214 external_first_app if external else first_app_config,
215 **kwargs,
216 )
217
218 assert package_command.tools.subprocess.run.mock_calls == [
219 # Compile MSI
220 mock.call(
221 [
222 tmp_path / "wix" / WIX_EXE_PATH,
223 "build",
224 "-ext",
225 tmp_path / "wix" / WIX_UI_PATH,
226 "-arch",
227 "x64",
228 "first-app.wxs",
229 "-loc",
230 "unicode.wxl",
231 "-pdbtype",
232 "none",
233 "-o",
234 tmp_path / "base_path/dist/First App-0.0.1.msi",
235 ],
236 check=True,
237 cwd=tmp_path / "base_path/build/first-app/windows/app",
238 ),
239 ]
240
241
242 @pytest.mark.parametrize(
243 "kwargs",
244 [
245 {}, # Default behavior (adhoc signing)
246 {"adhoc_sign": True}, # Explicit adhoc signing
247 ],
248 )
249 def test_package_zip(package_command_with_files, first_app_config, kwargs, tmp_path):
250 """A Windows app can be packaged as a zip file."""
251
252 first_app_config.packaging_format = "zip"
253 package_command_with_files.package_app(first_app_config, **kwargs)
254
255 # No signing was performed
256 assert package_command_with_files.tools.subprocess.run.mock_calls == []
257
258 archive_file = tmp_path / "base_path/dist/First App-0.0.1.zip"
259 source_folders_and_files = (
260 "app/",
261 "app/first-app/",
262 "app/first-app/resources/",
263 "app/first-app-0.0.1.dist-info/",
264 "app_packages/",
265 "app_packages/toga_winforms/",
266 "First App.exe",
267 "python.exe",
268 "python3.dll",
269 "vcruntime140.dll",
270 "app/first-app/app.py",
271 "app/first-app/resources/__init__.py",
272 "app/first-app-0.0.1.dist-info/top_level.txt",
273 "app_packages/clr.py",
274 "app_packages/toga_winforms/command.py",
275 )
276
277 # The zip file exists
278 assert archive_file.exists()
279
280 # Check content of zip file
281 with ZipFile(archive_file) as archive:
282 root = "First App-0.0.1/"
283 # All folders and files in zip are from source
284 for name in archive.namelist():
285 # name.removeprefix(f'{root}') will only work in Python > 3.8
286 assert name[len(root) :] in source_folders_and_files
287 # All files from source are in zip
288 for file in source_folders_and_files:
289 assert f"{root}{file}" in archive.namelist()
290
291
292 @pytest.mark.parametrize(
293 ("use_local_machine", "additional_args"),
294 [(False, []), (True, ["-sm"])],
295 )
296 def test_package_msi_with_codesigning(
297 package_command,
298 first_app_config,
299 tmp_path,
300 use_local_machine,
301 additional_args,
302 ):
303 """A Windows app can be packaged as an MSI and code signed."""
304
305 package_command.package_app(
306 first_app_config,
307 identity="80ee4c3321122916f5637522451993c2a0a4a56a",
308 file_digest="sha42",
309 use_local_machine=use_local_machine,
310 cert_store="mystore",
311 timestamp_url="http://freetimestamps.com",
312 timestamp_digest="sha56",
313 )
314
315 assert package_command.tools.subprocess.run.mock_calls == [
316 # Codesign app exe
317 mock.call(
318 [
319 tmp_path
320 / "windows_sdk"
321 / "bin"
322 / "81.2.1.0"
323 / "groovy"
324 / "signtool.exe",
325 "sign",
326 "-s",
327 "mystore",
328 "-sha1",
329 "80ee4c3321122916f5637522451993c2a0a4a56a",
330 "-fd",
331 "sha42",
332 "-d",
333 "The first simple app \\ demonstration",
334 "-du",
335 "https://example.com/first-app",
336 "-tr",
337 "http://freetimestamps.com",
338 "-td",
339 "sha56",
340 *additional_args,
341 tmp_path
342 / "base_path"
343 / "build"
344 / "first-app"
345 / "windows"
346 / "app"
347 / "src"
348 / "First App.exe",
349 ],
350 check=True,
351 ),
352 # Compile MSI
353 mock.call(
354 [
355 tmp_path / "wix" / WIX_EXE_PATH,
356 "build",
357 "-ext",
358 tmp_path / "wix" / WIX_UI_PATH,
359 "-arch",
360 "x64",
361 "first-app.wxs",
362 "-loc",
363 "unicode.wxl",
364 "-pdbtype",
365 "none",
366 "-o",
367 tmp_path / "base_path/dist/First App-0.0.1.msi",
368 ],
369 check=True,
370 cwd=tmp_path / "base_path/build/first-app/windows/app",
371 ),
372 # Codesign app MSI
373 mock.call(
374 [
375 tmp_path
376 / "windows_sdk"
377 / "bin"
378 / "81.2.1.0"
379 / "groovy"
380 / "signtool.exe",
381 "sign",
382 "-s",
383 "mystore",
384 "-sha1",
385 "80ee4c3321122916f5637522451993c2a0a4a56a",
386 "-fd",
387 "sha42",
388 "-d",
389 "The first simple app \\ demonstration",
390 "-du",
391 "https://example.com/first-app",
392 "-tr",
393 "http://freetimestamps.com",
394 "-td",
395 "sha56",
396 *additional_args,
397 tmp_path / "base_path/dist/First App-0.0.1.msi",
398 ],
399 check=True,
400 ),
401 ]
402
403
404 @pytest.mark.parametrize(
405 ("use_local_machine", "additional_args"),
406 [(False, []), (True, ["-sm"])],
407 )
408 def test_package_zip_with_codesigning(
409 package_command_with_files,
410 first_app_config,
411 tmp_path,
412 use_local_machine,
413 additional_args,
414 ):
415 """In a ZIP package, only the binary will be code signed."""
416
417 first_app_config.packaging_format = "zip"
418
419 package_command_with_files.package_app(
420 first_app_config,
421 identity="80ee4c3321122916f5637522451993c2a0a4a56a",
422 file_digest="sha42",
423 use_local_machine=use_local_machine,
424 cert_store="mystore",
425 timestamp_url="http://freetimestamps.com",
426 timestamp_digest="sha56",
427 )
428
429 assert package_command_with_files.tools.subprocess.run.mock_calls == [
430 # Codesign app exe
431 mock.call(
432 [
433 tmp_path
434 / "windows_sdk"
435 / "bin"
436 / "81.2.1.0"
437 / "groovy"
438 / "signtool.exe",
439 "sign",
440 "-s",
441 "mystore",
442 "-sha1",
443 "80ee4c3321122916f5637522451993c2a0a4a56a",
444 "-fd",
445 "sha42",
446 "-d",
447 "The first simple app \\ demonstration",
448 "-du",
449 "https://example.com/first-app",
450 "-tr",
451 "http://freetimestamps.com",
452 "-td",
453 "sha56",
454 *additional_args,
455 tmp_path / "base_path/build/first-app/windows/app/src/First App.exe",
456 ],
457 check=True,
458 ),
459 ]
460
461
462 def test_package_msi_invalid_identity(package_command, first_app_config):
463 """Codesigning fails, along with packaging, if the identity is invalid."""
464 with pytest.raises(
465 BriefcaseCommandError,
466 match="Codesigning identify 'asdf' must be a certificate SHA-1 thumbprint",
467 ):
468 package_command.package_app(first_app_config, identity="asdf")
469
470
471 def test_package_msi_failed_sign_app(package_command, first_app_config, tmp_path):
472 """An error is raised if signtool fails for the app exe."""
473 # Mock a failure in the call to signtool.exe
474 package_command.tools.subprocess.run.side_effect = [
475 CalledProcessError(cmd=["signtool.exe"], returncode=1),
476 ]
477
478 with pytest.raises(BriefcaseCommandError, match=r"Unable to sign "):
479 package_command.package_app(
480 first_app_config,
481 identity="80ee4c3321122916f5637522451993c2a0a4a56a",
482 file_digest="sha42",
483 cert_store="mystore",
484 timestamp_url="http://freetimestamps.com",
485 timestamp_digest="sha56",
486 )
487
488 assert package_command.tools.subprocess.run.mock_calls == [
489 # Codesign app exe
490 mock.call(
491 [
492 tmp_path
493 / "windows_sdk"
494 / "bin"
495 / "81.2.1.0"
496 / "groovy"
497 / "signtool.exe",
498 "sign",
499 "-s",
500 "mystore",
501 "-sha1",
502 "80ee4c3321122916f5637522451993c2a0a4a56a",
503 "-fd",
504 "sha42",
505 "-d",
506 "The first simple app \\ demonstration",
507 "-du",
508 "https://example.com/first-app",
509 "-tr",
510 "http://freetimestamps.com",
511 "-td",
512 "sha56",
513 tmp_path / "base_path/build/first-app/windows/app/src/First App.exe",
514 ],
515 check=True,
516 )
517 ]
518
519
520 def test_package_msi_failed_compile(package_command, first_app_config, tmp_path):
521 """An error is raised if compilation failed."""
522 # Mock a failure in the call to wix.exe
523 package_command.tools.subprocess.run.side_effect = [
524 CalledProcessError(cmd=["wix.exe"], returncode=1),
525 ]
526
527 with pytest.raises(
528 BriefcaseCommandError,
529 match=r"Unable to package app first-app.",
530 ):
531 package_command.package_app(first_app_config)
532
533 assert package_command.tools.subprocess.run.mock_calls == [
534 # Compile MSI
535 mock.call(
536 [
537 tmp_path / "wix" / WIX_EXE_PATH,
538 "build",
539 "-ext",
540 tmp_path / "wix" / WIX_UI_PATH,
541 "-arch",
542 "x64",
543 "first-app.wxs",
544 "-loc",
545 "unicode.wxl",
546 "-pdbtype",
547 "none",
548 "-o",
549 tmp_path / "base_path/dist/First App-0.0.1.msi",
550 ],
551 check=True,
552 cwd=tmp_path / "base_path/build/first-app/windows/app",
553 ),
554 ]
555
556
557 def test_package_msi_failed_signing_msi(package_command, first_app_config, tmp_path):
558 """An error is raised if signtool fails for the app MSI."""
559 # Mock a failure in the call to signtool.exe
560 package_command.tools.subprocess.run.side_effect = [
561 None, # signtool.exe
562 None, # wix.exe
563 CalledProcessError(cmd=["signtool.exe"], returncode=1),
564 ]
565
566 with pytest.raises(BriefcaseCommandError, match=r"Unable to sign "):
567 package_command.package_app(
568 first_app_config,
569 identity="80ee4c3321122916f5637522451993c2a0a4a56a",
570 file_digest="sha42",
571 cert_store="mystore",
572 timestamp_url="http://freetimestamps.com",
573 timestamp_digest="sha56",
574 )
575
576 assert package_command.tools.subprocess.run.mock_calls == [
577 # Codesign app exe
578 mock.call(
579 [
580 tmp_path
581 / "windows_sdk"
582 / "bin"
583 / "81.2.1.0"
584 / "groovy"
585 / "signtool.exe",
586 "sign",
587 "-s",
588 "mystore",
589 "-sha1",
590 "80ee4c3321122916f5637522451993c2a0a4a56a",
591 "-fd",
592 "sha42",
593 "-d",
594 "The first simple app \\ demonstration",
595 "-du",
596 "https://example.com/first-app",
597 "-tr",
598 "http://freetimestamps.com",
599 "-td",
600 "sha56",
601 tmp_path / "base_path/build/first-app/windows/app/src/First App.exe",
602 ],
603 check=True,
604 ),
605 # Compile MSI
606 mock.call(
607 [
608 tmp_path / "wix" / WIX_EXE_PATH,
609 "build",
610 "-ext",
611 tmp_path / "wix" / WIX_UI_PATH,
612 "-arch",
613 "x64",
614 "first-app.wxs",
615 "-loc",
616 "unicode.wxl",
617 "-pdbtype",
618 "none",
619 "-o",
620 tmp_path / "base_path/dist/First App-0.0.1.msi",
621 ],
622 check=True,
623 cwd=tmp_path / "base_path/build/first-app/windows/app",
624 ),
625 # Codesign app MSI
626 mock.call(
627 [
628 tmp_path
629 / "windows_sdk"
630 / "bin"
631 / "81.2.1.0"
632 / "groovy"
633 / "signtool.exe",
634 "sign",
635 "-s",
636 "mystore",
637 "-sha1",
638 "80ee4c3321122916f5637522451993c2a0a4a56a",
639 "-fd",
640 "sha42",
641 "-d",
642 "The first simple app \\ demonstration",
643 "-du",
644 "https://example.com/first-app",
645 "-tr",
646 "http://freetimestamps.com",
647 "-td",
648 "sha56",
649 tmp_path / "base_path/dist/First App-0.0.1.msi",
650 ],
651 check=True,
652 ),
653 ]
654
655
656 def test_external_package_zip(
657 package_command_with_files,
658 external_first_app,
659 tmp_path,
660 ):
661 """In a ZIP package, only the binary will be code signed."""
662
663 external_first_app.packaging_format = "zip"
664
665 package_command_with_files.package_app(
666 external_first_app,
667 identity="80ee4c3321122916f5637522451993c2a0a4a56a",
668 file_digest="sha42",
669 use_local_machine=False,
670 cert_store="mystore",
671 timestamp_url="http://freetimestamps.com",
672 timestamp_digest="sha56",
673 )
674
675 assert package_command_with_files.tools.subprocess.run.mock_calls == [
676 # Codesign app exe
677 mock.call(
678 [
679 tmp_path
680 / "windows_sdk"
681 / "bin"
682 / "81.2.1.0"
683 / "groovy"
684 / "signtool.exe",
685 "sign",
686 "-s",
687 "mystore",
688 "-sha1",
689 "80ee4c3321122916f5637522451993c2a0a4a56a",
690 "-fd",
691 "sha42",
692 "-d",
693 "The first simple app \\ demonstration",
694 "-du",
695 "https://example.com/first-app",
696 "-tr",
697 "http://freetimestamps.com",
698 "-td",
699 "sha56",
700 tmp_path / "base_path/external/src/internal/app.exe",
701 ],
702 check=True,
703 ),
704 ]