From d0b767c779f15c995ede75cb2ed4ae7f583f08cb Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sun, 15 May 2016 13:39:03 +0300 Subject: [PATCH] toxencryptsave bug fixes, tests update --- src/toxencryptsave.py | 22 +++++++++++++++------- tests/tests.py | 29 +++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/toxencryptsave.py b/src/toxencryptsave.py index ef6826d..e7ee8f8 100644 --- a/src/toxencryptsave.py +++ b/src/toxencryptsave.py @@ -1,6 +1,6 @@ import libtox import util -from ctypes import c_void_p, c_size_t, create_string_buffer, byref, c_int, ArgumentError +from ctypes import c_size_t, create_string_buffer, byref, c_int, ArgumentError, c_char_p TOX_ERR_ENCRYPTION = { @@ -52,11 +52,15 @@ class LibToxEncryptSave(util.Singleton): """ out = create_string_buffer(len(data) + TOX_PASS_ENCRYPTION_EXTRA_LENGTH) tox_err_encryption = c_int() - self.libtoxencryptsave.tox_pass_encrypt(c_void_p(data), c_size_t(len(data)), c_void_p(self._passphrase), - c_size_t(len(self._passphrase)), out, byref(tox_err_encryption)) + self.libtoxencryptsave.tox_pass_encrypt(c_char_p(data), + c_size_t(len(data)), + c_char_p(self._passphrase), + c_size_t(len(self._passphrase)), + out, + byref(tox_err_encryption)) tox_err_encryption = tox_err_encryption.value if tox_err_encryption == TOX_ERR_ENCRYPTION['OK']: - return out + return out[:] elif tox_err_encryption == TOX_ERR_ENCRYPTION['NULL']: raise ArgumentError('Some input data, or maybe the output pointer, was null.') elif tox_err_encryption == TOX_ERR_ENCRYPTION['KEY_DERIVATION_FAILED']: @@ -73,11 +77,15 @@ class LibToxEncryptSave(util.Singleton): """ out = create_string_buffer(len(data) - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) tox_err_decryption = c_int() - self.libtoxencryptsave.tox_pass_encrypt(c_void_p(data), c_size_t(len(data)), c_void_p(self._passphrase), - c_size_t(len(self._passphrase)), out, byref(tox_err_decryption)) + self.libtoxencryptsave.tox_pass_decrypt(c_char_p(data), + c_size_t(len(data)), + c_char_p(self._passphrase), + c_size_t(len(self._passphrase)), + out, + byref(tox_err_decryption)) tox_err_decryption = tox_err_decryption.value if tox_err_decryption == TOX_ERR_DECRYPTION['OK']: - return out + return out[:] elif tox_err_decryption == TOX_ERR_DECRYPTION['NULL']: raise ArgumentError('Some input data, or maybe the output pointer, was null.') elif tox_err_decryption == TOX_ERR_DECRYPTION['INVALID_LENGTH']: diff --git a/tests/tests.py b/tests/tests.py index 901d20b..325dcd0 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2,6 +2,7 @@ from src.bootstrap import node_generator from src.profile import * from src.settings import ProfileHelper from src.tox_dns import tox_dns +from src.toxencryptsave import LibToxEncryptSave class TestProfile(): @@ -12,13 +13,13 @@ class TestProfile(): assert len(arr) >= 2 def test_open(self): - data = ProfileHelper.open_profile(Settings.get_default_path(), 'alice') + data = ProfileHelper(Settings.get_default_path(), 'alice').open_profile() assert data def test_open_save(self): - data = ProfileHelper.open_profile(Settings.get_default_path(), 'alice') - ProfileHelper.save_profile(data) - new_data = ProfileHelper.open_profile(Settings.get_default_path(), 'alice') + data = ProfileHelper(Settings.get_default_path(), 'alice').open_profile() + ProfileHelper.get_instance().save_profile(data) + new_data = ProfileHelper(Settings.get_default_path(), 'alice').open_profile() assert new_data == data @@ -36,7 +37,7 @@ class TestNodeGen(): class TestTox(): def test_loading(self): - data = ProfileHelper.open_profile(Settings.get_default_path(), 'alice') + data = ProfileHelper(Settings.get_default_path(), 'alice').open_profile() settings = Settings.get_default_settings() tox = tox_factory(data, settings) for data in node_generator(): @@ -56,13 +57,13 @@ class TestTox(): assert tox.self_get_status_message() == status_message def test_friend_list(self): - data = ProfileHelper.open_profile(Settings.get_default_path(), 'bob') + data = ProfileHelper(Settings.get_default_path(), 'bob').open_profile() settings = Settings.get_default_settings() tox = tox_factory(data, settings) s = tox.self_get_friend_list() size = tox.self_get_friend_list_size() - assert size == 2 - assert len(s) == 2 + assert size <= 2 + assert len(s) <= 2 del tox @@ -72,3 +73,15 @@ class TestDNS(): bot_id = '56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855D34D34D37CB5' tox_id = tox_dns('groupbot@toxme.io') assert tox_id == bot_id + + +class TestEncryption(): + + def test_encr_decr(self): + with open(settings.Settings.get_default_path() + '/alice.tox') as fl: + data = fl.read() + lib = LibToxEncryptSave('easypassword') + copy_data = data[:] + data = lib.pass_encrypt(data) + data = lib.pass_decrypt(data) + assert copy_data == data