A test case for address generator #2170

Merged
PeterSurda merged 6 commits from gitea-33 into v0.6 2024-01-17 09:34:05 +01:00
3 changed files with 113 additions and 23 deletions

View File

@ -5,19 +5,20 @@ import hashlib
import time import time
from binascii import hexlify from binascii import hexlify
from six.moves import configparser, queue
import defaults import defaults
import highlevelcrypto import highlevelcrypto
import queues import queues
import shared import shared
import state import state
import tr
from addresses import decodeAddress, encodeAddress, encodeVarint from addresses import decodeAddress, encodeAddress, encodeVarint
from bmconfigparser import config from bmconfigparser import config
from fallback import RIPEMD160Hash from fallback import RIPEMD160Hash
from network import StoppableThread from network import StoppableThread
from pyelliptic import arithmetic from pyelliptic import arithmetic
from pyelliptic.openssl import OpenSSL from pyelliptic.openssl import OpenSSL
from six.moves import configparser, queue from tr import _translate
class AddressGeneratorException(Exception): class AddressGeneratorException(Exception):
@ -31,6 +32,7 @@ class addressGenerator(StoppableThread):
name = "addressGenerator" name = "addressGenerator"
def stopThread(self): def stopThread(self):
"""Tell the thread to stop putting a special command to it's queue"""
try: try:
queues.addressGeneratorQueue.put(("stopThread", "data")) queues.addressGeneratorQueue.put(("stopThread", "data"))
except queue.Full: except queue.Full:
@ -43,8 +45,7 @@ class addressGenerator(StoppableThread):
Process the requests for addresses generation Process the requests for addresses generation
from `.queues.addressGeneratorQueue` from `.queues.addressGeneratorQueue`
""" """
# pylint: disable=too-many-locals, too-many-branches # pylint: disable=too-many-locals,too-many-branches,too-many-statements
# pylint: disable=protected-access, too-many-statements
# pylint: disable=too-many-nested-blocks # pylint: disable=too-many-nested-blocks
while state.shutdown == 0: while state.shutdown == 0:
@ -119,7 +120,7 @@ class addressGenerator(StoppableThread):
if command == 'createRandomAddress': if command == 'createRandomAddress':
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateStatusBar', 'updateStatusBar',
tr._translate( _translate(
"MainWindow", "Generating one new address") "MainWindow", "Generating one new address")
)) ))
# This next section is a little bit strange. We're going # This next section is a little bit strange. We're going
@ -187,9 +188,10 @@ class addressGenerator(StoppableThread):
config.set(address, 'payloadlengthextrabytes', str( config.set(address, 'payloadlengthextrabytes', str(
payloadLengthExtraBytes)) payloadLengthExtraBytes))
config.set( config.set(
address, 'privsigningkey', privSigningKeyWIF) address, 'privsigningkey', privSigningKeyWIF.decode())
config.set( config.set(
address, 'privencryptionkey', privEncryptionKeyWIF) address, 'privencryptionkey',
privEncryptionKeyWIF.decode())
config.save() config.save()
# The API and the join and create Chan functionality # The API and the join and create Chan functionality
@ -198,7 +200,7 @@ class addressGenerator(StoppableThread):
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateStatusBar', 'updateStatusBar',
tr._translate( _translate(
"MainWindow", "MainWindow",
"Done generating address. Doing work necessary" "Done generating address. Doing work necessary"
" to broadcast it...") " to broadcast it...")
@ -213,9 +215,10 @@ class addressGenerator(StoppableThread):
queues.workerQueue.put(( queues.workerQueue.put((
'sendOutOrStoreMyV4Pubkey', address)) 'sendOutOrStoreMyV4Pubkey', address))
elif command == 'createDeterministicAddresses' \ elif command in (
or command == 'getDeterministicAddress' \ 'createDeterministicAddresses', 'createChan',
or command == 'createChan' or command == 'joinChan': 'getDeterministicAddress', 'joinChan'
):
if not deterministicPassphrase: if not deterministicPassphrase:
self.logger.warning( self.logger.warning(
'You are creating deterministic' 'You are creating deterministic'
@ -224,7 +227,7 @@ class addressGenerator(StoppableThread):
if command == 'createDeterministicAddresses': if command == 'createDeterministicAddresses':
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateStatusBar', 'updateStatusBar',
tr._translate( _translate(
"MainWindow", "MainWindow",
"Generating %1 new addresses." "Generating %1 new addresses."
).arg(str(numberOfAddressesToMake)) ).arg(str(numberOfAddressesToMake))
@ -329,7 +332,7 @@ class addressGenerator(StoppableThread):
) )
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateStatusBar', 'updateStatusBar',
tr._translate( _translate(
"MainWindow", "MainWindow",
"%1 is already in 'Your Identities'." "%1 is already in 'Your Identities'."
" Not adding it again." " Not adding it again."
@ -340,8 +343,7 @@ class addressGenerator(StoppableThread):
config.set(address, 'label', label) config.set(address, 'label', label)
config.set(address, 'enabled', 'true') config.set(address, 'enabled', 'true')
config.set(address, 'decoy', 'false') config.set(address, 'decoy', 'false')
if command == 'joinChan' \ if command in ('createChan', 'joinChan'):
or command == 'createChan':
config.set(address, 'chan', 'true') config.set(address, 'chan', 'true')
config.set( config.set(
address, 'noncetrialsperbyte', address, 'noncetrialsperbyte',
@ -350,11 +352,11 @@ class addressGenerator(StoppableThread):
address, 'payloadlengthextrabytes', address, 'payloadlengthextrabytes',
str(payloadLengthExtraBytes)) str(payloadLengthExtraBytes))
config.set( config.set(
address, 'privSigningKey', address, 'privsigningkey',
privSigningKeyWIF) privSigningKeyWIF.decode())
config.set( config.set(
address, 'privEncryptionKey', address, 'privencryptionkey',
privEncryptionKeyWIF) privEncryptionKeyWIF.decode())
config.save() config.save()
queues.UISignalQueue.put(( queues.UISignalQueue.put((
@ -383,7 +385,7 @@ class addressGenerator(StoppableThread):
'sendOutOrStoreMyV4Pubkey', address)) 'sendOutOrStoreMyV4Pubkey', address))
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateStatusBar', 'updateStatusBar',
tr._translate( _translate(
"MainWindow", "Done generating address") "MainWindow", "Done generating address")
)) ))
elif saveAddressToDisk and not live \ elif saveAddressToDisk and not live \
@ -392,8 +394,9 @@ class addressGenerator(StoppableThread):
address) address)
# Done generating addresses. # Done generating addresses.
if command == 'createDeterministicAddresses' \ if command in (
or command == 'joinChan' or command == 'createChan': 'createDeterministicAddresses', 'createChan', 'joinChan'
):
queues.apiAddressGeneratorReturnQueue.put( queues.apiAddressGeneratorReturnQueue.put(
listOfNewAddressesToSendOutThroughTheAPI) listOfNewAddressesToSendOutThroughTheAPI)
elif command == 'getDeterministicAddress': elif command == 'getDeterministicAddress':

View File

@ -36,7 +36,7 @@ sample_point = (
94730058721143827257669456336351159718085716196507891067256111928318063085006 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 # Deterministic addresses with stream 1 and versions 3, 4
sample_deterministic_ripe = b'00cfb69416ae76f68a81c459de4e13460c7d17eb' sample_deterministic_ripe = b'00cfb69416ae76f68a81c459de4e13460c7d17eb'
sample_deterministic_addr3 = 'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN' sample_deterministic_addr3 = 'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN'

View File

@ -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'))