def test_hex_characters_in_key(self): """ Test the ``decryption_key_type`` function on raising an exception when non-hexadecimal value are present in passed string, and normal returning the same string when there is no non-hexadecimal character within it. """ valid_lengths = (32, 48, 64) for key_len in valid_lengths: # Test the valid character set for the "decryption key" for i in range(10): key_value = self.get_rand_string(key_len, string.hexdigits) self.assertIs( decryption_key_type(key_value), key_value, "Function should just return the passed argument, because " "in the passed string were used only hexadecimal digits!" ) # Test the raising an exception when non-hexadecimal character # are present in the decryption key. non_hex_char = ''.join( set(string.ascii_letters) - set(string.hexdigits) ) for bad_char in non_hex_char: key_value = self.get_rand_string(key_len, string.hexdigits) key_value = key_value.replace( random.choice(key_value), bad_char, 1) assert not all(chr_ in string.hexdigits for chr_ in key_value) self.assertRaises( argparse.ArgumentTypeError, decryption_key_type, (key_value,) )
def test_normal_decryption_key_length(self): """ Test that ``decryption_key_type`` properly inspects the length of passed key. """ valid_lengths = (32, 48, 64) # Check the correct variants of passed string length. for length_case in valid_lengths: key_example = self.get_rand_string(length_case, string.hexdigits) assert len(key_example) == length_case self.assertIs( decryption_key_type(key_example), key_example, "Function should just return the passed argument, because " "the %d is valid length for a decryption key!" % length_case ) # Check the raising an exception when the length of passed # argument is wrong. for i in set(range(100)) - set(valid_lengths): key_example = self.get_rand_string(i, string.hexdigits) self.assertRaises( argparse.ArgumentTypeError, decryption_key_type, (key_example,) )