test_frame.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:dataplicity-lomond 作者: wildfoundry 项目源码 文件源码
def test_binary_output_length_smaller_or_equal_127(frame_factory, monkeypatch):
    # this unit test is very similar to previous one, the difference being of
    # course the length of the packet in question
    monkeypatch.setattr(
        'lomond.frame.make_masking_key', lambda: b'\x00\x00\x00\x00')
    frame = frame_factory(Opcode.BINARY, payload=b'\x01' * 127)
    frame_binary = frame.to_bytes()

    expected_length = 1 + 1 + 2 + 4 + 127
    #                 ^   ^   ^   ^    ^
    #                 |   |   |   |    +-- payload length     (127 bytes)
    #                 |   |   |   +------- mask length        (4 bytes  )
    #                 |   |   +----------- length as uint16_t (2 bytes  )
    #                 |   +--------------- masking byte
    #                 +------------------- opcode byte
    assert len(frame_binary) == expected_length

    # the first byte doesn't change at all, so please look inside the previous
    # function for in-depth explanation
    assert six.indexbytes(frame_binary, 0) == 0b10000010
    # in the second byte of the header, length field should be set to 126 to
    # indicate a payload of length which should be encoded as uint16_t
    # just for the fun of it, we can decode the actual length value:
    _length = six.indexbytes(frame_binary, 1) & 0b01111111
    assert _length == 126
    assert frame_binary[4:8] == b'\x00\x00\x00\x00'
    assert frame_binary[8:] == frame.payload
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号