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 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
from subprocess import CalledProcessError
from unittest import mock
from zipfile import ZipFile
import pytest
import briefcase.platforms.windows
from briefcase.exceptions import BriefcaseCommandError
from briefcase.integrations.subprocess import Subprocess
from briefcase.integrations.windows_sdk import WindowsSDK
from briefcase.integrations.wix import WiX
from briefcase.platforms.windows.app import WindowsAppPackageCommand
from ....integrations.wix.conftest import WIX_EXE_PATH, WIX_UI_PATH
from ....utils import create_file
@pytest.fixture
def package_command(dummy_console, tmp_path):
command = WindowsAppPackageCommand(
console=dummy_console,
base_path=tmp_path / "base_path",
data_path=tmp_path / "briefcase",
)
command.tools.host_os = "Windows"
command.tools.subprocess = mock.MagicMock(spec_set=Subprocess)
command.tools.wix = WiX(command.tools, wix_home=tmp_path / "wix")
command.tools.windows_sdk = WindowsSDK(
tools=command.tools,
root_path=tmp_path / "windows_sdk",
version="81.2.1.0",
arch="groovy",
)
return command
@pytest.fixture
def package_command_with_files(package_command, tmp_path):
# Build the paths for the source and the distribution folders:
src_path = tmp_path / "base_path/build/first-app/windows/app/src"
dist_path = tmp_path / "base_path/dist"
src_path.mkdir(parents=True)
dist_path.mkdir(parents=True)
# Mock some typical folders and files in the src folder:
files = (
src_path / "First App.exe",
src_path / "python.exe",
src_path / "python3.dll",
src_path / "vcruntime140.dll",
src_path / "app/first-app/app.py",
src_path / "app/first-app/resources/__init__.py",
src_path / "app/first-app-0.0.1.dist-info/top_level.txt",
src_path / "app_packages/clr.py",
src_path / "app_packages/toga_winforms/command.py",
)
for file in files:
create_file(file, "")
return package_command
def test_package_formats(package_command):
"""Packaging formats are as expected."""
assert package_command.packaging_formats == ["msi", "zip"]
assert package_command.default_packaging_format == "msi"
def test_verify(package_command, monkeypatch):
"""Verifying on Windows creates a WiX wrapper."""
# prime Command to _not_ need Windows SDK
package_command._windows_sdk_needed = False
mock_wix_verify = mock.MagicMock(wraps=WiX.verify)
monkeypatch.setattr(
briefcase.platforms.windows.WiX,
"verify",
mock_wix_verify,
)
package_command.verify_tools()
# WiX tool was verified
mock_wix_verify.assert_called_once_with(tools=package_command.tools)
assert isinstance(package_command.tools.wix, WiX)
def test_verify_with_signing(package_command, monkeypatch):
"""Verifying on Windows creates WiX and WindowsSDK wrappers when code signing."""
# prime Command to need Windows SDK
package_command._windows_sdk_needed = True
mock_windows_sdk_verify = mock.MagicMock(wraps=WindowsSDK.verify)
monkeypatch.setattr(
briefcase.platforms.windows.WindowsSDK,
"verify",
mock_windows_sdk_verify,
)
mock_wix_verify = mock.MagicMock(wraps=WiX.verify)
monkeypatch.setattr(
briefcase.platforms.windows.WiX,
"verify",
mock_wix_verify,
)
package_command.verify_tools()
# WiX tool was verified
mock_wix_verify.assert_called_once_with(tools=package_command.tools)
assert isinstance(package_command.tools.wix, WiX)
# WindowsSDK tool was verified
mock_windows_sdk_verify.assert_called_once_with(tools=package_command.tools)
assert isinstance(package_command.tools.windows_sdk, WindowsSDK)
@pytest.mark.parametrize(
"cli_args, signing_options, is_sdk_needed",
[
([], {}, False),
(["--adhoc-sign"], {"adhoc_sign": True}, False),
(["--file-digest", "sha2000"], {"file_digest": "sha2000"}, False),
(["-i", "asdf"], {"identity": "asdf"}, True),
(["--identity", "asdf"], {"identity": "asdf"}, True),
(["--identity", "asdf"], {"identity": "asdf"}, True),
(
[
"-i",
"asdf",
"--file-digest",
"sha42",
"--use-local-machine-stores",
"--cert-store",
"mystore",
"--timestamp-url",
"http://freetimestamps.com",
"--timestamp-digest",
"sha56",
],
{
"identity": "asdf",
"file_digest": "sha42",
"use_local_machine": True,
"cert_store": "mystore",
"timestamp_url": "http://freetimestamps.com",
"timestamp_digest": "sha56",
},
True,
),
(
[
"-i",
"asdf",
"--file-digest",
"sha42",
"--cert-store",
"mystore",
"--timestamp-url",
"http://freetimestamps.com",
"--timestamp-digest",
"sha56",
],
{
"identity": "asdf",
"file_digest": "sha42",
"cert_store": "mystore",
"timestamp_url": "http://freetimestamps.com",
"timestamp_digest": "sha56",
},
True,
),
],
)
def test_parse_options(package_command, cli_args, signing_options, is_sdk_needed):
"""Command line arguments are parsed as expected; Windows SDK is required if an
identity is specified."""
default_options = {
"identity": None,
"file_digest": "sha256",
"use_local_machine": False,
"cert_store": "My",
"timestamp_url": "http://timestamp.digicert.com",
"timestamp_digest": "sha256",
"adhoc_sign": False,
"packaging_format": "msi",
"update": False,
}
expected_options = {**default_options, **signing_options}
options, overrides = package_command.parse_options(extra=cli_args)
assert options == expected_options
assert overrides == {}
assert package_command._windows_sdk_needed is is_sdk_needed
@pytest.mark.parametrize(
"kwargs, external",
[
# Default behavior (adhoc signing, internal app)
({}, False),
# External app mode makes no difference when packaging an MSI, because it's
# entirely implemented in `briefcase create`.
({}, True),
# Explicit adhoc signing
({"adhoc_sign": True}, False),
],
)
def test_package_msi(
package_command, first_app_config, external_first_app, tmp_path, kwargs, external
):
"""A Windows app can be packaged as an MSI."""
package_command.package_app(
external_first_app if external else first_app_config,
**kwargs,
)
assert package_command.tools.subprocess.run.mock_calls == [
# Compile MSI
mock.call(
[
tmp_path / "wix" / WIX_EXE_PATH,
"build",
"-ext",
tmp_path / "wix" / WIX_UI_PATH,
"-arch",
"x64",
"first-app.wxs",
"-loc",
"unicode.wxl",
"-o",
tmp_path / "base_path/dist/First App-0.0.1.msi",
],
check=True,
cwd=tmp_path / "base_path/build/first-app/windows/app",
),
]
@pytest.mark.parametrize(
"kwargs",
[
{}, # Default behavior (adhoc signing)
{"adhoc_sign": True}, # Explicit adhoc signing
],
)
def test_package_zip(package_command_with_files, first_app_config, kwargs, tmp_path):
"""A Windows app can be packaged as a zip file."""
first_app_config.packaging_format = "zip"
package_command_with_files.package_app(first_app_config, **kwargs)
# No signing was performed
assert package_command_with_files.tools.subprocess.run.mock_calls == []
archive_file = tmp_path / "base_path/dist/First App-0.0.1.zip"
source_folders_and_files = (
"app/",
"app/first-app/",
"app/first-app/resources/",
"app/first-app-0.0.1.dist-info/",
"app_packages/",
"app_packages/toga_winforms/",
"First App.exe",
"python.exe",
"python3.dll",
"vcruntime140.dll",
"app/first-app/app.py",
"app/first-app/resources/__init__.py",
"app/first-app-0.0.1.dist-info/top_level.txt",
"app_packages/clr.py",
"app_packages/toga_winforms/command.py",
)
# The zip file exists
assert archive_file.exists()
# Check content of zip file
with ZipFile(archive_file) as archive:
root = "First App-0.0.1/"
# All folders and files in zip are from source
for name in archive.namelist():
# name.removeprefix(f'{root}') will only work in Python > 3.8
assert name[len(root) :] in source_folders_and_files
# All files from source are in zip
for file in source_folders_and_files:
assert f"{root}{file}" in archive.namelist()
@pytest.mark.parametrize(
"use_local_machine, additional_args",
[(False, []), (True, ["-sm"])],
)
def test_package_msi_with_codesigning(
package_command,
first_app_config,
tmp_path,
use_local_machine,
additional_args,
):
"""A Windows app can be packaged as an MSI and code signed."""
package_command.package_app(
first_app_config,
identity="80ee4c3321122916f5637522451993c2a0a4a56a",
file_digest="sha42",
use_local_machine=use_local_machine,
cert_store="mystore",
timestamp_url="http://freetimestamps.com",
timestamp_digest="sha56",
)
assert package_command.tools.subprocess.run.mock_calls == [
# Codesign app exe
mock.call(
[
tmp_path
/ "windows_sdk"
/ "bin"
/ "81.2.1.0"
/ "groovy"
/ "signtool.exe",
"sign",
"-s",
"mystore",
"-sha1",
"80ee4c3321122916f5637522451993c2a0a4a56a",
"-fd",
"sha42",
"-d",
"The first simple app \\ demonstration",
"-du",
"https://example.com/first-app",
"-tr",
"http://freetimestamps.com",
"-td",
"sha56",
]
+ additional_args
+ [
tmp_path
/ "base_path"
/ "build"
/ "first-app"
/ "windows"
/ "app"
/ "src"
/ "First App.exe"
],
check=True,
),
# Compile MSI
mock.call(
[
tmp_path / "wix" / WIX_EXE_PATH,
"build",
"-ext",
tmp_path / "wix" / WIX_UI_PATH,
"-arch",
"x64",
"first-app.wxs",
"-loc",
"unicode.wxl",
"-o",
tmp_path / "base_path/dist/First App-0.0.1.msi",
],
check=True,
cwd=tmp_path / "base_path/build/first-app/windows/app",
),
# Codesign app MSI
mock.call(
[
tmp_path
/ "windows_sdk"
/ "bin"
/ "81.2.1.0"
/ "groovy"
/ "signtool.exe",
"sign",
"-s",
"mystore",
"-sha1",
"80ee4c3321122916f5637522451993c2a0a4a56a",
"-fd",
"sha42",
"-d",
"The first simple app \\ demonstration",
"-du",
"https://example.com/first-app",
"-tr",
"http://freetimestamps.com",
"-td",
"sha56",
]
+ additional_args
+ [tmp_path / "base_path/dist/First App-0.0.1.msi"],
check=True,
),
]
@pytest.mark.parametrize(
"use_local_machine, additional_args",
[(False, []), (True, ["-sm"])],
)
def test_package_zip_with_codesigning(
package_command_with_files,
first_app_config,
tmp_path,
use_local_machine,
additional_args,
):
"""In a ZIP package, only the binary will be code signed."""
first_app_config.packaging_format = "zip"
package_command_with_files.package_app(
first_app_config,
identity="80ee4c3321122916f5637522451993c2a0a4a56a",
file_digest="sha42",
use_local_machine=use_local_machine,
cert_store="mystore",
timestamp_url="http://freetimestamps.com",
timestamp_digest="sha56",
)
assert package_command_with_files.tools.subprocess.run.mock_calls == [
# Codesign app exe
mock.call(
[
tmp_path
/ "windows_sdk"
/ "bin"
/ "81.2.1.0"
/ "groovy"
/ "signtool.exe",
"sign",
"-s",
"mystore",
"-sha1",
"80ee4c3321122916f5637522451993c2a0a4a56a",
"-fd",
"sha42",
"-d",
"The first simple app \\ demonstration",
"-du",
"https://example.com/first-app",
"-tr",
"http://freetimestamps.com",
"-td",
"sha56",
]
+ additional_args
+ [
tmp_path / "base_path/build/first-app/windows/app/src/First App.exe",
],
check=True,
),
]
def test_package_msi_invalid_identity(package_command, first_app_config):
"""Codesigning fails, along with packaging, if the identity is invalid."""
with pytest.raises(
BriefcaseCommandError,
match="Codesigning identify 'asdf' must be a certificate SHA-1 thumbprint",
):
package_command.package_app(first_app_config, identity="asdf")
def test_package_msi_failed_sign_app(package_command, first_app_config, tmp_path):
"""An error is raised if signtool fails for the app exe."""
# Mock a failure in the call to signtool.exe
package_command.tools.subprocess.run.side_effect = [
CalledProcessError(cmd=["signtool.exe"], returncode=1),
]
with pytest.raises(BriefcaseCommandError, match=r"Unable to sign "):
package_command.package_app(
first_app_config,
identity="80ee4c3321122916f5637522451993c2a0a4a56a",
file_digest="sha42",
cert_store="mystore",
timestamp_url="http://freetimestamps.com",
timestamp_digest="sha56",
)
assert package_command.tools.subprocess.run.mock_calls == [
# Codesign app exe
mock.call(
[
tmp_path
/ "windows_sdk"
/ "bin"
/ "81.2.1.0"
/ "groovy"
/ "signtool.exe",
"sign",
"-s",
"mystore",
"-sha1",
"80ee4c3321122916f5637522451993c2a0a4a56a",
"-fd",
"sha42",
"-d",
"The first simple app \\ demonstration",
"-du",
"https://example.com/first-app",
"-tr",
"http://freetimestamps.com",
"-td",
"sha56",
tmp_path / "base_path/build/first-app/windows/app/src/First App.exe",
],
check=True,
)
]
def test_package_msi_failed_compile(package_command, first_app_config, tmp_path):
"""An error is raised if compilation failed."""
# Mock a failure in the call to wix.exe
package_command.tools.subprocess.run.side_effect = [
CalledProcessError(cmd=["wix.exe"], returncode=1),
]
with pytest.raises(
BriefcaseCommandError,
match=r"Unable to package app first-app.",
):
package_command.package_app(first_app_config)
assert package_command.tools.subprocess.run.mock_calls == [
# Compile MSI
mock.call(
[
tmp_path / "wix" / WIX_EXE_PATH,
"build",
"-ext",
tmp_path / "wix" / WIX_UI_PATH,
"-arch",
"x64",
"first-app.wxs",
"-loc",
"unicode.wxl",
"-o",
tmp_path / "base_path/dist/First App-0.0.1.msi",
],
check=True,
cwd=tmp_path / "base_path/build/first-app/windows/app",
),
]
def test_package_msi_failed_signing_msi(package_command, first_app_config, tmp_path):
"""An error is raised if signtool fails for the app MSI."""
# Mock a failure in the call to signtool.exe
package_command.tools.subprocess.run.side_effect = [
None, # signtool.exe
None, # wix.exe
CalledProcessError(cmd=["signtool.exe"], returncode=1),
]
with pytest.raises(BriefcaseCommandError, match=r"Unable to sign "):
package_command.package_app(
first_app_config,
identity="80ee4c3321122916f5637522451993c2a0a4a56a",
file_digest="sha42",
cert_store="mystore",
timestamp_url="http://freetimestamps.com",
timestamp_digest="sha56",
)
assert package_command.tools.subprocess.run.mock_calls == [
# Codesign app exe
mock.call(
[
tmp_path
/ "windows_sdk"
/ "bin"
/ "81.2.1.0"
/ "groovy"
/ "signtool.exe",
"sign",
"-s",
"mystore",
"-sha1",
"80ee4c3321122916f5637522451993c2a0a4a56a",
"-fd",
"sha42",
"-d",
"The first simple app \\ demonstration",
"-du",
"https://example.com/first-app",
"-tr",
"http://freetimestamps.com",
"-td",
"sha56",
tmp_path / "base_path/build/first-app/windows/app/src/First App.exe",
],
check=True,
),
# Compile MSI
mock.call(
[
tmp_path / "wix" / WIX_EXE_PATH,
"build",
"-ext",
tmp_path / "wix" / WIX_UI_PATH,
"-arch",
"x64",
"first-app.wxs",
"-loc",
"unicode.wxl",
"-o",
tmp_path / "base_path/dist/First App-0.0.1.msi",
],
check=True,
cwd=tmp_path / "base_path/build/first-app/windows/app",
),
# Codesign app MSI
mock.call(
[
tmp_path
/ "windows_sdk"
/ "bin"
/ "81.2.1.0"
/ "groovy"
/ "signtool.exe",
"sign",
"-s",
"mystore",
"-sha1",
"80ee4c3321122916f5637522451993c2a0a4a56a",
"-fd",
"sha42",
"-d",
"The first simple app \\ demonstration",
"-du",
"https://example.com/first-app",
"-tr",
"http://freetimestamps.com",
"-td",
"sha56",
tmp_path / "base_path/dist/First App-0.0.1.msi",
],
check=True,
),
]
def test_external_package_zip(
package_command_with_files,
external_first_app,
tmp_path,
):
"""In a ZIP package, only the binary will be code signed."""
external_first_app.packaging_format = "zip"
package_command_with_files.package_app(
external_first_app,
identity="80ee4c3321122916f5637522451993c2a0a4a56a",
file_digest="sha42",
use_local_machine=False,
cert_store="mystore",
timestamp_url="http://freetimestamps.com",
timestamp_digest="sha56",
)
assert package_command_with_files.tools.subprocess.run.mock_calls == [
# Codesign app exe
mock.call(
[
tmp_path
/ "windows_sdk"
/ "bin"
/ "81.2.1.0"
/ "groovy"
/ "signtool.exe",
"sign",
"-s",
"mystore",
"-sha1",
"80ee4c3321122916f5637522451993c2a0a4a56a",
"-fd",
"sha42",
"-d",
"The first simple app \\ demonstration",
"-du",
"https://example.com/first-app",
"-tr",
"http://freetimestamps.com",
"-td",
"sha56",
]
+ [
tmp_path / "base_path/external/src/internal/app.exe",
],
check=True,
),
]
|