Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid timezone values #42

Merged
merged 2 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions gsmmodem/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def _setPduOffsetStr(self, pduOffsetStr):
# - Read HEX value as decimal
# - Multiply by 15
# See: https://en.wikipedia.org/wiki/GSM_03.40#Time_Format
try:
tzOffsetMinutes = int('{0:0>2X}'.format(tzHexVal & 0x7F)) * 15
except:
# Possible fix for #15
tzHexVal = int((tzHexVal & 0x0F) * 0x10) + int((tzHexVal & 0x0F) / 0x10)
tzOffsetMinutes = int('{0:0>2X}'.format(tzHexVal & 0x7F)) * 15

# Possible fix for #15 - convert invalid character to BCD-value
if (tzHexVal & 0x0F) > 0x9:
tzHexVal +=0x06

tzOffsetMinutes = int('{0:0>2X}'.format(tzHexVal & 0x7F)) * 15

if tzHexVal & 0x80 == 0: # positive
self._offset = timedelta(minutes=(tzOffsetMinutes))
Expand Down
59 changes: 38 additions & 21 deletions test/test_modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2210,31 +2210,48 @@ def writeCallbackFunc3(data):
time.sleep(0.1)
self.modem.close()

def test_receiveSmsPduMode_zeroLengthSmscAndHugeTimeZoneValue(self):
def test_receiveSmsPduMode_invalidPDUsRecordedFromModems(self):
""" Test receiving PDU-mode SMS using data captured from failed operations/bug reports """
modemResponse = ['+CMGR: 1,,26\r\n', '0006230E9126983575169498610103409544C26101034095448200\r\n', 'OK\r\n']
tests = ((['+CMGR: 1,,26\r\n', '0006230E9126983575169498610103409544C26101034095448200\r\n', 'OK\r\n'], # see: babca/python-gsmmodem#15
Sms.STATUS_RECEIVED_READ, # message read status
'+62895357614989', # number
35, # reference
datetime(2016, 10, 30, 4, 59, 44, tzinfo=SimpleOffsetTzInfo(8)), # sentTime
datetime(2016, 10, 30, 4, 59, 44, tzinfo=SimpleOffsetTzInfo(7)), # deliverTime
StatusReport.DELIVERED), # delivery status
)

callbackInfo = [False, '', '', -1, None, '', None]
def smsCallbackFunc1(sms):
try:
self.assertIsInstance(sms, gsmmodem.modem.StatusReport)
self.assertEqual(sms.status, gsmmodem.modem.Sms.STATUS_RECEIVED_READ)
finally:
callbackInfo[0] = True
callbackDone = [False]

def writeCallback1(data):
if data.startswith('AT+CMGR'):
self.modem.serial.flushResponseSequence = True
self.modem.serial.responseSequence = modemResponse
for modemResponse, msgStatus, number, reference, sentTime, deliverTime, deliveryStatus in tests:
def smsCallbackFunc1(sms):
try:
self.assertIsInstance(sms, gsmmodem.modem.StatusReport)
self.assertEqual(sms.status, msgStatus, 'Status report read status incorrect. Expected: "{0}", got: "{1}"'.format(msgStatus, sms.status))
self.assertEqual(sms.number, number, 'SMS sender number incorrect. Expected: "{0}", got: "{1}"'.format(number, sms.number))
self.assertEqual(sms.reference, reference, 'Status report SMS reference number incorrect. Expected: "{0}", got: "{1}"'.format(reference, sms.reference))
self.assertIsInstance(sms.timeSent, datetime, 'SMS sent time type invalid. Expected: datetime.datetime, got: {0}"'.format(type(sms.timeSent)))
self.assertEqual(sms.timeSent, sentTime, 'SMS sent time incorrect. Expected: "{0}", got: "{1}"'.format(sentTime, sms.timeSent))
self.assertIsInstance(sms.timeFinalized, datetime, 'SMS finalized time type invalid. Expected: datetime.datetime, got: {0}"'.format(type(sms.timeFinalized)))
self.assertEqual(sms.timeFinalized, deliverTime, 'SMS finalized time incorrect. Expected: "{0}", got: "{1}"'.format(deliverTime, sms.timeFinalized))
self.assertEqual(sms.deliveryStatus, deliveryStatus, 'SMS delivery status incorrect. Expected: "{0}", got: "{1}"'.format(deliveryStatus, sms.deliveryStatus))
self.assertEqual(sms.smsc, None, 'This SMS should not have any SMSC information')
finally:
callbackDone[0] = True

self.initModem(smsStatusReportCallback=smsCallbackFunc1)
# Fake a "new message" notification
self.modem.serial.writeCallbackFunc = writeCallback1
self.modem.serial.flushResponseSequence = True
self.modem.serial.responseSequence = ['+CDSI: "SM",1\r\n']
# Wait for the handler function to finish
while callbackInfo[0] == False:
time.sleep(0.1)
def writeCallback1(data):
if data.startswith('AT+CMGR'):
self.modem.serial.flushResponseSequence = True
self.modem.serial.responseSequence = modemResponse

self.initModem(smsStatusReportCallback=smsCallbackFunc1)
# Fake a "new message" notification
self.modem.serial.writeCallbackFunc = writeCallback1
self.modem.serial.flushResponseSequence = True
self.modem.serial.responseSequence = ['+CDSI: "SM",1\r\n']
# Wait for the handler function to finish
while callbackDone[0] == False:
time.sleep(0.1)



Expand Down