|
| 1 | +""" |
| 2 | +Test library linking |
| 3 | +""" |
| 4 | +import re |
| 5 | +from pathlib import Path |
| 6 | +import pytest |
| 7 | +from crytic_compile.crytic_compile import CryticCompile |
| 8 | + |
| 9 | +TEST_DIR = Path(__file__).resolve().parent |
| 10 | + |
| 11 | +LIBRARY_PLACEHOLDER_REGEX = r"__.{36}__" |
| 12 | + |
| 13 | + |
| 14 | +def test_library_linking() -> None: |
| 15 | + """Test that the placeholder is not present in the bytecode when the libraries are provided""" |
| 16 | + cc = CryticCompile( |
| 17 | + Path(TEST_DIR / "library_linking.sol").as_posix(), |
| 18 | + compile_libraries="(NeedsLinkingA, 0xdead),(NeedsLinkingB, 0x000000000000000000000000000000000000beef)", |
| 19 | + ) |
| 20 | + for compilation_unit in cc.compilation_units.values(): |
| 21 | + for source_unit in compilation_unit.source_units.values(): |
| 22 | + assert ( |
| 23 | + len(re.findall(r"__.{36}__", source_unit.bytecode_init("TestLibraryLinking"))) == 2 |
| 24 | + ) |
| 25 | + assert ( |
| 26 | + len(re.findall(r"__.{36}__", source_unit.bytecode_runtime("TestLibraryLinking"))) |
| 27 | + == 2 |
| 28 | + ) |
| 29 | + libraries = compilation_unit.crytic_compile.libraries |
| 30 | + assert ( |
| 31 | + len( |
| 32 | + re.findall( |
| 33 | + r"__.{36}__", source_unit.bytecode_init("TestLibraryLinking", libraries) |
| 34 | + ) |
| 35 | + ) |
| 36 | + == 0 |
| 37 | + ) |
| 38 | + assert ( |
| 39 | + len( |
| 40 | + re.findall( |
| 41 | + r"__.{36}__", source_unit.bytecode_runtime("TestLibraryLinking", libraries) |
| 42 | + ) |
| 43 | + ) |
| 44 | + == 0 |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def test_library_linking_validation() -> None: |
| 49 | + """Test that invalid compile libraries argument raises an error""" |
| 50 | + with pytest.raises(ValueError): |
| 51 | + CryticCompile( |
| 52 | + Path(TEST_DIR / "library_linking.sol").as_posix(), |
| 53 | + compile_libraries="(NeedsLinkingA, 0x)", |
| 54 | + ) |
0 commit comments