def test_pwd(self): path = pathlib.Path("/bin/pwd") deps = lddwrap.list_dependencies(path=path) expected_deps = [ lddwrap.Dependency(soname="linux-vdso.so.1", path=None, found=True, mem_address="", unused=None), lddwrap.Dependency( soname="libc.so.6", path=pathlib.Path("/lib/x86_64-linux-gnu/libc.so.6"), found=True, mem_address="", unused=None), lddwrap.Dependency( soname=None, path=pathlib.Path("/lib64/ld-linux-x86-64.so.2"), found=True, mem_address="", unused=None) ] for dep in deps: for exp_dep in expected_deps: if exp_dep.soname == dep.soname: self.assertTrue( dependencies_equal(dep, exp_dep), "Incorrect dependency, expected {}\ngot {}".format( exp_dep, dep)) break
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_json(self): deps = [] # type: List[lddwrap.Dependency] for index in range(2): deps.append( lddwrap.Dependency(found=True, soname='lib{}.so'.format(index), path=pathlib.Path( '/bin/lib{}.so'.format(index)), mem_address=hex(index), unused=False)) buf = io.StringIO() stream = cast(TextIO, buf) lddwrap._output_json(deps=deps, stream=stream) expected_output = textwrap.dedent("""\ [ { "soname": "lib0.so", "path": "/bin/lib0.so", "found": true, "mem_address": "0x0", "unused": false }, { "soname": "lib1.so", "path": "/bin/lib1.so", "found": true, "mem_address": "0x1", "unused": false } ]""") output = textwrap.dedent(buf.getvalue()) self.assertEqual(expected_output, output)
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_json_format(self): deps = [] # type: List[lddwrap.Dependency] for index in range(2): deps.append( lddwrap.Dependency(found=True, soname='lib{}.so'.format(index), path=pathlib.Path( '/bin/lib{}.so'.format(index)), mem_address=hex(index), unused=False)) buf = io.StringIO() stream = cast(TextIO, buf) json.dump(obj=[dep.as_mapping() for dep in deps], fp=stream, indent=2) try: json.loads(buf.getvalue()) except ValueError: self.fail("The following data was not in json format\n\n{}".format( buf.getvalue()))
def test_verbose(self): deps = [] # type: List[lddwrap.Dependency] for index in range(3): deps.append( lddwrap.Dependency(found=True, soname='lib{}.so'.format(index), path=pathlib.Path( '/bin/lib{}.so'.format(index)), mem_address=hex(index), unused=False)) buf = io.StringIO() stream = cast(TextIO, buf) lddwrap._output_verbose(deps=deps, stream=stream) # pylint: disable=trailing-whitespace expected_output = textwrap.dedent("""\ soname | path | found | mem_address | unused --------+--------------+-------+-------------+------- lib0.so | /bin/lib0.so | True | 0x0 | False lib1.so | /bin/lib1.so | True | 0x1 | False lib2.so | /bin/lib2.so | True | 0x2 | False """) output = textwrap.dedent(buf.getvalue()) self.assertEqual(expected_output, output)
def test_parse_line(self): # yapf: disable lines = [ "libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 " "(0x00007f9a19d8a000)", "/lib64/ld-linux-x86-64.so.2 (0x00007f9a1a329000)", "libboost_program_options.so.1.62.0 => not found", "linux-vdso.so.1 => (0x00007ffd7c7fd000)", "libopencv_stitching.so.3.3 => not found", "libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 " "(0x00007f4b78462000)", "libz.so.1 => not found", "../build/debug/libextstr.so => not found", "/home/user/lib/liblmdb.so => not found" ] # yapf: enable expected_deps = [ lddwrap.Dependency( soname="libstdc++.so.6", path=pathlib.Path("/usr/lib/x86_64-linux-gnu/libstdc++.so.6"), found=True, mem_address="0x00007f9a19d8a000", unused=None), lddwrap.Dependency( soname=None, path=pathlib.Path("/lib64/ld-linux-x86-64.so.2"), found=True, mem_address="0x00007f9a1a329000", unused=None), lddwrap.Dependency(soname="libboost_program_options.so.1.62.0", path=None, found=False, mem_address=None, unused=None), lddwrap.Dependency(soname="linux-vdso.so.1", path=None, found=True, mem_address="0x00007ffd7c7fd000", unused=None), lddwrap.Dependency(soname="libopencv_stitching.so.3.3", path=None, found=False, mem_address=None, unused=None), lddwrap.Dependency( soname="libstdc++.so.6", path=pathlib.Path("/usr/lib/x86_64-linux-gnu/libstdc++.so.6"), found=True, mem_address="0x00007f4b78462000", unused=None), lddwrap.Dependency(soname="libz.so.1", path=None, found=False, mem_address=None), lddwrap.Dependency( soname=None, path=pathlib.Path("../build/debug/libextstr.so"), found=False, mem_address=None, unused=None), lddwrap.Dependency(soname=None, path=pathlib.Path("/home/user/lib/liblmdb.so"), found=False, mem_address=None, unused=None) ] for i, line in enumerate(lines): # pylint: disable=protected-access dep = lddwrap._parse_line(line=line) self.assertListEqual( [], diff_dependencies(ours=dep, theirs=expected_deps[i]), "Incorrect dependency read from the line {} {!r}".format( i + 1, line))
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))