Пример #1
0
    def test_bad_creation(self):
        """SerialAPIGetCapabilities bad packet"""
        body = [0x10, 0x20, 0x35, 0x86, 0x19, 0xa7, 0x87, 0x23,
                0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0xa7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x05,
                0x00, 0x80 ]
        # OK Packet
        packet = Packet(0x01, length=0x2b, packet_type=0x01, message_type=0x07,
                        body=body, checksum=0xe1)
        message = SerialAPIGetCapabilities(packet)

        # Bad preamble
        packet.preamble = 0x00
        assert_that(calling(SerialAPIGetCapabilities).with_args(packet),
                    raises(ValueError))
        packet.preamble = 0x01

        # Bad length
        packet.length = 0x24
        assert_that(calling(SerialAPIGetCapabilities).with_args(packet),
                    raises(ValueError))
        packet.length = 0x2b

        # Bad packet type
        packet.packet_type = 0x00
        assert_that(calling(SerialAPIGetCapabilities).with_args(packet),
                    raises(ValueError))
        packet.packet_type = 0x01

        # Bad message type
        packet.message_type = 0x03
        assert_that(calling(SerialAPIGetCapabilities).with_args(packet),
                    raises(ValueError))
        packet.message_type = 0x07
Пример #2
0
    def test_bad_creation(self):
        """ZWGetControllerCapabilities bad packet"""
        body = [0x0f]
        # OK Packet
        packet = Packet(0x01, length=0x04, packet_type=0x01, message_type=0x05,
                        body=body, checksum=0xe1)
        message = ZWGetControllerCapabilities(packet)

        # Bad preamble
        packet.preamble = 0x00
        assert_that(calling(ZWGetControllerCapabilities).with_args(packet),
                    raises(ValueError))
        packet.preamble = 0x01

        # Bad length
        packet.length = 0x03
        assert_that(calling(ZWGetControllerCapabilities).with_args(packet),
                    raises(ValueError))
        packet.length = 0x04

        # Bad packet type
        packet.packet_type = 0x00
        assert_that(calling(ZWGetControllerCapabilities).with_args(packet),
                    raises(ValueError))
        packet.packet_type = 0x01

        # Bad message type
        packet.message_type = 0x03
        assert_that(calling(ZWGetControllerCapabilities).with_args(packet),
                    raises(ValueError))
        packet.message_type = 0x05
Пример #3
0
    def test_good_creation(self):
        """SerialAPIGetCapabilities packet parsing"""
        body = [
            0x10, 0x20, 0x35, 0x86, 0x19, 0xa7, 0x87, 0x23, 0x07, 0x02, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x81, 0x05, 0x00, 0x80
        ]
        # OK Packet
        packet = Packet(0x01,
                        length=0x2b,
                        packet_type=0x01,
                        message_type=0x07,
                        body=body,
                        checksum=0xe1)
        message = SerialAPIGetCapabilities(packet)

        # Check version/manufacturer/product type/id
        assert_that((message.version, message.manufacturer_id,
                     message.product_type, message.product_id),
                    equal_to((0x2010, 0x3586, 0x19a7, 0x8723)))

        # Check body
        assert_that(message.bitmap_bytes, has_length(32))
        assert_that(message.bitmap_bytes, equal_to(body[8:]))

        # Check supported message types
        assert_that(
            message.message_types,
            equal_to(
                [1, 2, 3, 10, 97, 98, 99, 102, 104, 225, 232, 233, 235, 256]))

        for x in range(256):
            assert_that(message.supports_message_type(x),
                        equal_to(x in message.message_types))
Пример #4
0
    def create_request(self):
        """Create a request packet for SERIAL_API_GET_CAPABILITIES

        Return:
            Packet

        """
        return Packet.create(packet_type=PacketType.REQUEST,
                             message_type=MessageType.SERIAL_API_GET_CAPABILITIES)
Пример #5
0
    def create_request(cls):
        """Create a request packet for ZW_GET_CONTROLLER_CAPABILITIES

        Return:
            Packet

        """
        return Packet.create(packet_type=PacketType.REQUEST,
                             message_type=MessageType.ZW_GET_CONTROLLER_CAPABILITIES)
Пример #6
0
    def create_request(cls):
        """Create a request packet for SERIAL_API_GET_INIT_DATA

        Return:
            Packet

        """
        return Packet.create(packet_type=PacketType.REQUEST,
                             message_type=MessageType.SERIAL_API_GET_INIT_DATA)
