Beispiel #1
0
def test_usecounts():
    rg = RendGuard()
    c = MockController()

    i = 0
    while i < REND_USE_GLOBAL_START_COUNT:
        rg.circ_event(c, rend_circ(i))
        assert c.closed_circ == None
        i += 1
        # Verify we're getting the right rend node
        assert rg.use_counts[
            "7791CA6B67303ACE46C2B6F5211206B765948147"].used == i

    # Test circuit closing functionality
    c.closed_circ = None
    rg.circ_event(c, rend_circ(i))
    assert c.closed_circ == str(i)

    i += 1
    vanguards.rendguard.REND_USE_CLOSE_CIRCUITS_ON_OVERUSE = False
    c.closed_circ = None
    rg.circ_event(c, rend_circ(i))
    assert c.closed_circ == None
    vanguards.rendguard.REND_USE_CLOSE_CIRCUITS_ON_OVERUSE = True

    # Test scaling
    state = VanguardState("tests/junk")
    state.rendguard = rg
    i += 1
    while i < REND_USE_SCALE_AT_COUNT * 1.5:
        rg.circ_event(c, rend_circ2(i))
        i += 1

    assert rg.total_use_counts == REND_USE_SCALE_AT_COUNT * 1.5
    assert rg.use_counts[
        "7791CA6B67303ACE46C2B6F5211206B765948147"].used == REND_USE_GLOBAL_START_COUNT + 2

    state.new_consensus_event(c, None)
    assert rg.total_use_counts == REND_USE_SCALE_AT_COUNT * 1.5 / 2
    assert rg.use_counts["7791CA6B67303ACE46C2B6F5211206B765948147"].used == (
        REND_USE_GLOBAL_START_COUNT + 2) / 2

    # Test leaving the consensus
    state = VanguardState("tests/junk")
    state.rendguard = rg
    i = 0
    while i < REND_USE_SCALE_AT_COUNT / 10:
        rg.circ_event(c, rend_circ3(i))
        i += 1

    assert rg.total_use_counts == REND_USE_SCALE_AT_COUNT * 1.5 / 2 + REND_USE_SCALE_AT_COUNT / 10
    assert rg.use_counts["7791CA6B67303ACE46C2B6F5211206B765948147"].used == (
        REND_USE_GLOBAL_START_COUNT + 2) / 2

    state.new_consensus_event(c, None)

    assert rg.total_use_counts == REND_USE_SCALE_AT_COUNT * 1.5 / 2
    assert rg.use_counts["7791CA6B67303ACE46C2B6F5211206B765948147"].used == (
        REND_USE_GLOBAL_START_COUNT + 2) / 2
Beispiel #2
0
def test_usecounts():
    rg = RendGuard()
    c = MockController()

    # First test not in consensus
    i = 1
    while i < REND_USE_GLOBAL_START_COUNT:
        rg.circ_event(c, rend_circ(i))
        assert c.closed_circ == None
        # Verify we're getting the right rend node
        assert rg.use_counts[_NOT_IN_CONSENSUS_ID].used == i
        i += 1

    # Test circuit closing functionality
    c.closed_circ = None
    rg.circ_event(c, rend_circ(i))
    assert c.closed_circ == str(i)

    i += 1
    vanguards.rendguard.REND_USE_CLOSE_CIRCUITS_ON_OVERUSE = False
    c.closed_circ = None
    rg.circ_event(c, rend_circ(i))
    assert c.closed_circ == None
    vanguards.rendguard.REND_USE_CLOSE_CIRCUITS_ON_OVERUSE = True

    # Test use limit with an in-consensus relay
    state = VanguardState("tests/junk")
    state.rendguard = rg
    state.new_consensus_event(c, None)
    r = 1
    while r < REND_USE_RELAY_START_COUNT:
        rg.circ_event(c, rend_circ2(i))
        assert c.closed_circ == None
        i += 1
        r += 1

    # Test closing in-consensus relay
    while rg.use_counts["BC630CBBB518BE7E9F4E09712AB0269E9DC7D626"].used < \
            rg.use_counts["BC630CBBB518BE7E9F4E09712AB0269E9DC7D626"].weight \
             * rg.total_use_counts * REND_USE_MAX_USE_TO_BW_RATIO:
        assert c.closed_circ == None
        rg.circ_event(c, rend_circ2(i))
        r += 1
        i += 1

    assert c.closed_circ == str(i - 1)

    # Test almost-scaling with an in-consensus relay
    while i < REND_USE_SCALE_AT_COUNT - 1:
        rg.circ_event(c, rend_circ2(i))
        i += 1
        r += 1

    state.new_consensus_event(c, None)
    assert rg.total_use_counts == REND_USE_SCALE_AT_COUNT - 1
    assert rg.use_counts[_NOT_IN_CONSENSUS_ID].used + \
           rg.use_counts["BC630CBBB518BE7E9F4E09712AB0269E9DC7D626"].used \
              == rg.total_use_counts

    # Test scaling with in-consensus relay
    rg.circ_event(c, rend_circ2(i))
    state.new_consensus_event(c, None)
    assert rg.total_use_counts == REND_USE_SCALE_AT_COUNT / 2
    assert rg.use_counts[_NOT_IN_CONSENSUS_ID].used + \
           rg.use_counts["BC630CBBB518BE7E9F4E09712AB0269E9DC7D626"].used \
              == rg.total_use_counts