示例#1
0
void refreshBannedNodes(void)
{	r1=tmpOptions->ExcludeExitNodes;
	r2=routerset_new();
	char *tmp1=tor_malloc(65536),*tmp2,*tmp3;tmp2=tmp1;tmp3=tmp1;GetDlgItemText(hDlgRouterRestrictions,15100,tmp1,65536);
	while(*tmp1)
	{	while((*tmp1==32)||(*tmp1==9))	tmp1++;
		if((*tmp1==13)||(*tmp1==10))
		{	while((*tmp1==13)||(*tmp1==10)){	tmp1++;}
			*tmp2++=',';
		}
		else if((*tmp1=='[')&&(*(tmp1+2)==']'))
		{	if((*(tmp1+1)=='x')||(*(tmp1+1)=='X'))
			{	tmp1+=3;
				while((*tmp1==32)||(*tmp1==9))	tmp1++;
				while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	*tmp2++=*tmp1++;
			}
			else
				while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	tmp1++;
		}
		else
			while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	tmp1++;
	}
	*tmp2=0;routerset_parse(r2,tmp3,"ExcludeExitNodes");
	tmpOptions->ExcludeExitNodes=r2;
	if(r1)	routerset_free(r1);
	r1=tmpOptions->ExcludeNodes;
	r2=routerset_new();
	tmp1=tmp3;tmp2=tmp3;GetDlgItemText(hDlgRouterRestrictions,15100,tmp1,65536);
	while(*tmp1)
	{	while((*tmp1==32)||(*tmp1==9))	tmp1++;
		if((*tmp1==13)||(*tmp1==10))
		{	while((*tmp1==13)||(*tmp1==10)){	tmp1++;}
			*tmp2++=',';
		}
		else if((*tmp1=='[')&&(*(tmp1+2)==']'))
		{	if((*(tmp1+1)=='x')||(*(tmp1+1)=='X'))
			{	while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	tmp1++;
			}
			else
			{	while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	*tmp2++=*tmp1++;
			}
		}
		else
		{	while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	*tmp2++=*tmp1++;
		}
	}
	*tmp2=0;routerset_parse(r2,tmp3,"ExcludeNodes");
	tmpOptions->ExcludeNodes=r2;
	if(r1)	routerset_free(r1);
	if(tmpOptions->ExcludeExitNodes || tmpOptions->ExcludeNodes)
	{	r2 = tmpOptions->_ExcludeExitNodesUnion;
		r1 = routerset_new();
		routerset_union(r1,tmpOptions->ExcludeExitNodes);
		routerset_union(r1,tmpOptions->ExcludeNodes);
		tmpOptions->_ExcludeExitNodesUnion = r1;
		if(r2)	routerset_free(r2);
	}
	tor_free(tmp3);
}
示例#2
0
void refreshFavoriteNodes(void)
{	r1=tmpOptions->EntryNodes;
	r2=routerset_new();
	char *tmp1=tor_malloc(65536),*tmp2,*tmp3;tmp2=tmp1;tmp3=tmp1;GetDlgItemText(hDlgRouterRestrictions,15101,tmp1,65536);
	while(*tmp1)
	{	while((*tmp1==32)||(*tmp1==9))	tmp1++;
		if((*tmp1==13)||(*tmp1==10))
		{	while((*tmp1==13)||(*tmp1==10)){	tmp1++;}
			*tmp2++=',';
		}
		else if((*tmp1=='[')&&(*(tmp1+2)==']'))
		{	if((*(tmp1+1)=='e')||(*(tmp1+1)=='E'))
			{	tmp1+=3;
				while((*tmp1==32)||(*tmp1==9))	tmp1++;
				while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	*tmp2++=*tmp1++;
			}
			else
				while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	tmp1++;
		}
		else
			while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	tmp1++;
	}
	*tmp2=0;routerset_parse(r2,tmp3,"EntryNodes");
	tmpOptions->EntryNodes=r2;
	if(r1)	routerset_free(r1);
	r1=tmpOptions->ExitNodes;
	r2=routerset_new();
	tmp1=tmp3;tmp2=tmp3;GetDlgItemText(hDlgRouterRestrictions,15101,tmp1,65536);
	while(*tmp1)
	{	while((*tmp1==32)||(*tmp1==9))	tmp1++;
		if((*tmp1==13)||(*tmp1==10))
		{	while((*tmp1==13)||(*tmp1==10)){	tmp1++;}
			*tmp2++=',';
		}
		else if((*tmp1=='[')&&(*(tmp1+2)==']'))
		{	if((*(tmp1+1)=='e')||(*(tmp1+1)=='E'))
			{	while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	tmp1++;
			}
			else if((*(tmp1+1)=='x')||(*(tmp1+1)=='X'))
			{	tmp1+=3;
				while((*tmp1==32)||(*tmp1==9))	tmp1++;
				while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	*tmp2++=*tmp1++;
			}
			else
			{	while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	*tmp2++=*tmp1++;
			}
		}
		else
		{	while((*tmp1!=0)&&(*tmp1!=13)&&(*tmp1!=10))	*tmp2++=*tmp1++;
		}
	}
	*tmp2=0;routerset_parse(r2,tmp3,"ExitNodes");
	tmpOptions->ExitNodes=r2;
	if(r1)	routerset_free(r1);
	tor_free(tmp3);
}
示例#3
0
void add_router_to_favorites(HWND hDlg,char *router,char favtype)
{	if(hDlgRouterRestrictions)
	{	char *favtmp2,*favtmp3;
		int favtmpsize;
		int i;
		favtmpsize=SendDlgItemMessage(hDlgRouterRestrictions,15101,WM_GETTEXTLENGTH,0,0);
		favtmp2=tor_malloc(favtmpsize+256+5);favtmp3=favtmp2;
		GetDlgItemText(hDlgRouterRestrictions,15101,favtmp2,favtmpsize+1);favtmp2+=favtmpsize;
		if((favtmpsize>2)&&((*(favtmp2-1)!=0x0d)&&(*(favtmp2-1)!=0x0a)))
		{	*favtmp2++=0x0d;*favtmp2++=0x0a;}
		*favtmp2++='[';*favtmp2++=favtype;*favtmp2++=']';*favtmp2++=32;
		for(i=0;router[i];i++)	*favtmp2++=router[i];
		*favtmp2++=13;*favtmp2++=10;*favtmp2++=0;
		tor_snprintf(favtmp2,100,get_lang_str(LANG_MB_FAV_ADDED),favtmp3+favtmpsize);
		LangMessageBox(hDlg,favtmp2,LANG_MB_FAVORITES,MB_OK);
		log(LOG_NOTICE,LD_APP,favtmp2);
		SetDlgItemText(hDlgRouterRestrictions,15101,favtmp3);
		refreshFavoriteNodes();
		tor_free(favtmp3);
	}
	else if(favtype=='X')
	{	char *tmp1=routerset_to_string(tmpOptions->ExitNodes);
		int i=strlen(tmp1)+256+5;
		char *tmp2=tor_malloc(i),*tmp3=tor_malloc(256);
		tor_snprintf(tmp2,i,"%s,%s",tmp1,router);
		r1=tmpOptions->ExitNodes;
		r2=routerset_new();
		routerset_parse(r2,tmp2,"ExitNodes");
		tmpOptions->ExitNodes=r2;
		if(r1)	routerset_free(r1);
		tor_snprintf(tmp3,256,"[X] %s",router);
		tor_snprintf(tmp2,100,get_lang_str(LANG_MB_FAV_ADDED),tmp3);
		LangMessageBox(hDlg,tmp2,LANG_MB_FAVORITES,MB_OK);
		log(LOG_NOTICE,LD_APP,tmp2);
		tor_free(tmp1);tor_free(tmp2);tor_free(tmp3);
	}
	else if(favtype=='E')
	{	char *tmp1=routerset_to_string(tmpOptions->EntryNodes);
		int i=strlen(tmp1)+256+5;
		char *tmp2=tor_malloc(i),*tmp3=tor_malloc(256);
		tor_snprintf(tmp2,i,"%s,%s",tmp1,router);
		r1=tmpOptions->EntryNodes;
		r2=routerset_new();
		routerset_parse(r2,tmp2,"EntryNodes");
		tmpOptions->EntryNodes=r2;
		if(r1)	routerset_free(r1);
		tor_snprintf(tmp3,256,"[E] %s",router);
		tor_snprintf(tmp2,100,get_lang_str(LANG_MB_FAV_ADDED),tmp3);
		LangMessageBox(hDlg,tmp2,LANG_MB_FAVORITES,MB_OK);
		log(LOG_NOTICE,LD_APP,tmp2);
		tor_free(tmp1);tor_free(tmp2);tor_free(tmp3);
	}
}
示例#4
0
文件: test_hs.c 项目: ageis/tor
/* Make sure we never pick an RP if Tor2webRendezvousPoints doesn't
 * correspond to an actual node. */
