def test_utcoffset(self): """ Test SmsPduTzInfo.utcoffest() """ tests = (('08', 2), ('B2', -8)) for pduOffsetStr, offset in tests: result = gsmmodem.pdu.SmsPduTzInfo(pduOffsetStr) expected = SimpleOffsetTzInfo(offset) self.assertEqual(result.utcoffset(0), expected.utcoffset(0))
def test_decode(self): """ Tests SMS PDU decoding """ tests = ( (b'06917228195339040B917228214365F700003130805120618005D4F29C2E03', { 'type': 'SMS-DELIVER', 'smsc': '+2782913593', 'number': '+27821234567', 'protocol_id': 0, 'time': datetime(2013, 3, 8, 15, 2, 16, tzinfo=SimpleOffsetTzInfo(2)), 'text': 'Test2' }), (b'07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E', { 'type': 'SMS-DELIVER', 'smsc': '+85290000000', 'number': '+85291234567', 'time': datetime( 2007, 4, 12, 23, 25, 42, tzinfo=SimpleOffsetTzInfo(8)), 'text': 'It is easy to read text messages via AT commands.' }), (b'06917228195339040B917228214365F70000313062315352800A800D8A5E98D337A910', { 'type': 'SMS-DELIVER', 'number': '+27821234567', 'text': '@{tést}!' }), (b'07911326040000F0310D0B911326880736F40000A90FF7FBDD454E87CDE1B0DB357EB701', { 'type': 'SMS-SUBMIT', 'smsc': '+31624000000', 'number': '+31628870634', 'validity': timedelta(days=3), 'text': 'www.diafaan.com' }), (b'0006D60B911326880736F4111011719551401110117195714000', { 'type': 'SMS-STATUS-REPORT', 'number': '+31628870634', 'reference': 214 })) for pdu, expected in tests: result = gsmmodem.pdu.decodeSmsPdu(pdu) self.assertIsInstance(result, dict) for key, value in expected.items(): self.assertIn(key, result) self.assertEqual( result[key], value, 'Failed to decode PDU value for "{0}". Expected "{1}", got "{2}".' .format(key, value, result[key]))
def test_SimpleOffsetTzInfo(self): """ Basic test for the SimpleOffsetTzInfo class """ tests = (2, -4, 0, 3.5) for hours in tests: tz = SimpleOffsetTzInfo(hours) self.assertEqual(tz.offsetInHours, hours) self.assertEqual(tz.utcoffset(None), timedelta(hours=hours)) self.assertEqual(tz.dst(None), timedelta(0)) self.assertIsInstance(tz.__repr__(), str)
def setUp(self): self.tests = ( (datetime(2015, 11, 27, 0, 0, 0, tzinfo=SimpleOffsetTzInfo(0)), b'51117200000000'), (datetime(2015, 11, 27, 0, 0, 0, tzinfo=SimpleOffsetTzInfo(2)), b'51117200000080'), # same as previous but with GMT+2 timezone (datetime(2007, 4, 12, 23, 25, 42, tzinfo=SimpleOffsetTzInfo(8)), b'70402132522423'), (datetime(2007, 4, 12, 23, 25, 42, tzinfo=SimpleOffsetTzInfo(-8)), b'7040213252242B'), # same as previous but with GMT-8 timezone )
def test_encodeSmsSubmit(self): """ Tests SMS PDU encoding """ tests = ( ('+27820001111', 'Hello World!', 0, None, None, False, False, b'0001000B917228001011F100000CC8329BFD065DDF72363904'), ('+27820001111', 'Flash SMS', 0, None, None, False, True, b'0005000B917228001011F10000094676788E064D9B53'), ('+123456789', '世界您好!', 0, timedelta(weeks=52), '+44000000000', False, False, b'07914400000000F01100099121436587F90008F40A4E16754C60A8597DFF01' ), ('0126541234', 'Test message: local numbers', 13, timedelta(days=3), '12345', True, False, b'04A12143F5310D0AA110624521430000A91BD4F29C0E6A97E7F3F0B9AC03B1DFE3301BE4AEB7C565F91C' ), ('+27820001111', 'Timestamp validity test', 0, datetime(2013, 7, 10, 13, 39, tzinfo=SimpleOffsetTzInfo(2)), None, False, False, b'0019000B917228001011F100003170013193008017D474BB3CA787DB70903DCC4E93D3F43C885E9ED301' ), ) for number, text, reference, validity, smsc, rejectDuplicates, sendFlash, pduHex in tests: pdu = bytearray(codecs.decode(pduHex, 'hex_codec')) result = gsmmodem.pdu.encodeSmsSubmitPdu(number, text, reference, validity, smsc, rejectDuplicates, sendFlash) self.assertIsInstance(result, list) self.assertEqual( len(result), 1, 'Only 1 PDU should have been created, but got {0}'.format( len(result))) self.assertIsInstance(result[0], gsmmodem.pdu.Pdu) self.assertEqual( result[0].data, pdu, 'Failed to encode SMS PDU for number: "{0}" and text "{1}". Expected: "{2}", got: "{3}"' .format( number, text, pduHex, codecs.encode(compat.str(result[0].data), 'hex_codec').upper()))
def test_decode(self): """ Tests SMS PDU decoding """ tests = ( (b'06917228195339040B917228214365F700003130805120618005D4F29C2E03', { 'type': 'SMS-DELIVER', 'smsc': '+2782913593', 'number': '+27821234567', 'protocol_id': 0, 'time': datetime(2013, 3, 8, 15, 2, 16, tzinfo=SimpleOffsetTzInfo(2)), 'text': 'Test2' }), (b'07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E', { 'type': 'SMS-DELIVER', 'smsc': '+85290000000', 'number': '+85291234567', 'time': datetime(2007, 4, 12, 23, 25, 42, tzinfo=SimpleOffsetTzInfo(8)), 'text': 'It is easy to read text messages via AT commands.' }), (b'06917228195339040B917228214365F70000313062315352800A800D8A5E98D337A910', { 'type': 'SMS-DELIVER', 'number': '+27821234567', 'text': '@{tést}!' }), (b'07911326040000F0310D0B911326880736F40000A90FF7FBDD454E87CDE1B0DB357EB701', { 'type': 'SMS-SUBMIT', 'smsc': '+31624000000', 'number': '+31628870634', 'validity': timedelta(days=3), 'text': 'www.diafaan.com' }), (b'0006D60B911326880736F4111011719551401110117195714000', { 'type': 'SMS-STATUS-REPORT', 'number': '+31628870634', 'reference': 214 }), (b'0591721891F1400781721881F800003160526104848059050003C30101916536FB1DCABEEB2074D85E064941B19CAB060319A5C522289C96D3D3ED32286C0FA7D96131BBEC024941B19CAB0603DDD36C36A88C87A7E565D0DB0D82C55EB0DB4B068BCD5C20', { 'type': 'SMS-DELIVER', 'number': '2781188', 'smsc': '+2781191', 'text': 'Hello!You have R 19.50 FREE airtime available. R 19.50 will expire on 01/07/2013. ', 'udh': [gsmmodem.pdu.Concatenation(0x00, 0x03, [0xC3, 0x01, 0x01])] }), ( b'07914346466554F601000B914316565811F9000806304253F68449', # Valid UCS-2 PDU { 'type': 'SMS-SUBMIT', 'number': '+34616585119', 'smsc': '+34646456456', 'text': 'あ叶葉' }), ( b'0041010C910661345542F60008A0050003000301306F3044', # UCS-2 PDU; User data length is invalid in this PDU (too long) { 'type': 'SMS-SUBMIT', 'number': '+60164355246', 'smsc': None, 'udh': [ gsmmodem.pdu.Concatenation(0x00, 0x03, [0x00, 0x03, 0x01]) ], 'text': 'はい' }), ( b'0591721891F101000B917228214365F700040C48656C6C6F20776F726C6421', # 8-bit data coding { 'type': 'SMS-SUBMIT', 'number': '+27821234567', 'smsc': '+2781191', 'text': 'Hello world!' }), ( b'0019000B917228001011F100003170013193008017D474BB3CA787DB70903DCC4E93D3F43C885E9ED301', # absolute validity period { 'text': 'Timestamp validity test', 'validity': datetime(2013, 7, 10, 13, 39, tzinfo=SimpleOffsetTzInfo(2)) }), # Semi-invalid status report PDU captured from a ZTE modem ( b'0297F1061C0F910B487228297020F5317062419272803170624192138000', { 'type': 'SMS-STATUS-REPORT', 'number': '+b08427829207025', # <- broken number (invalid PDU data; the reference number is more than a single byte (or they added something)) 'reference': 28, 'time': datetime(2013, 7, 26, 14, 29, 27, tzinfo=SimpleOffsetTzInfo(2)), 'discharge': datetime(2013, 7, 26, 14, 29, 31, tzinfo=SimpleOffsetTzInfo(2)) }), (b'07919762020033F1400DD0CDF2396C7EBB010008415072411084618C0500035602010053004D005300200063006F00640065003A00200034003800350036002C00200063006F006E006600690072006D006100740069006F006E0020006F00660020006100730073006F00630069006100740069006F006E0020006200650074007700650065006E0020006100630063006F0075006E007400200061006E00640020004D00650067', { 'type': 'SMS-DELIVER', 'smsc': '+79262000331', 'number': 'Megafon', 'text': 'SMS code: 4856, confirmation of association between account and Meg', 'time': datetime(2014, 5, 27, 14, 1, 48, tzinfo=SimpleOffsetTzInfo(4)) })) for pdu, expected in tests: result = gsmmodem.pdu.decodeSmsPdu(pdu) self.assertIsInstance(result, dict) for key, value in expected.items(): self.assertIn(key, result) if key == 'udh': self.assertEqual( len(result[key]), len(value), 'Incorrect number of UDH information elements; expected {0}, got {1}' .format(len(result[key]), len(value))) for i in range(len(value)): got = result[key][i] expected = value[i] self.assertIsInstance(got, expected.__class__) self.assertEqual(expected.id, got.id) self.assertEqual(expected.dataLength, got.dataLength) self.assertEqual(expected.data, got.data) if isinstance(expected, gsmmodem.pdu.Concatenation): self.assertEqual(got.reference, expected.reference) self.assertEqual(got.parts, expected.parts) self.assertEqual(got.number, expected.number) elif isinstance(expected, gsmmodem.pdu.PortAddress): self.assertEqual(got.destination, expected.destination) self.assertEqual(got.source, expected.source) else: self.assertEqual( result[key], value, 'Failed to decode PDU value for "{0}". Expected "{1}", got "{2}".' .format(key, value, result[key]))
def test_decode(self): """ Tests SMS PDU decoding """ tests = ((b'06917228195339040B917228214365F700003130805120618005D4F29C2E03', {'type': 'SMS-DELIVER', 'smsc': '+2782913593', 'number': '+27821234567', 'protocol_id': 0, 'time': datetime(2013, 3, 8, 15, 2, 16, tzinfo=SimpleOffsetTzInfo(2)), 'text': 'Test2'}), (b'07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E', {'type': 'SMS-DELIVER', 'smsc': '+85290000000', 'number': '+85291234567', 'time': datetime(2007, 4, 12, 23, 25, 42, tzinfo=SimpleOffsetTzInfo(8)), 'text': 'It is easy to read text messages via AT commands.'}), (b'06917228195339040B917228214365F70000313062315352800A800D8A5E98D337A910', {'type': 'SMS-DELIVER', 'number': '+27821234567', 'text': '@{tést}!'}), (b'07911326040000F0310D0B911326880736F40000A90FF7FBDD454E87CDE1B0DB357EB701', {'type': 'SMS-SUBMIT', 'smsc': '+31624000000', 'number': '+31628870634', 'validity': timedelta(days=3), 'text': 'www.diafaan.com'}), (b'0006D60B911326880736F4111011719551401110117195714000', {'type': 'SMS-STATUS-REPORT', 'number': '+31628870634', 'reference': 214}), (b'0591721891F1400781721881F800003160526104848059050003C30101916536FB1DCABEEB2074D85E064941B19CAB060319A5C522289C96D3D3ED32286C0FA7D96131BBEC024941B19CAB0603DDD36C36A88C87A7E565D0DB0D82C55EB0DB4B068BCD5C20', {'type': 'SMS-DELIVER', 'number': '2781188', 'smsc': '+2781191', 'text': 'Hello!You have R 19.50 FREE airtime available. R 19.50 will expire on 01/07/2013. ', 'udh': [gsmmodem.pdu.Concatenation(0x00, 0x03, [0xC3, 0x01, 0x01])]}), (b'07914346466554F601000B914316565811F9000806304253F68449', # Valid UCS-2 PDU {'type': 'SMS-SUBMIT', 'number': '+34616585119', 'smsc': '+34646456456', 'text': 'あ叶葉'}), (b'0041010C910661345542F60008A0050003000301306F3044', # UCS-2 PDU; User data length is invalid in this PDU (too long) {'type': 'SMS-SUBMIT', 'number': '+60164355246', 'smsc': None, 'udh': [gsmmodem.pdu.Concatenation(0x00, 0x03, [0x00, 0x03, 0x01])], 'text': 'はい'}), (b'0591721891F101000B917228214365F700040C48656C6C6F20776F726C6421', # 8-bit data coding {'type': 'SMS-SUBMIT', 'number': '+27821234567', 'smsc': '+2781191', 'text': 'Hello world!'})) for pdu, expected in tests: result = gsmmodem.pdu.decodeSmsPdu(pdu) self.assertIsInstance(result, dict) for key, value in expected.items(): self.assertIn(key, result) if key == 'udh': self.assertEqual(len(result[key]), len(value), 'Incorrect number of UDH information elements; expected {0}, got {1}'.format(len(result[key]), len(value))) for i in range(len(value)): got = result[key][i] expected = value[i] self.assertIsInstance(got, expected.__class__) self.assertEqual(expected.id, got.id) self.assertEqual(expected.dataLength, got.dataLength) self.assertEqual(expected.data, got.data) if isinstance(expected, gsmmodem.pdu.Concatenation): self.assertEqual(got.reference, expected.reference) self.assertEqual(got.parts, expected.parts) self.assertEqual(got.index, expected.index) elif isinstance(expected, gsmmodem.pdu.PortAddress): self.assertEqual(got.destinationPort, expected.destinationPort) self.assertEqual(got.originPort, expected.originPort) else: self.assertEqual(result[key], value, 'Failed to decode PDU value for "{0}". Expected "{1}", got "{2}".'.format(key, value, result[key]))