From 4681d37377f97faa60308d4a3bb31ed7bfbd2729 Mon Sep 17 00:00:00 2001 From: navjot Date: Wed, 18 Nov 2020 01:06:05 +0530 Subject: [PATCH] validate address in insert method and improve test cases --- src/api.py | 12 ++---------- src/bitmessagecurses/__init__.py | 3 +-- src/bitmessageqt/__init__.py | 11 ++--------- src/class_objectProcessor.py | 4 +--- src/helper_sent.py | 25 ++++++++++++++----------- src/tests/core.py | 8 ++++++-- 6 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/api.py b/src/api.py index da8b0d51..c93eb4d8 100644 --- a/src/api.py +++ b/src/api.py @@ -90,7 +90,6 @@ from addresses import ( ) from bmconfigparser import BMConfigParser from debug import logger -from helper_ackPayload import genAckPayload from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure from inventory import Inventory from network.threads import StoppableThread @@ -1101,7 +1100,6 @@ class BMRPCDispatcher(object): TTL = 28 * 24 * 60 * 60 toAddress = addBMIfNotPresent(toAddress) fromAddress = addBMIfNotPresent(fromAddress) - streamNumber, toRipe = self._verifyAddress(toAddress)[2:] self._verifyAddress(fromAddress) try: fromAddressEnabled = self.config.getboolean( @@ -1112,11 +1110,7 @@ class BMRPCDispatcher(object): if not fromAddressEnabled: raise APIError(14, 'Your fromAddress is disabled. Cannot send.') - stealthLevel = self.config.safeGetInt( - 'bitmessagesettings', 'ackstealthlevel') - ackdata = genAckPayload(streamNumber, stealthLevel) - - helper_sent.insert( + ackdata = helper_sent.insert( toAddress=toAddress, fromAddress=fromAddress, subject=subject, message=message, encoding=encodingType) @@ -1158,11 +1152,9 @@ class BMRPCDispatcher(object): except BaseException: raise APIError( 13, 'Could not find your fromAddress in the keys.dat file.') - streamNumber = decodeAddress(fromAddress)[2] - ackdata = genAckPayload(streamNumber, 0) toAddress = str_broadcast_subscribers - helper_sent.insert( + ackdata = helper_sent.insert( fromAddress=fromAddress, subject=subject, message=message, status='broadcastqueued', encoding=encodingType) diff --git a/src/bitmessagecurses/__init__.py b/src/bitmessagecurses/__init__.py index eefe32d3..ff734e72 100644 --- a/src/bitmessagecurses/__init__.py +++ b/src/bitmessagecurses/__init__.py @@ -919,8 +919,7 @@ def sendMessage(sender="", recv="", broadcast=None, subject="", body="", reply=F list(set(recvlist)) # Remove exact duplicates for addr in recvlist: if addr != "": - # pylint: disable=redefined-outer-name - status, version, stream, ripe = decodeAddress(addr) # pylint: disable=unused-variable + status, version, stream, ripe = decodeAddress(addr) if status != "success": set_background_title(d, "Recipient address error") err = "Could not decode" + addr + " : " + status + "\n\n" diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 81f73922..240324ef 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -35,7 +35,6 @@ from foldertree import ( MessageList_TimeWidget) import settingsmixin import support -from helper_ackPayload import genAckPayload from helper_sql import sqlQuery, sqlExecute, sqlExecuteChunked, sqlStoredProcedure import helper_search import l10n @@ -2071,7 +2070,6 @@ class MyForm(settingsmixin.SMainWindow): ).arg(email) ) return - # pylint: disable=unused-variable status, addressVersionNumber, streamNumber, ripe = decodeAddress( toAddress) if status != 'success': @@ -2164,10 +2162,7 @@ class MyForm(settingsmixin.SMainWindow): " send the message but it won\'t send until" " you connect.") ) - stealthLevel = BMConfigParser().safeGetInt( - 'bitmessagesettings', 'ackstealthlevel') - ackdata = genAckPayload(streamNumber, stealthLevel) - helper_sent.insert( + ackdata = helper_sent.insert( toAddress=toAddress, fromAddress=fromAddress, subject=subject, message=message, encoding=encoding) toLabel = '' @@ -2203,12 +2198,10 @@ class MyForm(settingsmixin.SMainWindow): # We don't actually need the ackdata for acknowledgement since # this is a broadcast message, but we can use it to update the # user interface when the POW is done generating. - streamNumber = decodeAddress(fromAddress)[2] - ackdata = genAckPayload(streamNumber, 0) toAddress = str_broadcast_subscribers # msgid. We don't know what this will be until the POW is done. - helper_sent.insert( + ackdata = helper_sent.insert( fromAddress=fromAddress, subject=subject, message=message, status='broadcastqueued', encoding=encoding) diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index 559644a2..0bec7454 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -745,11 +745,9 @@ class objectProcessor(threading.Thread): # We don't actually need the ackdata for acknowledgement # since this is a broadcast message but we can use it to # update the user interface when the POW is done generating. - streamNumber = decodeAddress(fromAddress)[2] - ackdata = genAckPayload(streamNumber, 0) toAddress = '[Broadcast subscribers]' - helper_sent.insert( + ackdata = helper_sent.insert( fromAddress=fromAddress, status='broadcastqueued', subject=subject, diff --git a/src/helper_sent.py b/src/helper_sent.py index 635afc6b..d83afce6 100644 --- a/src/helper_sent.py +++ b/src/helper_sent.py @@ -18,11 +18,11 @@ def insert(msgid=None, toAddress='[Broadcast subscribers]', fromAddress=None, su # pylint: disable=unused-variable # pylint: disable-msg=too-many-locals - msgid = msgid if msgid else uuid.uuid4().bytes - + valid_addr = True if not ripe or not ackdata: addr = fromAddress if toAddress == '[Broadcast subscribers]' else toAddress new_status, addressVersionNumber, streamNumber, new_ripe = decodeAddress(addr) + valid_addr = True if new_status == 'success' else False if not ripe: ripe = new_ripe @@ -31,15 +31,18 @@ def insert(msgid=None, toAddress='[Broadcast subscribers]', fromAddress=None, su 'bitmessagesettings', 'ackstealthlevel') new_ackdata = genAckPayload(streamNumber, stealthLevel) ackdata = new_ackdata + if valid_addr: + msgid = msgid if msgid else uuid.uuid4().bytes + sentTime = sentTime if sentTime else int(time.time()) # sentTime (this doesn't change) + lastActionTime = lastActionTime if lastActionTime else int(time.time()) - sentTime = sentTime if sentTime else int(time.time()) # sentTime (this doesn't change) - lastActionTime = lastActionTime if lastActionTime else int(time.time()) + ttl = ttl if ttl else BMConfigParser().getint('bitmessagesettings', 'ttl') - ttl = ttl if ttl else BMConfigParser().getint('bitmessagesettings', 'ttl') + t = (msgid, toAddress, ripe, fromAddress, subject, message, ackdata, + sentTime, lastActionTime, sleeptill, status, retryNumber, folder, + encoding, ttl) - t = (msgid, toAddress, ripe, fromAddress, subject, message, ackdata, - sentTime, lastActionTime, sleeptill, status, retryNumber, folder, - encoding, ttl) - - sqlExecute('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t) - return ackdata + sqlExecute('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t) + return ackdata + else: + return None diff --git a/src/tests/core.py b/src/tests/core.py index 42ebe83b..5c222fa4 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -244,7 +244,7 @@ class TestCore(unittest.TestCase): def test_insert_method_msgid(self): """Test insert method of helper_sent module with message sending""" fromAddress = 'BM-2cTrmD22fLRrumi3pPLg1ELJ6PdAaTRTdfg' - toAddress = 'BM-2cVWtdUzPwF7UNGDrZftWuHWgjdfkj89fdf' + toAddress = 'BM-2cUGaEcGz9Zft1SPAo8FJtfzyADTpEgU9U' message = 'test message' subject = 'test subject' result = helper_sent.insert( @@ -253,7 +253,11 @@ class TestCore(unittest.TestCase): ) queryreturn = sqlQuery( '''select msgid from sent where ackdata=?''', result) - self.assertNotEqual(queryreturn[0][0], '') + self.assertNotEqual(queryreturn[0][0] if queryreturn else '' , '') + + column_type = sqlQuery( + '''select typeof(msgid) from sent where ackdata=?''', result) + self.assertEqual(column_type[0][0] if column_type else '', 'text') def run():