60 lines
2.6 KiB
Python
60 lines
2.6 KiB
Python
import importlib.util
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SPEC = importlib.util.spec_from_file_location("rmm_linux_install", ROOT / "deploy/linux/install.py")
|
|
assert SPEC is not None and SPEC.loader is not None
|
|
installer = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(installer)
|
|
|
|
|
|
class InstallationTests(unittest.TestCase):
|
|
def test_create_only_staging_and_no_service_activation(self):
|
|
with tempfile.TemporaryDirectory(prefix="rmm-package-test-") as temporary:
|
|
path = Path(temporary)
|
|
binary = path / "agent"
|
|
data = bytearray(64)
|
|
data[:6] = b"\x7fELF\x02\x01"
|
|
data[18:20] = b"\x3e\x00"
|
|
binary.write_bytes(data)
|
|
ca = path / "ca.pem"
|
|
ca.write_bytes(b"-----BEGIN CERTIFICATE-----\ntest public fixture\n-----END CERTIFICATE-----\n")
|
|
stage = path / "stage"
|
|
installer.install(stage, binary, ca, binary, False)
|
|
self.assertTrue((stage / "usr/local/libexec/rmm-agent").is_file())
|
|
self.assertFalse((stage / "var/lib/rmm-agent").exists())
|
|
self.assertFalse((stage / "etc/rmm/helper.json").exists())
|
|
self.assertFalse((stage / "etc/systemd/system/multi-user.target.wants").exists())
|
|
with self.assertRaises(ValueError):
|
|
installer.install(stage, binary, ca, binary, False)
|
|
with self.assertRaises(ValueError):
|
|
installer.install(Path("/"), binary, ca, None, False)
|
|
|
|
def test_rejects_private_keys_and_symlink_directories(self):
|
|
with tempfile.TemporaryDirectory(prefix="rmm-package-guard-") as temporary:
|
|
path = Path(temporary)
|
|
root = path / "root"
|
|
root.mkdir()
|
|
(root / "usr").symlink_to(path)
|
|
with self.assertRaises(ValueError):
|
|
installer.directory(root / "usr", 0o755)
|
|
artifact = path / "not-elf"
|
|
artifact.write_bytes(b"not an executable")
|
|
with self.assertRaises(ValueError):
|
|
installer.elf(artifact)
|
|
data = bytearray(64)
|
|
data[:6] = b"\x7fELF\x02\x01"
|
|
data[18:20] = b"\x3e\x00"
|
|
artifact.write_bytes(data)
|
|
ca = path / "private.pem"
|
|
ca.write_bytes(b"-----BEGIN CERTIFICATE-----\n-----BEGIN PRIVATE KEY-----\n")
|
|
with self.assertRaises(ValueError):
|
|
installer.install(path / "no-install", artifact, ca, None, False)
|
|
self.assertFalse((path / "no-install").exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|