# Path: azmq/errors.py
# class ProtocolError(AZMQError):
#     def __init__(self, msg, fatal=False):
#         super().__init__(msg)
#         self.fatal = fatal
# 
# Path: azmq/metadata.py
# def buffer_to_metadata(buffer):
#     """
#     Transform a buffer to a metadata dictionary.
# 
#     :param buffer: The buffer, as received in a READY command.
#     :returns: A metadata dictionary, with its keys normalized (in
#         lowercase).
#     """
#     offset = 0
#     size = len(buffer)
#     metadata = {}
# 
#     while offset < size:
#         name_size = struct.unpack_from('B', buffer, offset)[0]
#         offset += 1
# 
#         if name_size > size - 4:
#             raise ProtocolError(
#                 "Invalid name size in metadata",
#                 fatal=True,
#             )
# 
#         name = buffer[offset:offset + name_size]
#         offset += name_size
# 
#         value_size = struct.unpack_from('!I', buffer, offset)[0]
#         offset += 4
# 
#         if value_size > size - name_size - 5:
#             raise ProtocolError(
#                 "Invalid value size in metadata",
#                 fatal=True,
#             )
# 
#         value = buffer[offset:offset + value_size]
#         offset += value_size
#         metadata[name.lower()] = value
# 
#     return metadata


import pytest
from azmq.errors import ProtocolError
from azmq.metadata import buffer_to_metadata
"""
Unit tests for metadata.
"""




def test_buffer_to_metadata_empty():
    result = buffer_to_metadata(b'')
    assert result == {}


def test_buffer_to_metadata():
    buffer = b'\x03foo\x00\x00\x00\x03bar'
    result = buffer_to_metadata(buffer)
    assert result == {b'foo': b'bar'}


def test_buffer_to_metadata_invalid_name_size():
    buffer = b'\x03'

    with pytest.raises(ProtocolError):
        buffer_to_metadata(buffer)


def test_buffer_to_metadata_invalid_value_size():
    buffer = b'\x03foo\x00\x00\x00\x03ba'


# Path: azmq/errors.py
# class ProtocolError(AZMQError):
#     def __init__(self, msg, fatal=False):
#         super().__init__(msg)
#         self.fatal = fatal
# 
# Path: azmq/metadata.py
# def buffer_to_metadata(buffer):
#     """
#     Transform a buffer to a metadata dictionary.
# 
#     :param buffer: The buffer, as received in a READY command.
#     :returns: A metadata dictionary, with its keys normalized (in
#         lowercase).
#     """
#     offset = 0
#     size = len(buffer)
#     metadata = {}
# 
#     while offset < size:
#         name_size = struct.unpack_from('B', buffer, offset)[0]
#         offset += 1
# 
#         if name_size > size - 4:
#             raise ProtocolError(
#                 "Invalid name size in metadata",
#                 fatal=True,
#             )
# 
#         name = buffer[offset:offset + name_size]
#         offset += name_size
# 
#         value_size = struct.unpack_from('!I', buffer, offset)[0]
#         offset += 4
# 
#         if value_size > size - name_size - 5:
#             raise ProtocolError(
#                 "Invalid value size in metadata",
#                 fatal=True,
#             )
# 
#         value = buffer[offset:offset + value_size]
#         offset += value_size
#         metadata[name.lower()] = value
# 
#     return metadata


# Path: tests/unit/test_metadata.py
import pytest
from azmq.errors import ProtocolError
from azmq.metadata import buffer_to_metadata

"""
Unit tests for metadata.
"""




def test_buffer_to_metadata_empty():
    result = buffer_to_metadata(b'')
    assert result == {}


def test_buffer_to_metadata():
    buffer = b'\x03foo\x00\x00\x00\x03bar'
    result = buffer_to_metadata(buffer)
    assert result == {b'foo': b'bar'}


def test_buffer_to_metadata_invalid_name_size():
    buffer = b'\x03'

    with pytest.raises(ProtocolError):
        buffer_to_metadata(buffer)


def test_buffer_to_metadata_invalid_value_size():
    buffer = b'\x03foo\x00\x00\x00\x03ba'


    with pytest.raises(ProtocolError):