def test_with_fantasy_unused(self): """Test against a fantasy executable with fantasy unused.""" # pylint: disable=line-too-long with tests.MockLdd(out=textwrap.dedent('''\ \tlinux-vdso.so.1 (0x00007ffd66ce2000) \tlibm.so.6 => /lib64/libm.so.6 (0x00007f72b7e76000)\n''' ), out_unused=textwrap.dedent('''\ Unused direct dependencies: \t/lib64/libm.so.6\n''')): # pylint: enable=line-too-long deps = lddwrap.list_dependencies(path=pathlib.Path("/bin/dir"), unused=True) unused = [dep for dep in deps if dep.unused] expected_unused = [ lddwrap.Dependency(soname="libm.so.6", path=pathlib.Path("/lib64/libm.so.6"), found=True, mem_address="0x00007f72b7e76000", unused=True) ] self.assertEqual(len(expected_unused), len(unused)) for i, (dep, exp_dep) in enumerate(zip(unused, expected_unused)): self.assertListEqual( [], diff_dependencies(ours=dep, theirs=exp_dep), "Mismatch at the unused dependency {}".format(i))
def test_sorting_by_all_attributes(self) -> None: # pylint: disable=line-too-long with tests.MockLdd(out=textwrap.dedent('''\ \tlinux-vdso.so.1 (0x00007ffd66ce2000) \tlibselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f72b88fc000) \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f72b850b000) \tlibpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f72b8299000) \tlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f72b8095000) \t/lib64/ld-linux-x86-64.so.2 (0x00007f72b8d46000) \tlibpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f72b7e76000)\n''' ), out_unused=''): # pylint: enable=line-too-long for attr in lddwrap.DEPENDENCY_ATTRIBUTES: deps = lddwrap.list_dependencies(path=pathlib.Path("/bin/dir"), unused=True) # pylint: disable=protected-access lddwrap._sort_dependencies_in_place(deps=deps, sort_by=attr) previous = getattr(deps[0], attr) previous = '' if previous is None else str(previous) for i in range(1, len(deps)): current = getattr(deps[i], attr) current = '' if current is None else str(current) self.assertLessEqual( previous, current, ("The dependencies must be sorted according to " "attribute {!r}: {!r}").format(attr, deps))
def test_sorted_with_specific_attribute(self): buf = io.StringIO() stream = cast(TextIO, buf) args = lddwrap.main.parse_args( sys_argv=["some-executable.py", "/bin/pwd", "--sorted", "path"]) with tests.MockLdd(out=textwrap.dedent('''\ \tlinux-vdso.so.1 (0x00007ffe0953f000) \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd548353000) \t/lib64/ld-linux-x86-64.so.2 (0x00007fd54894d000)\n'''), out_unused=''): retcode = lddwrap.main._main(args=args, stream=stream) self.assertEqual(0, retcode) # pylint: disable=trailing-whitespace expected_output = textwrap.dedent("""\ soname | path | found | mem_address | unused ----------------+---------------------------------+-------+--------------------+------- linux-vdso.so.1 | None | True | 0x00007ffe0953f000 | False libc.so.6 | /lib/x86_64-linux-gnu/libc.so.6 | True | 0x00007fd548353000 | False None | /lib64/ld-linux-x86-64.so.2 | True | 0x00007fd54894d000 | False """) output = textwrap.dedent(buf.getvalue()) self.assertEqual(expected_output, output)
def test_with_static_library(self) -> None: """Test against a fantasy static library.""" with tempfile.TemporaryDirectory() as tmp_dir: lib_pth = pathlib.Path(tmp_dir) / "my_static_lib.so" lib_pth.write_text("totally static!") with tests.MockLdd(out=textwrap.dedent('''\ my_static_lib.so: \tstatically linked\n'''), out_unused=''): # pylint: enable=line-too-long deps = lddwrap.list_dependencies(path=lib_pth, unused=True) # The dependencies are empty since the library is # statically linked. self.assertListEqual([], deps)
def test_bin_dir_with_empty_unused(self): # pylint: disable=line-too-long with tests.MockLdd(out=textwrap.dedent('''\ \tlinux-vdso.so.1 (0x00007ffd66ce2000) \tlibselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f72b88fc000) \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f72b850b000) \tlibpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f72b8299000) \tlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f72b8095000) \t/lib64/ld-linux-x86-64.so.2 (0x00007f72b8d46000) \tlibpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f72b7e76000)\n''' ), out_unused=''): # pylint: enable=line-too-long deps = lddwrap.list_dependencies(path=pathlib.Path("/bin/dir"), unused=True) unused = [dep for dep in deps if dep.unused] self.assertListEqual([], unused)
def test_pwd(self): """Test parsing the captured output of ``ldd`` on ``/bin/pwd``.""" with tests.MockLdd(out=textwrap.dedent('''\ \tlinux-vdso.so.1 (0x00007ffe0953f000) \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd548353000) \t/lib64/ld-linux-x86-64.so.2 (0x00007fd54894d000)\n'''), out_unused=''): deps = lddwrap.list_dependencies(path=pathlib.Path('/bin/pwd'), unused=False) expected_deps = [ lddwrap.Dependency(soname="linux-vdso.so.1", path=None, found=True, mem_address="0x00007ffe0953f000", unused=None), lddwrap.Dependency( soname='libc.so.6', path=pathlib.Path("/lib/x86_64-linux-gnu/libc.so.6"), found=True, mem_address="0x00007fd548353000", unused=None), lddwrap.Dependency( soname=None, path=pathlib.Path("/lib64/ld-linux-x86-64.so.2"), found=True, mem_address="0x00007fd54894d000", unused=None) ] self.assertEqual(len(expected_deps), len(deps)) for i, (dep, expected_dep) in enumerate(zip(deps, expected_deps)): self.assertListEqual([], diff_dependencies(ours=dep, theirs=expected_dep), "Mismatch at the dependency {}".format(i))
def test_bin_dir(self): """Test parsing the captured output of ``ldd`` on ``/bin/dir``.""" # pylint: disable=line-too-long with tests.MockLdd(out=textwrap.dedent('''\ \tlinux-vdso.so.1 (0x00007ffd66ce2000) \tlibselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f72b88fc000) \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f72b850b000) \tlibpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f72b8299000) \tlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f72b8095000) \t/lib64/ld-linux-x86-64.so.2 (0x00007f72b8d46000) \tlibpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f72b7e76000)\n''' ), out_unused=''): # pylint: enable=line-too-long deps = lddwrap.list_dependencies(path=pathlib.Path('/bin/dir'), unused=False) expected_deps = [ lddwrap.Dependency(soname="linux-vdso.so.1", path=None, found=True, mem_address="0x00007ffd66ce2000", unused=None), lddwrap.Dependency( soname="libselinux.so.1", path=pathlib.Path("/lib/x86_64-linux-gnu/libselinux.so.1"), found=True, mem_address="0x00007f72b88fc000", unused=None), lddwrap.Dependency( soname="libc.so.6", path=pathlib.Path("/lib/x86_64-linux-gnu/libc.so.6"), found=True, mem_address="0x00007f72b850b000", unused=None), lddwrap.Dependency( soname="libpcre.so.3", path=pathlib.Path("/lib/x86_64-linux-gnu/libpcre.so.3"), found=True, mem_address="0x00007f72b8299000", unused=None), lddwrap.Dependency( soname="libdl.so.2", path=pathlib.Path("/lib/x86_64-linux-gnu/libdl.so.2"), found=True, mem_address="0x00007f72b8095000", unused=None), lddwrap.Dependency( soname=None, path=pathlib.Path("/lib64/ld-linux-x86-64.so.2"), found=True, mem_address="0x00007f72b8d46000", unused=None), lddwrap.Dependency( soname="libpthread.so.0", path=pathlib.Path("/lib/x86_64-linux-gnu/libpthread.so.0"), found=True, mem_address="0x00007f72b7e76000", unused=None), ] self.assertEqual(len(expected_deps), len(deps)) for i, (dep, expected_dep) in enumerate(zip(deps, expected_deps)): self.assertListEqual([], diff_dependencies(ours=dep, theirs=expected_dep), "Mismatch at the dependency {}".format(i))