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

View File

@ -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"

View File

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

View File

@ -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,

View File

@ -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,7 +31,8 @@ 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())
@ -43,3 +44,5 @@ def insert(msgid=None, toAddress='[Broadcast subscribers]', fromAddress=None, su
sqlExecute('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t)
return ackdata
else:
return None

View File

@ -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():