109 lines · python
1import os2import os.path3import lldb4from lldbsuite.test.lldbtest import *5from lldbsuite.test.gdbclientutils import *6 7 8class GDBRemoteTestBase(TestBase):9 """10 Base class for GDB client tests.11 12 This class will setup and start a mock GDB server for the test to use.13 It also provides assertPacketLogReceived, which simplifies the checking14 of packets sent by the client.15 """16 17 NO_DEBUG_INFO_TESTCASE = True18 server = None19 server_socket_class = TCPServerSocket20 21 def setUp(self):22 TestBase.setUp(self)23 self.server = MockGDBServer(self.server_socket_class())24 self.server.start()25 26 def tearDown(self):27 # TestBase.tearDown will kill the process, but we need to kill it early28 # so its client connection closes and we can stop the server before29 # finally calling the base tearDown.30 if self.process() is not None:31 self.process().Kill()32 self.server.stop()33 TestBase.tearDown(self)34 35 def createTarget(self, yaml_path):36 """37 Create a target by auto-generating the object based on the given yaml38 instructions.39 40 This will track the generated object so it can be automatically removed41 during tearDown.42 """43 yaml_base, ext = os.path.splitext(yaml_path)44 obj_path = self.getBuildArtifact(yaml_base)45 self.yaml2obj(yaml_path, obj_path)46 return self.dbg.CreateTarget(obj_path)47 48 def connect(self, target, plugin="gdb-remote"):49 """50 Create a process by connecting to the mock GDB server.51 52 Includes assertions that the process was successfully created.53 """54 listener = self.dbg.GetListener()55 error = lldb.SBError()56 process = target.ConnectRemote(57 listener, self.server.get_connect_url(), plugin, error58 )59 self.assertTrue(error.Success(), error.description)60 self.assertTrue(process, PROCESS_IS_VALID)61 return process62 63 def assertPacketLogReceived(self, packets, log: PacketLog = None):64 """65 Assert that the mock server's packet log received the given packets.66 67 The packet log includes all packets sent by the client and received68 by the server. This function makes it easy to verify that the client69 sent the expected packets to the server.70 71 The check does not require that the packets be consecutive, but does72 require that they are ordered in the log as they ordered in the arg.73 """74 if log is None:75 received = self.server.responder.packetLog.get_received()76 else:77 received = log.get_received()78 i = 079 j = 080 81 while i < len(packets) and j < len(received):82 if received[j] == packets[i]:83 i += 184 j += 185 if i < len(packets):86 self.fail(87 "Did not receive: %s\nLast 10 packets:\n\t%s"88 % (packets[i], "\n\t".join(received))89 )90 91 92class GDBPlatformClientTestBase(GDBRemoteTestBase):93 """94 Base class for platform server clients.95 96 This class extends GDBRemoteTestBase by automatically connecting97 via "platform connect" in the setUp() method.98 """99 100 def setUp(self):101 super().setUp()102 self.runCmd("platform select remote-gdb-server")103 self.runCmd("platform connect " + self.server.get_connect_url())104 self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected())105 106 def tearDown(self):107 self.dbg.GetSelectedPlatform().DisconnectRemote()108 super().tearDown()109