From d427c27e7ea37902a1a6e3b96c9047b0330aa587 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Mon, 6 Dec 2021 20:09:10 +0200 Subject: [PATCH 1/6] Started a test case for address generator --- src/tests/test_addressgenerator.py | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/tests/test_addressgenerator.py diff --git a/src/tests/test_addressgenerator.py b/src/tests/test_addressgenerator.py new file mode 100644 index 00000000..0549242f --- /dev/null +++ b/src/tests/test_addressgenerator.py @@ -0,0 +1,54 @@ +import unittest +from binascii import unhexlify + +from pybitmessage import pathmagic + +from .samples import ( + sample_seed, sample_deterministic_addr3, sample_deterministic_addr4, + sample_deterministic_ripe) + + +class TestAddressGenerator(unittest.TestCase): + """Test case for AddressGenerator (with thread or not)""" + + @classmethod + def setUpClass(cls): + pathmagic.setup() # need this because of import state in network ): + import queues + import state + + state.enableGUI = False + from class_addressGenerator import addressGenerator + + # import helper_startup + # from bmconfigparser import BMConfigParser + + # helper_startup.loadConfig() + # config = BMConfigParser() + + cls.command_queue = queues.addressGeneratorQueue + cls.return_queue = queues.apiAddressGeneratorReturnQueue + cls.worker_queue = queues.workerQueue + + thread = addressGenerator() + thread.daemon = True + thread.start() + + def _execute(self, command, *args): + self.command_queue.put((command,) + args) + try: + return self.return_queue.get()[0] + except IndexError: + self.fail('Failed to execute command %s' % command) + + def test_createChan(self): + """Test createChan command""" + self.assertEqual( + sample_deterministic_addr3, + self._execute('createChan', 3, 1, 'test', sample_seed, True)) + self.assertEqual( + self.worker_queue.get(), + ('sendOutOrStoreMyV3Pubkey', unhexlify(sample_deterministic_ripe))) + self.assertEqual( + sample_deterministic_addr4, + self._execute('createChan', 4, 1, 'test', sample_seed, True)) -- 2.45.1 From 799aadc9bcb5fe7b8b22d7efafaaee771f35fc04 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Mon, 4 Jul 2022 04:10:18 +0300 Subject: [PATCH 2/6] Rewrite TestAddressGenerator as a subclass of TestPartialRun, edit docstrings and add more checks. Closes: #1895 --- src/tests/test_addressgenerator.py | 39 +++++++++++++++++++----------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/tests/test_addressgenerator.py b/src/tests/test_addressgenerator.py index 0549242f..764286e8 100644 --- a/src/tests/test_addressgenerator.py +++ b/src/tests/test_addressgenerator.py @@ -1,35 +1,37 @@ -import unittest +"""Tests for AddressGenerator (with thread or not)""" + from binascii import unhexlify -from pybitmessage import pathmagic - +from .partial import TestPartialRun from .samples import ( sample_seed, sample_deterministic_addr3, sample_deterministic_addr4, sample_deterministic_ripe) -class TestAddressGenerator(unittest.TestCase): - """Test case for AddressGenerator (with thread or not)""" +class TestAddressGenerator(TestPartialRun): + """Test case for AddressGenerator thread""" @classmethod def setUpClass(cls): - pathmagic.setup() # need this because of import state in network ): - import queues - import state + super(TestAddressGenerator, cls).setUpClass() - state.enableGUI = False + import defaults + import queues from class_addressGenerator import addressGenerator - # import helper_startup - # from bmconfigparser import BMConfigParser - - # helper_startup.loadConfig() - # config = BMConfigParser() + 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() @@ -52,3 +54,12 @@ class TestAddressGenerator(unittest.TestCase): self.assertEqual( sample_deterministic_addr4, self._execute('createChan', 4, 1, 'test', sample_seed, True)) + self.assertEqual( + self.worker_queue.get(), + ('sendOutOrStoreMyV4Pubkey', sample_deterministic_addr4)) + self.assertEqual( + self.config.get(sample_deterministic_addr4, 'label'), 'test') + self.assertTrue( + self.config.getboolean(sample_deterministic_addr4, 'chan')) + self.assertTrue( + self.config.getboolean(sample_deterministic_addr4, 'enabled')) -- 2.45.1 From 9a96cab080502479c47b0d9cc295a17e2b8a7621 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Mon, 4 Jul 2022 18:32:43 +0300 Subject: [PATCH 3/6] Make sample_seed bytes as in crypto --- src/tests/samples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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' -- 2.45.1 From 6045b587e95038e94fbdc56e3fc687602bf97965 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Mon, 4 Jul 2022 18:33:42 +0300 Subject: [PATCH 4/6] Config values are strings --- src/class_addressGenerator.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/class_addressGenerator.py b/src/class_addressGenerator.py index a308a52e..05143cd8 100644 --- a/src/class_addressGenerator.py +++ b/src/class_addressGenerator.py @@ -187,9 +187,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 @@ -350,11 +351,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(( -- 2.45.1 From d8e0c52ecdfeb7b12b325cafe661cc99e1bbb1b7 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Wed, 20 Dec 2023 05:00:51 +0200 Subject: [PATCH 5/6] Resolve some pylint warnings in class_addressGenerator --- src/class_addressGenerator.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/class_addressGenerator.py b/src/class_addressGenerator.py index 05143cd8..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 @@ -199,7 +200,7 @@ class addressGenerator(StoppableThread): queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "Done generating address. Doing work necessary" " to broadcast it...") @@ -214,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' @@ -225,7 +227,7 @@ class addressGenerator(StoppableThread): if command == 'createDeterministicAddresses': queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "Generating %1 new addresses." ).arg(str(numberOfAddressesToMake)) @@ -330,7 +332,7 @@ class addressGenerator(StoppableThread): ) queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "%1 is already in 'Your Identities'." " Not adding it again." @@ -341,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', @@ -384,7 +385,7 @@ class addressGenerator(StoppableThread): 'sendOutOrStoreMyV4Pubkey', address)) queues.UISignalQueue.put(( 'updateStatusBar', - tr._translate( + _translate( "MainWindow", "Done generating address") )) elif saveAddressToDisk and not live \ @@ -393,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': -- 2.45.1 From c45f1eae1404c0198677c9d37b4acca660756731 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Wed, 27 Dec 2023 04:09:21 +0200 Subject: [PATCH 6/6] Test more deterministic commands, add timeouts in get from queue --- src/tests/test_addressgenerator.py | 48 ++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/tests/test_addressgenerator.py b/src/tests/test_addressgenerator.py index 764286e8..236b4d8b 100644 --- a/src/tests/test_addressgenerator.py +++ b/src/tests/test_addressgenerator.py @@ -2,11 +2,15 @@ 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""" @@ -39,26 +43,44 @@ class TestAddressGenerator(TestPartialRun): def _execute(self, command, *args): self.command_queue.put((command,) + args) try: - return self.return_queue.get()[0] - except IndexError: + return self.return_queue.get(timeout=30)[0] + except (IndexError, queue.Empty): self.fail('Failed to execute command %s' % command) - def test_createChan(self): - """Test createChan 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('createChan', 3, 1, 'test', sample_seed, True)) - self.assertEqual( - self.worker_queue.get(), - ('sendOutOrStoreMyV3Pubkey', unhexlify(sample_deterministic_ripe))) + 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', sample_seed, True)) + 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.worker_queue.get(), - ('sendOutOrStoreMyV4Pubkey', sample_deterministic_addr4)) - self.assertEqual( - self.config.get(sample_deterministic_addr4, 'label'), 'test') + self.config.get(sample_deterministic_addr4, 'label'), TEST_LABEL) self.assertTrue( self.config.getboolean(sample_deterministic_addr4, 'chan')) self.assertTrue( -- 2.45.1