1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
# lldb test suite imports
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import TestBase
# gdb-remote-specific imports
import lldbgdbserverutils
from gdbremote_testcase import GdbRemoteTestCaseBase
import binascii
import os
import stat
import struct
import typing
class GDBStat(typing.NamedTuple):
st_dev: int
st_ino: int
st_mode: int
st_nlink: int
st_uid: int
st_gid: int
st_rdev: int
st_size: int
st_blksize: int
st_blocks: int
st_atime: int
st_mtime: int
st_ctime: int
def uint32_or_zero(x):
return x if x < 2**32 and x >= 0 else 0
def uint32_or_max(x):
return x if x < 2**32 and x >= 0 else 2**32 - 1
def uint32_trunc(x):
return x & (2**32 - 1)
class TestGdbRemotePlatformFile(GdbRemoteTestCaseBase):
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_rdonly(self):
self.vFile_test(read=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_wronly(self):
self.vFile_test(write=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_rdwr(self):
self.vFile_test(read=True, write=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_wronly_append(self):
self.vFile_test(write=True, append=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_rdwr_append(self):
self.vFile_test(read=True, write=True, append=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_wronly_trunc(self):
self.vFile_test(write=True, trunc=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_rdwr_trunc(self):
self.vFile_test(read=True, write=True, trunc=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_wronly_creat(self):
self.vFile_test(write=True, creat=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_wronly_creat_excl(self):
self.vFile_test(write=True, creat=True, excl=True)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_wronly_fail(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
temp_path = self.getBuildArtifact("test")
self.assertFalse(os.path.exists(temp_path))
# attempt to open the file without O_CREAT
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:open:%s,1,0#00" % (
binascii.b2a_hex(temp_path.encode()).decode(),),
{"direction": "send",
"regex": r"^\$F-1,[0-9a-fA-F]+#[0-9a-fA-F]{2}$"}],
True)
self.expect_gdbremote_sequence()
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_wronly_creat_excl_fail(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
temp_file = self.getBuildArtifact("test")
with open(temp_file, "wb"):
pass
# attempt to open the file with O_CREAT|O_EXCL
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:open:%s,a01,0#00" % (
binascii.b2a_hex(temp_file.encode()).decode(),),
{"direction": "send",
"regex": r"^\$F-1,[0-9a-fA-F]+#[0-9a-fA-F]{2}$"}],
True)
self.expect_gdbremote_sequence()
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_size(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
temp_path = self.getBuildArtifact("test")
test_data = b"test data of some length"
with open(temp_path, "wb") as temp_file:
temp_file.write(test_data)
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:size:%s#00" % (
binascii.b2a_hex(temp_path.encode()).decode(),),
{"direction": "send",
"regex": r"^\$F([0-9a-fA-F]+)+#[0-9a-fA-F]{2}$",
"capture": {1: "size"}}],
True)
context = self.expect_gdbremote_sequence()
self.assertEqual(int(context["size"], 16), len(test_data))
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_mode(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
temp_path = self.getBuildArtifact("test")
test_mode = 0o751
with open(temp_path, "wb") as temp_file:
os.chmod(temp_file.fileno(), test_mode)
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:mode:%s#00" % (
binascii.b2a_hex(temp_path.encode()).decode(),),
{"direction": "send",
"regex": r"^\$F([0-9a-fA-F]+)+#[0-9a-fA-F]{2}$",
"capture": {1: "mode"}}],
True)
context = self.expect_gdbremote_sequence()
self.assertEqual(int(context["mode"], 16), test_mode)
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_mode_fail(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
temp_path = self.getBuildArtifact("nonexist")
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:mode:%s#00" % (
binascii.b2a_hex(temp_path.encode()).decode(),),
{"direction": "send",
"regex": r"^\$F-1,0*2+#[0-9a-fA-F]{2}$"}],
True)
self.expect_gdbremote_sequence()
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_exists(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
temp_path = self.getBuildArtifact("test")
with open(temp_path, "wb"):
pass
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:exists:%s#00" % (
binascii.b2a_hex(temp_path.encode()).decode(),),
"send packet: $F,1#00"],
True)
self.expect_gdbremote_sequence()
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_exists_not(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
test_path = self.getBuildArtifact("nonexist")
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:exists:%s#00" % (
binascii.b2a_hex(test_path.encode()).decode(),),
"send packet: $F,0#00"],
True)
self.expect_gdbremote_sequence()
@skipIfWindows
@add_test_categories(["llgs"])
def test_platform_file_fstat(self):
server = self.connect_to_debug_monitor()
self.assertIsNotNone(server)
with tempfile.NamedTemporaryFile() as temp_file:
temp_file.write(b"some test data for stat")
temp_file.flush()
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:open:%s,0,0#00" % (
binascii.b2a_hex(temp_file.name.encode()).decode(),),
{"direction": "send",
"regex": r"^\$F([0-9a-fA-F]+)#[0-9a-fA-F]{2}$",
"capture": {1: "fd"}}],
True)
context = self.expect_gdbremote_sequence()
self.assertIsNotNone(context)
fd = int(context["fd"], 16)
self.reset_test_sequence()
self.test_sequence.add_log_lines(
["read packet: $vFile:fstat:%x#00" % (fd,),
{"direction": "send",
"regex": r"^\$F([0-9a-fA-F]+);(.*)#[0-9a-fA-F]{2}$",
"capture": {1: "size", 2: "data"}}],
True)
context = self.expect_gdbremote_sequence()
self.assertEqual(int(context["size"], 16), 64)
# NB: we're using .encode() as a hack because the test suite
# is wrongly using (unicode) str instead of bytes
gdb_stat = GDBStat(
*struct.unpack(">IIIIIIIQQQIII",
self.decode_gdbremote_binary(context["data"])
.encode("iso-8859-1")))
sys_stat = os.fstat(temp_file.fileno())
self.assertEqual(gdb_stat.st_dev, uint32_or_zero(sys_stat.st_dev))
self.assertEqual(gdb_stat.st_ino, uint32_or_zero(sys_stat.st_ino))
self.assertEqual(gdb_stat.st_mode, uint32_trunc(sys_stat.st_mode))
self.assertEqual(gdb_stat.st_nlink, uint32_or_max(sys_stat.st_nlink))
self.assertEqual(gdb_stat.st_uid, uint32_or_zero(sys_stat.st_uid))
self.assertEqual(gdb_stat.st_gid, uint32_or_zero(sys_stat.st_gid))
self.assertEqual(gdb_stat.st_rdev, uint32_or_zero(sys_stat.st_rdev))
self.assertEqual(gdb_stat.st_size, sys_stat.st_size)
self.assertEqual(gdb_stat.st_blksize, sys_stat.st_blksize)
self.assertEqual(gdb_stat.st_blocks, sys_stat.st_blocks)
self.assertEqual(gdb_stat.st_atime,
uint32_or_zero(int(sys_stat.st_atime)))
self.assertEqual(gdb_stat.st_mtime,
uint32_or_zero(int(sys_stat.st_mtime)))
self.assertEqual(gdb_stat.st_ctime,
uint32_or_zero(int(sys_stat.st_ctime)))
self.reset_test_sequence()
self.test_sequence.add_log_lines(
["read packet: $vFile:close:%x#00" % (fd,),
"send packet: $F0#00"],
True)
self.expect_gdbremote_sequence()
def expect_error(self):
self.test_sequence.add_log_lines(
[{"direction": "send",
"regex": r"^\$F-1,[0-9a-fA-F]+#[0-9a-fA-F]{2}$"}],
True)
self.expect_gdbremote_sequence()
def vFile_test(self, read=False, write=False, append=False, trunc=False,
creat=False, excl=False):
if read and write:
mode = 2
elif write:
mode = 1
else: # read
mode = 0
if append:
mode |= 8
if creat:
mode |= 0x200
if trunc:
mode |= 0x400
if excl:
mode |= 0x800
old_umask = os.umask(0o22)
try:
server = self.connect_to_debug_monitor()
finally:
os.umask(old_umask)
self.assertIsNotNone(server)
# create a temporary file with some data
temp_path = self.getBuildArtifact("test")
test_data = 'some test data longer than 16 bytes\n'
if creat:
self.assertFalse(os.path.exists(temp_path))
else:
with open(temp_path, "wb") as temp_file:
temp_file.write(test_data.encode())
# open the file for reading
self.do_handshake()
self.test_sequence.add_log_lines(
["read packet: $vFile:open:%s,%x,1a0#00" % (
binascii.b2a_hex(temp_path.encode()).decode(),
mode),
{"direction": "send",
"regex": r"^\$F([0-9a-fA-F]+)#[0-9a-fA-F]{2}$",
"capture": {1: "fd"}}],
True)
context = self.expect_gdbremote_sequence()
self.assertIsNotNone(context)
fd = int(context["fd"], 16)
# read data from the file
self.reset_test_sequence()
self.test_sequence.add_log_lines(
["read packet: $vFile:pread:%x,11,10#00" % (fd,)],
True)
if read:
self.test_sequence.add_log_lines(
[{"direction": "send",
"regex": r"^\$F([0-9a-fA-F]+);(.*)#[0-9a-fA-F]{2}$",
"capture": {1: "size", 2: "data"}}],
True)
context = self.expect_gdbremote_sequence()
self.assertIsNotNone(context)
if trunc:
self.assertEqual(context["size"], "0")
self.assertEqual(context["data"], "")
else:
self.assertEqual(context["size"], "11") # hex
self.assertEqual(context["data"], test_data[0x10:0x10 + 0x11])
else:
self.expect_error()
# another offset
if read and not trunc:
self.reset_test_sequence()
self.test_sequence.add_log_lines(
["read packet: $vFile:pread:%x,6,3#00" % (fd,),
{"direction": "send",
"regex": r"^\$F([0-9a-fA-F]+);(.+)#[0-9a-fA-F]{2}$",
"capture": {1: "size", 2: "data"}}],
True)
context = self.expect_gdbremote_sequence()
self.assertIsNotNone(context)
self.assertEqual(context["size"], "6") # hex
self.assertEqual(context["data"], test_data[3:3 + 6])
# write data to the file
self.reset_test_sequence()
self.test_sequence.add_log_lines(
["read packet: $vFile:pwrite:%x,6,somedata#00" % (fd,)],
True)
if write:
self.test_sequence.add_log_lines(
["send packet: $F8#00"],
True)
self.expect_gdbremote_sequence()
else:
self.expect_error()
# close the file
self.reset_test_sequence()
self.test_sequence.add_log_lines(
["read packet: $vFile:close:%x#00" % (fd,),
"send packet: $F0#00"],
True)
self.expect_gdbremote_sequence()
if write:
# check if the data was actually written
with open(temp_path, "rb") as temp_file:
if creat:
self.assertEqual(os.fstat(temp_file.fileno()).st_mode & 0o7777,
0o640)
data = test_data.encode()
if trunc or creat:
data = b"\0" * 6 + b"somedata"
elif append:
data += b"somedata"
else:
data = data[:6] + b"somedata" + data[6 + 8:]
self.assertEqual(temp_file.read(), data)
|