from target_simulator.core.sfp_transport import SfpTransport
from target_simulator.core.sfp_structures import SFPHeader


def test_small_packet_ignored():
    s = SfpTransport('127.0.0.1', 0, {})
    # pass empty data
    s._process_packet(b'', ('1.2.3.4', 1000))


def test_bad_header_handled(monkeypatch):
    s = SfpTransport('127.0.0.1', 0, {})
    # craft short data smaller than header
    s._process_packet(b'123', ('1.2.3.4', 1000))


def test_buffer_overflow_logged():
    s = SfpTransport('127.0.0.1', 0, {})
    # Build a header with tiny total_size but offset/pl_size that would overflow
    hdr = bytearray(SFPHeader.size())
    hdr[SFPHeader.get_field_offset('SFP_FLOW')] = ord('M')
    hdr[SFPHeader.get_field_offset('SFP_TID')] = 1
    hdr[SFPHeader.get_field_offset('SFP_TOTFRGAS')] = 1
    hdr[SFPHeader.get_field_offset('SFP_TOTSIZE')] = 1
    hdr[SFPHeader.get_field_offset('SFP_PLSIZE')] = 10
    hdr[SFPHeader.get_field_offset('SFP_PLOFFSET')] = 0
    data = bytes(hdr) + b'1234567890'
    s._process_packet(data, ('1.2.3.4', 1000))