static void
test_pick_bad_tor2web_rendezvous_node(void *arg)
{
  or_options_t *options = get_options_mutable();
  const node_t *chosen_rp = NULL;
  router_crn_flags_t flags = CRN_NEED_DESC;
  int retval, i;
  const char *tor2web_rendezvous_str = "dummy";

  (void) arg;

  /* Setup fake routerlist. */
  helper_setup_fake_routerlist();

  /* Parse Tor2webRendezvousPoints as a routerset. */
  options->Tor2webRendezvousPoints = routerset_new();
  retval = routerset_parse(options->Tor2webRendezvousPoints,
                           tor2web_rendezvous_str,
                           "test_tor2web_rp");
  tt_int_op(retval, OP_GE, 0);

  /* Pick rendezvous point. Since Tor2webRendezvousPoints was set to a
     dummy value, we shouldn't find any eligible RPs. */
  for (i = 0; i < 50 ; i++) {
    chosen_rp = pick_tor2web_rendezvous_node(flags, options);
    tt_ptr_op(chosen_rp, OP_EQ, NULL);
  }

 done:
  routerset_free(options->Tor2webRendezvousPoints);
}
示例#5
0
文件: test_hs.c 项目: ageis/tor
/* Make sure we always pick the right RP, given a well formatted
 * Tor2webRendezvousPoints value. */
