Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ffpuppet_26(tmp_path):
"""test multiprocess target"""
prefs = (tmp_path / "prefs.js")
prefs.write_bytes(b"//fftest_multi_proc\n")
with FFPuppet() as ffp:
with HTTPTestServer() as srv:
ffp.launch(TESTFF_BIN, prefs_js=str(prefs), location=srv.get_addr())
assert ffp.is_running()
assert not ffp.wait(timeout=0)
c_procs = Process(ffp.get_pid()).children()
assert c_procs
# terminate one of the child processes
c_procs[-1].terminate()
assert ffp.is_running()
ffp.close()
assert not ffp.is_running()
assert ffp.wait(timeout=0)
def test_ffpuppet_00(tmp_path):
"""test that invalid executables raise the right exception"""
with FFPuppet() as ffp:
with pytest.raises(IOError, match="is not an executable"):
ffp.launch(str(tmp_path))
def test_ffpuppet_20(tmp_path):
"""test detecting invalid prefs file"""
prefs = (tmp_path / "prefs.js")
prefs.write_bytes(b"//fftest_invalid_js\n")
with FFPuppet() as ffp:
with HTTPTestServer() as srv:
with pytest.raises(LaunchError, match="'.+?' is invalid"):
ffp.launch(TESTFF_BIN, location=srv.get_addr(), prefs_js=str(prefs))
def test_ffpuppet_28(tmp_path):
"""test launching with rr"""
if platform.system() != "Linux":
with pytest.raises(EnvironmentError, match="rr is only supported on Linux"):
FFPuppet(use_rr=True)
return
# NOTE: this can hang if ptrace is blocked by seccomp
if call(["rr", "record", "echo"]) != 0:
pytest.skip("Environment not configured to run rr")
with FFPuppet(use_rr=True) as ffp:
bin_path = str(check_output(["which", "echo"]).strip().decode("ascii"))
# launch will fail b/c 'echo' will exit right away but that's fine
with pytest.raises(LaunchError, match="Failure during browser startup"):
ffp.launch(bin_path, env_mod={"_RR_TRACE_DIR": str(tmp_path / "rr_wp")})
ffp.close()
assert ffp.reason == ffp.RC_EXITED
logs = (tmp_path / "logs")
ffp.save_logs(str(logs))
log_data = (logs / "log_stderr.txt").read_bytes()
# verify rr ran and executed the script
assert b"rr record" in log_data
def test_ffpuppet_24(tmp_path):
"""test collecting and cleaning up ASan logs"""
test_logs = list()
with FFPuppet() as ffp:
ffp.launch(TESTFF_BIN)
asan_prefix = os.path.join(ffp._logs.working_path, ffp._logs.PREFIX_SAN)
for i in range(3):
test_logs.append(".".join([asan_prefix, str(i)]))
# small log with nothing interesting
with open(test_logs[0], "w") as log_fp:
log_fp.write("SHORT LOG\n")
log_fp.write("filler line")
# crash on another thread
with open(test_logs[1], "w") as log_fp:
log_fp.write("GOOD LOG\n")
log_fp.write("==70811==ERROR: AddressSanitizer: SEGV on unknown address 0x00000BADF00D")
log_fp.write(" (pc 0x7f4c0bb54c67 bp 0x7f4c07bea380 sp 0x7f4c07bea360 T0)\n") # must be 2nd line
for _ in range(4): # pad out to 6 lines
log_fp.write("filler line\n")
# child log that should be ignored (created when parent crashes)
def test_ffpuppet_25(tmp_path):
"""test multiple minidumps"""
profile = (tmp_path / "profile")
profile.mkdir()
(profile / "minidumps").mkdir()
# 'symbols' directory needs to exist to satisfy a check
(profile / "symbols").mkdir()
with FFPuppet(use_profile=str(profile)) as ffp:
ffp.launch(TESTFF_BIN)
ffp._last_bin_path = ffp.profile
# create "test.dmp" files
md_path = os.path.join(ffp._last_bin_path, "minidumps")
with open(os.path.join(md_path, "test1.dmp"), "w") as out_fp:
out_fp.write("1a\n1b")
with open(os.path.join(md_path, "test2.dmp"), "w") as out_fp:
out_fp.write("2a\n2b")
with open(os.path.join(md_path, "test3.dmp"), "w") as out_fp:
out_fp.write("3a\n3b")
assert not ffp.is_healthy()
ffp.close()
logs = (tmp_path / "logs")
ffp.save_logs(str(logs))
assert any(logs.glob("log_minidump_01.txt"))
assert any(logs.glob("log_minidump_02.txt"))
def test_ffpuppet_06():
"""test is_running()"""
with FFPuppet() as ffp:
assert not ffp.is_running()
with HTTPTestServer() as srv:
ffp.launch(TESTFF_BIN, location=srv.get_addr())
assert ffp.is_running()
ffp.close()
assert not ffp.is_running()
assert not ffp.is_running() # call 2x
def test_ffpuppet_22():
"""test running multiple instances in parallel"""
ffps = list()
try:
with HTTPTestServer() as srv:
# use test pool size of 10
for _ in range(10):
ffps.append(FFPuppet())
# NOTE: launching truly in parallel can DoS the test webserver
ffps[-1].launch(TESTFF_BIN, location=srv.get_addr())
# list of ffps needs to be reversed to deal with inheriting open file handles in Popen
# this is not a problem in production only in the test environment
for ffp in reversed(ffps):
assert ffp.launches == 1
ffp.close()
finally:
for ffp in ffps:
ffp.clean_up()
def test_ffpuppet_31(tmp_path):
"""test _crashreports()"""
class StubbedLaunch(FFPuppet):
def __init__(self):
super(StubbedLaunch, self).__init__()
self._use_valgrind = True
def launch(self): # pylint: disable=arguments-differ
profile = (tmp_path / "profile")
profile.mkdir()
(profile / "minidumps").mkdir()
self.profile = str(profile)
def close(self, force_close=False):
if os.path.isdir(self.profile):
shutil.rmtree(self.profile)
self.profile = None
with StubbedLaunch() as ffp:
ffp.launch()
assert not any(ffp._crashreports())
ign_log = "%s.1" % (ffp._logs.PREFIX_SAN,)
def test_ffpuppet_19(tmp_path):
"""test launching with Valgrind"""
if platform.system() != "Linux":
with pytest.raises(EnvironmentError, match="Valgrind is only supported on Linux"):
FFPuppet(use_valgrind=True)
return
vmv = FFPuppet.VALGRIND_MIN_VERSION
try:
FFPuppet.VALGRIND_MIN_VERSION = 9999999999.99
with pytest.raises(EnvironmentError, match=r"Valgrind >= \d+\.\d+ is required"):
FFPuppet(use_valgrind=True)
FFPuppet.VALGRIND_MIN_VERSION = 0
with FFPuppet(use_valgrind=True) as ffp:
bin_path = str(check_output(["which", "echo"]).strip().decode("ascii"))
# launch will fail b/c 'echo' will exit right away but that's fine
with pytest.raises(LaunchError, match="Failure waiting for browser connection"):
ffp.launch(bin_path)
ffp.close()
ffp.save_logs(str(tmp_path / "logs"))
log_data = (tmp_path / "logs" / "log_stderr.txt").read_bytes()
# verify Valgrind ran and executed the script
assert b"valgrind -q" in log_data
assert b"[ffpuppet] Reason code: EXITED" in log_data
finally:
FFPuppet.VALGRIND_MIN_VERSION = vmv