From 18935c5b10ce3349222a5d7243e0438303641bc4 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Fri, 1 Jul 2016 23:15:00 +0300 Subject: [PATCH] tests update, bug fix --- src/bootstrap.py | 2 +- src/history.py | 8 ++++---- src/main.py | 4 ++-- src/menu.py | 2 +- src/plugin_support.py | 2 +- src/settings.py | 8 ++++---- src/toxencryptsave.py | 2 +- tests/tests.py | 41 ++++++++++++----------------------------- 8 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/bootstrap.py b/src/bootstrap.py index b4446b2..89534b7 100644 --- a/src/bootstrap.py +++ b/src/bootstrap.py @@ -6,7 +6,7 @@ class Node: self._ip, self._port, self._tox_key, self.rand = ip, port, tox_key, rand def get_data(self): - return self._ip, self._port, self._tox_key + return bytes(self._ip, 'utf-8'), self._port, self._tox_key def node_generator(): diff --git a/src/history.py b/src/history.py index 5489f6c..ad18ee5 100644 --- a/src/history.py +++ b/src/history.py @@ -3,7 +3,7 @@ from sqlite3 import connect import settings from os import chdir import os.path -from toxencryptsave import LibToxEncryptSave +from toxencryptsave import ToxEncryptSave PAGE_SIZE = 42 @@ -22,7 +22,7 @@ class History: chdir(settings.ProfileHelper.get_path()) path = settings.ProfileHelper.get_path() + self._name + '.hstr' if os.path.exists(path): - decr = LibToxEncryptSave.get_instance() + decr = ToxEncryptSave.get_instance() try: with open(path, 'rb') as fin: data = fin.read() @@ -40,7 +40,7 @@ class History: db.close() def save(self): - encr = LibToxEncryptSave.get_instance() + encr = ToxEncryptSave.get_instance() if encr.has_password(): path = settings.ProfileHelper.get_path() + self._name + '.hstr' with open(path, 'rb') as fin: @@ -54,7 +54,7 @@ class History: new_path = directory + self._name + '.hstr' with open(path, 'rb') as fin: data = fin.read() - encr = LibToxEncryptSave.get_instance() + encr = ToxEncryptSave.get_instance() if encr.has_password(): data = encr.pass_encrypt(data) with open(new_path, 'wb') as fout: diff --git a/src/main.py b/src/main.py index 297e8c4..54e4891 100644 --- a/src/main.py +++ b/src/main.py @@ -36,7 +36,7 @@ class Toxygen: Show password screen """ tmp = [data] - p = PasswordScreen(toxencryptsave.LibToxEncryptSave.get_instance(), tmp) + p = PasswordScreen(toxencryptsave.ToxEncryptSave.get_instance(), tmp) p.show() self.app.connect(self.app, QtCore.SIGNAL("lastWindowClosed()"), self.app, QtCore.SLOT("quit()")) self.app.exec_() @@ -58,7 +58,7 @@ class Toxygen: dark_style = fl.read() app.setStyleSheet(dark_style) - encrypt_save = toxencryptsave.LibToxEncryptSave() + encrypt_save = toxencryptsave.ToxEncryptSave() if self.path is not None: path = os.path.dirname(self.path) + '/' diff --git a/src/menu.py b/src/menu.py index acf8c93..79903bc 100644 --- a/src/menu.py +++ b/src/menu.py @@ -205,7 +205,7 @@ class ProfileSettings(CenteredWidget): def new_password(self): if self.password.text() == self.confirm_password.text(): if not len(self.password.text()) or len(self.password.text()) >= 8: - e = toxencryptsave.LibToxEncryptSave.get_instance() + e = toxencryptsave.ToxEncryptSave.get_instance() e.set_password(self.password.text()) self.close() else: diff --git a/src/plugin_support.py b/src/plugin_support.py index 2a1aebf..a865038 100644 --- a/src/plugin_support.py +++ b/src/plugin_support.py @@ -16,7 +16,7 @@ class PluginLoader(util.Singleton): self._settings = settings self._plugins = {} # dict. key - plugin unique short name, value - tuple (plugin instance, is active) self._tox = tox - self._encr = toxencryptsave.LibToxEncryptSave.get_instance() + self._encr = toxencryptsave.ToxEncryptSave.get_instance() def set_tox(self, tox): """ diff --git a/src/settings.py b/src/settings.py index e67d681..7250397 100644 --- a/src/settings.py +++ b/src/settings.py @@ -4,7 +4,7 @@ import os import locale from util import Singleton, curr_directory, log import pyaudio -from toxencryptsave import LibToxEncryptSave +from toxencryptsave import ToxEncryptSave import smileys @@ -20,7 +20,7 @@ class Settings(dict, Singleton): if os.path.isfile(self.path): with open(self.path, 'rb') as fl: data = fl.read() - inst = LibToxEncryptSave.get_instance() + inst = ToxEncryptSave.get_instance() try: if inst.is_data_encrypted(data): data = inst.pass_decrypt(data) @@ -145,7 +145,7 @@ class Settings(dict, Singleton): def save(self): text = json.dumps(self) - inst = LibToxEncryptSave.get_instance() + inst = ToxEncryptSave.get_instance() if inst.has_password(): text = bytes(inst.pass_encrypt(bytes(text, 'utf-8'))) else: @@ -224,7 +224,7 @@ class ProfileHelper(Singleton): return self._directory def save_profile(self, data): - inst = LibToxEncryptSave.get_instance() + inst = ToxEncryptSave.get_instance() if inst.has_password(): data = inst.pass_encrypt(data) with open(self._path, 'wb') as fl: diff --git a/src/toxencryptsave.py b/src/toxencryptsave.py index 03d8b1c..191dcb2 100644 --- a/src/toxencryptsave.py +++ b/src/toxencryptsave.py @@ -34,7 +34,7 @@ TOX_ERR_DECRYPTION = { TOX_PASS_ENCRYPTION_EXTRA_LENGTH = 80 -class LibToxEncryptSave(util.Singleton): +class ToxEncryptSave(util.Singleton): libtoxencryptsave = libtox.LibToxEncryptSave() diff --git a/tests/tests.py b/tests/tests.py index 325dcd0..46be179 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2,39 +2,21 @@ 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 +import src.toxencryptsave as encr -class TestProfile(): +class TestProfile: def test_search(self): arr = ProfileHelper.find_profiles() - assert arr assert len(arr) >= 2 def test_open(self): data = ProfileHelper(Settings.get_default_path(), 'alice').open_profile() assert data - def test_open_save(self): - 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 - -class TestNodeGen(): - - def test_generator(self): - for elem in node_generator(): - assert len(elem) == 3 - - def test_ports(self): - for elem in node_generator(): - assert elem[1] in [33445, 443, 5190, 2306, 1813] - - -class TestTox(): +class TestTox: def test_loading(self): data = ProfileHelper(Settings.get_default_path(), 'alice').open_profile() @@ -45,16 +27,16 @@ class TestTox(): del tox def test_creation(self): - name = 'Toxygen User' - status_message = 'Toxing on Toxygen' + name = b'Toxygen User' + status_message = b'Toxing on Toxygen' tox = tox_factory() tox.self_set_name(name) tox.self_set_status_message(status_message) data = tox.get_savedata() del tox tox = tox_factory(data) - assert tox.self_get_name() == name - assert tox.self_get_status_message() == status_message + assert tox.self_get_name() == str(name, 'utf-8') + assert tox.self_get_status_message() == str(status_message, 'utf-8') def test_friend_list(self): data = ProfileHelper(Settings.get_default_path(), 'bob').open_profile() @@ -67,7 +49,7 @@ class TestTox(): del tox -class TestDNS(): +class TestDNS: def test_dns(self): bot_id = '56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855D34D34D37CB5' @@ -75,12 +57,13 @@ class TestDNS(): assert tox_id == bot_id -class TestEncryption(): +class TestEncryption: def test_encr_decr(self): - with open(settings.Settings.get_default_path() + '/alice.tox') as fl: + with open(settings.Settings.get_default_path() + '/alice.tox', 'rb') as fl: data = fl.read() - lib = LibToxEncryptSave('easypassword') + lib = encr.ToxEncryptSave() + lib.set_password('easypassword') copy_data = data[:] data = lib.pass_encrypt(data) data = lib.pass_decrypt(data)