def test_simple_filter_mqtt_01(self): """ Test the -eq and -ne operators """ b = MatchingFilterBuilder() b.identifier = 'test_filter_01' b.value = 1 b.field = 'type' b.operator = '-eq' con_filter = b.build() b.operator = '-ne' non_con_filter = b.build() b.operator = '-eq' b.value = 3 publish_filter = b.build() b.field = 'QOS' b.value = 0 qos0_filter = b.build() b.value = 2 qos2_filter = b.build() mqtt_connect = ScapyMessage(MQTT_PROTO_DESCRIPTION, MQTT(DEC_MQTT_CONNECT)) mqtt_connack = ScapyMessage(MQTT_PROTO_DESCRIPTION, MQTT(DEC_MQTT_CONNACK)) mqtt_publish = ScapyMessage(MQTT_PROTO_DESCRIPTION, MQTT(DEC_MQTT_PUB_QOS2)) # match CONNECT filter self.assertTrue(con_filter.match(mqtt_connect)) self.assertFalse(con_filter.match(mqtt_connack)) self.assertFalse(con_filter.match(mqtt_publish)) # match NON CONNECT filter self.assertFalse(non_con_filter.match(mqtt_connect)) self.assertTrue(non_con_filter.match(mqtt_connack)) self.assertTrue(non_con_filter.match(mqtt_publish)) # match PUBLISH filter self.assertTrue(publish_filter.match(mqtt_publish)) self.assertFalse(publish_filter.match(mqtt_connect)) self.assertFalse(publish_filter.match(mqtt_connack)) # match QoS 0 filter self.assertFalse(qos0_filter.match(mqtt_publish)) # -> matches because scapy's connack packet has QoS = 0 as default self.assertTrue(qos0_filter.match(mqtt_connack)) # -> matches because scapy's connect packet has QoS = 0 as default self.assertTrue(qos0_filter.match(mqtt_connect)) # match QoS 2 filter self.assertTrue(qos2_filter.match(mqtt_publish)) self.assertFalse(qos2_filter.match(mqtt_connack)) self.assertFalse(qos2_filter.match(mqtt_connect))
def test_composite_filter_mqtt_02(self): """ test a composite filter with a more complex filter """ left = MatchingFilterBuilder() left.identifier = 'test_filter_01' left.value = 3 left.field = 'type' left.operator = '-eq' pub_filter = left.build() right = MatchingFilterBuilder() right.identifier = 'test_filter_02' right.value = 2 right.field = "QOS" right.operator = '-lt' qos_filter = right.build() pub_qos_lt2 = ComplexFilter(pub_filter, qos_filter, 'AND') right = MatchingFilterBuilder() right.identifier = 'test_filter_03' right.field = 'type' right.value = 8 right.operator = '-eq' sub_filter = right.build() comp_filter = ComplexFilter(pub_qos_lt2, sub_filter, 'OR') mqtt_pub_qos1 = ScapyMessage(MQTT_PROTO_DESCRIPTION, MQTT(DEC_MQTT_PUB_QOS1)) mqtt_pub_qos2 = ScapyMessage(MQTT_PROTO_DESCRIPTION, MQTT(DEC_MQTT_PUB_QOS2)) mqtt_sub_qos0 = ScapyMessage(MQTT_PROTO_DESCRIPTION, MQTT(DEC_MQTT_SUB_QOS0)) # first check the simple filter self.assertFalse(qos_filter.match(mqtt_pub_qos2)) self.assertTrue(qos_filter.match(mqtt_pub_qos1)) self.assertTrue(qos_filter.match(mqtt_sub_qos0)) self.assertTrue(sub_filter.match(mqtt_sub_qos0)) self.assertFalse(sub_filter.match(mqtt_pub_qos1)) self.assertFalse(sub_filter.match(mqtt_pub_qos2)) self.assertFalse(pub_filter.match(mqtt_sub_qos0)) self.assertTrue(pub_filter.match(mqtt_pub_qos1)) self.assertTrue(pub_filter.match(mqtt_pub_qos2)) # now check to composite filters self.assertTrue(pub_qos_lt2.match(mqtt_pub_qos1)) self.assertFalse(pub_qos_lt2.match(mqtt_pub_qos2)) self.assertFalse(pub_qos_lt2.match(mqtt_sub_qos0)) self.assertFalse(comp_filter.match(mqtt_pub_qos2)) self.assertTrue(comp_filter.match(mqtt_pub_qos1)) self.assertTrue(comp_filter.match(mqtt_sub_qos0))
def ping(test_params, show_result=False): """Checks MQTT service availability by sending ping packet and waiting for response.""" # MQTT ping is using Connect message for packet_hex in [MQTT_CONN_MQTT, MQTT_CONN_MQISDP]: packet_data = dehex(packet_hex) out_packet = MQTT(packet_data) if mqtt_request(test_params, out_packet): return True return False
def ping(test_params, show_result=False): """Check MQTT-SN service availability by sending ping packet and waiting for response.""" if not test_params: return None # MQTT-SN ping is using Search Gateway message for packet_hex in [MQTTSN_SEARCH_GATEWAY]: packet_data = dehex(packet_hex) out_packet = MQTT(packet_data) if mqttsn_request(test_params, out_packet): return True return False
def mqtt_ping(test_params): """Checks MQTT service availability by sending ping packet and waiting for response.""" # MQTT ping is using Connect message packet_data = dehex(MQTT_CONN) out_packet = MQTT(packet_data) try: for i in range(1 + test_params.nr_retries): in_data = tcp_sr1(test_params, out_packet) in_packet = MQTT(in_data) show_verbose(test_params, in_packet) if ( in_packet[MQTT].type in CONTROL_PACKET_TYPE and CONTROL_PACKET_TYPE[in_packet[MQTT].type] == "CONNACK" ): print_verbose( test_params, "MQTT ping {}: in_packet[MQTTConnack].retcode: {}".format( i + 1, RETURN_CODE[in_packet[MQTTConnack].retcode] ), ) return True except (socket.timeout, socket.error) as error: print_verbose(test_params, error) return False
def mqtt_request(test_params, out_packet): """Send MQTT request to broker and waiting for response.""" try: for i in range(1 + test_params.nr_retries): in_data = tcp_sr1(test_params, out_packet) in_packet = MQTT(in_data) show_verbose(test_params, in_packet) if (in_packet[MQTT].type in CONTROL_PACKET_TYPE and CONTROL_PACKET_TYPE[in_packet[MQTT].type] == "CONNACK"): print_verbose( test_params, "MQTT ping {}: in_packet[MQTTConnack].retcode: {}".format( i + 1, RETURN_CODE[in_packet[MQTTConnack].retcode]), ) return True except (socket.timeout, socket.error) as error: print_verbose(test_params, error) return False
def test_composite_filter_mqtt_01(self): """ test a composite filter """ left = MatchingFilterBuilder() left.identifier = 'test_filter_01' left.value = 3 left.field = 'type' left.operator = '-eq' pub_filter = left.build() right = MatchingFilterBuilder() right.identifier = 'test_filter_02' right.value = 2 right.field = "QOS" right.operator = '-eq' qos_filter = right.build() comp_filter = ComplexFilter(pub_filter, qos_filter, 'AND') mqtt_pub_qos2 = ScapyMessage(MQTT_PROTO_DESCRIPTION, MQTT(DEC_MQTT_PUB_QOS2)) self.assertTrue(comp_filter.match(mqtt_pub_qos2))