Esempio n. 1
0
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'
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
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)
Esempio n. 5
0
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
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
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)