# Path: hbmqtt/adapters.py
# class BufferReader(ReaderAdapter):
#     """
#     Byte Buffer reader adapter
#     This adapter simply adapt reading a byte buffer.
#     """
#     def __init__(self, buffer: bytes):
#         self._stream = io.BytesIO(buffer)
# 
#     @asyncio.coroutine
#     def read(self, n=-1) -> bytes:
#         return self._stream.read(n)


import unittest
import asyncio
from hbmqtt.mqtt.packet import CONNECT, MQTTFixedHeader
from hbmqtt.errors import MQTTException
from hbmqtt.adapters import BufferReader
# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.



class TestMQTTFixedHeaderTest(unittest.TestCase):
    def setUp(self):
        self.loop = asyncio.new_event_loop()

    def test_from_bytes(self):
        data = b'\x10\x7f'
        stream = BufferReader(data)
        header = self.loop.run_until_complete(MQTTFixedHeader.from_stream(stream))
        self.assertEqual(header.packet_type, CONNECT)
        self.assertFalse(header.flags & 0x08)
        self.assertEqual((header.flags & 0x06) >> 1, 0)
        self.assertFalse(header.flags & 0x01)
        self.assertEqual(header.remaining_length, 127)

    def test_from_bytes_with_length(self):
        data = b'\x10\xff\xff\xff\x7f'

# Path: hbmqtt/adapters.py
# class BufferReader(ReaderAdapter):
#     """
#     Byte Buffer reader adapter
#     This adapter simply adapt reading a byte buffer.
#     """
#     def __init__(self, buffer: bytes):
#         self._stream = io.BytesIO(buffer)
# 
#     @asyncio.coroutine
#     def read(self, n=-1) -> bytes:
#         return self._stream.read(n)


# Path: tests/mqtt/test_packet.py
import unittest
import asyncio
from hbmqtt.mqtt.packet import CONNECT, MQTTFixedHeader
from hbmqtt.errors import MQTTException
from hbmqtt.adapters import BufferReader

# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.



class TestMQTTFixedHeaderTest(unittest.TestCase):
    def setUp(self):
        self.loop = asyncio.new_event_loop()

    def test_from_bytes(self):
        data = b'\x10\x7f'
        stream = BufferReader(data)
        header = self.loop.run_until_complete(MQTTFixedHeader.from_stream(stream))
        self.assertEqual(header.packet_type, CONNECT)
        self.assertFalse(header.flags & 0x08)
        self.assertEqual((header.flags & 0x06) >> 1, 0)
        self.assertFalse(header.flags & 0x01)
        self.assertEqual(header.remaining_length, 127)

    def test_from_bytes_with_length(self):
        data = b'\x10\xff\xff\xff\x7f'

        stream = BufferReader(data)