def test_ResultSuccess_from_dict(time_mock): t = 2000 time_mock.side_effect = monotonic_time(start=t) fp1 = 'A' * 40 fp2 = 'Z' * 40 circ = [fp1, fp2] dest_url = 'http://example.com/sbws.bin' scanner_nick = 'sbwsscanner' nick = 'Mooooooo' relay_ip = '169.254.100.1' relay = Result.Relay(fp1, nick, relay_ip) rtts = [5, 25] downloads = [{'duration': 4, 'amount': 40}] r1 = ResultSuccess(rtts, downloads, relay, circ, dest_url, scanner_nick) d = { 'rtts': rtts, 'downloads': downloads, 'fingerprint': fp1, 'nickname': nick, 'address': relay_ip, 'circ': circ, 'dest_url': dest_url, 'scanner': scanner_nick, 'version': RESULT_VERSION, 'type': _ResultType.Success, 'time': t, } r2 = Result.from_dict(d) assert isinstance(r1, ResultSuccess) assert isinstance(r2, ResultSuccess) assert str(r1) == str(r2)
def test_ResultErrorAuth_from_dict(time_mock): t = 2000 time_mock.side_effect = monotonic_time(start=t) fp1 = 'A' * 40 fp2 = 'Z' * 40 circ = [fp1, fp2] dest_url = 'http://example.com/sbws.bin' scanner_nick = 'sbwsscanner' nick = 'Mooooooo' relay_ip = '169.254.100.1' relay = Result.Relay(fp1, nick, relay_ip) msg = 'aaaaayyyyyy bb' r1 = ResultErrorAuth(relay, circ, dest_url, scanner_nick, msg=msg) d = { 'msg': msg, 'fingerprint': fp1, 'nickname': nick, 'address': relay_ip, 'circ': circ, 'dest_url': dest_url, 'scanner': scanner_nick, 'version': RESULT_VERSION, 'type': _ResultType.ErrorAuth, 'time': t, } r2 = Result.from_dict(d) assert isinstance(r1, ResultErrorAuth) assert isinstance(r2, ResultErrorAuth) assert str(r1) == str(r2)
def test_ResultSuccess(time_mock): t = 2000 time_mock.side_effect = monotonic_time(start=t) fp1 = 'A' * 40 fp2 = 'Z' * 40 circ = [fp1, fp2] dest_url = 'http://example.com/sbws.bin' scanner_nick = 'sbwsscanner' nick = 'Mooooooo' relay_ip = '169.254.100.1' relay = Result.Relay(fp1, nick, relay_ip) rtts = [5, 25] downloads = [{'duration': 4, 'amount': 40}] r1 = ResultSuccess(rtts, downloads, relay, circ, dest_url, scanner_nick) r2 = ResultSuccess(rtts, downloads, relay, circ, dest_url, scanner_nick, t=t) assert r1.downloads == downloads assert r1.rtts == rtts assert r1.nickname == nick assert r1.time == t assert r1.fingerprint == fp1 assert r1.scanner == scanner_nick assert r1.type == _ResultType.Success assert r1.address == relay_ip assert r1.circ == circ assert r1.dest_url == dest_url assert r1.version == RESULT_VERSION assert str(r1) == str(r2)
def dotsbws_success_result(empty_dotsbws_datadir): ''' Creates an ~/.sbws with a single fresh ResultSuccess in it ''' fp1 = 'A' * 40 fp2 = 'B' * 40 circ = [fp1, fp2] nick = 'CowSayWhat' relay_ip = '169.254.100.1' server_ip = '169.254.100.2' scanner_nick = 'SBWSscanner' rtts = [4.242] downloads = [{'duration': 4, 'amount': 40 * 1024}] t = time.time() relay = Result.Relay(fp1, nick, relay_ip) result = ResultSuccess(rtts, downloads, relay, circ, server_ip, scanner_nick, t=t) args = _PseudoArguments(directory=empty_dotsbws_datadir.name) conf = get_config(args) dd = conf['paths']['datadir'] write_result_to_datadir(result, dd) return empty_dotsbws_datadir
def test_ResultErrorAuth(time_mock): t = 2000 time_mock.side_effect = monotonic_time(start=t) fp1 = 'A' * 40 fp2 = 'Z' * 40 circ = [fp1, fp2] dest_url = 'http://example.com/sbws.bin' scanner_nick = 'sbwsscanner' nick = 'Mooooooo' relay_ip = '169.254.100.1' relay = Result.Relay(fp1, nick, relay_ip) msg = 'aaaaayyyyyy bb' r1 = ResultErrorAuth(relay, circ, dest_url, scanner_nick, msg=msg) r2 = ResultErrorAuth(relay, circ, dest_url, scanner_nick, msg=msg, t=t) assert r1.msg == msg assert r1.nickname == nick assert r1.time == t assert r1.fingerprint == fp1 assert r1.scanner == scanner_nick assert r1.type == _ResultType.ErrorAuth assert r1.address == relay_ip assert r1.circ == circ assert r1.dest_url == dest_url assert r1.version == RESULT_VERSION assert str(r1) == str(r2)
def test_ResultErrorCircuit_from_dict(time_mock): t = 2000 time_mock.side_effect = monotonic_time(start=t) fp1 = 'A' * 40 fp2 = 'Z' * 40 ed25519 = 'g+Shk00y9Md0hg1S6ptnuc/wWKbADBgdjT0Kg+TSF3s' circ = [fp1, fp2] dest_url = 'http://example.com/sbws.bin' scanner_nick = 'sbwsscanner' nick = 'Mooooooo' relay_ip = '169.254.100.1' relay = Result.Relay(fp1, nick, relay_ip, ed25519) msg = 'aaaaayyyyyy bb' r1 = ResultErrorCircuit(relay, circ, dest_url, scanner_nick, msg=msg) d = { 'msg': msg, 'fingerprint': fp1, 'nickname': nick, 'address': relay_ip, 'circ': circ, 'dest_url': dest_url, 'scanner': scanner_nick, 'version': RESULT_VERSION, 'type': _ResultType.ErrorCircuit, 'time': t, 'master_key_ed25519': ed25519, } r2 = Result.from_dict(d) assert isinstance(r1, ResultErrorCircuit) assert isinstance(r2, ResultErrorCircuit) assert str(r1) == str(r2)
def add_single_fresh_result(dname): r = ResultError(Result.Relay('DEADBEEF1111', 'CowSayWhat', '127.0.0.1'), ['DEADBEEF1111', 'BEADDEEF2222'], '127.0.1.1', 'SBWSscanner', t=time.time()) dd = os.path.join(str(dname), 'datadir') os.makedirs(dd) write_result_to_datadir(r, dd)
def add_two_fresh_results(dname, t): r1 = ResultError(Result.Relay('DEADBEEF1111', 'CowSayWhat', '127.0.0.1'), ['DEADBEEF1111', 'BEADDEEF2222'], '127.0.1.1', 'SBWSscanner', t=t) r2 = ResultSuccess([1, 2, 3], [{ 'amount': 100, 'duration': 1 }], Result.Relay('DEADBEEF1111', 'CowSayWhat', '127.0.0.1'), ['DEADBEEF1111', 'BEADDEEF2222'], '127.0.1.1', 'SBWSscanner', t=t) dd = os.path.join(str(dname), 'datadir') os.makedirs(dd) write_result_to_datadir(r1, dd) write_result_to_datadir(r2, dd)
def dotsbws_error_result(empty_dotsbws_datadir): ''' Creates an ~/.sbws with a single fresh ResultError in it ''' fp1 = 'A' * 40 fp2 = 'B' * 40 circ = [fp1, fp2] nick = 'CowSayWhat' relay_ip = '169.254.100.1' server_ip = '169.254.100.2' scanner_nick = 'SBWSscanner' msg = 'UnitTest error message' t = time.time() relay = Result.Relay(fp1, nick, relay_ip) result = ResultError(relay, circ, server_ip, scanner_nick, t=t, msg=msg) args = _PseudoArguments(directory=empty_dotsbws_datadir.name) conf = get_config(args) dd = conf['paths']['datadir'] write_result_to_datadir(result, dd) return empty_dotsbws_datadir
def test_Result(time_mock): ''' A standard Result should not be convertible to a string because Result.type is not implemented. ''' time_mock.side_effect = monotonic_time() fp1 = 'A' * 40 fp2 = 'Z' * 40 circ = [fp1, fp2] dest_url = 'http://example.com/sbws.bin' scanner_nick = 'sbwsscanner' nick = 'Mooooooo' relay_ip = '169.254.100.1' relay = Result.Relay(fp1, nick, relay_ip) r = Result(relay, circ, dest_url, scanner_nick) try: str(r) except NotImplementedError: pass else: assert None, 'Should have failed'
}, { "duration": 30.438726663589478, "amount": 644411 }] SCANNER = "test" AVG_BW = 966080 BUR_BW = 1048576 OBS_BW = 524288 BW = 600000 UNMEASURED = False RELAY1 = Result.Relay(FP1, NICK1, IP1, ED25519, average_bandwidth=AVG_BW, burst_bandwidth=BUR_BW, observed_bandwidth=OBS_BW, consensus_bandwidth=BW, consensus_bandwidth_is_unmeasured=UNMEASURED, relay_in_recent_consensus_count=2) RELAY2 = Result.Relay(FP2, NICK2, IP2, ED25519) RESULT = Result(RELAY1, CIRC12, DEST_URL, SCANNER, t=TIME1) RESULT_SUCCESS1 = ResultSuccess(RTTS1, DOWNLOADS1, RELAY1, CIRC12, DEST_URL, SCANNER, t=TIME1) RESULT_SUCCESS2 = ResultSuccess(RTTS2,
}] SCANNER = "test" AVG_BW = 966080 BUR_BW = 1048576 OBS_BW = 524288 BW = 600000 UNMEASURED = False now = datetime.utcnow() RELAY1 = Result.Relay(FP1, NICK1, IP1, ED25519, average_bandwidth=AVG_BW, burst_bandwidth=BUR_BW, observed_bandwidth=OBS_BW, consensus_bandwidth=BW, consensus_bandwidth_is_unmeasured=UNMEASURED, relay_in_recent_consensus=[now], relay_recent_measurement_attempt=[now], relay_recent_priority_list=[now]) RELAY2 = Result.Relay(FP2, NICK2, IP2, ED25519) RESULT = Result(RELAY1, CIRC12, DEST_URL, SCANNER, t=TIME1) RESULT_SUCCESS1 = ResultSuccess(RTTS1, DOWNLOADS1, RELAY1, CIRC12, DEST_URL, SCANNER, t=TIME1)
def dotsbws_success_result_two_relays(empty_dotsbws_datadir): ''' Creates an ~/.sbws with a a couple of fresh ResultSuccess for a couple or relays ''' args = _PseudoArguments(directory=empty_dotsbws_datadir.name) conf = get_config(args) dd = conf['paths']['datadir'] fp1 = 'A' * 40 fp2 = 'C' * 40 circ = [fp1, fp2] nick = 'CowSayWhat1' relay_ip = '169.254.100.1' server_ip = '169.254.100.3' scanner_nick = 'SBWSscanner' rtts = [5, 25] downloads = [{'duration': 4, 'amount': 40 * 1024}] t = time.time() relay = Result.Relay(fp1, nick, relay_ip) result = ResultSuccess(rtts, downloads, relay, circ, server_ip, scanner_nick, t=t) write_result_to_datadir(result, dd) rtts = [10, 20] downloads = [{'duration': 4, 'amount': 80 * 1024}] t = time.time() result = ResultSuccess(rtts, downloads, relay, circ, server_ip, scanner_nick, t=t) write_result_to_datadir(result, dd) fp1 = 'B' * 40 circ = [fp1, fp2] nick = 'CowSayWhat2' relay_ip = '169.254.100.2' rtts = [50, 250] downloads = [{'duration': 4, 'amount': 400 * 1024}] t = time.time() relay = Result.Relay(fp1, nick, relay_ip) result = ResultSuccess(rtts, downloads, relay, circ, server_ip, scanner_nick, t=t) write_result_to_datadir(result, dd) rtts = [100, 200] downloads = [{'duration': 4, 'amount': 800 * 1024}] t = time.time() result = ResultSuccess(rtts, downloads, relay, circ, server_ip, scanner_nick, t=t) write_result_to_datadir(result, dd) return empty_dotsbws_datadir