| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from pathlib import Path |
| 20 |
import pytest |
| 21 |
import os |
| 22 |
import logging |
| 23 |
|
| 24 |
|
| 25 |
from pyocd.core.helpers import ConnectHelper |
| 26 |
from pyocd.core.target import Target |
| 27 |
from pyocd.debug import semihost |
| 28 |
from pyocd.utility.server import StreamServer |
| 29 |
from pyocd.utility.timeout import Timeout |
| 30 |
from pyocd.utility.compatibility import to_bytes_safe |
| 31 |
|
| 32 |
@pytest.fixture(scope='module') |
| 33 |
def tgt(request): |
| 34 |
session = None |
| 35 |
try: |
| 36 |
session = ConnectHelper.session_with_chosen_probe(blocking=False, return_first=True) |
| 37 |
except Exception as error: |
| 38 |
logging.error("Exception during session_with_chosen_probe", exc_info=error) |
| 39 |
if session is None: |
| 40 |
pytest.skip("No probe present") |
| 41 |
return |
| 42 |
session.open() |
| 43 |
session.options['resume_on_disconnect'] = False |
| 44 |
assert session.target |
| 45 |
session.target.reset_and_halt() |
| 46 |
|
| 47 |
def close_session(): |
| 48 |
session.close() |
| 49 |
|
| 50 |
request.addfinalizer(close_session) |
| 51 |
return session.target |
| 52 |
|
| 53 |
@pytest.fixture(scope='module') |
| 54 |
def ctx(tgt): |
| 55 |
return tgt.get_target_context() |
| 56 |
|
| 57 |
@pytest.fixture(scope='function') |
| 58 |
def semihostagent(ctx, request): |
| 59 |
io_handler = semihost.InternalSemihostIOHandler() |
| 60 |
agent = semihost.SemihostAgent(ctx, io_handler) |
| 61 |
def cleanup(): |
| 62 |
agent.cleanup() |
| 63 |
request.addfinalizer(cleanup) |
| 64 |
return agent |
| 65 |
|
| 66 |
@pytest.fixture(scope='module') |
| 67 |
def ramrgn(tgt): |
| 68 |
map = tgt.get_memory_map() |
| 69 |
for rgn in map: |
| 70 |
if rgn.is_ram: |
| 71 |
return rgn |
| 72 |
pytest.skip("No RAM available to load test") |
| 73 |
|
| 74 |
def run_til_halt(tgt, semihostagent): |
| 75 |
with Timeout(2.0) as t: |
| 76 |
logging.info("Resuming target") |
| 77 |
tgt.resume() |
| 78 |
|
| 79 |
while True: |
| 80 |
if not t.check(): |
| 81 |
tgt.halt() |
| 82 |
return False |
| 83 |
if tgt.get_state() == Target.State.HALTED: |
| 84 |
logging.info("Target halted") |
| 85 |
didHandle = semihostagent.check_and_handle_semihost_request() |
| 86 |
if didHandle: |
| 87 |
logging.info("Semihost request handled") |
| 88 |
else: |
| 89 |
logging.info("Non-semihost break") |
| 90 |
return didHandle |
| 91 |
|
| 92 |
NOP = 0x46c0 |
| 93 |
BKPT_00 = 0xbe00 |
| 94 |
BKPT_AB = 0xbeab |
| 95 |
|
| 96 |
class RecordingSemihostIOHandler(semihost.SemihostIOHandler): |
| 97 |
"""@brief Semihost IO handler that records output. |
| 98 |
|
| 99 |
This handler is only meant to be used for console I/O since it doesn't implement |
| 100 |
open() or close(). |
| 101 |
""" |
| 102 |
def __init__(self): |
| 103 |
self._out_data = {} |
| 104 |
self._in_data = {} |
| 105 |
|
| 106 |
def set_input_data(self, fd, data): |
| 107 |
self._in_data[fd] = data |
| 108 |
|
| 109 |
def get_output_data(self, fd): |
| 110 |
if fd in self._out_data: |
| 111 |
return self._out_data[fd] |
| 112 |
else: |
| 113 |
return None |
| 114 |
|
| 115 |
def write(self, fd, ptr, length): |
| 116 |
assert self.agent |
| 117 |
if fd not in self._out_data: |
| 118 |
self._out_data[fd] = b'' |
| 119 |
assert self.agent |
| 120 |
s = self.agent.get_data(ptr, length) |
| 121 |
self._out_data[fd] += s |
| 122 |
return 0 |
| 123 |
|
| 124 |
def read(self, fd, ptr, length): |
| 125 |
assert self.agent |
| 126 |
if fd not in self._in_data: |
| 127 |
return length |
| 128 |
d = self._in_data[fd][:length] |
| 129 |
self._in_data[fd] = self._in_data[fd][length:] |
| 130 |
assert self.agent |
| 131 |
self.agent.context.write_memory_block8(ptr, bytearray(to_bytes_safe(d))) |
| 132 |
return length - len(d) |
| 133 |
|
| 134 |
def readc(self): |
| 135 |
if semihost.STDIN_FD not in self._in_data: |
| 136 |
return -1 |
| 137 |
d = self._in_data[semihost.STDIN_FD][:1] |
| 138 |
self._in_data[semihost.STDIN_FD] = self._in_data[semihost.STDIN_FD][1:] |
| 139 |
if len(d): |
| 140 |
return ord(d[0]) |
| 141 |
else: |
| 142 |
return -1 |
| 143 |
|
| 144 |
class SemihostRequestBuilder: |
| 145 |
"""@brief Utility to build code and set registers to perform a semihost request.""" |
| 146 |
def __init__(self, tgt, semihostagent, ramrgn): |
| 147 |
self.tgt = tgt |
| 148 |
self.ctx = tgt.get_target_context() |
| 149 |
self.semihostagent = semihostagent |
| 150 |
self.ramrgn = ramrgn |
| 151 |
|
| 152 |
def set_agent(self, agent): |
| 153 |
self.semihostagent = agent |
| 154 |
|
| 155 |
def setup_semihost_request(self, rqnum): |
| 156 |
assert self.tgt.get_state() == Target.State.HALTED |
| 157 |
|
| 158 |
self.ctx.write16(self.ramrgn.start, NOP) |
| 159 |
self.ctx.write16(self.ramrgn.start + 2, BKPT_AB) |
| 160 |
self.ctx.write16(self.ramrgn.start + 4, BKPT_00) |
| 161 |
|
| 162 |
self.ctx.write_core_register('pc', self.ramrgn.start) |
| 163 |
self.ctx.write_core_register('sp', self.ramrgn.start + 0x100) |
| 164 |
self.ctx.write_core_register('r0', rqnum) |
| 165 |
self.ctx.write_core_register('r1', self.ramrgn.start + 0x200) |
| 166 |
self.ctx.flush() |
| 167 |
return self.ramrgn.start + 0x200 |
| 168 |
|
| 169 |
def do_open(self, filename, mode): |
| 170 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_OPEN) |
| 171 |
|
| 172 |
|
| 173 |
filename = bytearray(to_bytes_safe(filename) + b'\x00') |
| 174 |
self.ctx.write_memory_block8(argsptr + 12, filename) |
| 175 |
|
| 176 |
self.ctx.write32(argsptr, argsptr + 12) |
| 177 |
self.ctx.write32(argsptr + 4, semihost.SemihostAgent.OPEN_MODES.index(mode)) |
| 178 |
self.ctx.write32(argsptr + 8, len(filename) - 1) |
| 179 |
|
| 180 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 181 |
assert was_semihost |
| 182 |
|
| 183 |
result = self.ctx.read_core_register('r0') |
| 184 |
return result |
| 185 |
|
| 186 |
def do_close(self, fd): |
| 187 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_CLOSE) |
| 188 |
self.ctx.write32(argsptr, fd) |
| 189 |
|
| 190 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 191 |
assert was_semihost |
| 192 |
|
| 193 |
result = self.ctx.read_core_register('r0') |
| 194 |
return result |
| 195 |
|
| 196 |
def do_write(self, fd, data): |
| 197 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_WRITE) |
| 198 |
|
| 199 |
|
| 200 |
data = to_bytes_safe(data) |
| 201 |
self.ctx.write_memory_block8(argsptr + 12, data) |
| 202 |
|
| 203 |
self.ctx.write32(argsptr, fd) |
| 204 |
self.ctx.write32(argsptr + 4, argsptr + 12) |
| 205 |
self.ctx.write32(argsptr + 8, len(data)) |
| 206 |
self.ctx.flush() |
| 207 |
|
| 208 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 209 |
assert was_semihost |
| 210 |
|
| 211 |
result = self.ctx.read_core_register('r0') |
| 212 |
return result |
| 213 |
|
| 214 |
def do_writec(self, c): |
| 215 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_WRITEC) |
| 216 |
self.ctx.write8(argsptr, ord(c)) |
| 217 |
|
| 218 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 219 |
assert was_semihost |
| 220 |
|
| 221 |
result = self.ctx.read_core_register('r0') |
| 222 |
return result |
| 223 |
|
| 224 |
def do_write0(self, data): |
| 225 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_WRITE0) |
| 226 |
|
| 227 |
data = data + b'\x00' |
| 228 |
self.ctx.write_memory_block8(argsptr, data) |
| 229 |
|
| 230 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 231 |
assert was_semihost |
| 232 |
|
| 233 |
result = self.ctx.read_core_register('r0') |
| 234 |
return result |
| 235 |
|
| 236 |
def do_read(self, fd, length): |
| 237 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_READ) |
| 238 |
|
| 239 |
|
| 240 |
self.ctx.write_memory_block8(argsptr + 12, bytearray(b'\x00') * length) |
| 241 |
|
| 242 |
self.ctx.write32(argsptr, fd) |
| 243 |
self.ctx.write32(argsptr + 4, argsptr + 12) |
| 244 |
self.ctx.write32(argsptr + 8, length) |
| 245 |
self.ctx.flush() |
| 246 |
|
| 247 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 248 |
assert was_semihost |
| 249 |
|
| 250 |
result = self.ctx.read_core_register('r0') |
| 251 |
|
| 252 |
|
| 253 |
data = bytes(self.tgt.read_memory_block8(argsptr + 12, length - result)) |
| 254 |
|
| 255 |
return result, data |
| 256 |
|
| 257 |
def do_seek(self, fd, pos): |
| 258 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_SEEK) |
| 259 |
|
| 260 |
self.ctx.write32(argsptr, fd) |
| 261 |
self.ctx.write32(argsptr + 4, pos) |
| 262 |
self.ctx.flush() |
| 263 |
|
| 264 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 265 |
assert was_semihost |
| 266 |
|
| 267 |
result = self.ctx.read_core_register('r0') |
| 268 |
|
| 269 |
return result |
| 270 |
|
| 271 |
def do_flen(self, fd): |
| 272 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_FLEN) |
| 273 |
|
| 274 |
self.ctx.write32(argsptr, fd) |
| 275 |
self.ctx.flush() |
| 276 |
|
| 277 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 278 |
assert was_semihost |
| 279 |
|
| 280 |
result = self.ctx.read_core_register('r0') |
| 281 |
|
| 282 |
return result |
| 283 |
|
| 284 |
def do_istty(self, fd): |
| 285 |
argsptr = self.setup_semihost_request(semihost.SemihostingRequests.SYS_ISTTY) |
| 286 |
|
| 287 |
self.ctx.write32(argsptr, fd) |
| 288 |
self.ctx.flush() |
| 289 |
|
| 290 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 291 |
assert was_semihost |
| 292 |
|
| 293 |
result = self.ctx.read_core_register('r0') |
| 294 |
|
| 295 |
return result |
| 296 |
|
| 297 |
def do_no_args_call(self, rq): |
| 298 |
argsptr = self.setup_semihost_request(rq) |
| 299 |
self.ctx.write_core_register('r1', 0) |
| 300 |
|
| 301 |
was_semihost = run_til_halt(self.tgt, self.semihostagent) |
| 302 |
assert was_semihost |
| 303 |
|
| 304 |
result = self.ctx.read_core_register('r0') |
| 305 |
return result |
| 306 |
|
| 307 |
@pytest.fixture(scope='function') |
| 308 |
def semihost_builder(tgt, semihostagent, ramrgn): |
| 309 |
return SemihostRequestBuilder(tgt, semihostagent, ramrgn) |
| 310 |
|
| 311 |
@pytest.fixture(scope='function') |
| 312 |
def console_semihost_builder(semihost_builder): |
| 313 |
console = RecordingSemihostIOHandler() |
| 314 |
agent = semihost.SemihostAgent(semihost_builder.ctx, console=console) |
| 315 |
semihost_builder.set_agent(agent) |
| 316 |
return semihost_builder |
| 317 |
|
| 318 |
@pytest.fixture |
| 319 |
def delete_testfile(request): |
| 320 |
def delete_it(): |
| 321 |
try: |
| 322 |
os.remove("testfile") |
| 323 |
except IOError: |
| 324 |
pass |
| 325 |
request.addfinalizer(delete_it) |
| 326 |
|
| 327 |
class TestSemihosting: |
| 328 |
"""@brief Tests for semihost requests.""" |
| 329 |
def test_open_stdio(self, semihost_builder): |
| 330 |
fd = semihost_builder.do_open(":tt", 'r') |
| 331 |
assert fd == 0 |
| 332 |
|
| 333 |
fd = semihost_builder.do_open(":tt", 'w') |
| 334 |
assert fd == 1 |
| 335 |
|
| 336 |
fd = semihost_builder.do_open(":tt", 'a') |
| 337 |
assert fd == 2 |
| 338 |
|
| 339 |
def test_open_home_file(self, semihost_builder, request): |
| 340 |
testfilepath = Path("~/testfile").expanduser() |
| 341 |
|
| 342 |
def delete_it(): |
| 343 |
try: |
| 344 |
testfilepath.unlink() |
| 345 |
except OSError: |
| 346 |
pass |
| 347 |
request.addfinalizer(delete_it) |
| 348 |
|
| 349 |
fd = semihost_builder.do_open("~/testfile", 'wb') |
| 350 |
assert fd > 2 |
| 351 |
|
| 352 |
result = semihost_builder.do_write(fd, b"foo") |
| 353 |
assert result == 0 |
| 354 |
|
| 355 |
result = semihost_builder.do_close(fd) |
| 356 |
assert result == 0 |
| 357 |
|
| 358 |
data = testfilepath.read_bytes() |
| 359 |
assert data == b"foo" |
| 360 |
|
| 361 |
def test_open_close_file(self, semihost_builder, delete_testfile): |
| 362 |
fd = semihost_builder.do_open("testfile", 'w+b') |
| 363 |
assert fd > 2 |
| 364 |
|
| 365 |
result = semihost_builder.do_close(fd) |
| 366 |
assert result == 0 |
| 367 |
|
| 368 |
@pytest.mark.parametrize(("mode", "writeData", "pos", "readLen", "readResult"), [ |
| 369 |
("w+b", b"12345678", 0, 8, 0), |
| 370 |
("w+b", b"hi", 0, 2, 0), |
| 371 |
("w+b", b"hello", 2, 3, 0), |
| 372 |
("w+b", b"", 0, 4, 4), |
| 373 |
("w+b", b"abcd", -1, 0, 0), |
| 374 |
("w+", "", 0, 0, 0), |
| 375 |
("w+", "1", 0, 1, 0), |
| 376 |
("w+", "12", 0, 2, 0), |
| 377 |
("w+", "123", 0, 3, 0), |
| 378 |
("w+", "1234", 0, 4, 0), |
| 379 |
("w+", "Hello this is an extraaaaaaaa long string", 0, 41, 0), |
| 380 |
("w+", "äöüÄÖÜ😀\u4500", 0, 6*2+4+3, 0), ncodings |
| 381 |
]) |
| 382 |
def test_file_write_read(self, semihost_builder, delete_testfile, mode, writeData, pos, readLen, readResult): |
| 383 |
fd = semihost_builder.do_open("testfile", mode) |
| 384 |
assert fd > 2 |
| 385 |
|
| 386 |
if len(writeData): |
| 387 |
result = semihost_builder.do_write(fd, writeData) |
| 388 |
assert result == 0 |
| 389 |
|
| 390 |
result = semihost_builder.do_flen(fd) |
| 391 |
assert result == len(to_bytes_safe(writeData)) |
| 392 |
|
| 393 |
if pos != -1: |
| 394 |
result = semihost_builder.do_seek(fd, pos) |
| 395 |
assert result == 0 |
| 396 |
|
| 397 |
result, data = semihost_builder.do_read(fd, readLen) |
| 398 |
assert result == readResult |
| 399 |
assert data == to_bytes_safe(writeData[pos:pos + readLen]) |
| 400 |
|
| 401 |
result = semihost_builder.do_close(fd) |
| 402 |
assert result == 0 |
| 403 |
|
| 404 |
def test_console_write(self, semihost_builder): |
| 405 |
console = RecordingSemihostIOHandler() |
| 406 |
agent = semihost.SemihostAgent(semihost_builder.ctx, console=console) |
| 407 |
semihost_builder.set_agent(agent) |
| 408 |
|
| 409 |
result = semihost_builder.do_write(semihost.STDOUT_FD, b'hello world') |
| 410 |
assert result == 0 |
| 411 |
|
| 412 |
assert console.get_output_data(semihost.STDOUT_FD) == b'hello world' |
| 413 |
|
| 414 |
def test_console_write_binary_pattern(self, semihost_builder): |
| 415 |
console = RecordingSemihostIOHandler() |
| 416 |
agent = semihost.SemihostAgent(semihost_builder.ctx, console=console) |
| 417 |
semihost_builder.set_agent(agent) |
| 418 |
|
| 419 |
binary_pattern = bytes.fromhex('8152666f72706cff08000000000000000000000000000000020000000000000000000000000000000300000000000000000000000000000016000000') |
| 420 |
assert len(binary_pattern) == 60 |
| 421 |
result = semihost_builder.do_write(semihost.STDOUT_FD, binary_pattern) |
| 422 |
assert result == 0 |
| 423 |
assert console.get_output_data(semihost.STDOUT_FD) == binary_pattern |
| 424 |
|
| 425 |
def test_console_writec(self, semihost_builder): |
| 426 |
console = RecordingSemihostIOHandler() |
| 427 |
agent = semihost.SemihostAgent(semihost_builder.ctx, console=console) |
| 428 |
semihost_builder.set_agent(agent) |
| 429 |
|
| 430 |
for c in 'abcdef': |
| 431 |
result = semihost_builder.do_writec(c) |
| 432 |
assert result == 0 |
| 433 |
|
| 434 |
assert console.get_output_data(semihost.STDOUT_FD) == b'abcdef' |
| 435 |
|
| 436 |
def test_console_write0(self, semihost_builder): |
| 437 |
console = RecordingSemihostIOHandler() |
| 438 |
agent = semihost.SemihostAgent(semihost_builder.ctx, console=console) |
| 439 |
semihost_builder.set_agent(agent) |
| 440 |
|
| 441 |
result = semihost_builder.do_write0(b'this is a very looooooooooooooooooooooooooooooooooooooooooooong string with more than 32 characters') |
| 442 |
assert result == 0 |
| 443 |
|
| 444 |
assert console.get_output_data(semihost.STDOUT_FD) == b'this is a very looooooooooooooooooooooooooooooooooooooooooooong string with more than 32 characters' |
| 445 |
|
| 446 |
@pytest.mark.parametrize(("data", "readlen"), [ |
| 447 |
(b"12345678", 8), |
| 448 |
(b"hi", 2), |
| 449 |
(b"hello", 3), |
| 450 |
(b"", 4), |
| 451 |
(b"abcd", 0) |
| 452 |
]) |
| 453 |
def test_console_read(self, semihost_builder, data, readlen): |
| 454 |
console = RecordingSemihostIOHandler() |
| 455 |
agent = semihost.SemihostAgent(semihost_builder.ctx, console=console) |
| 456 |
semihost_builder.set_agent(agent) |
| 457 |
|
| 458 |
console.set_input_data(semihost.STDIN_FD, data) |
| 459 |
|
| 460 |
result, resultData = semihost_builder.do_read(semihost.STDIN_FD, readlen) |
| 461 |
|
| 462 |
assert result == (readlen - min(readlen, len(data))) |
| 463 |
|
| 464 |
expectedData = data[:min(len(data), readlen)] |
| 465 |
assert resultData == expectedData |
| 466 |
|
| 467 |
def test_console_readc(self, semihost_builder): |
| 468 |
console = RecordingSemihostIOHandler() |
| 469 |
agent = semihost.SemihostAgent(semihost_builder.ctx, console=console) |
| 470 |
semihost_builder.set_agent(agent) |
| 471 |
|
| 472 |
console.set_input_data(semihost.STDIN_FD, 'x') |
| 473 |
|
| 474 |
result = semihost_builder.do_no_args_call(semihost.SemihostingRequests.SYS_READC) |
| 475 |
assert chr(result) == 'x' |
| 476 |
|
| 477 |
def test_clock(self, semihost_builder): |
| 478 |
result = semihost_builder.do_no_args_call(semihost.SemihostingRequests.SYS_CLOCK) |
| 479 |
assert result != -1 |
| 480 |
assert result != 0 |
| 481 |
logging.info("clock = %d cs", result) |
| 482 |
|
| 483 |
result2 = semihost_builder.do_no_args_call(semihost.SemihostingRequests.SYS_CLOCK) |
| 484 |
assert result2 != -1 |
| 485 |
assert result2 != 0 |
| 486 |
assert result2 > result |
| 487 |
logging.info("clock = %d cs", result2) |
| 488 |
|
| 489 |
def test_time(self, semihost_builder): |
| 490 |
result = semihost_builder.do_no_args_call(semihost.SemihostingRequests.SYS_TIME) |
| 491 |
assert result != 0 |
| 492 |
logging.info("time = %d sec", result) |
| 493 |
|
| 494 |
def test_errno_no_err(self, semihost_builder): |
| 495 |
result = semihost_builder.do_no_args_call(semihost.SemihostingRequests.SYS_ERRNO) |
| 496 |
assert result == 0 |
| 497 |
|
| 498 |
@pytest.mark.parametrize(("fd"), [ |
| 499 |
(semihost.STDIN_FD), |
| 500 |
(semihost.STDOUT_FD), |
| 501 |
(semihost.STDERR_FD), |
| 502 |
]) |
| 503 |
def test_istty_stdio(self, semihost_builder, fd): |
| 504 |
result = semihost_builder.do_istty(fd) |
| 505 |
assert result == 1 |
| 506 |
|
| 507 |
def test_istty_non_stdio(self, semihost_builder, delete_testfile): |
| 508 |
fd = semihost_builder.do_open("testfile", 'w+b') |
| 509 |
assert fd > 2 |
| 510 |
|
| 511 |
result = semihost_builder.do_istty(fd) |
| 512 |
assert result == 0 |
| 513 |
|
| 514 |
result = semihost_builder.do_close(fd) |
| 515 |
assert result == 0 |
| 516 |
|
| 517 |
def test_feature_bits(self, semihost_builder): |
| 518 |
fd = semihost_builder.do_open(":semihosting-features", 'rb') |
| 519 |
assert fd > 2 |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
n = semihost_builder.do_flen(fd) |
| 524 |
assert n >= 5 |
| 525 |
|
| 526 |
result = semihost_builder.do_istty(fd) |
| 527 |
assert result == 0 |
| 528 |
|
| 529 |
result = semihost_builder.do_seek(fd, 0) |
| 530 |
assert result == 0 |
| 531 |
|
| 532 |
|
| 533 |
result, data = semihost_builder.do_read(fd, 4) |
| 534 |
assert result == 0 |
| 535 |
assert data == b'SHFB' |
| 536 |
|
| 537 |
result = semihost_builder.do_seek(fd, 4) |
| 538 |
assert result == 0 |
| 539 |
|
| 540 |
|
| 541 |
result, data = semihost_builder.do_read(fd, 1) |
| 542 |
assert result == 0 |
| 543 |
assert (data[0] & 0x3) == 0x02 |
| 544 |
|
| 545 |
|
| 546 |
result = semihost_builder.do_seek(fd, 2) |
| 547 |
assert result == 0 |
| 548 |
|
| 549 |
result, data = semihost_builder.do_read(fd, 2) |
| 550 |
assert result == 0 |
| 551 |
assert data == b'FB' |
| 552 |
|
| 553 |
|
| 554 |
result = semihost_builder.do_close(fd) |
| 555 |
assert result == 0 |
| 556 |
|
| 557 |
|
| 558 |
@pytest.fixture(scope='function') |
| 559 |
def telnet_server(request): |
| 560 |
telnet_server = StreamServer( |
| 561 |
0, |
| 562 |
True, |
| 563 |
"Semihost", |
| 564 |
False, |
| 565 |
extra_info="test" |
| 566 |
) |
| 567 |
def stopit(): |
| 568 |
telnet_server.stop() |
| 569 |
request.addfinalizer(stopit) |
| 570 |
return telnet_server |
| 571 |
|
| 572 |
@pytest.fixture(scope='function') |
| 573 |
def semihost_telnet_agent(ctx, telnet_server, request): |
| 574 |
semihost_console = semihost.ConsoleIOHandler(telnet_server) |
| 575 |
agent = semihost.SemihostAgent(ctx, console=semihost_console) |
| 576 |
def cleanup(): |
| 577 |
agent.cleanup() |
| 578 |
request.addfinalizer(cleanup) |
| 579 |
return agent |
| 580 |
|
| 581 |
@pytest.fixture(scope='function') |
| 582 |
def semihost_telnet_builder(tgt, semihost_telnet_agent, ramrgn): |
| 583 |
return SemihostRequestBuilder(tgt, semihost_telnet_agent, ramrgn) |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
class TestSemihostAgent: |
| 645 |
def test_no_io_handler(self, ctx): |
| 646 |
a = semihost.SemihostAgent(ctx, io_handler=None, console=None) |
| 647 |
assert type(a.io_handler) is semihost.SemihostIOHandler |
| 648 |
assert type(a.console) is semihost.SemihostIOHandler |
| 649 |
assert a.console is a.io_handler |
| 650 |
|
| 651 |
def test_only_io_handler(self, ctx): |
| 652 |
c = RecordingSemihostIOHandler() |
| 653 |
a = semihost.SemihostAgent(ctx, io_handler=c, console=None) |
| 654 |
assert a.io_handler is c |
| 655 |
assert a.console is c |
| 656 |
|
| 657 |
def test_only_console(self, ctx): |
| 658 |
c = RecordingSemihostIOHandler() |
| 659 |
a = semihost.SemihostAgent(ctx, io_handler=None, console=c) |
| 660 |
assert type(a.io_handler) is semihost.SemihostIOHandler |
| 661 |
assert a.console is c |
| 662 |
|
| 663 |
@pytest.fixture |
| 664 |
def ioh(ctx): |
| 665 |
handler = semihost.SemihostIOHandler() |
| 666 |
agent = semihost.SemihostAgent(ctx, io_handler=handler) |
| 667 |
return handler, agent |
| 668 |
|
| 669 |
class TestSemihostIOHandlerBase: |
| 670 |
@pytest.mark.parametrize(("filename", "mode", "expectedFd"), [ |
| 671 |
(":tt", 'r', semihost.STDIN_FD), |
| 672 |
(":tt", 'w', semihost.STDOUT_FD), |
| 673 |
(":tt", 'a', semihost.STDERR_FD), |
| 674 |
(":tt", 'r+b', -1), |
| 675 |
("somefile", 'r+b', None), |
| 676 |
]) |
| 677 |
def test_std_open(self, ctx, ramrgn, ioh, filename, mode, expectedFd): |
| 678 |
handler, agent = ioh |
| 679 |
ctx.write_memory_block8(ramrgn.start, bytearray(to_bytes_safe(filename) + b'\x00')) |
| 680 |
assert handler._std_open(ramrgn.start, len(filename), mode) == (expectedFd, filename) |
| 681 |
|
| 682 |
@pytest.mark.parametrize(("op", "args"), [ |
| 683 |
('open', (0, 0, 'r')), |
| 684 |
('close', (1,)), |
| 685 |
('write', (1, 0, 0)), |
| 686 |
('read', (1, 0, 0)), |
| 687 |
('readc', tuple()), |
| 688 |
('istty', (1,)), |
| 689 |
('seek', (1, 0)), |
| 690 |
('flen', (1,)), |
| 691 |
('remove', (0, 0)), |
| 692 |
('rename', (0, 0, 0, 0)), |
| 693 |
]) |
| 694 |
def test_unimplemented(self, op, args): |
| 695 |
handler = semihost.SemihostIOHandler() |
| 696 |
with pytest.raises(NotImplementedError): |
| 697 |
handler.__getattribute__(op)(*args) |
| 698 |
|
| 699 |
|