2016-09-08 17:11:37 +03:00
|
|
|
import random
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
import unittest
|
|
|
|
|
2016-09-17 21:42:34 +03:00
|
|
|
import telethon.network.authenticator as authenticator
|
2017-06-09 17:13:39 +03:00
|
|
|
from telethon.extensions import TcpClient
|
2017-08-28 22:23:31 +03:00
|
|
|
from telethon.network import Connection
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
def run_server_echo_thread(port):
|
|
|
|
def server_thread():
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
s.bind(('', port))
|
|
|
|
s.listen(1)
|
2017-05-21 14:59:16 +03:00
|
|
|
connection, address = s.accept()
|
|
|
|
with connection:
|
|
|
|
data = connection.recv(16)
|
|
|
|
connection.send(data)
|
2016-09-08 17:11:37 +03:00
|
|
|
|
|
|
|
server = threading.Thread(target=server_thread)
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
|
|
class NetworkTests(unittest.TestCase):
|
|
|
|
@staticmethod
|
|
|
|
def test_tcp_client():
|
|
|
|
port = random.randint(50000, 60000) # Arbitrary non-privileged port
|
|
|
|
run_server_echo_thread(port)
|
|
|
|
|
|
|
|
msg = b'Unit testing...'
|
|
|
|
client = TcpClient()
|
|
|
|
client.connect('localhost', port)
|
|
|
|
client.write(msg)
|
2016-11-30 00:29:05 +03:00
|
|
|
assert msg == client.read(
|
2017-02-19 17:21:25 +03:00
|
|
|
15), 'Read message does not equal sent message'
|
2016-09-08 17:11:37 +03:00
|
|
|
client.close()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def test_authenticator():
|
2017-08-28 22:23:31 +03:00
|
|
|
transport = Connection('149.154.167.91', 443)
|
2016-09-17 21:42:34 +03:00
|
|
|
authenticator.do_authentication(transport)
|
2016-09-08 17:11:37 +03:00
|
|
|
transport.close()
|