validate address in insert method and improve test cases

This commit is contained in:
navjot 2020-11-18 01:06:05 +05:30
parent 360539b320
commit 4681d37377
No known key found for this signature in database
GPG Key ID: 9EE70AFD71357F1C
6 changed files with 26 additions and 37 deletions

View File

@ -90,7 +90,6 @@ from addresses import (
) )
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from debug import logger from debug import logger
from helper_ackPayload import genAckPayload
from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure
from inventory import Inventory from inventory import Inventory
from network.threads import StoppableThread from network.threads import StoppableThread
@ -1101,7 +1100,6 @@ class BMRPCDispatcher(object):
TTL = 28 * 24 * 60 * 60 TTL = 28 * 24 * 60 * 60
toAddress = addBMIfNotPresent(toAddress) toAddress = addBMIfNotPresent(toAddress)
fromAddress = addBMIfNotPresent(fromAddress) fromAddress = addBMIfNotPresent(fromAddress)
streamNumber, toRipe = self._verifyAddress(toAddress)[2:]
self._verifyAddress(fromAddress) self._verifyAddress(fromAddress)
try: try:
fromAddressEnabled = self.config.getboolean( fromAddressEnabled = self.config.getboolean(
@ -1112,11 +1110,7 @@ class BMRPCDispatcher(object):
if not fromAddressEnabled: if not fromAddressEnabled:
raise APIError(14, 'Your fromAddress is disabled. Cannot send.') raise APIError(14, 'Your fromAddress is disabled. Cannot send.')
stealthLevel = self.config.safeGetInt( ackdata = helper_sent.insert(
'bitmessagesettings', 'ackstealthlevel')
ackdata = genAckPayload(streamNumber, stealthLevel)
helper_sent.insert(
toAddress=toAddress, fromAddress=fromAddress, toAddress=toAddress, fromAddress=fromAddress,
subject=subject, message=message, encoding=encodingType) subject=subject, message=message, encoding=encodingType)
@ -1158,11 +1152,9 @@ class BMRPCDispatcher(object):
except BaseException: except BaseException:
raise APIError( raise APIError(
13, 'Could not find your fromAddress in the keys.dat file.') 13, 'Could not find your fromAddress in the keys.dat file.')
streamNumber = decodeAddress(fromAddress)[2]
ackdata = genAckPayload(streamNumber, 0)
toAddress = str_broadcast_subscribers toAddress = str_broadcast_subscribers
helper_sent.insert( ackdata = helper_sent.insert(
fromAddress=fromAddress, subject=subject, fromAddress=fromAddress, subject=subject,
message=message, status='broadcastqueued', message=message, status='broadcastqueued',
encoding=encodingType) encoding=encodingType)

View File

@ -919,8 +919,7 @@ def sendMessage(sender="", recv="", broadcast=None, subject="", body="", reply=F
list(set(recvlist)) # Remove exact duplicates list(set(recvlist)) # Remove exact duplicates
for addr in recvlist: for addr in recvlist:
if addr != "": if addr != "":
# pylint: disable=redefined-outer-name status, version, stream, ripe = decodeAddress(addr)
status, version, stream, ripe = decodeAddress(addr) # pylint: disable=unused-variable
if status != "success": if status != "success":
set_background_title(d, "Recipient address error") set_background_title(d, "Recipient address error")
err = "Could not decode" + addr + " : " + status + "\n\n" err = "Could not decode" + addr + " : " + status + "\n\n"

View File

@ -35,7 +35,6 @@ from foldertree import (
MessageList_TimeWidget) MessageList_TimeWidget)
import settingsmixin import settingsmixin
import support import support
from helper_ackPayload import genAckPayload
from helper_sql import sqlQuery, sqlExecute, sqlExecuteChunked, sqlStoredProcedure from helper_sql import sqlQuery, sqlExecute, sqlExecuteChunked, sqlStoredProcedure
import helper_search import helper_search
import l10n import l10n
@ -2071,7 +2070,6 @@ class MyForm(settingsmixin.SMainWindow):
).arg(email) ).arg(email)
) )
return return
# pylint: disable=unused-variable
status, addressVersionNumber, streamNumber, ripe = decodeAddress( status, addressVersionNumber, streamNumber, ripe = decodeAddress(
toAddress) toAddress)
if status != 'success': if status != 'success':
@ -2164,10 +2162,7 @@ class MyForm(settingsmixin.SMainWindow):
" send the message but it won\'t send until" " send the message but it won\'t send until"
" you connect.") " you connect.")
) )
stealthLevel = BMConfigParser().safeGetInt( ackdata = helper_sent.insert(
'bitmessagesettings', 'ackstealthlevel')
ackdata = genAckPayload(streamNumber, stealthLevel)
helper_sent.insert(
toAddress=toAddress, fromAddress=fromAddress, toAddress=toAddress, fromAddress=fromAddress,
subject=subject, message=message, encoding=encoding) subject=subject, message=message, encoding=encoding)
toLabel = '' toLabel = ''
@ -2203,12 +2198,10 @@ class MyForm(settingsmixin.SMainWindow):
# We don't actually need the ackdata for acknowledgement since # We don't actually need the ackdata for acknowledgement since
# this is a broadcast message, but we can use it to update the # this is a broadcast message, but we can use it to update the
# user interface when the POW is done generating. # user interface when the POW is done generating.
streamNumber = decodeAddress(fromAddress)[2]
ackdata = genAckPayload(streamNumber, 0)
toAddress = str_broadcast_subscribers toAddress = str_broadcast_subscribers
# msgid. We don't know what this will be until the POW is done. # msgid. We don't know what this will be until the POW is done.
helper_sent.insert( ackdata = helper_sent.insert(
fromAddress=fromAddress, fromAddress=fromAddress,
subject=subject, message=message, subject=subject, message=message,
status='broadcastqueued', encoding=encoding) status='broadcastqueued', encoding=encoding)

