# Path: azmq/crypto.py
# @requires_libsodium
# def curve_gen_keypair():
#     """
#     Generate and return a pair of (public, secret) keys for use with the CURVE
#     mechanism.
# 
#     :returns: A public, secret key pair, as a tuple.
#     """
#     return crypto_box_keypair()
# 
# Path: azmq/mechanisms/curve_client.py
# class CurveClient(object):
#     @requires_libsodium
#     def __init__(
#         self,
#         server_key,
#         public_key=None,
#         secret_key=None,
#     ):
#         self.set_permanent_server_key(server_key)
# 
#         if public_key or secret_key:
#             self.set_permanent_keypair(
#                 public_key=public_key,
#                 secret_key=secret_key,
#             )
#         else:
#             self.public_key = None
#             self.secret_key = None
# 
#     def set_permanent_server_key(self, server_key):
#         assert server_key and len(server_key) == 32, (
#             "A 32 bytes server key must be specified."
#         )
#         self.server_key = server_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):
#         assert self.server_key, "No server key was specified."
# 
#         if not self.public_key:
#             logger.info(
#                 "No keypair was specified for CURVE client. Generating a "
#                 "random one.",
#             )
#             self.public_key, self.secret_key = crypto_box_keypair()
# 
#         return CurveClientMechanism(
#             server_key=self.server_key,
#             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 curve_gen_keypair
from azmq.mechanisms.curve_client import CurveClient
from azmq.errors import ProtocolError
from ...conftest import requires_libsodium
"""





@pytest.fixture
def mechanism():
    public_key, _ = curve_gen_keypair()
    return CurveClient(
        server_key=public_key,
    )()


@requires_libsodium
@pytest.mark.asyncio
async def test_read_curve_welcome_invalid(reader, mechanism):
    reader.write(b'\x04\x07\x07WELCOME')
    reader.seek(0)

    with pytest.raises(ProtocolError):
        await mechanism._read_curve_welcome(reader=reader)


@requires_libsodium
@pytest.mark.asyncio
async def test_read_curve_welcome_invalid_box(reader, mechanism):
    reader.write(b'\x04\xa8\x07WELCOME' + b'\0' * 160)
    reader.seek(0)


# Path: azmq/crypto.py
# @requires_libsodium
# def curve_gen_keypair():
#     """
#     Generate and return a pair of (public, secret) keys for use with the CURVE
#     mechanism.
# 
#     :returns: A public, secret key pair, as a tuple.
#     """
#     return crypto_box_keypair()
# 
# Path: azmq/mechanisms/curve_client.py
# class CurveClient(object):
#     @requires_libsodium
#     def __init__(
#         self,
#         server_key,
#         public_key=None,
#         secret_key=None,
#     ):
#         self.set_permanent_server_key(server_key)
# 
#         if public_key or secret_key:
#             self.set_permanent_keypair(
#                 public_key=public_key,
#                 secret_key=secret_key,
#             )
#         else:
#             self.public_key = None
#             self.secret_key = None
# 
#     def set_permanent_server_key(self, server_key):
#         assert server_key and len(server_key) == 32, (
#             "A 32 bytes server key must be specified."
#         )
#         self.server_key = server_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):
#         assert self.server_key, "No server key was specified."
# 
#         if not self.public_key:
#             logger.info(
#                 "No keypair was specified for CURVE client. Generating a "
#                 "random one.",
#             )
#             self.public_key, self.secret_key = crypto_box_keypair()
# 
#         return CurveClientMechanism(
#             server_key=self.server_key,
#             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_client.py
import pytest
from azmq.crypto import curve_gen_keypair
from azmq.mechanisms.curve_client import CurveClient
from azmq.errors import ProtocolError
from ...conftest import requires_libsodium

"""





@pytest.fixture
def mechanism():
    public_key, _ = curve_gen_keypair()
    return CurveClient(
        server_key=public_key,
    )()


@requires_libsodium
@pytest.mark.asyncio
async def test_read_curve_welcome_invalid(reader, mechanism):
    reader.write(b'\x04\x07\x07WELCOME')
    reader.seek(0)

    with pytest.raises(ProtocolError):
        await mechanism._read_curve_welcome(reader=reader)


@requires_libsodium
@pytest.mark.asyncio
async def test_read_curve_welcome_invalid_box(reader, mechanism):
    reader.write(b'\x04\xa8\x07WELCOME' + b'\0' * 160)
    reader.seek(0)


    with pytest.raises(ProtocolError):