|
8 | 8 | from python.runfiles import runfiles |
9 | 9 |
|
10 | 10 | class CheckLinkageTest(unittest.TestCase): |
11 | | - def test_linkage(self): |
| 11 | + @unittest.skipUnless(sys.platform.startswith("win")) |
| 12 | + def test_linkage_windows(self): |
12 | 13 | rf = runfiles.Create() |
13 | | - d = rf.Rlocation("_main/tests/cc/current_py_cc_headers") |
14 | | - d = pathlib.Path(d) |
15 | | - for file_path in d.glob("*.dll"): |
16 | | - print(f"[*] Analyzing dependencies for: {file_path}\n") |
| 14 | + dll_path = rf.Rlocation("_main/tests/cc/current_py_cc_headers/bin_abi3.dll") |
| 15 | + if not os.path.exists(dll_path): |
| 16 | + self.fail(f"dll at {dll_path} does not exist") |
| 17 | + |
| 18 | + pe = pefile.PE(dll_path) |
| 19 | + if not hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'): |
| 20 | + self.fail("No import directory found.") |
| 21 | + |
| 22 | + imported_dlls = [ |
| 23 | + entry.dll.decode('utf-8').lower() |
| 24 | + for entry in pe.DIRECTORY_ENTRY_IMPORT |
| 25 | + ] |
| 26 | + python_dlls = [ |
| 27 | + dll for dll in imported_dlls if dll.startswith("python3") |
| 28 | + ] |
| 29 | + self.assertEqual(python_dlls, ["python3.dll"]) |
17 | 30 |
|
18 | | - try: |
19 | | - # Parse the PE file |
20 | | - pe = pefile.PE(file_path) |
21 | | - |
22 | | - if not hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'): |
23 | | - print("[!] No import directory found. The file may not have dependencies or is packed.") |
24 | | - raise Exception("no deps?") |
25 | | - |
26 | | - print("Imported DLLs:") |
27 | | - |
28 | | - # Iterate over the import directory entries |
29 | | - # Each 'entry' corresponds to one imported DLL |
30 | | - for entry in pe.DIRECTORY_ENTRY_IMPORT: |
31 | | - # entry.dll is a bytes string, so we decode it to utf-8 |
32 | | - dll_name = entry.dll.decode('utf-8') |
33 | | - print(f" - {dll_name}") |
34 | | - |
35 | | - except pefile.PEFormatError as e: |
36 | | - print(f"Error: Not a valid PE file (DLL/EXE). \nDetails: {e}") |
37 | | - except Exception as e: |
38 | | - print(f"An unexpected error occurred: {e}") |
39 | | - |
40 | | - raise Exception("done") |
41 | 31 |
|
42 | 32 | if __name__ == "__main__": |
43 | 33 | unittest.main() |
0 commit comments