1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
#!/usr/bin/env python3
# This test script is hereby placed into the public domain
# for anyone to used for any purpose.
#
# Run in test dir as:
# test_dwarfdump.py filetype buildsys sourcedirbase builddirbase
# where filetype is Elf, PE, or Macos
# where buildsys is conf, cmake, or meson
import os
import sys
import shutil
from subprocess import Popen, PIPE, STDOUT
# For reporting differences.
import difflib
gmaxlines = 700
testbase = [
[
"Elf",
"testuriLE64ELf.base",
"testuriLE64ELf.testme",
"junk.LE64ELf.new",
],
[
"Macos",
"test-mach-o-32.base",
"test-mach-o-32.dSYM",
"junk.mach-o.new",
],
["PE", "testobjLE32PE.base", "testobjLE32PE.exe", "junk.PE.new"],
]
class ddfiles:
def __init__(self):
self.testbase = False
self.testobj = False
self.newtest = False
self.ddopts = ""
def ddprint(self):
print("Test baseline :", self.testbase)
print("Test object :", self.testobj)
print("Test output :", self.newtest)
print("Test dd options :", self.ddopts)
class tdata:
def __init__(self):
self.objtype = False
self.buildsystem = False
self.srcbase = False
self.bldbase = False
self.cwd = False
def tprint(self):
print("Testdata objtype :", self.objtype)
print("Testdata build system :", self.buildsystem)
print("Testdata source base dir:", self.srcbase)
print("Testdata build base dir :", self.bldbase)
print("Testdata working dir :", self.cwd)
def setupfilesinvolved(td, dd):
ftype = td.objtype
for n, t in enumerate(testbase):
if not t[0] == ftype:
continue
dd.testbase = t[1]
dd.testobj = t[2]
dd.newtest = t[3]
dd.ddopts = ["-a", "-vvv"]
return
print(" FAIL test_dwarfdump.py to setup files for type ", ftype)
sys.exit(1)
def fixtimestamp(txtout):
# "Fix Last Time to 0 so compares can work ok
# sed 's/last time 0x.*/last time 0x0/' <$i >$t
out = []
for n, s in enumerate(txtout):
f = s.find("last time 0x")
if f == -1:
out += [s]
continue
l = "".join([s[0:f], "last time 0x0"])
# print("dadebug timestamp fixed",l);
out += [l]
return out
def writetextout(text, filepath):
try:
f = open(filepath, "w")
except:
print(
"Unable to write dwarfdump putput ",
filepath,
" giving up",
)
sys.exit(1)
for s in txtout:
# Even on windows writes in Linux text format
print(s.rstrip("\n"), file=f, end="\n")
f.close()
def copytobuild(srcfile, targetfile):
try:
f = open(srcfile, "r")
except:
print("Unable to open input conf ", srcfile, " giving up")
sys.exit(1)
y = f.readlines()
f.close()
try:
w = open(targetfile, "w")
except:
print("Unable to output conf ", targetfile, " giving up")
sys.exit(1)
# print("dadebug write conf file, linecount",len(y))
w.writelines(y)
w.close()
# Read in lines to a list, strip off line-ends.
def getbaseline(path):
try:
f = open(path, "r")
except:
print("Unable to read in conf ", path, " giving up")
sys.exit(1)
y = f.readlines()
# print("dadebug getbaseline return len",len(y))
# y hase line endings (newline)
out = []
for x in y:
out += [x.rstrip()]
return out
# Run dwarfdump, limiting output to gmaxlines lines
def rundwarfdump(td, dd, dwarfdumppath, objpath, lmaxlines):
out = []
print("Run:", dwarfdumppath, dd.ddopts[0], dd.ddopts[1], objpath)
p1 = Popen(
[dwarfdumppath, dd.ddopts[0], dd.ddopts[1], objpath],
stdout=PIPE,
stderr=PIPE,
)
bx = p1.stdout.read()
# bx is a byte array
# For windows this replaces cr lf with lf.
# for Linux does nothing
y = bx.decode("utf-8")
# y is a single long string, multiline.
z = y.splitlines()
for n, s in enumerate(z):
if n >= lmaxlines:
break
out += [s.rstrip()]
return out
# we are in windows copy dll from lib build to
# the dwarfdump build directory.
def copydll(td):
dllpath = os.path.join(td.bldbase,
"src/lib/libdwarf/libdwarf-0.dll")
targetdllpath= os.path.join(td.bldbase,
"src/bin/dwarfdump/libdwarf-0.dll")
if os.path.exists(dllpath):
if not os.path.exists(targetdllpath):
shutil.copy(dllpath,targetdllpath)
if __name__ == "__main__":
if len(sys.argv) != 5:
print("FAIL test_dwarfdump.py arg count wrong")
sys.exit(1)
td = tdata()
td.objtype = sys.argv[1]
td.buildsystem = sys.argv[2]
td.srcbase = sys.argv[3]
td.bldbase = sys.argv[4]
td.cwd = os.getcwd()
print("Running dwarfdump test")
td.tprint()
dd = ddfiles()
setupfilesinvolved(td, dd)
dd.ddprint()
confsrcpath = os.path.join(
td.srcbase, "src/bin/dwarfdump/dwarfdump.conf"
)
conftargpath = os.path.join(td.bldbase, "test", "dwarfdump.conf")
testconf = os.path.exists(conftargpath)
if not testconf:
print(
"Copy to local directory", confsrcpath, "to", conftargpath
)
copytobuild(confsrcpath, conftargpath)
if td.buildsystem == "meson":
mesonloc = os.path.join(td.bldbase, "dwarfdump.conf")
if not os.path.exists(mesonloc):
print(
"Copy to local directory", confsrcpath, "to", mesonloc
)
copytobuild(confsrcpath, mesonloc)
dwarfdumppath = os.path.join(
td.bldbase, "src/bin/dwarfdump/dwarfdump")
dwarfdumppathexe = os.path.join(
td.bldbase, "src/bin/dwarfdump/dwarfdump.exe")
if os.path.exists(dwarfdumppathexe):
dwarfdumppath = dwarfdumppathexe
copydll(td)
objpath = os.path.join(td.srcbase, "test", dd.testobj)
baseline_path = os.path.join(td.srcbase, "test", dd.testbase)
testout_path = os.path.join(td.bldbase, "test", dd.newtest)
# extract N lines from dwarfdump putput
print("Extract ", gmaxlines, "from dwarfdump output")
txtout = rundwarfdump(td, dd, dwarfdumppath, objpath, gmaxlines)
t = fixtimestamp(txtout)
txtout = t
writetextout(txtout, testout_path)
basetext = getbaseline(baseline_path)
# print("now do the diff")
diffs = difflib.unified_diff(basetext, txtout, lineterm="")
used = False
for s in diffs:
print("There are differences.")
used = True
break
if used:
tempfilepath = os.path.join(td.bldbase, "test", dd.newtest)
# For meson will need "test" too
if td.buildsystem == "meson":
tempfilepath = os.path.join(td.cwd, "test", dd.newtest)
else:
tempfilepath = os.path.join(td.cwd, dd.newtest)
print(
"Line Count Base=",
len(basetext),
" Line Count Test=",
len(txtout),
)
for s in diffs:
print(s)
print("FAIL test_dwarfdump.py on", td.objtype, " test object")
print("If update to baseline desired then:")
print("mv", tempfilepath, baseline_path)
sys.exit(1)
print("PASS", td.objtype)
sys.exit(0)
|