def test_initiate_deserializer(): token = "TOKEN12345" action_id = 7 seq_id = 0x1234567890ABCDEF send_id = 0x5F5F5F5F5F5F5F5F status = pySRUPLib.SRUP_Response().srup_response_status_update_fail_file() x = pySRUPLib.SRUP_Response() y = pySRUPLib.SRUP_Response() x.action_id = action_id x.token = token x.sequence_id = seq_id x.sender_id = send_id x.status = status assert x.sign(keyfile) is True z = x.serialize() assert y.deserialize(z) is True assert y.verify(pubkeyfile) is True assert y.token == token assert y.sender_id == send_id assert y.sequence_id == seq_id assert y.status == status
def test_deserializer_from_response(): # To test deserialization - we need to start with a different message-type and then convert # back to a generic message... token = "TOKEN12345" seq_id = 0x1234567890ABCDEF send_id = 0x5F5F5F5F5F5F5F5F status = pySRUPLib.SRUP_Response().srup_response_status_update_fail_file() x = pySRUPLib.SRUP_Response() y = pySRUPLib.SRUP_Generic() x.token = token x.sequence_id = seq_id x.sender_id = send_id x.status = status assert x.sign(keyfile) is True z = x.serialize() assert y.deserialize(z) is True # Remember we can't verify the generic message... assert y.verify(pubkeyfile) is False assert y.sender_id == send_id assert y.sequence_id == seq_id assert y.token == token
def __on_initiate(self, SRUP_initiate_message): # print("INITIATE MESSAGE Received") SRUP_response_message = pySRUPLib.SRUP_Response() status = None try: self.__fetch_check_file(SRUP_initiate_message.url, SRUP_initiate_message.digest, self.__fetch_filename, self.__fetch_auth) except pySRUP_Exceptions.SRUP_FETCHER_DIGEST_ERROR: status = SRUP_response_message.srup_response_status_update_fail_digest( ) except pySRUP_Exceptions.SRUP_FETCHER_SERVER_ERROR: status = SRUP_response_message.srup_response_status_update_fail_server( ) except pySRUP_Exceptions.SRUP_FETCHER_FILE_ERROR: status = SRUP_response_message.srup_response_status_update_fail_file( ) except pySRUP_Exceptions.SRUP_FETCHER_LOCAL_FILE_IO_ERROR: raise IOError else: status = SRUP_response_message.srup_response_status_update_success( ) finally: # We need to send the target id in string format... self.send_SRUP_Response( hex(SRUP_initiate_message.sender_id)[2:], status, SRUP_initiate_message.token)
def test_resonse_status(): ZERO = 0 VALID = 128 MAX = 255 HIGH = 512 LOW = -17 x = pySRUPLib.SRUP_Response() x.status = VALID assert x.status == VALID x.status = ZERO assert x.status == ZERO x.status = MAX assert x.status == MAX with pytest.raises(OverflowError): x.status = MAX + 1 with pytest.raises(OverflowError): x.status = HIGH with pytest.raises(OverflowError): x.status = ZERO - 1 with pytest.raises(OverflowError): x.status = LOW
def test_empty_object(): x = pySRUPLib.SRUP_Response() assert x.token is None assert x.sequence_id is None assert x.sender_id is None assert x.status is None assert x.sign("") is False
def test_response_signing(): blank = "" x = pySRUPLib.SRUP_Response() assert x.sign(blank) is False assert x.sign(keyfile) is False x.action_id = 7 assert x.sign(keyfile) is False x.token = "TOKEN12345" assert x.sign(keyfile) is False x.sequence_id = 0x1234567890ABCDEF assert x.sign(keyfile) is False x.sender_id = 0x5F5F5F5F5F5F5F5F assert x.sign(keyfile) is False x.status = x.srup_response_status_group_delete_success() assert x.sign(blank) is False assert x.sign(keyfile) is True assert x.verify(pubkeyfile) is True # Transpose a digit in the digest... x.status = x.srup_response_status_join_fail() assert x.verify(pubkeyfile) is False
def test_response_serializer(): x = pySRUPLib.SRUP_Response() x.action_id = 7 x.token = "TOKEN12345" x.sequence_id = 0x1234567890ABCDEF x.sender_id = 0x5F5F5F5F5F5F5F5F x.status = x.srup_response_status_deregister_success() assert x.sign(keyfile) is True z = x.serialize()
def test_initiate_generic_deserializer(): token = "TOKEN12345" action_id = 7 seq_id = 0x1234567890ABCDEF send_id = 0x5F5F5F5F5F5F5F5F status = pySRUPLib.SRUP_Response().srup_response_status_update_fail_file() x = pySRUPLib.SRUP_Response() i = pySRUPLib.SRUP_Generic() x.action_id = action_id x.token = token x.sequence_id = seq_id x.sender_id = send_id x.status = status assert x.sign(keyfile) is True z = x.serialize() assert i.deserialize(z) is True assert i.msg_type == pySRUPLib.__response_message_type()
def test_response_sender(): MAX_SENDER = 0xFFFFFFFFFFFFFFFF ZERO_SENDER = 0x00 VALID_SENDER = 0x7FFFFFFFFFFFFFE7 x = pySRUPLib.SRUP_Response() x.sender_id = MAX_SENDER assert x.sender_id == MAX_SENDER x.sender_id = VALID_SENDER assert x.sender_id == VALID_SENDER x.sender_id = ZERO_SENDER assert x.sender_id == ZERO_SENDER with pytest.raises(OverflowError): x.sender_id = MAX_SENDER + 1 with pytest.raises(OverflowError): x.sender_id = ZERO_SENDER - 1
def test_response_seqid(): MAX_SEQID = 0xFFFFFFFFFFFFFFFF ZERO_SEQID = 0x00 VALID_SEQID = 0x7FFFFFFFFFFFFFE7 x = pySRUPLib.SRUP_Response() x.sequence_id = MAX_SEQID assert x.sequence_id == MAX_SEQID x.sequence_id = VALID_SEQID assert x.sequence_id == VALID_SEQID x.sequence_id = ZERO_SEQID assert x.sequence_id == ZERO_SEQID with pytest.raises(OverflowError): x.sequence_id = MAX_SEQID + 1 with pytest.raises(OverflowError): x.sequence_id = ZERO_SEQID - 1
def send_SRUP_Response(self, target_id, status, token): SRUP_response_message = pySRUPLib.SRUP_Response() # Note that this time we need to pass in a token rather than generate one... SRUP_response_message.token = token iTarget = int(target_id, 16) if iTarget not in self.__seq_id: self.__seq_id.update({iTarget: 0}) self.__seq_id.update({iTarget: self.__seq_id[iTarget] + 1}) s = self.__seq_id[iTarget] SRUP_response_message.sequence_id = s SRUP_response_message.sender_id = int(self.__device_id, 16) SRUP_response_message.target = target_id SRUP_response_message.status = status SRUP_response_message.sign(self.__local_private_key) serial_data = SRUP_response_message.serialize() if self.__isServer: pre_topic = target_id else: pre_topic = self.__device_id if serial_data is not None: topic = "SRUP/{}".format(pre_topic) self.__mqtt_client.publish(topic, serial_data) time.sleep(1) # self.__mqtt_client.loop_write() else: # TODO: THROW A CUSTOM EXCEPTION print("Message did not serialize") # The last thing to do for an update response – is to note the token... if status == SRUP_response_message.srup_response_status_update_success( ): self.__open_update_tokens.update({int(target_id, 16): token})
def __on_message(self, client, userdata, msg): # First check if the message is even for us... # Remembering that server's are wild... topic = None ch_topic = msg.topic if ch_topic[0:5] == 'SRUP/': topic = ch_topic[5:] # First check if the message is for us (or if we're a server read it anyway) if topic == self.__device_id or self.__isServer: SRUP_generic_message = pySRUPLib.SRUP_Generic() # if if deserializes then it's probably a SRUP message... if SRUP_generic_message.deserialize(msg.payload): # Did we send it? If so, ignore it... if SRUP_generic_message.sender_id != int(self.__device_id, 16): # Check to see if we've had a message from this sender before (creating a counter if we haven't) if SRUP_generic_message.sender_id not in self.__seq_id: self.__seq_id.update( {SRUP_generic_message.sender_id: 0}) # Get current sequence ID for this sender... s = self.__seq_id[SRUP_generic_message.sender_id] # Check to see the sequence ID of the message is greater than the last received message # to avoid replay attack... if SRUP_generic_message.sequence_id > s: # Update the "last received" sequence ID for this sender... self.__seq_id[ SRUP_generic_message. sender_id] = SRUP_generic_message.sequence_id msg_type = SRUP_generic_message.msg_type if msg_type == SRUP_ACTION_MESSAGE_TYPE: SRUP_action_message = pySRUPLib.SRUP_Action() SRUP_action_message.deserialize(msg.payload) if SRUP_action_message.verify( self.__remote_public_key): self.__on_action(SRUP_action_message) else: # TODO: THROW A CUSTOM EXCEPTION print("Message did not verify using {}".format( self.__remote_public_key)) elif msg_type == SRUP_DATA_MESSAGE_TYPE: SRUP_data_message = pySRUPLib.SRUP_Data() SRUP_data_message.deserialize(msg.payload) if SRUP_data_message.verify( self.__remote_public_key): self.__on_data(SRUP_data_message) else: # TODO: THROW A CUSTOM EXCEPTION print("Message did not verify using {}".format( self.__remote_public_key)) elif msg_type == SRUP_INITIATE_MESSAGE_TYPE: # Devices can't send init messages – so skip this if we're a server... if not self.__isServer: SRUP_initiate_message = pySRUPLib.SRUP_Initiate( ) SRUP_initiate_message.deserialize(msg.payload) if SRUP_initiate_message.verify( self.__remote_public_key): self.__on_initiate(SRUP_initiate_message) else: # TODO: THROW A CUSTOM EXCEPTION print("Message did not verify using {}". format(self.__remote_public_key)) elif msg_type == SRUP_RESPONSE_MESSAGE_TYPE: SRUP_response_message = pySRUPLib.SRUP_Response() SRUP_response_message.deserialize(msg.payload) if SRUP_response_message.verify( self.__remote_public_key): self.__on_response(SRUP_response_message) else: # TODO: THROW A CUSTOM EXCEPTION print("Message did not verify using {}".format( self.__remote_public_key)) elif msg_type == SRUP_ACTIVATE_MESSAGE_TYPE: # Devices can't send activate messages either – so again, we'll skip if we're a server. if not self.__isServer: SRUP_activate_message = pySRUPLib.SRUP_Activate( ) SRUP_activate_message.deserialize(msg.payload) if SRUP_activate_message.verify( self.__remote_public_key): self.__on_activate(SRUP_activate_message) else: # TODO: THROW A CUSTOM EXCEPTION print("Message did not verify using {}". format(self.__remote_public_key)) else: # We have received a message type that we can't handle... # TODO: THROW A CUSTOM EXCEPTION print("Invalid message type or format") print(SRUP_generic_message.sequence_id) else: # TODO: THROW A CUSTOM EXCEPTION print("Sequence ID 0x{:02X} is invalid".format( SRUP_generic_message.sequence_id)) # print("Message Type: {}".format(SRUP_generic_message.msg_type)) else: pass # This is our own message – so ignore it... else: pass # TODO: Not a SRUP Message ... else: pass
def test_response_token(): x = pySRUPLib.SRUP_Response() assert x.token is None x.token = "TEST_TOKEN" assert x.token == "TEST_TOKEN"
def test_response_status_values(): # Here we'll use hard coded values to test that the correct implementation has been achieved from the C++ header... x = pySRUPLib.SRUP_Response() t = x.srup_response_status_update_success() assert t == 0x00 t = x.srup_response_status_update_fail_server() assert t == 0xFD t = x.srup_response_status_update_fail_file() assert t == 0xFE t = x.srup_response_status_update_fail_digest() assert t == 0xFF t = x.srup_response_status_update_fail_http_error() assert t == 0xFC t = x.srup_response_status_activate_success() assert t == 0x10 t = x.srup_response_status_activate_fail() assert t == 0x1F t = x.srup_response_status_action_success() assert t == 0x20 t = x.srup_response_status_action_unknown() assert t == 0x2E t = x.srup_response_status_action_fail() assert t == 0x2F t = x.srup_response_status_data_type_unknown() assert t == 0x3F t = x.srup_response_status_group_add_success() assert t == 0x40 t = x.srup_response_status_group_delete_success() assert t == 0x41 t = x.srup_response_status_group_delete_invalid() assert t == 0x4C t = x.srup_response_status_group_delete_fail() assert t == 0x4D t = x.srup_response_status_group_add_fail_limit() assert t == 0x4E t = x.srup_response_status_group_add_fail() assert t == 0x4F t = x.srup_response_status_join_success() assert t == 0x50 t = x.srup_response_status_join_refused() assert t == 0x5E t = x.srup_response_status_join_fail() assert t == 0x5F t = x.srup_response_status_observed_join_valid() assert t == 0x60 t = x.srup_response_status_observed_join_invalid() assert t == 0x6E t = x.srup_response_status_observed_join_fail() assert t == 0x6F t = x.srup_response_status_resign_success() assert t == 0x70 t = x.srup_response_status_resign_fail() assert t == 0x7F t = x.srup_response_status_deregister_success() assert t == 0x80 t = x.srup_response_status_deregister_fail() assert t == 0x8F
def test_response_type(): x = pySRUPLib.SRUP_Response() assert x.msg_type == pySRUPLib.__response_message_type()