2020-04-20 08:36:41 +00:00
import unittest
import socket
from threading import Thread
2020-04-24 23:43:24 +00:00
import sys
2020-04-20 08:36:41 +00:00
import time
import stem
import stem . process
from stem . control import Controller
from youandme import client
2020-04-24 03:22:50 +00:00
class Connection :
connected = True
2020-04-23 09:23:57 +00:00
def get_open_port ( ) :
# taken from (but modified) https://stackoverflow.com/a/2838309 by https://stackoverflow.com/users/133374/albert ccy-by-sa-3 https://creativecommons.org/licenses/by-sa/3.0/
# changes from source: import moved to top of file, bind specifically to localhost
s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM )
s . bind ( ( " 127.0.0.1 " , 0 ) )
s . listen ( 1 )
port = s . getsockname ( ) [ 1 ]
s . close ( )
return port
control_port = str ( get_open_port ( ) )
socks_port = str ( get_open_port ( ) )
assert control_port != socks_port
2020-04-20 08:36:41 +00:00
stem . process . launch_tor_with_config (
config = {
' ControlPort ' : control_port ,
' SocksPort ' : socks_port ,
' Log ' : [
' NOTICE stdout '
] ,
} , take_ownership = True )
2020-04-24 23:43:24 +00:00
class Address :
address = " "
def fake_server ( ) :
with Controller . from_port ( port = int ( control_port ) ) as controller :
controller . authenticate ( )
with socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) as s :
ip = ' 127.0.0.1 '
s . bind ( ( ip , 0 ) )
s . listen ( 1 )
port = s . getsockname ( ) [ 1 ]
serv = controller . create_ephemeral_hidden_service (
{ 1337 : ' 127.0.0.1: ' + str ( port ) } ,
key_content = ' ED25519-V3 ' ,
await_publication = True ,
)
Address . address = serv . service_id
conn , addr = s . accept ( )
while True :
conn . send ( chr ( 116 ) . encode ( ' utf-8 ' ) )
data = conn . recv ( 1 )
2020-04-20 08:36:41 +00:00
class TestClient ( unittest . TestCase ) :
def test_client ( self ) :
2020-04-24 23:43:24 +00:00
Thread ( target = fake_server , daemon = True ) . start ( )
2020-04-20 08:36:41 +00:00
send_data = bytearray ( )
recv_data = bytearray ( )
2020-04-24 23:43:24 +00:00
while Address . address == " " :
time . sleep ( 1 )
print ( Address . address )
Thread ( target = client . client , args = [ 0.01 , Address . address , int ( socks_port ) , send_data , recv_data , Connection ] , daemon = True ) . start ( )
start = time . time ( )
try :
while True :
try :
if chr ( recv_data . pop ( 0 ) ) in " t " * 100 :
break
except IndexError :
2020-04-24 23:54:12 +00:00
self . assertLess ( ( time . time ( ) - start ) , 60 )
2020-04-24 23:43:24 +00:00
time . sleep ( 0.01 )
except KeyboardInterrupt :
raise
2020-04-20 08:36:41 +00:00
unittest . main ( )