# Path: azmq/crypto.py
# def libsodium_check(*args, **kwargs):
# def requires_libsodium(func):
#     def wrapper(*args, **kwargs):
# def curve_gen_keypair():
#     HAS_LIBSODIUM = True
#         HAS_LIBSODIUM = True
#         HAS_LIBSODIUM = False
# 
# Path: azmq/mechanisms/curve_server.py
# class CurveServer(object):
#     @requires_libsodium
#     def __init__(self, public_key=None, secret_key=None):
#         self.set_permanent_keypair(
#             public_key=public_key,
#             secret_key=secret_key,
#         )
# 
#     def set_permanent_keypair(self, public_key, secret_key):
#         assert public_key and len(public_key) == 32, (
#             "A 32 bytes public key must be specified."
#         )
#         assert secret_key and len(secret_key) == 32, (
#             "A 32 bytes secret key must be specified."
#         )
#         self.public_key = public_key
#         self.secret_key = secret_key
# 
#     def __call__(self):
#         return CurveServerMechanism(
#             public_key=self.public_key,
#             secret_key=self.secret_key,
#         )
# 
# Path: azmq/errors.py
# class ProtocolError(AZMQError):
#     def __init__(self, msg, fatal=False):
#         super().__init__(msg)
#         self.fatal = fatal
# 
# Path: tests/conftest.py
# def event_loop():
# def reader():
#     async def readexactly(count):
# def writer():
# ENDPOINTS = [
#     'inproc://mypath',
#     'tcp://127.0.0.1:3333',
# ]


import pytest
from azmq.crypto import (
    crypto_box,
    crypto_box_afternm,
    crypto_box_beforenm,
    crypto_secretbox,
    curve_gen_keypair,
)
from azmq.mechanisms.curve_server import CurveServer
from azmq.errors import ProtocolError
from ...conftest import requires_libsodium
        )


@requires_libsodium
@pytest.mark.asyncio
async def test_read_curve_initiate_invalid_vouch_size(reader, mechanism):
    cookie_nonce = b'\0' * 16
    cookie_key = b'\0' * 32
    cookie = crypto_secretbox(
        mechanism.rcp + mechanism.sp,
        nonce=b'COOKIE--' + cookie_nonce,
        k=cookie_key,
    )
    nonce = b'\0' * 8
    plain = b'\0' * 47
    box = crypto_box_afternm(
        plain,
        nonce=b'CurveZMQINITIATE' + nonce,
        k=mechanism.rkp,
    )
    reader.write(b'\x04\xb1\x08INITIATE' + cookie_nonce + cookie + nonce + box)
    reader.seek(0)

    with pytest.raises(ProtocolError):
        await mechanism._read_curve_initiate(
            reader=reader,
            cookie_key=cookie_key,
        )



# Path: azmq/crypto.py
# def libsodium_check(*args, **kwargs):
# def requires_libsodium(func):
#     def wrapper(*args, **kwargs):
# def curve_gen_keypair():
#     HAS_LIBSODIUM = True
#         HAS_LIBSODIUM = True
#         HAS_LIBSODIUM = False
# 
# Path: azmq/mechanisms/curve_server.py
# class CurveServer(object):
#     @requires_libsodium
#     def __init__(self, public_key=None, secret_key=None):
#         self.set_permanent_keypair(
#             public_key=public_key,
#             secret_key=secret_key,
#         )
# 
#     def set_permanent_keypair(self, public_key, secret_key):
#         assert public_key and len(public_key) == 32, (
#             "A 32 bytes public key must be specified."
#         )
#         assert secret_key and len(secret_key) == 32, (
#             "A 32 bytes secret key must be specified."
#         )
#         self.public_key = public_key
#         self.secret_key = secret_key
# 
#     def __call__(self):
#         return CurveServerMechanism(
#             public_key=self.public_key,
#             secret_key=self.secret_key,
#         )
# 
# Path: azmq/errors.py
# class ProtocolError(AZMQError):
#     def __init__(self, msg, fatal=False):
#         super().__init__(msg)
#         self.fatal = fatal
# 
# Path: tests/conftest.py
# def event_loop():
# def reader():
#     async def readexactly(count):
# def writer():
# ENDPOINTS = [
#     'inproc://mypath',
#     'tcp://127.0.0.1:3333',
# ]


# Path: tests/unit/test_mechanisms/test_curve_server.py
import pytest
from azmq.crypto import (
    crypto_box,
    crypto_box_afternm,
    crypto_box_beforenm,
    crypto_secretbox,
    curve_gen_keypair,
)
from azmq.mechanisms.curve_server import CurveServer
from azmq.errors import ProtocolError
from ...conftest import requires_libsodium

        )


@requires_libsodium
@pytest.mark.asyncio
async def test_read_curve_initiate_invalid_vouch_size(reader, mechanism):
    cookie_nonce = b'\0' * 16
    cookie_key = b'\0' * 32
    cookie = crypto_secretbox(
        mechanism.rcp + mechanism.sp,
        nonce=b'COOKIE--' + cookie_nonce,
        k=cookie_key,
    )
    nonce = b'\0' * 8
    plain = b'\0' * 47
    box = crypto_box_afternm(
        plain,
        nonce=b'CurveZMQINITIATE' + nonce,
        k=mechanism.rkp,
    )
    reader.write(b'\x04\xb1\x08INITIATE' + cookie_nonce + cookie + nonce + box)
    reader.seek(0)

    with pytest.raises(ProtocolError):
        await mechanism._read_curve_initiate(
            reader=reader,
            cookie_key=cookie_key,
        )



@requires_libsodium