View File

@ -745,11 +745,9 @@ class objectProcessor(threading.Thread):
# We don't actually need the ackdata for acknowledgement # We don't actually need the ackdata for acknowledgement
# since this is a broadcast message but we can use it to # since this is a broadcast message but we can use it to
# update the user interface when the POW is done generating. # update the user interface when the POW is done generating.
streamNumber = decodeAddress(fromAddress)[2]
ackdata = genAckPayload(streamNumber, 0)
toAddress = '[Broadcast subscribers]' toAddress = '[Broadcast subscribers]'
helper_sent.insert( ackdata = helper_sent.insert(
fromAddress=fromAddress, fromAddress=fromAddress,
status='broadcastqueued', status='broadcastqueued',
subject=subject, subject=subject,

View File

@ -18,11 +18,11 @@ def insert(msgid=None, toAddress='[Broadcast subscribers]', fromAddress=None, su
# pylint: disable=unused-variable # pylint: disable=unused-variable
# pylint: disable-msg=too-many-locals # pylint: disable-msg=too-many-locals
msgid = msgid if msgid else uuid.uuid4().bytes valid_addr = True
if not ripe or not ackdata: if not ripe or not ackdata:
addr = fromAddress if toAddress == '[Broadcast subscribers]' else toAddress addr = fromAddress if toAddress == '[Broadcast subscribers]' else toAddress
new_status, addressVersionNumber, streamNumber, new_ripe = decodeAddress(addr) new_status, addressVersionNumber, streamNumber, new_ripe = decodeAddress(addr)
valid_addr = True if new_status == 'success' else False
if not ripe: if not ripe:
ripe = new_ripe ripe = new_ripe
@ -31,7 +31,8 @@ def insert(msgid=None, toAddress='[Broadcast subscribers]', fromAddress=None, su
'bitmessagesettings', 'ackstealthlevel') 'bitmessagesettings', 'ackstealthlevel')
new_ackdata = genAckPayload(streamNumber, stealthLevel) new_ackdata = genAckPayload(streamNumber, stealthLevel)
ackdata = new_ackdata 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) sentTime = sentTime if sentTime else int(time.time()) # sentTime (this doesn't change)
lastActionTime = lastActionTime if lastActionTime else int(time.time()) lastActionTime = lastActionTime if lastActionTime else int(time.time())
@ -43,3 +44,5 @@ def insert(msgid=None, toAddress='[Broadcast subscribers]', fromAddress=None, su
sqlExecute('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t) sqlExecute('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t)
return ackdata return ackdata
else:
return None

View File

@ -244,7 +244,7 @@ class TestCore(unittest.TestCase):
def test_insert_method_msgid(self): def test_insert_method_msgid(self):
"""Test insert method of helper_sent module with message sending""" """Test insert method of helper_sent module with message sending"""
fromAddress = 'BM-2cTrmD22fLRrumi3pPLg1ELJ6PdAaTRTdfg' fromAddress = 'BM-2cTrmD22fLRrumi3pPLg1ELJ6PdAaTRTdfg'
toAddress = 'BM-2cVWtdUzPwF7UNGDrZftWuHWgjdfkj89fdf' toAddress = 'BM-2cUGaEcGz9Zft1SPAo8FJtfzyADTpEgU9U'
message = 'test message' message = 'test message'
subject = 'test subject' subject = 'test subject'
result = helper_sent.insert( result = helper_sent.insert(
@ -253,7 +253,11 @@ class TestCore(unittest.TestCase):
) )
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select msgid from sent where ackdata=?''', result) '''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(): def run():