1from __future__ import print_function 2 3# lldb test suite imports 4from lldbsuite.test.decorators import * 5from lldbsuite.test.lldbtest import TestBase 6 7# gdb-remote-specific imports 8import lldbgdbserverutils 9from gdbremote_testcase import GdbRemoteTestCaseBase 10 11import binascii 12import stat 13import tempfile 14 15 16class TestGdbRemotePlatformFile(GdbRemoteTestCaseBase): 17 18 mydir = TestBase.compute_mydir(__file__) 19 20 @expectedFailureAll(oslist=["windows"]) 21 def test_platform_file_rdonly(self): 22 self.vFile_test(read=True) 23 24 @expectedFailureAll(oslist=["windows"]) 25 def test_platform_file_wronly(self): 26 self.vFile_test(write=True) 27 28 @expectedFailureAll(oslist=["windows"]) 29 def test_platform_file_rdwr(self): 30 self.vFile_test(read=True, write=True) 31 32 @expectedFailureAll(oslist=["windows"]) 33 def test_platform_file_wronly_append(self): 34 self.vFile_test(write=True, append=True) 35 36 @expectedFailureAll(oslist=["windows"]) 37 def test_platform_file_rdwr_append(self): 38 self.vFile_test(read=True, write=True, append=True) 39 40 @expectedFailureAll(oslist=["windows"]) 41 def test_platform_file_wronly_trunc(self): 42 self.vFile_test(write=True, trunc=True) 43 44 @expectedFailureAll(oslist=["windows"]) 45 def test_platform_file_rdwr_trunc(self): 46 self.vFile_test(read=True, write=True, trunc=True) 47 48 def test_platform_file_wronly_creat(self): 49 self.vFile_test(write=True, creat=True) 50 51 def test_platform_file_wronly_creat_excl(self): 52 self.vFile_test(write=True, creat=True, excl=True) 53 54 def test_platform_file_wronly_fail(self): 55 server = self.connect_to_debug_monitor() 56 self.assertIsNotNone(server) 57 58 # create a temporary directory 59 with tempfile.TemporaryDirectory() as temp_dir: 60 temp_path = os.path.join(temp_dir, "test") 61 self.assertFalse(os.path.exists(temp_path)) 62 63 # attempt to open the file without O_CREAT 64 self.do_handshake() 65 self.test_sequence.add_log_lines( 66 ["read packet: $vFile:open:%s,1,0#00" % ( 67 binascii.b2a_hex(temp_path.encode()).decode(),), 68 {"direction": "send", 69 "regex": r"^\$F-1,[0-9a-fA-F]+#[0-9a-fA-F]{2}$"}], 70 True) 71 self.expect_gdbremote_sequence() 72 73 def test_platform_file_wronly_creat_excl_fail(self): 74 server = self.connect_to_debug_monitor() 75 self.assertIsNotNone(server) 76 77 with tempfile.NamedTemporaryFile() as temp_file: 78 # attempt to open the file with O_CREAT|O_EXCL 79 self.do_handshake() 80 self.test_sequence.add_log_lines( 81 ["read packet: $vFile:open:%s,a01,0#00" % ( 82 binascii.b2a_hex(temp_file.name.encode()).decode(),), 83 {"direction": "send", 84 "regex": r"^\$F-1,[0-9a-fA-F]+#[0-9a-fA-F]{2}$"}], 85 True) 86 self.expect_gdbremote_sequence() 87 88 def expect_error(self): 89 self.test_sequence.add_log_lines( 90 [{"direction": "send", 91 "regex": r"^\$F-1,[0-9a-fA-F]+#[0-9a-fA-F]{2}$"}], 92 True) 93 self.expect_gdbremote_sequence() 94 95 def vFile_test(self, read=False, write=False, append=False, trunc=False, 96 creat=False, excl=False): 97 if read and write: 98 mode = 2 99 elif write: 100 mode = 1 101 else: # read 102 mode = 0 103 if append: 104 mode |= 8 105 if creat: 106 mode |= 0x200 107 if trunc: 108 mode |= 0x400 109 if excl: 110 mode |= 0x800 111 112 old_umask = os.umask(0) 113 try: 114 server = self.connect_to_debug_monitor() 115 finally: 116 os.umask(old_umask) 117 self.assertIsNotNone(server) 118 119 # create a temporary file with some data 120 test_data = 'some test data longer than 16 bytes\n' 121 if creat: 122 temp_dir = tempfile.TemporaryDirectory() 123 else: 124 temp_file = tempfile.NamedTemporaryFile() 125 126 try: 127 if creat: 128 temp_path = os.path.join(temp_dir.name, "test") 129 self.assertFalse(os.path.exists(temp_path)) 130 else: 131 temp_file.write(test_data.encode()) 132 temp_file.flush() 133 temp_path = temp_file.name 134 135 # open the file for reading 136 self.do_handshake() 137 self.test_sequence.add_log_lines( 138 ["read packet: $vFile:open:%s,%x,1b6#00" % ( 139 binascii.b2a_hex(temp_path.encode()).decode(), 140 mode), 141 {"direction": "send", 142 "regex": r"^\$F([0-9a-fA-F]+)#[0-9a-fA-F]{2}$", 143 "capture": {1: "fd"}}], 144 True) 145 146 context = self.expect_gdbremote_sequence() 147 self.assertIsNotNone(context) 148 fd = int(context["fd"], 16) 149 150 # read data from the file 151 self.reset_test_sequence() 152 self.test_sequence.add_log_lines( 153 ["read packet: $vFile:pread:%x,11,10#00" % (fd,)], 154 True) 155 if read: 156 self.test_sequence.add_log_lines( 157 [{"direction": "send", 158 "regex": r"^\$F([0-9a-fA-F]+);(.*)#[0-9a-fA-F]{2}$", 159 "capture": {1: "size", 2: "data"}}], 160 True) 161 context = self.expect_gdbremote_sequence() 162 self.assertIsNotNone(context) 163 if trunc: 164 self.assertEqual(context["size"], "0") 165 self.assertEqual(context["data"], "") 166 else: 167 self.assertEqual(context["size"], "11") # hex 168 self.assertEqual(context["data"], test_data[0x10:0x10 + 0x11]) 169 else: 170 self.expect_error() 171 172 # another offset 173 if read and not trunc: 174 self.reset_test_sequence() 175 self.test_sequence.add_log_lines( 176 ["read packet: $vFile:pread:%x,6,3#00" % (fd,), 177 {"direction": "send", 178 "regex": r"^\$F([0-9a-fA-F]+);(.+)#[0-9a-fA-F]{2}$", 179 "capture": {1: "size", 2: "data"}}], 180 True) 181 context = self.expect_gdbremote_sequence() 182 self.assertIsNotNone(context) 183 self.assertEqual(context["size"], "6") # hex 184 self.assertEqual(context["data"], test_data[3:3 + 6]) 185 186 # write data to the file 187 self.reset_test_sequence() 188 self.test_sequence.add_log_lines( 189 ["read packet: $vFile:pwrite:%x,6,somedata#00" % (fd,)], 190 True) 191 if write: 192 self.test_sequence.add_log_lines( 193 ["send packet: $F8#00"], 194 True) 195 self.expect_gdbremote_sequence() 196 else: 197 self.expect_error() 198 199 # close the file 200 self.reset_test_sequence() 201 self.test_sequence.add_log_lines( 202 ["read packet: $vFile:close:%x#00" % (fd,), 203 "send packet: $F0#00"], 204 True) 205 self.expect_gdbremote_sequence() 206 207 if write: 208 # check if the data was actually written 209 if creat: 210 temp_file = open(temp_path, "rb") 211 self.assertEqual(os.fstat(temp_file.fileno()).st_mode & 0o7777, 212 0o666) 213 temp_file.seek(0) 214 data = test_data.encode() 215 if trunc or creat: 216 data = b"\0" * 6 + b"somedata" 217 elif append: 218 data += b"somedata" 219 else: 220 data = data[:6] + b"somedata" + data[6 + 8:] 221 self.assertEqual(temp_file.read(), data) 222 finally: 223 if creat: 224 temp_dir.cleanup() 225 else: 226 temp_file.close() 227