124 lines
3.9 KiB
Python
124 lines
3.9 KiB
Python
"""Installed software on Windows: the machine-wide Uninstall keys (NetOrk/netork#2890).
|
|
|
|
One script reads the native and the 32-bit (``WOW6432Node``) Uninstall key of HKLM; a
|
|
fake transport answers it from tests/fixtures/synthetic/packages.json. The fixture holds
|
|
the cases the issue names: 7-Zip installed both as a 64-bit and a 32-bit program, a
|
|
``SystemComponent`` entry, and an update listed under its product (``ParentKeyName``).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from napalm_windows import WindowsDriver
|
|
from napalm_windows import packages as mod
|
|
|
|
FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "packages.json"
|
|
|
|
|
|
class FakeTransport:
|
|
def __init__(self, answer: str) -> None:
|
|
self.answer = answer
|
|
self.sent: list[str] = []
|
|
self.is_open = True
|
|
|
|
def run(self, script: str) -> str:
|
|
self.sent.append(script)
|
|
return self.answer
|
|
|
|
|
|
def _reading() -> list[dict[str, Any]]:
|
|
return json.loads(FIXTURE.read_text())
|
|
|
|
|
|
def _driver(reading: Any = None) -> WindowsDriver:
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
d._transport = FakeTransport(json.dumps(_reading() if reading is None else reading))
|
|
return d
|
|
|
|
|
|
def _packages(reading: Any = None) -> list[Any]:
|
|
return list(_driver(reading).get_packages())
|
|
|
|
|
|
def test_a_program_reads_as_a_package():
|
|
git = next(p for p in _packages() if p["name"] == "Git")
|
|
assert git == {
|
|
"name": "Git",
|
|
"version": "2.43.0",
|
|
"installed": True,
|
|
"description": "",
|
|
"size": 309000 * 1024,
|
|
"source": "exe",
|
|
"publisher": "The Git Development Community",
|
|
"architecture": "x64",
|
|
}
|
|
|
|
|
|
def test_a_windows_installer_package_is_msi():
|
|
acrobat = next(p for p in _packages() if p["name"] == "Adobe Acrobat Reader DC")
|
|
assert acrobat["source"] == "msi"
|
|
assert acrobat["architecture"] == "x86"
|
|
|
|
|
|
def test_a_32_and_a_64_bit_install_of_one_program_stay_two_packages():
|
|
zips = [p for p in _packages() if p["name"].startswith("7-Zip")]
|
|
assert sorted(p["architecture"] for p in zips) == ["x64", "x86"]
|
|
|
|
|
|
def test_system_components_updates_and_unnamed_keys_are_no_packages():
|
|
names = [p["name"] for p in _packages()]
|
|
assert "Office 16 Click-to-Run Extensibility Component" not in names
|
|
assert "Security Update for Windows Server (KB5034441)" not in names
|
|
assert "" not in names
|
|
assert len(names) == 5
|
|
|
|
|
|
def test_packages_come_sorted_by_name():
|
|
names = [p["name"] for p in _packages()]
|
|
assert names == sorted(names, key=str.lower)
|
|
|
|
|
|
def test_a_missing_size_is_zero():
|
|
reading = _reading()
|
|
reading[5]["size"] = ""
|
|
assert next(p for p in _packages(reading) if p["name"] == "Git")["size"] == 0
|
|
|
|
|
|
def test_a_single_package_survives_powershells_unwrapping():
|
|
assert [p["name"] for p in _packages(_reading()[5])] == ["Git"]
|
|
|
|
|
|
def test_a_host_with_nothing_installed_has_no_packages():
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
d._transport = FakeTransport("[]")
|
|
assert d.get_packages() == []
|
|
|
|
|
|
def test_one_script_reads_both_machine_wide_keys_and_no_users_hive():
|
|
d = _driver()
|
|
d.get_packages()
|
|
(script,) = d._transport.sent # type: ignore[union-attr]
|
|
assert script == mod._PS_PACKAGES
|
|
assert r"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" in script
|
|
assert r"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" in script
|
|
# The connecting account's own hive says nothing about the host.
|
|
assert "HKCU" not in script
|
|
assert "winget" not in script
|
|
|
|
|
|
def test_installing_and_removing_stay_not_offered():
|
|
assert not hasattr(WindowsDriver, "install_package")
|
|
assert not hasattr(WindowsDriver, "uninstall_package")
|
|
|
|
|
|
@pytest.mark.parametrize("answer", ["", "null"])
|
|
def test_an_empty_answer_has_no_packages(answer):
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
d._transport = FakeTransport(answer)
|
|
assert d.get_packages() == []
|