Skip to content

Commit

Permalink
shared: add tests for shared library contents
Browse files Browse the repository at this point in the history
  • Loading branch information
wade-arista committed Nov 23, 2023
1 parent 8f9f1d7 commit a699d5e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
21 changes: 20 additions & 1 deletion shared/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,33 @@ cc_shared_library(
)

assert_linking_test(
name = "check_libthree_so",
name = "check_libthree_linking",
lib_root_name = "three",
require = [
"libone.so",
"libtwo.so",
],
)

py_test(
name = "check_libthree_dynsyms",
srcs = ["check_symtab.py"],
args = [
"--sut",
"libthree.so",
"--undefined",
"_Z3onev",
"--undefined",
"_Z3twov",
"--defined",
"_Z5threev",
"--",
"$(locations :three_so)",
],
data = [":three_so"],
main = "check_symtab.py",
)

# Expected to pass
cc_test(
name = "test1",
Expand Down
61 changes: 61 additions & 0 deletions shared/check_symtab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
from argparse import ArgumentParser
from collections.abc import Iterable
from os.path import basename
from re import split
from shlex import quote
from subprocess import check_output
from typing import Optional


def _fmt(v: Optional[Iterable[str]]) -> Optional[str]:
return ("\n " + "\n ".join(sorted(v))) if v else None


def assert_empty(entries: set[str], tag: str) -> None:
assert not entries, f"{tag} entries ({len(entries)}): {_fmt(entries)}"


def main():
ap = ArgumentParser()
ap.add_argument("--sut")
ap.add_argument("--undefined", action="append")
ap.add_argument("--defined", action="append")
ap.add_argument("files", metavar="FILE", nargs="+")
args = ap.parse_args()

sut = None
for fn in args.files:
if basename(fn) == args.sut:
sut = fn
break

assert sut, f"SUT {args.sut} not found in files args {args.files}"
print("sut:", sut)

cmd = ["readelf", "--dyn-syms", sut]
print("+", " ".join(map(quote, cmd)))
out = check_output(cmd, text=True)
defined = set()
undefined = set()
for line in out.splitlines():
parts = split(r"\s+", line)
try:
defState = parts[7]
name = " ".join(parts[8:])
except IndexError:
continue
if not name.strip():
continue
if defState == "Ndx":
continue
(undefined if defState == "UND" else defined).add(name)

print("undefined:", _fmt(undefined))
print("defined:", _fmt(defined))

assert_empty(set(args.undefined) - undefined, "missing undefined")
assert_empty(set(args.defined) - defined, "missing defined")


main()

0 comments on commit a699d5e

Please sign in to comment.