Пример #7
0
    def test_bad_creation(self):
        """SerialAPIGetCapabilities bad packet"""
        body = [
            0x10, 0x20, 0x35, 0x86, 0x19, 0xa7, 0x87, 0x23, 0x07, 0x02, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x81, 0x05, 0x00, 0x80
        ]
        # OK Packet
        packet = Packet(0x01,
                        length=0x2b,
                        packet_type=0x01,
                        message_type=0x07,
                        body=body,
                        checksum=0xe1)
        message = SerialAPIGetCapabilities(packet)

        # Bad preamble
        packet.preamble = 0x00
        assert_that(
            calling(SerialAPIGetCapabilities).with_args(packet),
            raises(ValueError))
        packet.preamble = 0x01

        # Bad length
        packet.length = 0x24
        assert_that(
            calling(SerialAPIGetCapabilities).with_args(packet),
            raises(ValueError))
        packet.length = 0x2b

        # Bad packet type
        packet.packet_type = 0x00
        assert_that(
            calling(SerialAPIGetCapabilities).with_args(packet),
            raises(ValueError))
        packet.packet_type = 0x01

        # Bad message type
        packet.message_type = 0x03
        assert_that(
            calling(SerialAPIGetCapabilities).with_args(packet),
            raises(ValueError))
        packet.message_type = 0x07
Пример #8
0
    def test_create_message_no_body(self):
        """Create message with no body"""
        packet = Packet.create(packet_type=0x00, message_type=0x02)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x00, 0x02, [], 0xfe)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(True))
Пример #9
0
    def test_create_message_no_body(self):
        """Create message with no body"""
        packet = Packet.create(packet_type=0x00, message_type=0x02)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x00, 0x02, [], 0xfe)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(True))
Пример #10
0
    def test_create_message_with_body(self):
        """Create message with a body"""
        # NOTE: bad preamble, but continue
        packet = Packet.create(preamble=0x04, packet_type=0x00,
                               message_type=0x02, body=[0x45, 0x78])

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x04, 0x05, 0x00, 0x02, [0x45, 0x78], 0xc5)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(True))
Пример #11
0
    def test_bad_creation(self):
        """ZWGetControllerCapabilities bad packet"""
        body = [0x0f]
        # OK Packet
        packet = Packet(0x01,
                        length=0x04,
                        packet_type=0x01,
                        message_type=0x05,
                        body=body,
                        checksum=0xe1)
        message = ZWGetControllerCapabilities(packet)

        # Bad preamble
        packet.preamble = 0x00
        assert_that(
            calling(ZWGetControllerCapabilities).with_args(packet),
            raises(ValueError))
        packet.preamble = 0x01

        # Bad length
        packet.length = 0x03
        assert_that(
            calling(ZWGetControllerCapabilities).with_args(packet),
            raises(ValueError))
        packet.length = 0x04

        # Bad packet type
        packet.packet_type = 0x00
        assert_that(
            calling(ZWGetControllerCapabilities).with_args(packet),
            raises(ValueError))
        packet.packet_type = 0x01

        # Bad message type
        packet.message_type = 0x03
        assert_that(
            calling(ZWGetControllerCapabilities).with_args(packet),
            raises(ValueError))
        packet.message_type = 0x05
Пример #12
0
    def test_message(self):
        """Message creation from packet"""
        packet = Packet(0x03,
                        length=0x23,
                        packet_type=0x12,
                        message_type=0x13,
                        body=[0x12, 0x32],
                        checksum=0x13)
        message = Message(packet)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x03, 0x23, 0x12, 0x13, [0x12, 0x32], 0x13)))
