| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Unit tests for zxdb-cli session directory and UDS path helpers. | |
| Covers runtime base directory precedence, Linux AF_UNIX sun_path length bounds, | |
| ownership and mode-0700 enforcement on XDG and /tmp directories, build-directory | |
| and session-ID hashing, session discovery, and multi-threaded lock serialization | |
| across socket setup and empty directory teardown. | |
| """ | |
| import concurrent.futures | |
| import errno | |
| import os | |
| import pathlib | |
| import socket | |
| import stat | |
| import tempfile | |
| import threading | |
| import unittest | |
| from unittest import mock | |
| from shared import session | |
| class TestSessionResolution(unittest.TestCase): | |
| """Verifies path resolution, security checks, and locking in shared.session.""" | |
| def test_runtime_base_dir_precedence_and_fallbacks(self) -> None: | |
| """Verifies env precedence, relative-path handling, and sun_path bounds.""" | |
| # Explicit ZXDB_CLI_RUNTIME_DIR wins over XDG_RUNTIME_DIR. | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": "/custom/runtime", | |
| "XDG_RUNTIME_DIR": "/xdg/runtime", | |
| }, | |
| clear=True, | |
| ): | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path("/custom/runtime"), | |
| ) | |
| # Blank overrides are treated as unset and fall through to XDG. | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": " ", | |
| "XDG_RUNTIME_DIR": "/xdg/runtime", | |
| }, | |
| clear=True, | |
| ): | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path("/xdg/runtime/zxdb-cli"), | |
| ) | |
| # scripts/hermetic-env strips XDG_RUNTIME_DIR and exports | |
| # FUCHSIA_XDG_RUNTIME_DIR_FROM_FX instead. | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "XDG_RUNTIME_DIR": " \t ", | |
| "FUCHSIA_XDG_RUNTIME_DIR_FROM_FX": "/xdg/from_fx", | |
| }, | |
| clear=True, | |
| ): | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path("/xdg/from_fx/zxdb-cli"), | |
| ) | |
| # Relative XDG paths violate the XDG spec and fall through rather than | |
| # anchoring sockets to the caller's working directory. | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "XDG_RUNTIME_DIR": "relative/xdg", | |
| "FUCHSIA_XDG_RUNTIME_DIR_FROM_FX": "/xdg/from_fx", | |
| }, | |
| clear=True, | |
| ): | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path("/xdg/from_fx/zxdb-cli"), | |
| ) | |
| with mock.patch.dict( | |
| os.environ, | |
| {"XDG_RUNTIME_DIR": "relative/xdg"}, | |
| clear=True, | |
| ): | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path(f"/tmp/zxdb-cli-{os.getuid()}"), | |
| ) | |
| # Overlong XDG paths silently fall back to /tmp/zxdb-cli-<uid> | |
| # (or FUCHSIA_XDG_RUNTIME_DIR_FROM_FX when valid) so deep | |
| # session directories don't break AF_UNIX socket creation. | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "XDG_RUNTIME_DIR": "/" + "x" * 70, | |
| "FUCHSIA_XDG_RUNTIME_DIR_FROM_FX": "/xdg/from_fx", | |
| }, | |
| clear=True, | |
| ): | |
| self.assertEqual( | |
| session.get_valid_xdg_runtime_dir(), | |
| pathlib.Path("/xdg/from_fx"), | |
| ) | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path("/xdg/from_fx/zxdb-cli"), | |
| ) | |
| with mock.patch.dict( | |
| os.environ, | |
| {"XDG_RUNTIME_DIR": "/" + "x" * 70}, | |
| clear=True, | |
| ): | |
| self.assertIsNone(session.get_valid_xdg_runtime_dir()) | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path(f"/tmp/zxdb-cli-{os.getuid()}"), | |
| ) | |
| # Explicit ZXDB_CLI_RUNTIME_DIR overrides fail fast when relative, | |
| # contain '..' segments, or are too long rather than silently | |
| # redirecting test sockets to /tmp. | |
| for invalid_override in ( | |
| "relative/runtime", | |
| "/custom/../runtime", | |
| "/" + "x" * 70, | |
| ): | |
| with ( | |
| self.subTest(invalid_override=invalid_override), | |
| mock.patch.dict( | |
| os.environ, | |
| {"ZXDB_CLI_RUNTIME_DIR": invalid_override}, | |
| clear=True, | |
| ), | |
| ): | |
| with self.assertRaises(ValueError): | |
| session.get_runtime_base_dir() | |
| # XDG paths containing '..' segments are rejected and fall back. | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "XDG_RUNTIME_DIR": "/xdg/../runtime", | |
| "FUCHSIA_XDG_RUNTIME_DIR_FROM_FX": "/xdg/from_fx", | |
| }, | |
| clear=True, | |
| ): | |
| self.assertEqual( | |
| session.get_valid_xdg_runtime_dir(), | |
| pathlib.Path("/xdg/from_fx"), | |
| ) | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path("/xdg/from_fx/zxdb-cli"), | |
| ) | |
| with mock.patch.dict( | |
| os.environ, | |
| {"XDG_RUNTIME_DIR": "/xdg/../runtime"}, | |
| clear=True, | |
| ): | |
| self.assertIsNone(session.get_valid_xdg_runtime_dir()) | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path(f"/tmp/zxdb-cli-{os.getuid()}"), | |
| ) | |
| with mock.patch.dict(os.environ, {}, clear=True): | |
| self.assertEqual( | |
| session.get_runtime_base_dir(), | |
| pathlib.Path(f"/tmp/zxdb-cli-{os.getuid()}"), | |
| ) | |
| def test_tmp_runtime_dir_rejects_symlink_and_wrong_owner(self) -> None: | |
| """Verifies write-path checks reject symlinks, wrong UIDs, and group/other bits.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| real_target = tmp_root / "real_target" | |
| real_target.mkdir() | |
| symlink_base = tmp_root / "zxdb-cli-symlink" | |
| symlink_base.symlink_to(real_target) | |
| with ( | |
| mock.patch.object( | |
| session, | |
| "_tmp_runtime_base_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=symlink_base, | |
| ), | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir(ensure_exists=True) | |
| # Wrap session.os rather than mutating global os so pathlib and | |
| # tempfile still call real os functions while UID comparison fails. | |
| owned_dir = tmp_root / "zxdb-cli-wrong-uid" | |
| real_uid = os.getuid() | |
| with ( | |
| mock.patch.object( | |
| session, | |
| "_tmp_runtime_base_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=owned_dir, | |
| ), | |
| mock.patch.object( | |
| session, "os", wraps=os, spec_set=True | |
| ) as mock_os, | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| mock_os.getuid = mock.create_autospec( | |
| os.getuid, spec_set=True, return_value=real_uid + 1 | |
| ) | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir(ensure_exists=True) | |
| permissive_dir = tmp_root / "zxdb-cli-permissive" | |
| permissive_dir.mkdir(mode=0o755) | |
| permissive_dir.chmod(0o755) | |
| with ( | |
| mock.patch.object( | |
| session, | |
| "_tmp_runtime_base_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=permissive_dir, | |
| ), | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir(ensure_exists=True) | |
| # XDG_RUNTIME_DIR pointing at a shared directory such as /tmp must | |
| # enforce the same symlink and 0700 checks on <xdg>/zxdb-cli. | |
| xdg_root = tmp_root / "shared_xdg" | |
| xdg_root.mkdir(mode=0o755) | |
| xdg_sub = xdg_root / "zxdb-cli" | |
| xdg_sub.symlink_to(real_target) | |
| with mock.patch.dict( | |
| os.environ, {"XDG_RUNTIME_DIR": str(xdg_root)}, clear=True | |
| ): | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir(ensure_exists=True) | |
| xdg_sub.unlink() | |
| xdg_sub.mkdir(mode=0o755) | |
| xdg_sub.chmod(0o755) | |
| with mock.patch.dict( | |
| os.environ, {"XDG_RUNTIME_DIR": str(xdg_root)}, clear=True | |
| ): | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir() | |
| def test_tmp_runtime_dir_read_and_cleanup_when_missing_or_valid( | |
| self, | |
| ) -> None: | |
| """Verifies read-only queries and cleanup don't create missing directories.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| missing_dir = tmp_root / "zxdb-cli-missing" | |
| with ( | |
| mock.patch.object( | |
| session, | |
| "_tmp_runtime_base_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=missing_dir, | |
| ), | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| runtime_dir = session.get_build_dir_runtime_dir() | |
| self.assertEqual(runtime_dir.parent, missing_dir) | |
| self.assertFalse(missing_dir.exists()) | |
| self.assertSequenceEqual(session.list_session_ids(), ()) | |
| sock = session.get_daemon_socket_path("123") | |
| session.cleanup_session_dir(sock) | |
| self.assertFalse(missing_dir.exists()) | |
| valid_dir = tmp_root / "zxdb-cli-valid" | |
| valid_dir.mkdir(mode=0o700) | |
| valid_dir.chmod(0o700) | |
| with ( | |
| mock.patch.object( | |
| session, | |
| "_tmp_runtime_base_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=valid_dir, | |
| ), | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| self.assertEqual( | |
| session.get_build_dir_runtime_dir().parent, valid_dir | |
| ) | |
| self.assertSequenceEqual(session.list_session_ids(), ()) | |
| _ = session.get_daemon_socket_path("123") | |
| def test_tmp_runtime_dir_read_and_cleanup_rejects_untrusted(self) -> None: | |
| """Verifies read-only queries and cleanup still reject pre-created untrusted dirs.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| real_target = tmp_root / "real_target" | |
| real_target.mkdir() | |
| symlink_base = tmp_root / "zxdb-cli-symlink-read" | |
| symlink_base.symlink_to(real_target) | |
| permissive_dir = tmp_root / "zxdb-cli-permissive-read" | |
| permissive_dir.mkdir(mode=0o755) | |
| permissive_dir.chmod(0o755) | |
| for untrusted in (symlink_base, permissive_dir): | |
| with ( | |
| self.subTest(untrusted=untrusted.name), | |
| mock.patch.object( | |
| session, | |
| "_tmp_runtime_base_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=untrusted, | |
| ), | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir() | |
| with self.assertRaises(PermissionError): | |
| session.list_session_ids() | |
| with self.assertRaises(PermissionError): | |
| session.get_daemon_socket_path("123") | |
| with self.assertRaises(PermissionError): | |
| session.cleanup_session_dir( | |
| untrusted / "default" / "123" / "daemon.sock" | |
| ) | |
| owned_dir = tmp_root / "zxdb-cli-wrong-uid-read" | |
| owned_dir.mkdir(mode=0o700) | |
| owned_dir.chmod(0o700) | |
| real_uid = os.getuid() | |
| with ( | |
| mock.patch.object( | |
| session, | |
| "_tmp_runtime_base_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=owned_dir, | |
| ), | |
| mock.patch.object( | |
| session, "os", wraps=os, spec_set=True | |
| ) as mock_os, | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| mock_os.getuid = mock.create_autospec( | |
| os.getuid, spec_set=True, return_value=real_uid + 1 | |
| ) | |
| with self.assertRaises(PermissionError): | |
| session.list_session_ids() | |
| with self.assertRaises(PermissionError): | |
| session.cleanup_session_dir( | |
| owned_dir / "default" / "123" / "daemon.sock" | |
| ) | |
| def test_build_dir_hash_normalization_and_relative_fuchsia_dir( | |
| self, | |
| ) -> None: | |
| """Verifies relative build dirs and symlinks hash to the same canonical key.""" | |
| with mock.patch.dict(os.environ, {}, clear=True): | |
| self.assertEqual(session.get_build_dir_hash(), "default") | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| workspace = pathlib.Path(tmp_dir) / "fuchsia" | |
| build_dir = workspace / "out" / "default" | |
| subdir = workspace / "scripts" / "debug" | |
| build_dir.mkdir(parents=True) | |
| subdir.mkdir(parents=True) | |
| symlink_build = workspace / "out" / "current" | |
| symlink_build.symlink_to(build_dir) | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "FUCHSIA_BUILD_DIR_FROM_FX": " ", | |
| "FUCHSIA_BUILD_DIR": f"{build_dir}/", | |
| }, | |
| clear=True, | |
| ): | |
| abs_hash = session.get_build_dir_hash() | |
| self.assertEqual(len(abs_hash), 12) | |
| # Anchor relative build paths against FUCHSIA_DIR rather than cwd so | |
| # invocations from subdirectories resolve the same daemon runtime. | |
| original_cwd = os.getcwd() | |
| try: | |
| os.chdir(subdir) | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "FUCHSIA_DIR": str(workspace), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "out/default", | |
| }, | |
| clear=True, | |
| ): | |
| rel_hash = session.get_build_dir_hash() | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "FUCHSIA_DIR": str(workspace), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "out/current", | |
| }, | |
| clear=True, | |
| ): | |
| symlink_hash = session.get_build_dir_hash() | |
| finally: | |
| os.chdir(original_cwd) | |
| self.assertEqual(abs_hash, rel_hash) | |
| self.assertEqual(abs_hash, symlink_hash) | |
| def test_session_id_normalization_and_sanitization(self) -> None: | |
| """Verifies reserved-name rejection and collision-free hash truncation.""" | |
| with ( | |
| tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir, | |
| mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": tmp_dir, | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ), | |
| ): | |
| self.assertIsNone(session.normalize_session_id(None)) | |
| self.assertIsNone(session.normalize_session_id("")) | |
| self.assertIsNone(session.normalize_session_id(" ")) | |
| self.assertEqual(session.normalize_session_id(" 12345 "), "12345") | |
| for reserved in ( | |
| "default", | |
| "Default", | |
| ".", | |
| "..", | |
| ".lock", | |
| ".foo", | |
| "./foo", | |
| ): | |
| with self.subTest(reserved=reserved): | |
| with self.assertRaises(ValueError): | |
| session.normalize_session_id(reserved) | |
| with self.assertRaises(ValueError): | |
| session.get_daemon_socket_path(reserved) | |
| canonical_slash = session.normalize_session_id( | |
| "session/with:slashes" | |
| ) | |
| self.assertIsNotNone(canonical_slash) | |
| assert canonical_slash is not None | |
| self.assertEqual(len(canonical_slash), 24) | |
| self.assertTrue(canonical_slash.startswith("session_with_sl_")) | |
| sock_path = session.get_daemon_socket_path("session/with:slashes") | |
| self.assertEqual(sock_path.name, "daemon.sock") | |
| self.assertEqual(sock_path.parent.name, canonical_slash) | |
| # Distinct unsafe inputs that map to the same underscore prefix still | |
| # produce distinct canonical IDs via their trailing SHA-256 digest. | |
| slash_id = session.normalize_session_id("a/b") | |
| colon_id = session.normalize_session_id("a:b") | |
| underscore_id = session.normalize_session_id("a_b") | |
| self.assertEqual(len({slash_id, colon_id, underscore_id}), 3) | |
| long_a = "a" * 30 + ":" | |
| long_b = "a" * 30 + "/" | |
| sanitized_a = session.get_daemon_socket_path(long_a).parent.name | |
| sanitized_b = session.get_daemon_socket_path(long_b).parent.name | |
| self.assertEqual(len(sanitized_a), 24) | |
| self.assertEqual(len(sanitized_b), 24) | |
| self.assertTrue(sanitized_a.startswith("a" * 15 + "_")) | |
| self.assertNotEqual(sanitized_a, sanitized_b) | |
| self.assertEqual( | |
| session.get_default_session_id(), str(os.getsid(0)) | |
| ) | |
| with self.assertRaises(ValueError): | |
| session.get_daemon_socket_path(" ") | |
| def test_merge_session_ids(self) -> None: | |
| """Verifies top-level and subcommand --session flags merge or reject conflicts.""" | |
| self.assertIsNone(session.merge_session_ids(None, None)) | |
| self.assertEqual(session.merge_session_ids("1111", None), "1111") | |
| self.assertEqual(session.merge_session_ids(None, "2222"), "2222") | |
| self.assertEqual(session.merge_session_ids("1111", "1111"), "1111") | |
| with self.assertRaisesRegex(ValueError, "Conflicting session IDs"): | |
| session.merge_session_ids("1111", "2222") | |
| def test_prepare_list_and_cleanup_session_dirs(self) -> None: | |
| """Verifies socket preparation, session discovery, and empty directory teardown.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": tmp_dir, | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| # prepare_session_socket tightens permissions to 0700 and clears | |
| # stale socket files while holding .lock. | |
| stale_created = session.get_daemon_socket_path("created") | |
| stale_created.parent.mkdir( | |
| parents=True, exist_ok=True, mode=0o755 | |
| ) | |
| stale_created.parent.chmod(0o755) | |
| stale_created.write_text("stale") | |
| with session.prepare_session_socket("created") as prepared_sock: | |
| self.assertEqual(prepared_sock, stale_created) | |
| self.assertLessEqual(len(str(prepared_sock)), 107) | |
| self.assertTrue(prepared_sock.parent.is_dir()) | |
| self.assertFalse(prepared_sock.exists()) | |
| self.assertEqual( | |
| stat.S_IMODE(prepared_sock.parent.stat().st_mode), 0o700 | |
| ) | |
| # Symlinked, dangling-symlinked, and regular-file session paths | |
| # raise PermissionError instead of following symlinks or leaking | |
| # FileExistsError from mkdir. | |
| build_runtime_dir = session.get_build_dir_runtime_dir( | |
| ensure_exists=True | |
| ) | |
| outside_dir = pathlib.Path(tmp_dir) / "outside_dir" | |
| outside_dir.mkdir(mode=0o700) | |
| (build_runtime_dir / "linked-sess").symlink_to(outside_dir) | |
| (build_runtime_dir / "dangling-sess").symlink_to( | |
| pathlib.Path(tmp_dir) / "nonexistent_target" | |
| ) | |
| (build_runtime_dir / "file-sess").write_text("not a directory") | |
| for invalid_sid in ( | |
| "linked-sess", | |
| "dangling-sess", | |
| "file-sess", | |
| ): | |
| with self.subTest(invalid_sid=invalid_sid): | |
| with self.assertRaises(PermissionError): | |
| with session.prepare_session_socket(invalid_sid): | |
| pass | |
| # list_session_ids reports sorted real session dirs that contain | |
| # daemon.sock while ignoring hidden, reserved, symlinked, | |
| # non-directory, or empty entries. | |
| sock_b = session.get_daemon_socket_path("sess-b") | |
| sock_a = session.get_daemon_socket_path("sess-a") | |
| sock_b.parent.mkdir(parents=True, exist_ok=True, mode=0o700) | |
| sock_b.touch() | |
| sock_a.parent.mkdir(parents=True, exist_ok=True, mode=0o700) | |
| sock_a.touch() | |
| (outside_dir / "daemon.sock").touch() | |
| (build_runtime_dir / ".hidden").mkdir(exist_ok=True) | |
| (build_runtime_dir / ".hidden" / "daemon.sock").touch() | |
| (build_runtime_dir / "default").mkdir(exist_ok=True) | |
| (build_runtime_dir / "default" / "daemon.sock").touch() | |
| (build_runtime_dir / "sess-sock-link").mkdir( | |
| mode=0o700, exist_ok=True | |
| ) | |
| ( | |
| build_runtime_dir / "sess-sock-link" / "daemon.sock" | |
| ).symlink_to(outside_dir / "daemon.sock") | |
| (build_runtime_dir / "sess-dangling-sock-link").mkdir( | |
| mode=0o700, exist_ok=True | |
| ) | |
| ( | |
| build_runtime_dir | |
| / "sess-dangling-sock-link" | |
| / "daemon.sock" | |
| ).symlink_to(pathlib.Path(tmp_dir) / "missing.sock") | |
| for unnormalized_name in ( | |
| " ", | |
| " default", | |
| " .foo", | |
| "a" * 30, | |
| "bad:name", | |
| ): | |
| unnorm_dir = build_runtime_dir / unnormalized_name | |
| unnorm_dir.mkdir(mode=0o700, exist_ok=True) | |
| (unnorm_dir / "daemon.sock").touch() | |
| self.assertSequenceEqual( | |
| session.list_session_ids(), ("sess-a", "sess-b") | |
| ) | |
| # Symlinked socket or session entries pointing to an empty | |
| # sibling session inside build_runtime_dir must not remove the | |
| # sibling directory. | |
| empty_sibling = session.get_daemon_socket_path("sess-empty") | |
| empty_sibling.parent.mkdir(mode=0o700, exist_ok=True) | |
| intra_sock_link = session.get_daemon_socket_path( | |
| "sess-intra-sock-link" | |
| ) | |
| intra_sock_link.parent.mkdir(mode=0o700, exist_ok=True) | |
| intra_sock_link.symlink_to(empty_sibling) | |
| session.cleanup_session_dir(intra_sock_link) | |
| self.assertTrue(empty_sibling.parent.is_dir()) | |
| intra_dir_link = build_runtime_dir / "sess-intra-dir-link" | |
| intra_dir_link.symlink_to(empty_sibling.parent) | |
| session.cleanup_session_dir(intra_dir_link / "daemon.sock") | |
| self.assertTrue(empty_sibling.parent.is_dir()) | |
| # Concurrent removal races on iterdir return (), whereas | |
| # PermissionError propagates loudly instead of returning (). | |
| mock_dir = mock.create_autospec( | |
| pathlib.Path, instance=True, spec_set=True | |
| ) | |
| mock_dir.__str__.return_value = str(build_runtime_dir) | |
| with mock.patch.object( | |
| session, | |
| "get_build_dir_runtime_dir", | |
| autospec=True, | |
| spec_set=True, | |
| return_value=mock_dir, | |
| ): | |
| for race_err in ( | |
| FileNotFoundError("removed"), | |
| NotADirectoryError("replaced"), | |
| ): | |
| mock_dir.iterdir.side_effect = race_err | |
| self.assertSequenceEqual(session.list_session_ids(), ()) | |
| mock_dir.iterdir.side_effect = None | |
| mock_dir.iterdir.return_value = ( | |
| build_runtime_dir / "bad\udcffname", | |
| ) | |
| self.assertSequenceEqual(session.list_session_ids(), ()) | |
| mock_dir.iterdir.side_effect = PermissionError("denied") | |
| with self.assertRaises(PermissionError): | |
| session.list_session_ids() | |
| if os.geteuid() != 0: | |
| build_runtime_dir.chmod(0o000) | |
| try: | |
| with self.assertRaises(PermissionError): | |
| session.list_session_ids() | |
| finally: | |
| build_runtime_dir.chmod(0o700) | |
| # Empty session directory is removed once daemon.sock is unlinked, | |
| # while non-empty directories retaining logs or artifacts stay. | |
| sock_a.unlink() | |
| session.cleanup_session_dir(sock_a) | |
| self.assertFalse(sock_a.parent.exists()) | |
| self.assertSequenceEqual( | |
| session.list_session_ids(), ("sess-b",) | |
| ) | |
| (sock_b.parent / "daemon.log").write_text("keep") | |
| sock_b.unlink() | |
| session.cleanup_session_dir(sock_b) | |
| self.assertTrue(sock_b.parent.is_dir()) | |
| self.assertSequenceEqual(session.list_session_ids(), ()) | |
| def test_concurrent_prepare_session_socket_and_cleanup_same_id( | |
| self, | |
| ) -> None: | |
| """Verifies prepare_session_socket prevents cleanup from deleting session_dir before bind.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": tmp_dir, | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| target_sock = session.get_daemon_socket_path("reused-sid") | |
| bind_lock = threading.Lock() | |
| def bind_cycle() -> None: | |
| with bind_lock: | |
| with session.prepare_session_socket( | |
| "reused-sid" | |
| ) as sock_path: | |
| srv = socket.socket( | |
| socket.AF_UNIX, socket.SOCK_STREAM | |
| ) | |
| try: | |
| srv.bind(str(sock_path)) | |
| srv.listen(8) | |
| finally: | |
| srv.close() | |
| sock_path.unlink(missing_ok=True) | |
| def cleanup_cycle() -> None: | |
| session.cleanup_session_dir(target_sock) | |
| with concurrent.futures.ThreadPoolExecutor( | |
| max_workers=4 | |
| ) as pool: | |
| futures = [] | |
| for _ in range(25): | |
| futures.append(pool.submit(bind_cycle)) | |
| futures.append(pool.submit(cleanup_cycle)) | |
| for fut in futures: | |
| fut.result() | |
| def test_cleanup_ignores_out_of_tree_paths(self) -> None: | |
| """Verifies custom --socket-path values outside the runtime tree are ignored.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| runtime_dir = pathlib.Path(tmp_dir) / "runtime" | |
| other_root = pathlib.Path(tmp_dir) / "other" | |
| custom_dir = other_root / "custom" | |
| custom_dir.mkdir(parents=True) | |
| custom_sock = custom_dir / "daemon.sock" | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": str(runtime_dir), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| session.cleanup_session_dir(custom_sock) | |
| self.assertTrue(custom_dir.is_dir()) | |
| def test_cleanup_rejects_lock_symlink(self) -> None: | |
| """Verifies runtime_lock opens .lock with O_NOFOLLOW and rejects symlinks.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| runtime_dir = pathlib.Path(tmp_dir) / "rt" | |
| outside_target = pathlib.Path(tmp_dir) / "outside-lock" | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": str(runtime_dir), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| build_runtime_dir = session.get_build_dir_runtime_dir( | |
| ensure_exists=True | |
| ) | |
| sock = session.get_daemon_socket_path("sess") | |
| sock.parent.mkdir(parents=True, exist_ok=True) | |
| (build_runtime_dir / ".lock").symlink_to(outside_target) | |
| with self.assertRaises(OSError) as ctx: | |
| with session.runtime_lock(build_runtime_dir): | |
| pass | |
| self.assertEqual(ctx.exception.errno, errno.ELOOP) | |
| with self.assertRaises(PermissionError): | |
| with session.prepare_session_socket("sess"): | |
| pass | |
| session.cleanup_session_dir(sock) | |
| self.assertTrue(sock.parent.is_dir()) | |
| self.assertFalse(outside_target.exists()) | |
| def test_short_symlink_runtime_dir_stays_within_sun_path_limit( | |
| self, | |
| ) -> None: | |
| """Verifies unresolved ZXDB_CLI_RUNTIME_DIR symlinks stay within sun_path bounds.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| deep_target = tmp_root / ("deep_" + "x" * 75) | |
| deep_target.mkdir(parents=True, mode=0o700) | |
| short_link = tmp_root / "rt" | |
| short_link.symlink_to(deep_target) | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": str(short_link), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| with session.prepare_session_socket("sess1") as sock_path: | |
| self.assertLessEqual(len(os.fsencode(sock_path)), 107) | |
| self.assertGreater( | |
| len(os.fsencode(sock_path.resolve())), 108 | |
| ) | |
| srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| try: | |
| srv.bind(str(sock_path)) | |
| self.assertSequenceEqual( | |
| session.list_session_ids(), ("sess1",) | |
| ) | |
| finally: | |
| srv.close() | |
| sock_path.unlink(missing_ok=True) | |
| session.cleanup_session_dir(sock_path.resolve()) | |
| self.assertFalse(sock_path.parent.exists()) | |
| def test_allocate_session_id_is_unique_and_normalized(self) -> None: | |
| """Verifies allocate_session_id produces distinct, already-normalized IDs.""" | |
| first = session.allocate_session_id() | |
| second = session.allocate_session_id() | |
| self.assertNotEqual(first, second) | |
| self.assertTrue(first.startswith(f"{os.getpid()}-")) | |
| self.assertEqual(session.normalize_session_id(first), first) | |
| self.assertEqual(session.normalize_session_id(second), second) | |
| def test_inspect_session_socket(self) -> None: | |
| """Verifies inspect_session_socket classifies missing, invalid, and live sockets.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| build_runtime_dir = tmp_root / "runtime" / "default" | |
| sock_path = build_runtime_dir / "sess-1" / "daemon.sock" | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "missing", | |
| ) | |
| build_runtime_dir.mkdir(parents=True, mode=0o700) | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "missing", | |
| ) | |
| sock_path.parent.mkdir(mode=0o700) | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "missing", | |
| ) | |
| with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as srv: | |
| srv.bind(str(sock_path)) | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| sock_path, build_runtime_dir | |
| ), | |
| "socket", | |
| ) | |
| sock_path.unlink() | |
| sock_path.write_text("regular file", encoding="utf-8") | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "invalid", | |
| ) | |
| sock_path.unlink() | |
| sock_path.mkdir() | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "invalid", | |
| ) | |
| sock_path.rmdir() | |
| os.mkfifo(sock_path) | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "invalid", | |
| ) | |
| sock_path.unlink() | |
| external_dir = tmp_root / "external" | |
| external_dir.mkdir() | |
| external_sock = external_dir / "external.sock" | |
| with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as ext_srv: | |
| ext_srv.bind(str(external_sock)) | |
| sock_path.symlink_to(external_sock) | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| sock_path, build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| sock_path.unlink() | |
| sock_path.symlink_to(tmp_root / "dangling.sock") | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| sock_path, build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| sock_path.unlink() | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| external_sock, build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| # ENAMETOOLONG and ELOOP map to "invalid", whereas PermissionError | |
| # propagates to the caller. | |
| overlong_dir = build_runtime_dir / ("a" * 300) | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| overlong_dir / "daemon.sock", build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| loop_sess = build_runtime_dir / "loop-sess" | |
| loop_sess.symlink_to(loop_sess) | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| loop_sess / "daemon.sock", build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| loop_sess.unlink() | |
| with mock.patch.object( | |
| session, "os", wraps=os, spec_set=True | |
| ) as mock_os: | |
| mock_os.lstat.side_effect = OSError( | |
| errno.ELOOP, "Too many levels of symbolic links" | |
| ) | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| sock_path, build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| mock_os.lstat.side_effect = PermissionError("denied") | |
| with self.assertRaises(PermissionError): | |
| session.inspect_session_socket(sock_path, build_runtime_dir) | |
| sock_path.parent.rmdir() | |
| sock_path.parent.symlink_to(external_dir) | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "invalid", | |
| ) | |
| sock_path.parent.unlink() | |
| sock_path = build_runtime_dir / "sess-1" / "daemon.sock" | |
| sock_path.parent.mkdir(mode=0o700) | |
| with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as srv: | |
| srv.bind(str(sock_path)) | |
| real_lstat = os.lstat | |
| def foreign_uid_lstat( | |
| path: pathlib.Path, | |
| ) -> os.stat_result: | |
| st = real_lstat(path) | |
| if path == sock_path: | |
| return os.stat_result( | |
| st[:4] + (os.getuid() + 1,) + st[5:] | |
| ) | |
| return st | |
| with ( | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| mock.patch.object( | |
| session, "os", wraps=os, spec_set=True | |
| ) as mock_os, | |
| ): | |
| mock_os.lstat.side_effect = foreign_uid_lstat | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| sock_path, build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| with ( | |
| mock.patch.dict( | |
| os.environ, | |
| {"ZXDB_CLI_RUNTIME_DIR": str(tmp_root / "runtime")}, | |
| clear=True, | |
| ), | |
| mock.patch.object( | |
| session, "os", wraps=os, spec_set=True | |
| ) as mock_os, | |
| ): | |
| mock_os.lstat.side_effect = foreign_uid_lstat | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| sock_path, build_runtime_dir | |
| ), | |
| "socket", | |
| ) | |
| sock_path.unlink() | |
| sock_path.parent.rmdir() | |
| sock_path.parent.write_text("not a dir", encoding="utf-8") | |
| self.assertEqual( | |
| session.inspect_session_socket(sock_path, build_runtime_dir), | |
| "invalid", | |
| ) | |
| symlink_runtime_dir = tmp_root / "runtime_link" | |
| symlink_runtime_dir.symlink_to(external_dir) | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| symlink_runtime_dir / "sess" / "daemon.sock", | |
| symlink_runtime_dir, | |
| ), | |
| "invalid", | |
| ) | |
| parent_sock = build_runtime_dir.parent / "daemon.sock" | |
| with socket.socket( | |
| socket.AF_UNIX, socket.SOCK_STREAM | |
| ) as parent_srv: | |
| parent_srv.bind(str(parent_sock)) | |
| for traversal_path in ( | |
| build_runtime_dir / ".." / "daemon.sock", | |
| build_runtime_dir / "." / "daemon.sock", | |
| build_runtime_dir / "sess" / "..", | |
| ): | |
| with self.subTest(traversal_path=traversal_path): | |
| self.assertEqual( | |
| session.inspect_session_socket( | |
| traversal_path, build_runtime_dir | |
| ), | |
| "invalid", | |
| ) | |
| def test_is_unix_socket_live(self) -> None: | |
| """Verifies is_unix_socket_live distinguishes live, stale, missing, and erroring sockets.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| sock_path = tmp_root / "daemon.sock" | |
| self.assertFalse(session.is_unix_socket_live(sock_path)) | |
| srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| try: | |
| srv.bind(str(sock_path)) | |
| srv.listen(1) | |
| self.assertTrue(session.is_unix_socket_live(sock_path)) | |
| finally: | |
| srv.close() | |
| self.assertFalse(session.is_unix_socket_live(sock_path)) | |
| regular_file = tmp_root / "not_a_dir" | |
| regular_file.write_text("file", encoding="utf-8") | |
| self.assertFalse( | |
| session.is_unix_socket_live(regular_file / "daemon.sock") | |
| ) | |
| overlong_sock = tmp_root / ("x" * 120) / "daemon.sock" | |
| self.assertFalse(session.is_unix_socket_live(overlong_sock)) | |
| loop_sock = tmp_root / "loop.sock" | |
| loop_sock.symlink_to(loop_sock) | |
| self.assertFalse(session.is_unix_socket_live(loop_sock)) | |
| live_target = tmp_root / "live_target.sock" | |
| symlink_to_live = tmp_root / "symlink_to_live.sock" | |
| symlink_to_live.symlink_to(live_target) | |
| with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as live_srv: | |
| live_srv.bind(str(live_target)) | |
| live_srv.listen(1) | |
| self.assertTrue(session.is_unix_socket_live(live_target)) | |
| with mock.patch.object( | |
| session, "socket", autospec=True, spec_set=True | |
| ) as mock_socket_mod: | |
| self.assertFalse( | |
| session.is_unix_socket_live(symlink_to_live) | |
| ) | |
| self.assertFalse(session.is_unix_socket_live(regular_file)) | |
| mock_socket_mod.socket.assert_not_called() | |
| mock_probe = mock.create_autospec(socket.socket, instance=True) | |
| mock_probe.__enter__.return_value = mock_probe | |
| with mock.patch.object( | |
| session, "socket", autospec=True, spec_set=True | |
| ) as mock_socket_mod: | |
| mock_socket_mod.AF_UNIX = socket.AF_UNIX | |
| mock_socket_mod.SOCK_STREAM = socket.SOCK_STREAM | |
| mock_socket_mod.socket.return_value = mock_probe | |
| for live_err in ( | |
| PermissionError("denied"), | |
| BlockingIOError("backlog full"), | |
| TimeoutError("timed out"), | |
| InterruptedError("interrupted"), | |
| ): | |
| with self.subTest(live_err=type(live_err).__name__): | |
| mock_probe.connect.side_effect = live_err | |
| self.assertTrue(session.is_unix_socket_live(sock_path)) | |
| for non_live_err in ( | |
| OSError(errno.ECONNRESET, "connection reset"), | |
| OSError(errno.ENOTSOCK, "not a socket"), | |
| OSError(errno.EPROTOTYPE, "wrong protocol type"), | |
| OSError("AF_UNIX path too long"), | |
| ): | |
| with self.subTest(non_live_err=non_live_err): | |
| mock_probe.connect.side_effect = non_live_err | |
| self.assertFalse(session.is_unix_socket_live(sock_path)) | |
| for fatal_err in ( | |
| OSError(errno.ENOMEM, "cannot allocate memory"), | |
| OSError(errno.EIO, "input/output error"), | |
| ): | |
| with self.subTest(fatal_err=fatal_err.errno): | |
| mock_probe.connect.side_effect = fatal_err | |
| with self.assertRaises(OSError) as ctx: | |
| session.is_unix_socket_live(sock_path) | |
| self.assertEqual(ctx.exception.errno, fatal_err.errno) | |
| def test_prepare_session_socket_rolls_back_empty_dir_on_exception( | |
| self, | |
| ) -> None: | |
| """Verifies prepare_session_socket rolls back bound sockets and empty dirs on failure.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": tmp_dir, | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| sock_path = session.get_daemon_socket_path("fail-bind") | |
| with self.assertRaisesRegex(RuntimeError, "bind failed"): | |
| with session.prepare_session_socket("fail-bind") as path: | |
| self.assertTrue(path.parent.is_dir()) | |
| raise RuntimeError("bind failed") | |
| self.assertFalse(sock_path.parent.exists()) | |
| for keep_log in (False, True): | |
| session_id = f"fail-listen-{int(keep_log)}" | |
| bound_sock = session.get_daemon_socket_path(session_id) | |
| with self.subTest(keep_log=keep_log): | |
| srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| try: | |
| with self.assertRaisesRegex( | |
| RuntimeError, "listen failed" | |
| ): | |
| with session.prepare_session_socket( | |
| session_id | |
| ) as path: | |
| srv.bind(str(path)) | |
| srv.listen(1) | |
| if keep_log: | |
| (path.parent / "daemon.log").write_text( | |
| "log\n", encoding="utf-8" | |
| ) | |
| raise RuntimeError("listen failed") | |
| finally: | |
| srv.close() | |
| self.assertFalse(bound_sock.exists()) | |
| if keep_log: | |
| self.assertTrue( | |
| (bound_sock.parent / "daemon.log").exists() | |
| ) | |
| else: | |
| self.assertFalse(bound_sock.parent.exists()) | |
| def test_list_and_cleanup_reject_symlink_build_dir_and_traversal( | |
| self, | |
| ) -> None: | |
| """Verifies symlinked build_runtime_dir and '..' traversal are rejected.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| real_target = tmp_root / "real_build_dir" | |
| real_target.mkdir(mode=0o700) | |
| target_empty = real_target / "empty-sess" | |
| target_empty.mkdir(mode=0o700) | |
| target_live = real_target / "live-sess" | |
| target_live.mkdir(mode=0o700) | |
| (target_live / "daemon.sock").touch() | |
| runtime_root = tmp_root / "runtime" | |
| runtime_root.mkdir(mode=0o700) | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": str(runtime_root), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| build_runtime_dir = session.get_build_dir_runtime_dir() | |
| build_runtime_dir.symlink_to(real_target) | |
| self.assertSequenceEqual(session.list_session_ids(), ()) | |
| session.cleanup_session_dir( | |
| build_runtime_dir / "empty-sess" / "daemon.sock" | |
| ) | |
| self.assertTrue(target_empty.is_dir()) | |
| build_runtime_dir.unlink() | |
| build_runtime_dir.mkdir(mode=0o700) | |
| sess_link = build_runtime_dir / "sess-link" | |
| sess_link.symlink_to(real_target) | |
| session.cleanup_session_dir( | |
| sess_link / ".." / "empty-sess" / "daemon.sock" | |
| ) | |
| self.assertTrue(target_empty.is_dir()) | |
| def test_get_build_dir_runtime_dir_rejects_file_and_dangling_symlink( | |
| self, | |
| ) -> None: | |
| """Verifies get_build_dir_runtime_dir(ensure_exists=True) rejects files and symlinks.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| runtime_root = tmp_root / "runtime" | |
| runtime_root.mkdir(mode=0o700) | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": str(runtime_root), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| build_runtime_dir = session.get_build_dir_runtime_dir() | |
| build_runtime_dir.write_text("not a dir", encoding="utf-8") | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir(ensure_exists=True) | |
| build_runtime_dir.unlink() | |
| build_runtime_dir.symlink_to(tmp_root / "missing-target") | |
| with self.assertRaises(PermissionError): | |
| session.get_build_dir_runtime_dir(ensure_exists=True) | |
| def test_prepare_session_socket_rejects_live_socket_and_directory( | |
| self, | |
| ) -> None: | |
| """Verifies prepare_session_socket leaves live sockets intact and rejects dirs.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": tmp_dir, | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| live_sock = session.get_daemon_socket_path("live-sess") | |
| live_sock.parent.mkdir(parents=True, mode=0o700) | |
| with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as srv: | |
| srv.bind(str(live_sock)) | |
| srv.listen(1) | |
| with self.assertRaises(FileExistsError) as ctx: | |
| with session.prepare_session_socket("live-sess"): | |
| pass | |
| self.assertEqual(ctx.exception.errno, errno.EADDRINUSE) | |
| self.assertTrue(live_sock.exists()) | |
| self.assertTrue(session.is_unix_socket_live(live_sock)) | |
| dir_sock = session.get_daemon_socket_path("dir-sess") | |
| dir_sock.mkdir(parents=True, mode=0o700) | |
| with self.assertRaises(PermissionError): | |
| with session.prepare_session_socket("dir-sess"): | |
| pass | |
| self.assertTrue(dir_sock.is_dir()) | |
| def test_cleanup_session_dir_atomic_missing_and_symlink_runtime_dir( | |
| self, | |
| ) -> None: | |
| """Verifies cleanup_session_dir handles missing and symlinked runtime dirs.""" | |
| with tempfile.TemporaryDirectory(dir="/tmp") as tmp_dir: | |
| tmp_root = pathlib.Path(tmp_dir) | |
| runtime_root = tmp_root / "runtime" | |
| runtime_root.mkdir(mode=0o700) | |
| with mock.patch.dict( | |
| os.environ, | |
| { | |
| "ZXDB_CLI_RUNTIME_DIR": str(runtime_root), | |
| "FUCHSIA_BUILD_DIR_FROM_FX": "/out/default", | |
| }, | |
| clear=True, | |
| ): | |
| missing_sock = session.get_daemon_socket_path("missing-rt") | |
| session.cleanup_session_dir(missing_sock) | |
| real_dir = tmp_root / "real_runtime" | |
| real_dir.mkdir(mode=0o700) | |
| real_sess = real_dir / "keep-sess" | |
| real_sess.mkdir(mode=0o700) | |
| build_runtime_dir = session.get_build_dir_runtime_dir() | |
| build_runtime_dir.symlink_to(real_dir) | |
| session.cleanup_session_dir( | |
| build_runtime_dir / "keep-sess" / "daemon.sock" | |
| ) | |
| self.assertTrue(real_sess.is_dir()) | |
| with self.assertRaises(PermissionError): | |
| with session.prepare_session_socket("keep-sess"): | |
| pass | |
| with self.assertRaises(PermissionError): | |
| with session.runtime_lock(build_runtime_dir / ".." / "x"): | |
| pass | |
| if __name__ == "__main__": | |
| unittest.main() |