示例#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
示例#2
0
def test_update_vanguards():
  controller = MockController()
  vanguards.vanguards.LAYER1_LIFETIME_DAYS = 30
  state = VanguardState.read_from_file("tests/state.mock")
  sanity_check(state)

  state.new_consensus_event(controller, None)
  sanity_check(state)
示例#3
0
def test_new_vanguards():
  state = VanguardState("tests/state.mock2")

  # - Load a routerlist using stem
  routers = list(stem.descriptor.parse_file("tests/cached-microdesc-consensus",
                 document_handler =
                    stem.descriptor.DocumentHandler.ENTRIES))
  weights = get_consensus_weights("tests/cached-microdesc-consensus")

  # - Perform basic rank checks from sort_and_index
  (sorted_r, dict_r) = state.sort_and_index_routers(routers)
  for i in xrange(len(sorted_r)-1):
    assert sorted_r[i].measured >= sorted_r[i+1].measured

  state.consensus_update(routers, weights, ExcludeNodes(MockController()))
  sanity_check(state)

  replacement_checks(state, routers, weights)
示例#4
0
def test_update_vanguards():
    controller = MockController()
    vanguards.vanguards.LAYER1_LIFETIME_DAYS = 30
    shutil.copy("tests/state.mock", "tests/state.mock.test")
    state = VanguardState.read_from_file("tests/state.mock.test")
    state.enable_vanguards = True
    sanity_check(state)

    state.new_consensus_event(controller, None)
    sanity_check(state)
    os.remove("tests/state.mock.test")
示例#5
0
def test_disable():
  controller = MockController()
  vanguards.vanguards.LAYER1_LIFETIME_DAYS = 30
  shutil.copy("tests/state.mock", "tests/state.mock.test")
  state = VanguardState.read_from_file("tests/state.mock.test")
  state.enable_vanguards = False
  sanity_check(state)

  state.new_consensus_event(controller, None)
  sanity_check(state)
  assert controller.got_set_conf == False
  assert controller.got_save_conf == False
  os.remove("tests/state.mock.test")
示例#6
0
def test_update_vanguards():
  controller = MockController()
  vanguards.vanguards.LAYER1_LIFETIME_DAYS = 30
  shutil.copy("tests/state.mock", "tests/state.mock.test")
  state = VanguardState.read_from_file("tests/state.mock.test")
  state.enable_vanguards = True
  sanity_check(state)

  state.new_consensus_event(controller, None)
  sanity_check(state)
  os.remove("tests/state.mock.test")

  # test signal HUP
  state.signal_event(controller,
                     ControlMessage.from_str("650 SIGNAL RELOAD\r\n",
                                             "EVENT"))
  sanity_check(state)
示例#7
0
def test_excludenodes():
  controller = MockController()
  state = VanguardState("tests/state.mock2")

  # - Load a routerlist using stem
  routers = list(stem.descriptor.parse_file("tests/cached-microdesc-consensus",
                 document_handler =
                    stem.descriptor.DocumentHandler.ENTRIES))
  weights = get_consensus_weights("tests/cached-microdesc-consensus")
  (sorted_r, dict_r) = state.sort_and_index_routers(routers)

  state.consensus_update(routers, weights, ExcludeNodes(controller))
  sanity_check(state)

  #   * IP, CIDR, quad-mask
  controller.exclude_nodes = \
       str(dict_r[state.layer2[0].idhex].address)+","+\
       str(dict_r[state.layer2[1].idhex].address)+"/24,"+\
       str(dict_r[state.layer2[2].idhex].address)+"/255.255.255.0"
  removed2 = [state.layer2[0].idhex, state.layer2[1].idhex,
              state.layer2[2].idhex]

  for r in removed2:
    assert r in map(lambda x: x.idhex, state.layer2)
  state.consensus_update(routers, weights, ExcludeNodes(controller))
  sanity_check(state)
  for r in removed2:
    assert not r in map(lambda x: x.idhex, state.layer2)

  #   * GeoIP case mismatch
  controller.exclude_nodes = "{Us}"
  controller.exclude_unknown = "auto"
  controller.get_info_vals["ip-to-country/"+dict_r[state.layer2[1].idhex].address] = "us"
  controller.get_info_vals["ip-to-country/ipv4-available"] = "1"
  removed2 = state.layer2[1].idhex
  keep2 = state.layer2[0].idhex
  state.consensus_update(routers, weights, ExcludeNodes(controller))

  sanity_check(state)
  assert keep2 in map(lambda x: x.idhex, state.layer2)
  assert not removed2 in map(lambda x: x.idhex, state.layer2)

  #   * Nicks
  controller.exclude_nodes = \
       str(dict_r[state.layer2[0].idhex].nickname)

  removed2 = state.layer2[0].idhex
  keep2 = state.layer2[1].idhex
  state.consensus_update(routers, weights, ExcludeNodes(controller))
  sanity_check(state)
  assert not removed2 in map(lambda x: x.idhex, state.layer2)
  assert keep2 in map(lambda x: x.idhex, state.layer2)
示例#8
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