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)
Beispiel #2
0
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 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_Result_from_dict_bad_type():
    '''
    If the result type string doesn't match any of the known types, then it
    should throw NotImplementedError
    '''
    d = {'version': RESULT_VERSION, 'type': 'NotARealType'}
    try:
        Result.from_dict(d)
    except NotImplementedError as e:
        assert str(e) == 'Unknown result type NotARealType'
    else:
        assert None, 'Should have failed'
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 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)
Beispiel #7
0
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_Result_from_dict_bad_version():
    '''
    The first thing that is checked is the version field, and a wrong one
    should return None
    '''
    d = {'version': RESULT_VERSION + 1}
    r = Result.from_dict(d)
    assert r is None
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 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'
Beispiel #12
0
def test_v3bwline_from_results_file(datadir):
    lines = datadir.readlines('results.txt')
    d = dict()
    for line in lines:
        r = Result.from_dict(json.loads(line.strip(), cls=CustomDecoder))
        fp = r.fingerprint
        if fp not in d:
            d[fp] = []
        d[fp].append(r)
    bwl, _ = V3BWLine.from_data(d, fp)
    # bw store now B, not KB
    bwl.bw = round(bwl.bw / 1000)
    assert raw_bwl_str == str(bwl)
Beispiel #13
0
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
Beispiel #14
0
def test_ResultError_from_dict(result_error_stream, result_error_stream_dict):
    r2 = Result.from_dict(result_error_stream_dict)
    assert isinstance(r2, ResultError)
    assert str(result_error_stream) == str(r2)
Beispiel #15
0
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
Beispiel #16
0
}, {
    "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,
Beispiel #17
0
def test_ResultSuccess_from_dict(result_success, result_success_dict):
    r2 = Result.from_dict(result_success_dict)
    assert isinstance(r2, ResultSuccess)
    assert str(result_success) == str(r2)