Changeset View
Changeset View
Standalone View
Standalone View
test/util/rpcauth-test.py
Show All 19 Lines | def setUp(self): | ||||
os.path.join(os.sep, os.path.abspath(os.path.dirname(__file__)), | os.path.join(os.sep, os.path.abspath(os.path.dirname(__file__)), | ||||
"../config.ini")) | "../config.ini")) | ||||
with open(config_path, encoding="utf8") as config_file: | with open(config_path, encoding="utf8") as config_file: | ||||
config.read_file(config_file) | config.read_file(config_file) | ||||
sys.path.insert(0, os.path.dirname(config['environment']['RPCAUTH'])) | sys.path.insert(0, os.path.dirname(config['environment']['RPCAUTH'])) | ||||
self.rpcauth = importlib.import_module('rpcauth') | self.rpcauth = importlib.import_module('rpcauth') | ||||
def test_generate_salt(self): | def test_generate_salt(self): | ||||
self.assertLessEqual(len(self.rpcauth.generate_salt()), 32) | for i in range(16, 32 + 1): | ||||
self.assertGreaterEqual(len(self.rpcauth.generate_salt()), 16) | self.assertEqual(len(self.rpcauth.generate_salt(i)), i * 2) | ||||
def test_generate_password(self): | def test_generate_password(self): | ||||
password = self.rpcauth.generate_password() | password = self.rpcauth.generate_password() | ||||
expected_password = base64.urlsafe_b64encode( | expected_password = base64.urlsafe_b64encode( | ||||
base64.urlsafe_b64decode(password)).decode('utf-8') | base64.urlsafe_b64decode(password)).decode('utf-8') | ||||
self.assertEqual(expected_password, password) | self.assertEqual(expected_password, password) | ||||
def test_check_password_hmac(self): | def test_check_password_hmac(self): | ||||
salt = self.rpcauth.generate_salt() | salt = self.rpcauth.generate_salt(16) | ||||
password = self.rpcauth.generate_password() | password = self.rpcauth.generate_password() | ||||
password_hmac = self.rpcauth.password_to_hmac(salt, password) | password_hmac = self.rpcauth.password_to_hmac(salt, password) | ||||
m = hmac.new(bytearray(salt, 'utf-8'), | m = hmac.new(bytearray(salt, 'utf-8'), | ||||
bytearray(password, 'utf-8'), 'SHA256') | bytearray(password, 'utf-8'), 'SHA256') | ||||
expected_password_hmac = m.hexdigest() | expected_password_hmac = m.hexdigest() | ||||
self.assertEqual(expected_password_hmac, password_hmac) | self.assertEqual(expected_password_hmac, password_hmac) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
unittest.main() | unittest.main() |