diff --git a/src/class_addressGenerator.py b/src/class_addressGenerator.py index a308a52e..06a0521a 100644 --- a/src/class_addressGenerator.py +++ b/src/class_addressGenerator.py @@ -5,19 +5,20 @@ import hashlib import time from binascii import hexlify +from six.moves import configparser, queue + import defaults import highlevelcrypto import queues import shared import state -import tr from addresses import decodeAddress, encodeAddress, encodeVarint from bmconfigparser import config from fallback import RIPEMD160Hash from network import StoppableThread from pyelliptic import arithmetic from pyelliptic.openssl import OpenSSL -from six.moves import configparser, queue +from tr import _translate class AddressGeneratorException(Exception): @@ -31,6 +32,7 @@ class addressGenerator(StoppableThread): name = "addressGenerator" def stopThread(self): + """Tell the thread to stop putting a special command to it's queue""" try: queues.addressGeneratorQueue.put(("stopThread", "data")) except queue.Full: @@ -43,8 +45,7 @@ class addressGenerator(StoppableThread): Process the requests for addresses generation from `.queues.addressGeneratorQueue` """ - # pylint: disable=too-many-locals, too-many-branches - # pylint: disable=protected-access, too-many-statements + # pylint: disable=too-many-locals,too-many-branches,too-many-statements # pylint: disable=too-many-nested-blocks while state.shutdown == 0: @@ -119,7 +120,7 @@ class addressGenerator(StoppableThread): if command == 'createRandomAddress': queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "Generating one new address") )) # This next section is a little bit strange. We're going @@ -187,9 +188,10 @@ class addressGenerator(StoppableThread): config.set(address, 'payloadlengthextrabytes', str( payloadLengthExtraBytes)) config.set( - address, 'privsigningkey', privSigningKeyWIF) + address, 'privsigningkey', privSigningKeyWIF.decode()) config.set( - address, 'privencryptionkey', privEncryptionKeyWIF) + address, 'privencryptionkey', + privEncryptionKeyWIF.decode()) config.save() # The API and the join and create Chan functionality @@ -198,7 +200,7 @@ class addressGenerator(StoppableThread): queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "Done generating address. Doing work necessary" " to broadcast it...") @@ -213,9 +215,10 @@ class addressGenerator(StoppableThread): queues.workerQueue.put(( 'sendOutOrStoreMyV4Pubkey', address)) - elif command == 'createDeterministicAddresses' \ - or command == 'getDeterministicAddress' \ - or command == 'createChan' or command == 'joinChan': + elif command in ( + 'createDeterministicAddresses', 'createChan', + 'getDeterministicAddress', 'joinChan' + ): if not deterministicPassphrase: self.logger.warning( 'You are creating deterministic' @@ -224,7 +227,7 @@ class addressGenerator(StoppableThread): if command == 'createDeterministicAddresses': queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "Generating %1 new addresses." ).arg(str(numberOfAddressesToMake)) @@ -329,7 +332,7 @@ class addressGenerator(StoppableThread): ) queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "%1 is already in 'Your Identities'." " Not adding it again." @@ -340,8 +343,7 @@ class addressGenerator(StoppableThread): config.set(address, 'label', label) config.set(address, 'enabled', 'true') config.set(address, 'decoy', 'false') - if command == 'joinChan' \ - or command == 'createChan': + if command in ('createChan', 'joinChan'): config.set(address, 'chan', 'true') config.set( address, 'noncetrialsperbyte', @@ -350,11 +352,11 @@ class addressGenerator(StoppableThread): address, 'payloadlengthextrabytes', str(payloadLengthExtraBytes)) config.set( - address, 'privSigningKey', - privSigningKeyWIF) + address, 'privsigningkey', + privSigningKeyWIF.decode()) config.set( - address, 'privEncryptionKey', - privEncryptionKeyWIF) + address, 'privencryptionkey', + privEncryptionKeyWIF.decode()) config.save() queues.UISignalQueue.put(( @@ -383,7 +385,7 @@ class addressGenerator(StoppableThread): 'sendOutOrStoreMyV4Pubkey', address)) queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "Done generating address") )) elif saveAddressToDisk and not live \ @@ -392,8 +394,9 @@ class addressGenerator(StoppableThread): address) # Done generating addresses. - if command == 'createDeterministicAddresses' \ - or command == 'joinChan' or command == 'createChan': + if command in ( + 'createDeterministicAddresses', 'createChan', 'joinChan' + ): queues.apiAddressGeneratorReturnQueue.put( listOfNewAddressesToSendOutThroughTheAPI) elif command == 'getDeterministicAddress': diff --git a/src/tests/samples.py b/src/tests/samples.py index 9237d19d..aa4bac9e 100644 --- a/src/tests/samples.py +++ b/src/tests/samples.py @@ -36,7 +36,7 @@ sample_point = ( 94730058721143827257669456336351159718085716196507891067256111928318063085006 ) -sample_seed = 'TIGER, tiger, burning bright. In the forests of the night' +sample_seed = b'TIGER, tiger, burning bright. In the forests of the night' # Deterministic addresses with stream 1 and versions 3, 4 sample_deterministic_ripe = b'00cfb69416ae76f68a81c459de4e13460c7d17eb' sample_deterministic_addr3 = 'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN' diff --git a/src/tests/test_addressgenerator.py b/src/tests/test_addressgenerator.py new file mode 100644 index 00000000..236b4d8b --- /dev/null +++ b/src/tests/test_addressgenerator.py @@ -0,0 +1,87 @@ +"""Tests for AddressGenerator (with thread or not)""" + +from binascii import unhexlify + +from six.moves import queue + +from .partial import TestPartialRun +from .samples import ( + sample_seed, sample_deterministic_addr3, sample_deterministic_addr4, + sample_deterministic_ripe) + +TEST_LABEL = 'test' + + +class TestAddressGenerator(TestPartialRun): + """Test case for AddressGenerator thread""" + + @classmethod + def setUpClass(cls): + super(TestAddressGenerator, cls).setUpClass() + + import defaults + import queues + from class_addressGenerator import addressGenerator + + cls.state.enableGUI = False + + cls.command_queue = queues.addressGeneratorQueue + cls.return_queue = queues.apiAddressGeneratorReturnQueue + cls.worker_queue = queues.workerQueue + + cls.config.set( + 'bitmessagesettings', 'defaultnoncetrialsperbyte', + str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) + cls.config.set( + 'bitmessagesettings', 'defaultpayloadlengthextrabytes', + str(defaults.networkDefaultPayloadLengthExtraBytes)) + + thread = addressGenerator() + thread.daemon = True + thread.start() + + def _execute(self, command, *args): + self.command_queue.put((command,) + args) + try: + return self.return_queue.get(timeout=30)[0] + except (IndexError, queue.Empty): + self.fail('Failed to execute command %s' % command) + + def test_deterministic(self): + """Test deterministic commands""" + self.command_queue.put(( + 'getDeterministicAddress', 3, 1, + TEST_LABEL, 1, sample_seed, False)) + self.assertEqual(sample_deterministic_addr3, self.return_queue.get()) + + self.assertEqual( + sample_deterministic_addr3, + self._execute( + 'createDeterministicAddresses', 3, 1, TEST_LABEL, 2, + sample_seed, False, 0, 0)) + + try: + self.assertEqual( + self.worker_queue.get(timeout=30), + ('sendOutOrStoreMyV3Pubkey', + unhexlify(sample_deterministic_ripe))) + + self.worker_queue.get(timeout=30) # get the next addr's task + except queue.Empty: + self.fail('No commands in the worker queue') + + self.assertEqual( + sample_deterministic_addr4, + self._execute('createChan', 4, 1, TEST_LABEL, sample_seed, True)) + try: + self.assertEqual( + self.worker_queue.get(), + ('sendOutOrStoreMyV4Pubkey', sample_deterministic_addr4)) + except queue.Empty: + self.fail('No commands in the worker queue') + self.assertEqual( + self.config.get(sample_deterministic_addr4, 'label'), TEST_LABEL) + self.assertTrue( + self.config.getboolean(sample_deterministic_addr4, 'chan')) + self.assertTrue( + self.config.getboolean(sample_deterministic_addr4, 'enabled'))