def test_coverage(): controller = MockController() state = BandwidthStats(controller) controller.bwstats = state circ_id = 1 # Stray circ_minor event state.circ_minor_event(cannibalized_circ(circ_id, "HS_SERVICE_REND")) assert str(circ_id) not in state.circs # Insane bw values circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) state.circbw_event(circ_bw(circ_id, _CELL_PAYLOAD_SIZE, _CELL_PAYLOAD_SIZE, _CELL_PAYLOAD_SIZE, _CELL_PAYLOAD_SIZE, 0, 0))
def test_connguard(): controller = MockController() state = BandwidthStats(controller) controller.bwstats = state circ_id = 1 # Check orconn-status assert len(state.live_guard_conns) == 2 assert state.max_fake_id == 2 assert state.live_guard_conns[ "0"].to_guard == "3E53D3979DB07EFD736661C934A1DED14127B684" assert "1" not in state.live_guard_conns assert state.live_guard_conns[ "2"].to_guard == "3E53D3979DB07EFD736661C934A1DED14127B684" # Test HS_INTRO close assert state.circs_destroyed_total == 0 state.orconn_event( orconn_event(11, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) state.circ_event( built_circ(23, "HS_SERVICE_INTRO", "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) state.orconn_event( orconn_event(11, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CLOSED")) state.circ_event( destroyed_circ(23, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) assert state.guards[ "5416F3E8F80101A133B1970495B04FDBD1C7446B"].killed_conns == 1 assert state.circs_destroyed_total == 1 # Test cannibalized close assert state.circs_destroyed_total == 1 state.orconn_event( orconn_event(12, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) state.circ_event( built_circ(24, "HS_VANGUARDS", "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) state.circ_minor_event( purpose_changed_circ( 24, "HS_VANGUARDS", "HS_SERVICE_REND", "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) state.orconn_event( orconn_event(12, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CLOSED")) state.circ_event( destroyed_circ(24, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) assert state.guards[ "5416F3E8F80101A133B1970495B04FDBD1C7446B"].killed_conns == 2 assert state.circs_destroyed_total == 2 # Test cannibalized close on pre-existing conn assert state.circs_destroyed_total == 2 state.circ_event( built_circ(2323, "HS_VANGUARDS", "$3E53D3979DB07EFD736661C934A1DED14127B684~Unnamed")) state.circ_minor_event( purpose_changed_circ( 2323, "HS_VANGUARDS", "HS_SERVICE_REND", "$3E53D3979DB07EFD736661C934A1DED14127B684~Unnamed")) assert not state.circs["2323"].possibly_destroyed_at assert state.circs["2323"].in_use state.orconn_event( orconn_event(5, "$3E53D3979DB07EFD736661C934A1DED14127B684~Unnamed", "CLOSED")) assert state.circs["2323"].possibly_destroyed_at state.circ_event( destroyed_circ(2323, "$3E53D3979DB07EFD736661C934A1DED14127B684~Unnamed")) assert state.guards[ "5416F3E8F80101A133B1970495B04FDBD1C7446B"].killed_conns == 2 assert state.guards[ "3E53D3979DB07EFD736661C934A1DED14127B684"].killed_conns == 1 assert state.circs_destroyed_total == 3 # Tests that should not trigger logs: # * Test HS_VANGUARDS assert state.circs_destroyed_total == 3 state.orconn_event( orconn_event(13, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) state.circ_event( built_circ(25, "HS_VANGUARDS", "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) state.orconn_event( orconn_event(13, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CLOSED")) state.circ_event( destroyed_circ(25, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) assert state.guards[ "5416F3E8F80101A133B1970495B04FDBD1C7446B"].killed_conns == 2 assert state.circs_destroyed_total == 3 # * Test general close assert state.circs_destroyed_total == 3 state.orconn_event( orconn_event(14, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) state.circ_event( built_general_circ( 26, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) state.orconn_event( orconn_event(14, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CLOSED")) state.circ_event( destroyed_circ(26, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) assert state.guards[ "5416F3E8F80101A133B1970495B04FDBD1C7446B"].killed_conns == 2 assert state.circs_destroyed_total == 3 # * Test late close of hs_intro assert state.circs_destroyed_total == 3 state.orconn_event( orconn_event(15, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) state.circ_event( built_circ(27, "HS_SERVICE_INTRO", "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) state.orconn_event( orconn_event(15, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CLOSED")) ev = destroyed_circ(27, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed") ev.arrived_at = time.time() + 5 state.circ_event(ev) assert state.guards[ "5416F3E8F80101A133B1970495B04FDBD1C7446B"].killed_conns == 2 assert state.circs_destroyed_total == 3 # * Different guard for hs_intro assert state.circs_destroyed_total == 3 state.orconn_event( orconn_event(16, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) state.circ_event( built_circ(28, "HS_SERVICE_INTRO", "$6416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) state.orconn_event( orconn_event(16, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CLOSED")) state.circ_event( destroyed_circ(28, "$6416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed")) assert state.guards[ "5416F3E8F80101A133B1970495B04FDBD1C7446B"].killed_conns == 2 assert state.circs_destroyed_total == 3 # Test orconn-status fake_id clearing, esp with launched assert len(state.live_guard_conns) == 1 assert not state.no_conns_since ev = orconn_event(6, "$3E53D3979DB07EFD736661C934A1DED14127B684~Unnamed", "CLOSED") last_conn = int(time.time()) ev.arrived_at = last_conn state.orconn_event(ev) assert state.no_conns_since == last_conn assert len(state.live_guard_conns) == 0 # Test no orconns for 5, 10 seconds ev = MockEvent(last_conn) state.bw_event(ev) assert state.disconnected_conns == False ev = MockEvent(last_conn + CONN_MAX_DISCONNECTED_SECS * 2) state.bw_event(ev) assert state.disconnected_conns # Test come back to life. state.orconn_event( orconn_event(15, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) last_conn = int(time.time()) ev = MockEvent(last_conn) state.bw_event(ev) assert state.disconnected_conns == False # Test disabled vanguards.bandguards.CONN_MAX_DISCONNECTED_SECS = 0 assert CONN_MAX_DISCONNECTED_SECS # Didn't change local val ev = MockEvent(last_conn + CONN_MAX_DISCONNECTED_SECS * 2) state.bw_event(ev) assert state.disconnected_conns == False vanguards.bandguards.CONN_MAX_DISCONNECTED_SECS = CONN_MAX_DISCONNECTED_SECS # Test no circuits for 5, 10 seconds state.orconn_event( orconn_event(9001, "$5416F3E8F80101A133B1970495B04FDBD1C7446B~Unnamed", "CONNECTED")) ev = MockEvent(last_conn + 20) state.bw_event(ev) ev = built_general_circ(29) ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since == None state.bw_event(ev) assert state.disconnected_circs == False assert state.disconnected_conns == False # Quiet doesn't mean no network ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.no_circs_since == None assert state.disconnected_circs == False # Failure then quiet doesn't, when no circs: ev = failed_circ(31) ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since == None assert state.disconnected_circs == False ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.disconnected_circs == False # Failure then quiet does, when no circs: ev = extended_circ(333, "HS_VANGUARDS") ev.arrived_at = last_conn state.circ_event(ev) ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.no_circs_since == None assert state.disconnected_circs == False ev = failed_circ(31) ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.disconnected_circs == True # Success clears it ev = built_general_circ(32) ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since == None state.bw_event(ev) assert state.disconnected_circs == False # Failure then success is also ok ev = failed_circ(34) ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since assert state.disconnected_circs == False ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.disconnected_circs == True ev = extended_circ(35, "GENERAL") ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since == None ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.disconnected_circs == False # Failure then circbw is also ok ev = failed_circ(34) ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since assert state.disconnected_circs == False ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.disconnected_circs == True check_dropped_bytes(state, controller, 23, 1, 0) assert state.no_circs_since == None ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.disconnected_circs == False # Test disabled vanguards.bandguards.CIRC_MAX_DISCONNECTED_SECS = 0 assert CIRC_MAX_DISCONNECTED_SECS # Didn't change local val ev = failed_circ(313) ev.arrived_at = last_conn state.circ_event(ev) assert state.no_circs_since assert state.disconnected_circs == False ev.arrived_at = last_conn + CIRC_MAX_DISCONNECTED_SECS * 2 state.bw_event(ev) assert state.disconnected_circs == False vanguards.bandguards.CIRC_MAX_DISCONNECTED_SECS = CIRC_MAX_DISCONNECTED_SECS
def test_bwstats(): global CIRC_MAX_DROPPED_CELLS global CIRC_MAX_MEGABYTES controller = MockController() state = BandwidthStats(controller) controller.bwstats = state circ_id = 1 # - BUILT -> FAILED,CLOSED removed from map # - BUILT -> CLOSED removed from map state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert str(circ_id) in state.circs state.circ_event(failed_circ(circ_id)) assert str(circ_id) not in state.circs state.circ_event(closed_circ(circ_id)) assert str(circ_id) not in state.circs circ_id += 1 state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert str(circ_id) in state.circs state.circ_event(closed_circ(circ_id)) assert str(circ_id) not in state.circs # - HSDIR size cap exceeded for direct service circ circ_id += 1 controller.closed_circ = None state.circ_event(built_hsdir_circ(circ_id)) assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == True check_hsdir(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - HSDIR size cap exceeded for cannibalized circ circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert state.circs[str(circ_id)].is_hsdir == False state.circ_minor_event(cannibalized_circ(circ_id, "HS_CLIENT_HSDIR")) assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == False check_hsdir(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - HSDIR size cap disabled circ_id += 1 controller.closed_circ = None state.circ_event(built_hsdir_circ(circ_id)) vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES = 0 assert vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES != CIRC_MAX_HSDESC_KILOBYTES assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == True check_hsdir(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES = CIRC_MAX_HSDESC_KILOBYTES # - Max bytes exceed (read, write) circ_id += 1 controller.closed_circ = None vanguards.bandguards.CIRC_MAX_MEGABYTES = 100 CIRC_MAX_MEGABYTES = 100 state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_maxbytes(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - Max bytes disabled circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_SERVICE_REND")) vanguards.bandguards.CIRC_MAX_MEGABYTES = 0 assert vanguards.bandguards.CIRC_MAX_MEGABYTES != CIRC_MAX_MEGABYTES check_maxbytes(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_MEGABYTES = CIRC_MAX_MEGABYTES # - Frob circ.created_at to close circ (bw event) circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == None state.circs[str(circ_id)].created_at = time.time() - \ (1+CIRC_MAX_AGE_HOURS*_SECS_PER_HOUR) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == str(circ_id) # - Test disabled circ lifetime circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_AGE_HOURS = 0 assert vanguards.bandguards.CIRC_MAX_AGE_HOURS != CIRC_MAX_AGE_HOURS state.circs[str(circ_id)].created_at = time.time() - \ (1+CIRC_MAX_AGE_HOURS*_SECS_PER_HOUR) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == None # Test that regular reading is ok circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 100, 0) assert controller.closed_circ == None # Test that no dropped cells are allowed before app data circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == str(circ_id) # Test that 1 dropped cell is allowed on pathbias circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_SERVICE_REND")) state.circ_minor_event( purpose_changed_circ(circ_id, "HS_SERVICE_REND", "PATH_BIAS_TESTING")) path_bias_cells = 0 while path_bias_cells < _MAX_PATH_BIAS_CELLS_SERVICE: check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == None path_bias_cells += 1 check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == str(circ_id) # Test that 1 dropped cell is allowed on pathbias circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) state.circ_minor_event( purpose_changed_circ(circ_id, "HS_CLIENT_REND", "PATH_BIAS_TESTING")) path_bias_cells = 0 while path_bias_cells < _MAX_PATH_BIAS_CELLS_CLIENT: check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == None path_bias_cells += 1 check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == str(circ_id) # Test that no dropped cells are allowed on not-built circ. circ_id += 1 controller.closed_circ = None state.circ_event(extended_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == str(circ_id) # Test that after app data, up to CIRC_MAX_DROPPED_CELLS # allowed, and then we close. circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 1000, CIRC_MAX_DROPPED_CELLS + 1) assert controller.closed_circ == str(circ_id) # - Non-HS circs ignored: circ_id += 1 state.circ_event(built_general_circ(circ_id)) assert str(circ_id) not in state.circs
def test_bwstats(): global CIRC_MAX_MEGABYTES global CIRC_MAX_SERV_INTRO_KILOBYTES controller = MockController() state = BandwidthStats(controller) controller.bwstats = state circ_id = 1 # - BUILT -> FAILED,CLOSED removed from map # - BUILT -> CLOSED removed from map state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert str(circ_id) in state.circs state.circ_event(failed_circ(circ_id)) assert str(circ_id) not in state.circs state.circ_event(closed_circ(circ_id)) assert str(circ_id) not in state.circs circ_id += 1 state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert str(circ_id) in state.circs state.circ_event(closed_circ(circ_id)) assert str(circ_id) not in state.circs # - HSDIR size cap exceeded for direct service circ circ_id += 1 controller.closed_circ = None state.circ_event(built_hsdir_circ(circ_id)) assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == True check_hsdir(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - HSDIR size cap exceeded for cannibalized circ circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert state.circs[str(circ_id)].is_hsdir == False state.circ_minor_event(cannibalized_circ(circ_id, "HS_CLIENT_HSDIR")) assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == False check_hsdir(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - HSDIR size cap disabled circ_id += 1 controller.closed_circ = None state.circ_event(built_hsdir_circ(circ_id)) vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES = 0 assert vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES != CIRC_MAX_HSDESC_KILOBYTES assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == True check_hsdir(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES = CIRC_MAX_HSDESC_KILOBYTES # - INTRO size cap disabled by default circ_id += 1 controller.closed_circ = None state.circ_event(built_serv_intro_circ(circ_id)) assert vanguards.bandguards.CIRC_MAX_SERV_INTRO_KILOBYTES == CIRC_MAX_SERV_INTRO_KILOBYTES assert state.circs[str(circ_id)].is_serv_intro == True assert state.circs[str(circ_id)].is_service == True check_serv_intro(state, controller, circ_id) assert controller.closed_circ == None # - INTRO size cap exceeded for direct service circ circ_id += 1 controller.closed_circ = None vanguards.bandguards.CIRC_MAX_SERV_INTRO_KILOBYTES = 1024 CIRC_MAX_SERV_INTRO_KILOBYTES = 1024 state.circ_event(built_serv_intro_circ(circ_id)) assert state.circs[str(circ_id)].is_serv_intro == True assert state.circs[str(circ_id)].is_service == True check_serv_intro(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - INTRO size cap exceeded for cannibalized circ circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert state.circs[str(circ_id)].is_serv_intro == False state.circ_minor_event(cannibalized_circ(circ_id, "HS_SERVICE_INTRO")) assert state.circs[str(circ_id)].is_serv_intro == True assert state.circs[str(circ_id)].is_service == True check_serv_intro(state, controller, circ_id) assert controller.closed_circ == str(circ_id) vanguards.bandguards.CIRC_MAX_SERV_INTRO_KILOBYTES = 0 CIRC_MAX_SERV_INTRO_KILOBYTES = 0 # - Max bytes exceed (read, write) circ_id += 1 controller.closed_circ = None vanguards.bandguards.CIRC_MAX_MEGABYTES = 100 CIRC_MAX_MEGABYTES = 100 state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_maxbytes(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - Max bytes disabled circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_SERVICE_REND")) vanguards.bandguards.CIRC_MAX_MEGABYTES = 0 assert vanguards.bandguards.CIRC_MAX_MEGABYTES != CIRC_MAX_MEGABYTES check_maxbytes(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_MEGABYTES = CIRC_MAX_MEGABYTES # - Frob circ.created_at to close circ (bw event) circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == None state.circs[str(circ_id)].created_at = time.time() - \ (1+CIRC_MAX_AGE_HOURS*_SECS_PER_HOUR) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == str(circ_id) # - Test disabled circ lifetime circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_AGE_HOURS = 0 assert vanguards.bandguards.CIRC_MAX_AGE_HOURS != CIRC_MAX_AGE_HOURS state.circs[str(circ_id)].created_at = time.time() - \ (1+CIRC_MAX_AGE_HOURS*_SECS_PER_HOUR) state.bw_event(MockEvent(time.time())) assert controller.closed_circ == None # Test that regular reading is ok circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 100, 0) assert controller.closed_circ == None # Test that no dropped cells are allowed before app data circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == str(circ_id) # Test that 0 dropped cells are allowed on pathbias circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_SERVICE_REND")) state.circ_minor_event(purpose_changed_circ(circ_id, "HS_SERVICE_REND", "PATH_BIAS_TESTING")) check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == str(circ_id) # Test that dropped cells are allowed on not-built circ. circ_id += 1 controller.closed_circ = None state.circ_event(extended_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 0, 1) assert controller.closed_circ == None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) state.circbw_event(circ_bw(circ_id, _CELL_PAYLOAD_SIZE, _CELL_PAYLOAD_SIZE, 12, 0, 12, 0)) assert controller.closed_circ == str(circ_id) # Test that after app data, no dropped cells allowed, # and then we close. circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test Non-HS circs closed with dropped cells. circ_id += 1 controller.closed_circ = None state.circ_event(built_general_circ(circ_id)) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test workaround for #29700: circ_id += 1 controller.closed_circ = None state.circ_event(built_hs_circ(circ_id, "HS_SERVICE_REND", "HSSR_CONNECTING")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test workaround for #29927: circ_id += 1 controller.closed_circ = None state.circ_event(built_hs_circ(circ_id, "HS_CLIENT_INTRO", "HSCI_DONE")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) circ_id += 1 controller.closed_circ = None state.circ_event(built_hs_circ(circ_id, "HS_CLIENT_INTRO", "HSCI_CONNECTING")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test workaround for #29699 circ_id += 1 controller.closed_circ = None state.circ_event(built_hs_circ(circ_id, "HS_SERVICE_INTRO", "HSSI_ESTABLISHED")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test workaround for #40359 circ_id += 1 controller.closed_circ = None state.circ_event(built_hs_circ(circ_id, "HS_CLIENT_INTRO", "HSCI_CONNECTING")) state.circ_minor_event(purpose_changed_hs_circ(circ_id, "HS_CLIENT_INTRO", "CIRCUIT_PADDING", "HSCI_INTRO_SENT", "None")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test workaround for #29786 (should close now) circ_id += 1 controller.closed_circ = None state.circ_event(built_hs_circ(circ_id, "HS_SERVICE_INTRO", "HSSI_ESTABLISHED")) state.circ_minor_event(purpose_changed_hs_circ(circ_id, "HS_SERVICE_INTRO", "PATH_BIAS_TESTING", "HSSI_CONNECTING", "None")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) circ_id += 1 controller.closed_circ = None state.circ_event(built_hs_circ(circ_id, "HS_CLIENT_INTRO", "HSCI_CONNECTING")) state.circ_minor_event(purpose_changed_hs_circ(circ_id, "HS_CLIENT_INTRO", "PATH_BIAS_TESTING", "HSCI_CONNECTING", "None")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test workaround for #29700 circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) state.circ_minor_event(purpose_changed_hs_circ(circ_id, "HS_VANGUARDS", "HS_SERVICE_REND", "None", "HSSR_CONNECTING")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) # Test workaround for #29927 circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) state.circ_minor_event(purpose_changed_hs_circ(circ_id, "HS_VANGUARDS", "HS_CLIENT_REND", "None", "HSCR_CONNECTING")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) state.circ_minor_event(purpose_changed_hs_circ(circ_id, "HS_VANGUARDS", "HS_CLIENT_INTRO", "None", "HSCI_DONE")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id) circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) state.circ_minor_event(purpose_changed_hs_circ(circ_id, "HS_VANGUARDS", "HS_CLIENT_INTRO", "None", "HSCI_CONNECTING")) check_dropped_bytes(state, controller, circ_id, 1000, 1) assert controller.closed_circ == str(circ_id)
def test_bwstats(): global CIRC_MAX_DROPPED_BYTES_PERCENT global CIRC_MAX_MEGABYTES controller = MockController() state = BandwidthStats(controller) controller.bwstats = state circ_id = 1 # - BUILT -> FAILED,CLOSED removed from map # - BUILT -> CLOSED removed from map state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert str(circ_id) in state.circs state.circ_event(failed_circ(circ_id)) assert str(circ_id) not in state.circs state.circ_event(closed_circ(circ_id)) assert str(circ_id) not in state.circs circ_id += 1 state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert str(circ_id) in state.circs state.circ_event(closed_circ(circ_id)) assert str(circ_id) not in state.circs # - HSDIR size cap exceeded for direct service circ circ_id += 1 controller.closed_circ = None state.circ_event(built_hsdir_circ(circ_id)) assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == True check_hsdir(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - HSDIR size cap exceeded for cannibalized circ circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) assert state.circs[str(circ_id)].is_hsdir == False state.circ_minor_event(cannibalized_circ(circ_id, "HS_CLIENT_HSDIR")) assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == False check_hsdir(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - HSDIR size cap disabled circ_id += 1 controller.closed_circ = None state.circ_event(built_hsdir_circ(circ_id)) vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES = 0 assert vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES != CIRC_MAX_HSDESC_KILOBYTES assert state.circs[str(circ_id)].is_hsdir == True assert state.circs[str(circ_id)].is_service == True check_hsdir(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_HSDESC_KILOBYTES = CIRC_MAX_HSDESC_KILOBYTES # - Max bytes exceed (read, write) circ_id += 1 controller.closed_circ = None vanguards.bandguards.CIRC_MAX_MEGABYTES = 100 CIRC_MAX_MEGABYTES = 100 state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) check_maxbytes(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - Max bytes disabled circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_SERVICE_REND")) vanguards.bandguards.CIRC_MAX_MEGABYTES = 0 assert vanguards.bandguards.CIRC_MAX_MEGABYTES != CIRC_MAX_MEGABYTES check_maxbytes(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_MEGABYTES = CIRC_MAX_MEGABYTES # - Frob circ.created_at to close circ (bw event) circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) state.bw_event(None) assert controller.closed_circ == None state.circs[str(circ_id)].created_at = time.time() - \ (1+CIRC_MAX_AGE_HOURS*_SECS_PER_HOUR) state.bw_event(None) assert controller.closed_circ == str(circ_id) # - Test disabled circ lifetime circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) state.bw_event(None) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_AGE_HOURS = 0 assert vanguards.bandguards.CIRC_MAX_AGE_HOURS != CIRC_MAX_AGE_HOURS state.circs[str(circ_id)].created_at = time.time() - \ (1+CIRC_MAX_AGE_HOURS*_SECS_PER_HOUR) state.bw_event(None) assert controller.closed_circ == None # - Read ratio exceeded (but writes are ignored) CIRC_MAX_DROPPED_BYTES_PERCENT = 2.5 vanguards.bandguards.CIRC_MAX_DROPPED_BYTES_PERCENT = 2.5 CIRC_MAX_DROPPED_BYTES_PERCENT /= 100.0 circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_CLIENT_REND")) check_ratio(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - Read ratio exceeded for service (but writes are ignored) circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) state.circ_minor_event(cannibalized_circ(circ_id, "HS_SERVICE_REND")) check_ratio(state, controller, circ_id) assert controller.closed_circ == str(circ_id) # - Read ratio disabled circ_id += 1 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) vanguards.bandguards.CIRC_MAX_DROPPED_BYTES_PERCENT = 100.0 check_ratio(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_DROPPED_BYTES_PERCENT = CIRC_MAX_DROPPED_BYTES_PERCENT * 100 # - Read ratio set to 0 but there still is overhead. circ_id += 1 vanguards.bandguards.CIRC_MAX_DROPPED_BYTES_PERCENT = 0.0 controller.closed_circ = None state.circ_event(built_circ(circ_id, "HS_VANGUARDS")) vanguards.bandguards.CIRC_MAX_DROPPED_BYTES_PERCENT = 100.0 check_ratio(state, controller, circ_id) assert controller.closed_circ == None vanguards.bandguards.CIRC_MAX_DROPPED_BYTES_PERCENT = CIRC_MAX_DROPPED_BYTES_PERCENT * 100 # - Non-HS circs ignored: circ_id += 1 state.circ_event(built_general_circ(circ_id)) assert str(circ_id) not in state.circs