From 474fb4295cc2ea9f96cfc885ae8e4cad0523be16 Mon Sep 17 00:00:00 2001
From: pompolic <pompolic@special-circumstanc.es>
Date: Thu, 21 Apr 2022 19:49:39 +0200
Subject: [PATCH] Test for adding init parser breakpoint

---
 gdb-port/breakpoint-manager.py            |  1 +
 gdb-port/tests/unit/breakpoint-manager.py | 13 +++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/gdb-port/breakpoint-manager.py b/gdb-port/breakpoint-manager.py
index 9ec9db8..99b348d 100644
--- a/gdb-port/breakpoint-manager.py
+++ b/gdb-port/breakpoint-manager.py
@@ -180,6 +180,7 @@ class BreakpointManager:
 		self.parse_whitespace.delete()
 		self.parse_xor.delete()
 
+	#TODO: this probably should be merged into the H_RULE functions array instead
 	def set_init_parser_breakpoint(self):
 		i_p_retq = self.locate_retq("init_parser")
 		self.init_parser_retq = InitParserBreakpoint("*"+hex(i_p_retq))
diff --git a/gdb-port/tests/unit/breakpoint-manager.py b/gdb-port/tests/unit/breakpoint-manager.py
index e5ff950..569a6d7 100644
--- a/gdb-port/tests/unit/breakpoint-manager.py
+++ b/gdb-port/tests/unit/breakpoint-manager.py
@@ -26,6 +26,7 @@ class BreakpointManagerSettingBreakpoints(unittest.TestCase):
 		# "constants" like rld_retq below could be moved to setUpClass (possibly better performance)
 		self.bpm = BreakpointManager(test_breakpoints)
 		self.arch = gdb.selected_frame().architecture()
+		# TODO: instead of locate_retq, get bp address from call_list, and disassemble
 		self.rld_retq = self.bpm.locate_retq(test_breakpoints[0])
 		self.lzw_retq = self.bpm.locate_retq(test_breakpoints[1])
 
@@ -37,12 +38,15 @@ class BreakpointManagerSettingBreakpoints(unittest.TestCase):
 		self.hpprbp_mock_object = self.hpprbp_patcher.start()
 		self.pvbp_patcher = unittest.mock.patch('__main__.ParserVirtualBreakpoint')
 		self.pvbp_mock_object = self.pvbp_patcher.start()
+		self.ipbp_patcher = unittest.mock.patch('__main__.InitParserBreakpoint')
+		self.ipbp_mock_object = self.ipbp_patcher.start()
 
 	def tearDown(self):
 		self.hpprbp_patcher.stop()
 		self.plprbp_patcher.stop()
 		self.hrbp_patcher.stop()
 		self.pvbp_patcher.stop()
+		self.ipbp_patcher.stop()
 
 # TODO: mock breakpoints, assert on arguments to constructor
 	def test_set_h_rule_breakpoints(self):
@@ -112,6 +116,15 @@ class BreakpointManagerSettingBreakpoints(unittest.TestCase):
 		#	with self.subTest(i=i):
 		#		virt_bps[i].delete.assert_called_once()
 
+	def test_set_init_parser_breakpoint(self):
+		self.bpm.set_init_parser_breakpoint()
+		#print(self.ipbp_mock_object.call_args.args)
+		gdb_addr_expr = self.ipbp_mock_object.call_args.args[0]
+		bp_addr = int(gdb_addr_expr.strip('*'), 16)
+		#TODO: probably could just get the machine word size
+		instr = self.arch.disassemble(bp_addr, bp_addr+8, 1)[0]
+		self.assertTrue(instr['asm'].startswith('ret'))
+
 	# TODO
 	#def tearDown(self):
 		#self.bpm.delete
-- 
GitLab