mirror-llvm-project/lldb/test/API/macosx/dsym_codesign/TestdSYMCodesign.py
Charles Zablit 67a20745d6
[lldb] Introduce the require decorator (#212753)
Currently, it is unclear if a test that is skipped is either:
- Unsupported because it will never run (macosx tests should not run on
Windows).
- Skipped because it's supposed to work but does not yet.
`@expectedFailure` is not always an option because a test might be
timing out and waiting 600s for it to fail is not acceptable.

This patch introduces 2 new decorators to differentiate those tests:
- `@requireX` meaning the tests require X to run (e.g `@requireDarwin`).
- `@requireNotX` meaning the tests requires anything but `X` to run (e.g
`@requireNotDarwin`).

In the tests results summary, this introduces a new category: `skipped`
tests:
- `@requireX` -> `UNSUPPORTED`
- `@skipIfX` -> `SKIPPED`

Here is how they can and cannot be stacked:

This will never run:
```py3
@requireDarwin
@requireWindows
def test():
    ...
```

This will not run on Linux, because `@requireWindows` is not satisfied.
```py3
@requireNotDarwin
@requireWindows
def test():
    ...
```

For "require Darwin or Windows", you need:
```py3
@requirePlatform(["darwin", "windows"])
def test():
    ...
```

For "require Darwin on ARM64 or Windows on ARM64", you need:
```py3
@requireARM64
@requirePlatform(["darwin", "windows"])
def test():
    ...
```

This patch also converts the tests in
`lldb/tests/API/{macosx,linux,windows}` to use these decorators. Patches
to convert the rest of the tests will follow.
2026-08-01 18:03:29 +02:00

81 lines
2.6 KiB
Python

import os
import shutil
import subprocess
import sys
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
def has_lldb_codesign():
"""Check if the lldb_codesign certificate is available."""
try:
if sys.platform != "darwin":
return False
result = subprocess.run(
[
"security",
"find-certificate",
"-c",
"lldb_codesign",
"/Library/Keychains/System.keychain",
],
capture_output=True,
)
return result.returncode == 0
except FileNotFoundError:
return False
@requireDarwin
class TestdSYMCodesign(TestBase):
NO_DEBUG_INFO_TESTCASE = True
SHARED_BUILD_TESTCASE = False
def build_dsym_with_script(self):
self.build(debug_info="dsym")
exe = self.getBuildArtifact("a.out")
dsym = self.getBuildArtifact("a.out.dSYM")
python_dir = os.path.join(dsym, "Contents", "Resources", "Python")
os.makedirs(python_dir, exist_ok=True)
shutil.copy(
os.path.join(self.getSourceDir(), "dsym_script.py"),
os.path.join(python_dir, "a.py"),
)
return exe, dsym
def test_adhoc_signed_dsym(self):
"""An ad-hoc signed dSYM should not be loaded because the
signature doesn't chain to a trusted root CA."""
exe, dsym = self.build_dsym_with_script()
subprocess.check_call(["codesign", "-f", "-s", "-", dsym])
self.runCmd("settings set target.load-script-from-symbol-file trusted")
self.createTestTarget(file_path=exe)
self.expect(
"script -- print('SENTINEL')",
substrs=["SENTINEL"],
)
# The script should NOT have been loaded.
self.assertFalse(
hasattr(lldb, "_dsym_codesign_test_loaded"),
"Script should not auto-load from ad-hoc signed dSYM",
)
@unittest.skipUnless(has_lldb_codesign(), "requires lldb_codesign certificate")
def test_trusted_signed_dsym_auto_loads(self):
"""A dSYM signed with the trusted lldb_codesign certificate should
auto-load scripts."""
exe, dsym = self.build_dsym_with_script()
subprocess.check_call(["codesign", "-f", "-s", "lldb_codesign", dsym])
self.runCmd("settings set target.load-script-from-symbol-file trusted")
self.createTestTarget(file_path=exe)
# The script sets a marker attribute on the lldb module.
self.assertTrue(
getattr(lldb, "_dsym_codesign_test_loaded", False),
"Script should auto-load from trusted signed dSYM",
)