Пример #13
0
    def test_create_message_with_body(self):
        """Create message with a body"""
        # NOTE: bad preamble, but continue
        packet = Packet.create(preamble=0x04,
                               packet_type=0x00,
                               message_type=0x02,
                               body=[0x45, 0x78])

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x04, 0x05, 0x00, 0x02, [0x45, 0x78], 0xc5)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(True))
Пример #14
0
    def test_good_creation(self):
        """ZWGetControllerCapabilities parsing"""
        body = [0x0f]
        # OK Packet
        packet = Packet(0x01,
                        length=0x04,
                        packet_type=0x01,
                        message_type=0x05,
                        body=body,
                        checksum=0xe1)
        message = ZWGetControllerCapabilities(packet)

        assert_that(message.capabilities, equal_to(0x0f))

        # Test capabilities
        message.capabilities = 0x01
        caps = (message.secondary, message.non_standard_home_id,
                message.suc_id_server, message.was_primary,
                message.static_update_controller)
        assert_that(caps, equal_to((True, False, False, False, False)))

        message.capabilities = 0x02
        caps = (message.secondary, message.non_standard_home_id,
                message.suc_id_server, message.was_primary,
                message.static_update_controller)
        assert_that(caps, equal_to((False, True, False, False, False)))

        message.capabilities = 0x04
        caps = (message.secondary, message.non_standard_home_id,
                message.suc_id_server, message.was_primary,
                message.static_update_controller)
        assert_that(caps, equal_to((False, False, True, False, False)))

        message.capabilities = 0x08
        caps = (message.secondary, message.non_standard_home_id,
                message.suc_id_server, message.was_primary,
                message.static_update_controller)
        assert_that(caps, equal_to((False, False, False, True, False)))

        message.capabilities = 0x10
        caps = (message.secondary, message.non_standard_home_id,
                message.suc_id_server, message.was_primary,
                message.static_update_controller)
        assert_that(caps, equal_to((False, False, False, False, True)))

        message.capabilities = 0x1f
        caps = (message.secondary, message.non_standard_home_id,
                message.suc_id_server, message.was_primary,
                message.static_update_controller)
        assert_that(caps, equal_to((True, True, True, True, True)))
Пример #15
0
    def test_good_creation(self):
        """SerialAPIGetInitData parsing"""
        body = [
            0x15, 0x23, 0x1d, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0xa7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x05,
            0x00
        ]
        packet = Packet(0x01,
                        length=0x25,
                        packet_type=0x01,
                        message_type=0x02,
                        body=body,
                        checksum=0xe1)
        message = SerialAPIGetInitData(packet)

        assert_that(message.version, equal_to(0x15))
        assert_that(message.capabilities, equal_to(0x23))

        assert_that(message.nodes,
                    equal_to([1, 2, 3, 10, 97, 98, 99, 102, 104, 225, 232]))

        # Test capabilities
        message.capabilities = 0x04
        assert_that(message.secondary, equal_to(True))
        assert_that(message.static_update, equal_to(False))

        message.capabilities = 0x08
        assert_that(message.secondary, equal_to(False))
        assert_that(message.static_update, equal_to(True))

        message.capabilities = 0x0c
        assert_that(message.secondary, equal_to(True))
        assert_that(message.static_update, equal_to(True))

        message.capabilities = 0xf3
        assert_that(message.secondary, equal_to(False))
        assert_that(message.static_update, equal_to(False))
