def test_deserializer_from_activate(): # 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 x = pySRUPLib.SRUP_Activate() y = pySRUPLib.SRUP_Generic() x.token = token x.sequence_id = seq_id x.sender_id = send_id 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 send_SRUP_Activate(self, target_id, token): SRUP_activate_message = pySRUPLib.SRUP_Activate() SRUP_activate_message.token = token # When we're sending a message – the sender ID is obviously the device ID of the "device" (or server) that's # sending the message... But the sequence ID should be one more than the last seq_id used in a message to # that recipient... 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_activate_message.sequence_id = s SRUP_activate_message.sender_id = int(self.__device_id, 16) SRUP_activate_message.sign(self.__local_private_key) serial_data = SRUP_activate_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")
def test_activate_serializer(): x = pySRUPLib.SRUP_Activate() x.token = "TOKEN12345" x.sequence_id = 0x1234567890ABCDEF x.sender_id = 0x5F5F5F5F5F5F5F5F assert x.sign(keyfile) is True z = x.serialize()
def test_activate_deserializer(): token = "TOKEN12345" seq_id = 0x1234567890ABCDEF send_id = 0x5F5F5F5F5F5F5F5F x = pySRUPLib.SRUP_Activate() y = pySRUPLib.SRUP_Activate() x.token = token x.sequence_id = seq_id x.sender_id = send_id 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
def test_activate_generic_deserializer(): token = "TOKEN12345" seq_id = 0x1234567890ABCDEF send_id = 0x5F5F5F5F5F5F5F5F x = pySRUPLib.SRUP_Activate() i = pySRUPLib.SRUP_Generic() x.token = token x.sequence_id = seq_id x.sender_id = send_id assert x.sign(keyfile) is True z = x.serialize() assert i.deserialize(z) is True assert i.msg_type == pySRUPLib.__activate_message_type()
def test_activate_sender(): MAX_SENDER = 0xFFFFFFFFFFFFFFFF ZERO_SENDER = 0x00 VALID_SENDER = 0x7FFFFFFFFFFFFFE7 x = pySRUPLib.SRUP_Activate() 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_activate_seqid(): MAX_SEQID = 0xFFFFFFFFFFFFFFFF ZERO_SEQID = 0x00 VALID_SEQID = 0x7FFFFFFFFFFFFFE7 x = pySRUPLib.SRUP_Activate() 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 test_activate_signing(): blank = "" x = pySRUPLib.SRUP_Activate() assert x.sign(blank) is False 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(blank) is False assert x.sign(keyfile) is True assert x.verify(pubkeyfile) is True # Transpose a digit in the digest... x.sequence_id = 0x5F5F5F5F5F5F5F5F - 1 assert x.verify(pubkeyfile) is False
def test_activate_token(): x = pySRUPLib.SRUP_Activate() assert x.token is None x.token = "TEST_TOKEN" assert x.token == "TEST_TOKEN"
def test_empty_object(): x = pySRUPLib.SRUP_Activate() assert x.token is None assert x.sequence_id is None assert x.sender_id is None assert x.sign("") is False
def test_activate_type(): x = pySRUPLib.SRUP_Activate() assert x.msg_type == pySRUPLib.__activate_message_type()
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