123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- import time
- import chutney
- def run_test(network):
- wait_time = network._dfltEnv['bootstrap_time']
- start_time = time.time()
- end_time = start_time + wait_time
- print("Verifying data transmission: (retrying for up to %d seconds)"
- % wait_time)
- status = False
-
- while not status and time.time() < end_time:
-
-
- status = _verify_traffic(network)
-
- if not status:
- time.sleep(5)
- print("Transmission: %s" % ("Success" if status else "Failure"))
- if not status:
- print("Set CHUTNEY_DEBUG to diagnose.")
- return status
- def _verify_traffic(network):
- """Verify (parts of) the network by sending traffic through it
- and verify what is received."""
-
- LISTEN_ADDR = network._dfltEnv['ip']
- LISTEN_PORT = 4747
-
-
-
- HS_PORT = 5858
-
-
-
-
- DATALEN = network._dfltEnv['data_bytes']
-
- DOTDATALEN = 5 * 1024 * 1024
- TIMEOUT = 3
-
- randomlen = _calculate_randomlen(DATALEN)
- reps = _calculate_reps(DATALEN, randomlen)
- connection_count = network._dfltEnv['connection_count']
-
- if reps == 0:
- DATALEN = 0
-
- if randomlen > 0:
-
- dot_reps = _calculate_reps(DOTDATALEN, randomlen)
-
- dot_reps = min(reps, dot_reps)
- with open('/dev/urandom', 'rb') as randfp:
- tmpdata = randfp.read(randomlen)
- else:
- dot_reps = 0
- tmpdata = {}
-
- bind_to = (LISTEN_ADDR, LISTEN_PORT)
- tt = chutney.Traffic.TrafficTester(bind_to, tmpdata, TIMEOUT, reps,
- dot_reps)
-
- client_list = filter(lambda n:
- n._env['tag'].startswith('c') or
- n._env['tag'].startswith('bc') or
- ('client' in n._env.keys() and n._env['client'] == 1),
- network._nodes)
- exit_list = filter(lambda n:
- ('exit' in n._env.keys() and n._env['exit'] == 1),
- network._nodes)
- hs_list = filter(lambda n:
- n._env['tag'].startswith('h') or
- ('hs' in n._env.keys() and n._env['hs'] == 1),
- network._nodes)
-
-
- client_list = list(client_list)
- exit_list = list(exit_list)
- hs_list = list(hs_list)
- if len(client_list) == 0:
- print(" Unable to verify network: no client nodes available")
- return False
- if len(exit_list) == 0 and len(hs_list) == 0:
- print(" Unable to verify network: no exit/hs nodes available")
- print(" Exit nodes must be declared 'relay=1, exit=1'")
- print(" HS nodes must be declared 'tag=\"hs\"'")
- return False
- print("Connecting:")
-
-
-
- total_path_node_count = 0
- total_path_node_count += _configure_exits(tt, bind_to, tmpdata, reps,
- client_list, exit_list,
- LISTEN_ADDR, LISTEN_PORT,
- connection_count)
- total_path_node_count += _configure_hs(tt, tmpdata, reps, client_list,
- hs_list, HS_PORT, LISTEN_ADDR,
- LISTEN_PORT, connection_count,
- network._dfltEnv['hs_multi_client'])
- print("Transmitting Data:")
- start_time = time.time()
- status = tt.run()
- end_time = time.time()
-
- if not status:
- return status
-
- _report_bandwidth(DATALEN, total_path_node_count, start_time, end_time)
- return status
- def _calculate_randomlen(datalen):
- MAX_RANDOMLEN = 128 * 1024
- if datalen > MAX_RANDOMLEN:
- return MAX_RANDOMLEN
- else:
- return datalen
- def _calculate_reps(datalen, replen):
-
- if datalen == 0 or replen == 0:
- return 0
-
- if replen < datalen:
- return (datalen + replen - 1) / replen
- else:
- return 1
- def _configure_exits(tt, bind_to, tmpdata, reps, client_list, exit_list,
- LISTEN_ADDR, LISTEN_PORT, connection_count):
- CLIENT_EXIT_PATH_NODES = 4
- exit_path_node_count = 0
- if len(exit_list) > 0:
- exit_path_node_count += (len(client_list) *
- CLIENT_EXIT_PATH_NODES *
- connection_count)
- for op in client_list:
- print(" Exit to %s:%d via client %s:%s"
- % (LISTEN_ADDR, LISTEN_PORT,
- 'localhost', op._env['socksport']))
- for _ in range(connection_count):
- proxy = ('localhost', int(op._env['socksport']))
- tt.add(chutney.Traffic.Source(tt, bind_to, tmpdata, proxy,
- reps))
- return exit_path_node_count
- def _configure_hs(tt, tmpdata, reps, client_list, hs_list, HS_PORT,
- LISTEN_ADDR, LISTEN_PORT, connection_count, hs_multi_client):
- CLIENT_HS_PATH_NODES = 8
- hs_path_node_count = (len(hs_list) * CLIENT_HS_PATH_NODES *
- connection_count)
-
- if hs_multi_client:
- hs_client_list = client_list
- hs_path_node_count *= len(client_list)
- else:
-
- hs_client_list = client_list[:1]
-
- for hs in hs_list:
- hs_bind_to = (hs._env['hs_hostname'], HS_PORT)
- for client in hs_client_list:
- print(" HS to %s:%d (%s:%d) via client %s:%s"
- % (hs._env['hs_hostname'], HS_PORT,
- LISTEN_ADDR, LISTEN_PORT,
- 'localhost', client._env['socksport']))
- for _ in range(connection_count):
- proxy = ('localhost', int(client._env['socksport']))
- tt.add(chutney.Traffic.Source(tt, hs_bind_to, tmpdata,
- proxy, reps))
- return hs_path_node_count
- def _report_bandwidth(data_length, total_path_node_count, start_time,
- end_time):
-
-
- MIN_BWDATA = 5 * 1024 * 1024
- MIN_ELAPSED_TIME = 1.0
- cumulative_data_sent = total_path_node_count * data_length
- elapsed_time = end_time - start_time
- if (cumulative_data_sent >= MIN_BWDATA and
- elapsed_time >= MIN_ELAPSED_TIME):
-
- BWDIVISOR = 1024*1024
- single_stream_bandwidth = (data_length / elapsed_time / BWDIVISOR)
- overall_bandwidth = (cumulative_data_sent / elapsed_time /
- BWDIVISOR)
- print("Single Stream Bandwidth: %.2f MBytes/s"
- % single_stream_bandwidth)
- print("Overall tor Bandwidth: %.2f MBytes/s"
- % overall_bandwidth)
|