static void
test_pick_tor2web_rendezvous_node(void *arg)
{
  or_options_t *options = get_options_mutable();
  const node_t *chosen_rp = NULL;
  router_crn_flags_t flags = CRN_NEED_DESC;
  int retval, i;
  const char *tor2web_rendezvous_str = "test003r";

  (void) arg;

  /* Setup fake routerlist. */
  helper_setup_fake_routerlist();

  /* Parse Tor2webRendezvousPoints as a routerset. */
  options->Tor2webRendezvousPoints = routerset_new();
  retval = routerset_parse(options->Tor2webRendezvousPoints,
                           tor2web_rendezvous_str,
                           "test_tor2web_rp");
  tt_int_op(retval, OP_GE, 0);

  /* Pick rendezvous point. Make sure the correct one is
     picked. Repeat many times to make sure it works properly. */
  for (i = 0; i < 50 ; i++) {
    chosen_rp = pick_tor2web_rendezvous_node(flags, options);
    tt_assert(chosen_rp);
    tt_str_op(chosen_rp->ri->nickname, OP_EQ, tor2web_rendezvous_str);
  }

 done:
  routerset_free(options->Tor2webRendezvousPoints);
}
示例#6
0
static void
NS(test_main)(void *arg)
{
  routerset_t *set = routerset_new();
  const char *s = "_";
  int r;
  (void)arg;

  r = routerset_parse(set, s, "");

  tt_int_op(r, ==, -1);

  done:
    routerset_free(set);
}
示例#7
0
static void
NS(test_main)(void *arg)
{
  routerset_t *set;
  const char *s;
  int r;
  (void)arg;

  set = routerset_new();
  s = "{cc}";
  r = routerset_parse(set, s, "");
  tt_int_op(r, ==, 0);
  tt_int_op(smartlist_len(set->country_names), !=, 0);

  done:
    routerset_free(set);
}
示例#8
0
static void
NS(test_main)(void *arg)
{
  routerset_t *set;
  const char *s;
  int r;
  (void)arg;

  set = routerset_new();
  s = "fred";
  r = routerset_parse(set, s, "");
  tt_int_op(r, ==, 0);
  tt_int_op(strmap_isempty(set->names), !=, 1);

  done:
    routerset_free(set);
}
示例#9
0
static void
NS(test_main)(void *arg)
{
  routerset_t *set;
  const char *s;
  int r;
  (void)arg;

  set = routerset_new();
  s = "$0000000000000000000000000000000000000000";
  r = routerset_parse(set, s, "");
  tt_int_op(r, OP_EQ, 0);
  tt_int_op(digestmap_isempty(set->digests), OP_NE, 1);

  done:
    routerset_free(set);
}
示例#10
0
static void
NS(test_main)(void *arg)
{
  routerset_t *set;
  const char *s;
  int r;
  (void)arg;

  NS_MOCK(router_parse_addr_policy_item_from_string);
  NS(mock_addr_policy) = tor_malloc_zero(sizeof(addr_policy_t));

  set = routerset_new();
  s = "*";
  r = routerset_parse(set, s, "");
  tt_int_op(r, ==, 0);
  tt_int_op(smartlist_len(set->policies), !=, 0);
  tt_int_op(CALLED(router_parse_addr_policy_item_from_string), ==, 1);

  done:
    routerset_free(set);
}
示例#11
0
/** <b>c</b>-\>key is known to be a real key. Update <b>options</b>
 * with <b>c</b>-\>value and return 0, or return -1 if bad value.
 *
 * Called from config_assign_line() and option_reset().
 */
