2016-09-16 15:04:46 +03:00
|
|
|
import os
|
2016-09-08 17:11:37 +03:00
|
|
|
import unittest
|
2017-09-28 12:49:45 +03:00
|
|
|
from telethon.tl import TLObject
|
|
|
|
from telethon.extensions import BinaryReader
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
class UtilsTests(unittest.TestCase):
|
2018-03-24 14:12:47 +03:00
|
|
|
def test_binary_writer_reader(self):
|
2017-09-28 12:49:45 +03:00
|
|
|
# Test that we can read properly
|
|
|
|
data = b'\x01\x05\x00\x00\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
|
|
|
|
b'\x88A\x00\x00\x00\x00\x00\x009@\x1a\x1b\x1c\x1d\x1e\x1f ' \
|
|
|
|
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
|
|
|
|
b'\x00\x80'
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
with BinaryReader(data) as reader:
|
|
|
|
value = reader.read_byte()
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, 1,
|
|
|
|
msg='Example byte should be 1 but is {}'.format(value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
value = reader.read_int()
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, 5,
|
|
|
|
msg='Example integer should be 5 but is {}'.format(value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
value = reader.read_long()
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, 13,
|
|
|
|
msg='Example long integer should be 13 but is {}'.format(value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
value = reader.read_float()
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, 17.0,
|
|
|
|
msg='Example float should be 17.0 but is {}'.format(value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
value = reader.read_double()
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, 25.0,
|
|
|
|
msg='Example double should be 25.0 but is {}'.format(value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
value = reader.read(7)
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, bytes([26, 27, 28, 29, 30, 31, 32]),
|
|
|
|
msg='Example bytes should be {} but is {}'
|
|
|
|
.format(bytes([26, 27, 28, 29, 30, 31, 32]), value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
value = reader.read_large_int(128, signed=False)
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, 2**127,
|
|
|
|
msg='Example large integer should be {} but is {}'.format(2**127, value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
2018-03-24 14:12:47 +03:00
|
|
|
def test_binary_tgwriter_tgreader(self):
|
2016-09-16 15:04:46 +03:00
|
|
|
small_data = os.urandom(33)
|
2017-09-28 12:49:45 +03:00
|
|
|
small_data_padded = os.urandom(19) # +1 byte for length = 20 (%4 = 0)
|
2016-09-08 17:11:37 +03:00
|
|
|
|
2016-09-16 15:04:46 +03:00
|
|
|
large_data = os.urandom(999)
|
|
|
|
large_data_padded = os.urandom(1024)
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
data = (small_data, small_data_padded, large_data, large_data_padded)
|
|
|
|
string = 'Testing Telegram strings, this should work properly!'
|
2017-09-28 12:49:45 +03:00
|
|
|
serialized = b''.join(TLObject.serialize_bytes(d) for d in data) + \
|
|
|
|
TLObject.serialize_bytes(string)
|
2016-09-08 17:11:37 +03:00
|
|
|
|
2017-09-28 12:49:45 +03:00
|
|
|
with BinaryReader(serialized) as reader:
|
|
|
|
# And then try reading it without errors (it should be unharmed!)
|
2016-09-08 17:11:37 +03:00
|
|
|
for datum in data:
|
2017-09-28 12:49:45 +03:00
|
|
|
value = reader.tgread_bytes()
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, datum,
|
|
|
|
msg='Example bytes should be {} but is {}'.format(datum, value))
|
2016-09-08 17:11:37 +03:00
|
|
|
|
2017-09-28 12:49:45 +03:00
|
|
|
value = reader.tgread_string()
|
2018-03-24 14:12:47 +03:00
|
|
|
self.assertEqual(value, string,
|
|
|
|
msg='Example string should be {} but is {}'.format(string, value))
|