vinix-ports / tests / native-layer.py
78 lines · 62 sloc · 3.0 KB · e827dd6f818964f440f1d24c8e33c30193ff416e
Raw
1"""Check that a desktop layer adds native files without replacing the vpac root."""
2import csv
3import hashlib
4import io
5from pathlib import Path
6import runpy
7import tempfile
8import unittest
9
10
11install_native_layer = runpy.run_path(
12 Path(__file__).resolve().parents[1] / "tools/stage-image.py"
13)["install_native_layer"]
14
15
16class NativeLayer(unittest.TestCase):
17 def setUp(self):
18 work = tempfile.TemporaryDirectory()
19 self.addCleanup(work.cleanup)
20 self.root = Path(work.name) / "root"
21 self.source = Path(work.name) / "native"
22 for tree in (self.root, self.source):
23 (tree / "usr/bin").mkdir(parents=True)
24 self.package_command = self.root / "usr/bin/pkg"
25 self.package_command.write_bytes(b"package-owned command")
26
27 def install(self):
28 receipt = io.StringIO()
29 install_native_layer(self.source, self.root, csv.writer(receipt, delimiter="\t"))
30 receipt.seek(0)
31 return list(csv.reader(receipt, delimiter="\t"))
32
33 def test_preserves_package_files_and_records_added_native_files(self):
34 program = self.source / "usr/bin/vinix-desktop"
35 program.write_bytes(b"native desktop")
36 program.chmod(0o755)
37 (self.source / "usr/bin/vinix-system-information").symlink_to("vinix-desktop")
38
39 rows = self.install()
40
41 self.assertEqual(self.package_command.read_bytes(), b"package-owned command")
42 self.assertEqual((self.root / "usr/bin/vinix-desktop").stat().st_mode & 0o777, 0o755)
43 self.assertEqual(rows, [
44 ["usr/bin/vinix-desktop", "file", "0755", hashlib.sha256(b"native desktop").hexdigest()],
45 ["usr/bin/vinix-system-information", "link", "", "vinix-desktop"],
46 ])
47
48 def test_refuses_to_replace_a_package_owner(self):
49 (self.source / "usr/bin/pkg").write_bytes(b"shadow")
50
51 with self.assertRaisesRegex(ValueError, "shadows a staged path"):
52 self.install()
53 self.assertEqual(self.package_command.read_bytes(), b"package-owned command")
54
55 def test_refuses_to_traverse_a_destination_symlink(self):
56 (self.root / "usr/share").symlink_to("bin")
57 (self.source / "usr/share").mkdir()
58 (self.source / "usr/share/new-file").touch()
59
60 with self.assertRaisesRegex(ValueError, "directory shadows a staged file"):
61 self.install()
62 self.assertFalse((self.root / "usr/bin/new-file").exists())
63
64 def test_refuses_paths_that_bypass_image_metadata(self):
65 (self.source / "var/lib/vpac").mkdir(parents=True)
66 with self.assertRaisesRegex(ValueError, "reserved path"):
67 self.install()
68 self.assertFalse((self.root / "var/lib/vpac").exists())
69
70 def test_refuses_links_that_leave_the_image(self):
71 (self.source / "usr/bin/vinix-desktop").symlink_to("../../../outside")
72 with self.assertRaisesRegex(ValueError, "leaves the guest root"):
73 self.install()
74 self.assertFalse((self.root / "usr/bin/vinix-desktop").is_symlink())
75
76
77if __name__ == "__main__":
78 unittest.main()
79