Skip to content

Commit

Permalink
Make more functions in agent tests idempotent
Browse files Browse the repository at this point in the history
This will hopefully make it easier to support multiple subtests per test
in the future.
  • Loading branch information
DemiMarie committed Apr 23, 2024
1 parent c01e533 commit a0e7438
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions qrexec/tests/socket/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ def make_executable_service(self, *args):
util.make_executable_service(self.tempdir, *args)

def setUp(self):
self.agent = self.dom0 = None
self.tempdir = tempfile.mkdtemp()
os.mkdir(os.path.join(self.tempdir, "local-rpc"))
os.mkdir(os.path.join(self.tempdir, "rpc"))
os.mkdir(os.path.join(self.tempdir, "rpc-config"))
self.addCleanup(shutil.rmtree, self.tempdir)

def start_agent(self):
if self.agent is not None:
return
env = os.environ.copy()
env["LD_LIBRARY_PATH"] = os.path.join(ROOT_PATH, "libqrexec")
env["VCHAN_DOMAIN"] = str(self.domain)
Expand Down Expand Up @@ -127,8 +130,11 @@ def wait_for_agent_children(self):
psutil.wait_procs(children)

def connect_dom0(self):
dom0 = qrexec.vchan_client(self.tempdir, self.domain, 0, 512)
if self.dom0 is not None:
return self.dom0
dom0 = self.dom0 = qrexec.vchan_client(self.tempdir, self.domain, 0, 512)
self.addCleanup(dom0.close)
dom0.handshake()
return dom0

def connect_target(self):
Expand All @@ -150,13 +156,11 @@ def test_handshake(self):
self.start_agent()

dom0 = self.connect_dom0()
dom0.handshake()

def _test_just_exec(self, cmd):
self.start_agent()

dom0 = self.connect_dom0()
dom0.handshake()

user = getpass.getuser().encode("ascii")

Expand Down Expand Up @@ -235,7 +239,6 @@ def test_exec_cmdline(self):
self.start_agent()

dom0 = self.connect_dom0()
dom0.handshake()

user = getpass.getuser().encode("ascii")

Expand All @@ -260,7 +263,6 @@ def test_trigger_service(self):
target_domain_name = b"target_domain"

dom0 = self.connect_dom0()
dom0.handshake()

client = self.connect_client()
ident = self.trigger_service(
Expand All @@ -286,7 +288,6 @@ def test_trigger_service_refused(self):
target_domain_name = b"target_domain"

dom0 = self.connect_dom0()
dom0.handshake()

client = self.connect_client()
ident = self.trigger_service(
Expand Down Expand Up @@ -337,7 +338,6 @@ def execute_qubesrpc(self, service: str, src_domain_name: str):
self.start_agent()

dom0 = self.connect_dom0()
dom0.handshake()

user = getpass.getuser()
cmdline = "{}:QUBESRPC {} {}\0".format(
Expand Down Expand Up @@ -977,7 +977,6 @@ def execute(self, cmd: str):
self.start_agent()

dom0 = self.connect_dom0()
dom0.handshake()

user = getpass.getuser()
cmdline = "{}:{}\0".format(user, cmd).encode("ascii")
Expand Down

0 comments on commit a0e7438

Please sign in to comment.