static int
config_assign_value(const config_format_t *fmt, void *options,
                    config_line_t *c, char **msg)
{
  int i, ok;
  const config_var_t *var;
  void *lvalue;
  int *csv_int;
  smartlist_t *csv_str;

  CONFIG_CHECK(fmt, options);

  var = config_find_option(fmt, c->key);
  tor_assert(var);

  lvalue = STRUCT_VAR_P(options, var->var_offset);

  switch (var->type) {

  case CONFIG_TYPE_PORT:
    if (!strcasecmp(c->value, "auto")) {
      *(int *)lvalue = CFG_AUTO_PORT;
      break;
    }
    /* fall through */
  case CONFIG_TYPE_INT:
  case CONFIG_TYPE_UINT:
    i = (int)tor_parse_long(c->value, 10,
                            var->type==CONFIG_TYPE_INT ? INT_MIN : 0,
                            var->type==CONFIG_TYPE_PORT ? 65535 : INT_MAX,
                            &ok, NULL);
    if (!ok) {
      tor_asprintf(msg,
          "Int keyword '%s %s' is malformed or out of bounds.",
          c->key, c->value);
      return -1;
    }
    *(int *)lvalue = i;
    break;

  case CONFIG_TYPE_INTERVAL: {
    i = config_parse_interval(c->value, &ok);
    if (!ok) {
      tor_asprintf(msg,
          "Interval '%s %s' is malformed or out of bounds.",
          c->key, c->value);
      return -1;
    }
    *(int *)lvalue = i;
    break;
  }

  case CONFIG_TYPE_MSEC_INTERVAL: {
    i = config_parse_msec_interval(c->value, &ok);
    if (!ok) {
      tor_asprintf(msg,
          "Msec interval '%s %s' is malformed or out of bounds.",
          c->key, c->value);
      return -1;
    }
    *(int *)lvalue = i;
    break;
  }

  case CONFIG_TYPE_MEMUNIT: {
    uint64_t u64 = config_parse_memunit(c->value, &ok);
    if (!ok) {
      tor_asprintf(msg,
          "Value '%s %s' is malformed or out of bounds.",
          c->key, c->value);
      return -1;
    }
    *(uint64_t *)lvalue = u64;
    break;
  }

  case CONFIG_TYPE_BOOL:
    i = (int)tor_parse_long(c->value, 10, 0, 1, &ok, NULL);
    if (!ok) {
      tor_asprintf(msg,
          "Boolean '%s %s' expects 0 or 1.",
          c->key, c->value);
      return -1;
    }
    *(int *)lvalue = i;
    break;

  case CONFIG_TYPE_AUTOBOOL:
    if (!strcmp(c->value, "auto"))
      *(int *)lvalue = -1;
    else if (!strcmp(c->value, "0"))
      *(int *)lvalue = 0;
    else if (!strcmp(c->value, "1"))
      *(int *)lvalue = 1;
    else {
      tor_asprintf(msg, "Boolean '%s %s' expects 0, 1, or 'auto'.",
                   c->key, c->value);
      return -1;
    }
    break;

  case CONFIG_TYPE_STRING:
  case CONFIG_TYPE_FILENAME:
    tor_free(*(char **)lvalue);
    *(char **)lvalue = tor_strdup(c->value);
    break;

  case CONFIG_TYPE_DOUBLE:
    *(double *)lvalue = atof(c->value);
    break;

  case CONFIG_TYPE_ISOTIME:
    if (parse_iso_time(c->value, (time_t *)lvalue)) {
      tor_asprintf(msg,
          "Invalid time '%s' for keyword '%s'", c->value, c->key);
      return -1;
    }
    break;

  case CONFIG_TYPE_ROUTERSET:
    if (*(routerset_t**)lvalue) {
      routerset_free(*(routerset_t**)lvalue);
    }
    *(routerset_t**)lvalue = routerset_new();
    if (routerset_parse(*(routerset_t**)lvalue, c->value, c->key)<0) {
      tor_asprintf(msg, "Invalid exit list '%s' for option '%s'",
                   c->value, c->key);
      return -1;
    }
    break;

  case CONFIG_TYPE_CSV:
    if (*(smartlist_t**)lvalue) {
      SMARTLIST_FOREACH(*(smartlist_t**)lvalue, char *, cp, tor_free(cp));
      smartlist_clear(*(smartlist_t**)lvalue);
    } else {
示例#12
0
void add_router_to_banlist(HWND hDlg,char *router,char bantype)
{	if(hDlgRouterRestrictions)
	{	char *bantmp2,*bantmp3;
		int bantmpsize,i;
		bantmpsize=SendDlgItemMessage(hDlgRouterRestrictions,15100,WM_GETTEXTLENGTH,0,0);
		bantmp2=tor_malloc(bantmpsize+256+5);bantmp3=bantmp2;
		GetDlgItemText(hDlgRouterRestrictions,15100,bantmp2,bantmpsize+1);bantmp2+=bantmpsize;
		if((bantmpsize>2)&&((*(bantmp2-1)!=0x0d)&&(*(bantmp2-1)!=0x0a)))
		{	*bantmp2++=0x0d;*bantmp2++=0x0a;}
		if(bantype){	*bantmp2++='[';*bantmp2++=bantype;*bantmp2++=']';*bantmp2++=32;}
		for(i=0;router[i];i++)	*bantmp2++=router[i];
		*bantmp2++=13;*bantmp2++=10;*bantmp2++=0;
		tor_snprintf(bantmp2,100,get_lang_str(LANG_MB_BAN_ADDED),bantmp3+bantmpsize);
		if(hDlg) LangMessageBox(hDlg,bantmp2,LANG_MB_BANS,MB_OK);
		log(LOG_NOTICE,LD_APP,bantmp2);
		SetDlgItemText(hDlgRouterRestrictions,15100,bantmp3);
		refreshBannedNodes();
		tor_free(bantmp3);
	}
	else if(bantype)
	{	char *tmp1=routerset_to_string(tmpOptions->ExcludeExitNodes);
		int i=strlen(tmp1)+256+5;
		char *tmp2=tor_malloc(i),*tmp3=tor_malloc(256);;
		tor_snprintf(tmp2,i,"%s,%s",tmp1,router);
		r1=tmpOptions->ExcludeExitNodes;
		r2=routerset_new();
		routerset_parse(r2,tmp2,"ExcludeExitNodes");
		tmpOptions->ExcludeExitNodes=r2;
		if(r1)	routerset_free(r1);
		tor_snprintf(tmp3,256,"[X] %s",router);
		tor_snprintf(tmp2,100,get_lang_str(LANG_MB_BAN_ADDED),tmp3);
		if(hDlg) LangMessageBox(hDlg,tmp2,LANG_MB_BANS,MB_OK);
		log(LOG_NOTICE,LD_APP,tmp2);
		tor_free(tmp1);tor_free(tmp2);tor_free(tmp3);
		if(tmpOptions->ExcludeExitNodes || tmpOptions->ExcludeNodes)
		{	r2 = tmpOptions->_ExcludeExitNodesUnion;
			r1 = routerset_new();
			routerset_union(r1,tmpOptions->ExcludeExitNodes);
			routerset_union(r1,tmpOptions->ExcludeNodes);
			tmpOptions->_ExcludeExitNodesUnion = r1;
			if(r2)	routerset_free(r2);
		}
	}
	else
	{	char *tmp1=routerset_to_string(tmpOptions->ExcludeNodes);
		int i=strlen(tmp1)+256+5;
		char *tmp2=tor_malloc(i);
		tor_snprintf(tmp2,i,"%s,%s",tmp1,router);
		r1=tmpOptions->ExcludeNodes;
		r2=routerset_new();
		routerset_parse(r2,tmp2,"ExcludeNodes");
		tmpOptions->ExcludeNodes=r2;
		if(r1)	routerset_free(r1);
		tor_snprintf(tmp2,100,get_lang_str(LANG_MB_BAN_ADDED),router);
		if(hDlg) LangMessageBox(hDlg,tmp2,LANG_MB_BANS,MB_OK);
		log(LOG_NOTICE,LD_APP,tmp2);
		tor_free(tmp1);tor_free(tmp2);
		if(tmpOptions->ExcludeExitNodes || tmpOptions->ExcludeNodes)
		{	r2 = tmpOptions->_ExcludeExitNodesUnion;
			r1 = routerset_new();
			routerset_union(r1,tmpOptions->ExcludeExitNodes);
			routerset_union(r1,tmpOptions->ExcludeNodes);
			tmpOptions->_ExcludeExitNodesUnion = r1;
			if(r2)	routerset_free(r2);
		}
	}
}
示例#13
0
/** Test that we will use our directory guards to fetch mds even if we don't
 *  have any dirinfo (tests bug #23862). */
static void
test_directory_guard_fetch_with_no_dirinfo(void *arg)
{
  int retval;
  char *consensus_text_md = NULL;
  or_options_t *options = get_options_mutable();
  time_t now = time(NULL);

  (void) arg;

  hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);

  /* Initialize the SRV subsystem */
  MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
                                               strlen(AUTHORITY_CERT_1),
                                               NULL);
  sr_init(0);
  UNMOCK(get_my_v3_authority_cert);

  /* Initialize the entry node configuration from the ticket */
  options->UseEntryGuards = 1;
  options->StrictNodes = 1;
  get_options_mutable()->EntryNodes = routerset_new();
  routerset_parse(get_options_mutable()->EntryNodes,
                  "2121212121212121212121212121212121212121", "foo");

  /* Mock some functions */
  dummy_state = tor_malloc_zero(sizeof(or_state_t));
  MOCK(get_or_state, get_or_state_replacement);
  MOCK(directory_initiate_request, mock_directory_initiate_request);
  /* we need to mock this one to avoid memleaks */
  MOCK(circuit_guard_state_new, mock_circuit_guard_state_new);

  /* Call guards_update_all() to simulate loading our state file (see
   * entry_guards_load_guards_from_state() and ticket #23989). */
  guards_update_all();

  /* Test logic: Simulate the arrival of a new consensus when we have no
   * dirinfo at all. Tor will need to fetch the mds from the consensus. Make
   * sure that Tor will use the specified entry guard instead of relying on the
   * fallback directories. */

  /* Fixup the dirconn that will deliver the consensus */
  dir_connection_t *conn = dir_connection_new(AF_INET);
  tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
  conn->base_.port = 8800;
  TO_CONN(conn)->address = tor_strdup("127.0.0.1");
  conn->base_.purpose = DIR_PURPOSE_FETCH_CONSENSUS;
  conn->requested_resource = tor_strdup("ns");

  /* Construct a consensus */
  construct_consensus(&consensus_text_md, now);
  tt_assert(consensus_text_md);

  /* Place the consensus in the dirconn */
  response_handler_args_t args;
  memset(&args, 0, sizeof(response_handler_args_t));
  args.status_code = 200;
  args.body = consensus_text_md;
  args.body_len = strlen(consensus_text_md);

  /* Update approx time so that the consensus is considered live */
  update_approx_time(now+1010);

  setup_capture_of_logs(LOG_DEBUG);

  /* Now handle the consensus */
  retval = handle_response_fetch_consensus(conn, &args);
  tt_int_op(retval, OP_EQ, 0);

  /* Make sure that our primary guard was chosen */
  expect_log_msg_containing("Selected primary guard router3");

 done:
  tor_free(consensus_text_md);
  tor_free(dummy_state);
  connection_free_minimal(TO_CONN(conn));
  entry_guards_free_all();
  teardown_capture_of_logs();
}