Пример #16
0
    def test_partial_message(self):
        """Partial messages from errors"""

        ############################# Only length #############################
        packet = Packet(0x01, length=0x03)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, None, None, [], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(), equal_to(bytearray([0x01, 0x03])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ########################## With message type ##########################
        packet = Packet(0x01, length=0x03, packet_type=0x01)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, None, [], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(), equal_to(bytearray([0x01, 0x03, 0x01])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ##################### With controller message type ####################
        packet = Packet(0x01, length=0x03, packet_type=0x01, message_type=0x02)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, 0x02, [], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(), equal_to(bytearray([0x01, 0x03, 0x01,
                                                        0x02])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ############################## With body ##############################
        packet = Packet(0x01, length=0x03, packet_type=0x01, message_type=0x02,
                        body=[0x04, 0x05])

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, 0x02, [0x04, 0x05], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(), equal_to(bytearray([0x01, 0x03, 0x01, 0x02,
                                                        0x04, 0x05])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ############################ With checksum ############################
        packet = Packet(0x01, length=0x03, packet_type=0x01, message_type=0x02,
                        body=[0x04, 0x05], checksum=0x34)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, 0x02, [0x04, 0x05], 0x34)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(), equal_to(bytearray([0x01, 0x03, 0x01, 0x02,
                                                        0x04, 0x05, 0x34])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)
Пример #17
0
    def test_partial_message(self):
        """Partial messages from errors"""

        ############################# Only length #############################
        packet = Packet(0x01, length=0x03)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, None, None, [], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(), equal_to(bytearray([0x01, 0x03])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ########################## With message type ##########################
        packet = Packet(0x01, length=0x03, packet_type=0x01)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, None, [], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(), equal_to(bytearray([0x01, 0x03, 0x01])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ##################### With controller message type ####################
        packet = Packet(0x01, length=0x03, packet_type=0x01, message_type=0x02)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, 0x02, [], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(),
                    equal_to(bytearray([0x01, 0x03, 0x01, 0x02])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ############################## With body ##############################
        packet = Packet(0x01,
                        length=0x03,
                        packet_type=0x01,
                        message_type=0x02,
                        body=[0x04, 0x05])

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, 0x02, [0x04, 0x05], None)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(packet.bytes(),
                    equal_to(bytearray([0x01, 0x03, 0x01, 0x02, 0x04, 0x05])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)

        ############################ With checksum ############################
        packet = Packet(0x01,
                        length=0x03,
                        packet_type=0x01,
                        message_type=0x02,
                        body=[0x04, 0x05],
                        checksum=0x34)

        # Check fields
        assert_that((packet.preamble, packet.length, packet.packet_type,
                     packet.message_type, packet.body, packet.checksum),
                    equal_to((0x01, 0x03, 0x01, 0x02, [0x04, 0x05], 0x34)))
        # Validate checksum
        assert_that(packet.validate_checksum(), equal_to(False))

        # Check contents
        assert_that(
            packet.bytes(),
            equal_to(bytearray([0x01, 0x03, 0x01, 0x02, 0x04, 0x05, 0x34])))

        # Check string functions work correctly
        string = "%s %r" % (packet, packet)
Пример #18
0
    def test_bad_creation(self):
        """SerialAPIGetInitData bad packet"""
        body = [
            0x15, 0x23, 0x1d, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0xa7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x05,
            0x00
        ]
        # OK Packet
        packet = Packet(0x01,
                        length=0x25,
                        packet_type=0x01,
                        message_type=0x02,
                        body=body,
                        checksum=0xe1)
        message = SerialAPIGetInitData(packet)

        # Bad preamble
        packet.preamble = 0x00
        assert_that(
            calling(SerialAPIGetInitData).with_args(packet),
            raises(ValueError))
        packet.preamble = 0x01

        # Bad length
        packet.length = 0x24
        assert_that(
            calling(SerialAPIGetInitData).with_args(packet),
            raises(ValueError))
        packet.length = 0x25

        # Bad packet type
        packet.packet_type = 0x00
        assert_that(
            calling(SerialAPIGetInitData).with_args(packet),
            raises(ValueError))
        packet.packet_type = 0x01

        # Bad message type
        packet.message_type = 0x03
        assert_that(
            calling(SerialAPIGetInitData).with_args(packet),
            raises(ValueError))
        packet.message_type = 0x02

        # Bad body bitmap length
        packet.body[2] = 0x1c
        assert_that(
            calling(SerialAPIGetInitData).with_args(packet),
            raises(ValueError))
        packet.body[2] = 0x1d
Пример #19
0
    def test_bad_creation(self):
        """SerialAPIGetInitData bad packet"""
        body = [0x15, 0x23, 0x1d, 
                0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0xa7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81,
                0x05, 0x00]
        # OK Packet
        packet = Packet(0x01, length=0x25, packet_type=0x01, message_type=0x02,
                        body=body, checksum=0xe1)
        message = SerialAPIGetInitData(packet)

        # Bad preamble
        packet.preamble = 0x00
        assert_that(calling(SerialAPIGetInitData).with_args(packet),
                    raises(ValueError))
        packet.preamble = 0x01

        # Bad length
        packet.length = 0x24
        assert_that(calling(SerialAPIGetInitData).with_args(packet),
                    raises(ValueError))
        packet.length = 0x25

        # Bad packet type
        packet.packet_type = 0x00
        assert_that(calling(SerialAPIGetInitData).with_args(packet),
                    raises(ValueError))
        packet.packet_type = 0x01

        # Bad message type
        packet.message_type = 0x03
        assert_that(calling(SerialAPIGetInitData).with_args(packet),
                    raises(ValueError))
        packet.message_type = 0x02

        # Bad body bitmap length
        packet.body[2] = 0x1c
        assert_that(calling(SerialAPIGetInitData).with_args(packet),
                    raises(ValueError))        
        packet.body[2] = 0x1d