Fix the position-check for ed25519 certs to work with annotations

When there are annotations on a router descriptor, the
ed25519-identity element won't be at position 0 or 1; it will be at
router+1 or router-1.

This patch also adds a missing smartlist function to search a list for
an item with a particular pointer.
This commit is contained in:
Nick Mathewson 2014-10-08 09:13:09 -04:00
parent 592a439107
commit 006b7ce5ff
4 changed files with 57 additions and 2 deletions

View File

@ -208,6 +208,19 @@ smartlist_string_pos(const smartlist_t *sl, const char *element)
return -1; return -1;
} }
/** If <b>element</b> is the same pointer as an element of <b>sl</b>, return
* that element's index. Otherwise, return -1. */
int
smartlist_pos(const smartlist_t *sl, const void *element)
{
int i;
if (!sl) return -1;
for (i=0; i < sl->num_used; i++)
if (element == sl->list[i])
return i;
return -1;
}
/** Return true iff <b>sl</b> has some element E such that /** Return true iff <b>sl</b> has some element E such that
* !strcasecmp(E,<b>element</b>) * !strcasecmp(E,<b>element</b>)
*/ */

View File

@ -38,6 +38,7 @@ void smartlist_reverse(smartlist_t *sl);
void smartlist_string_remove(smartlist_t *sl, const char *element); void smartlist_string_remove(smartlist_t *sl, const char *element);
int smartlist_contains(const smartlist_t *sl, const void *element); int smartlist_contains(const smartlist_t *sl, const void *element);
int smartlist_contains_string(const smartlist_t *sl, const char *element); int smartlist_contains_string(const smartlist_t *sl, const char *element);
int smartlist_pos(const smartlist_t *sl, const void *element);
int smartlist_string_pos(const smartlist_t *, const char *elt); int smartlist_string_pos(const smartlist_t *, const char *elt);
int smartlist_contains_string_case(const smartlist_t *sl, const char *element); int smartlist_contains_string_case(const smartlist_t *sl, const char *element);
int smartlist_contains_int_as_string(const smartlist_t *sl, int num); int smartlist_contains_int_as_string(const smartlist_t *sl, int num);

View File

@ -1195,6 +1195,7 @@ router_parse_entry_from_string(const char *s, const char *end,
} }
tok = find_by_keyword(tokens, K_ROUTER); tok = find_by_keyword(tokens, K_ROUTER);
const int router_token_pos = smartlist_pos(tokens, tok);
tor_assert(tok->n_args >= 5); tor_assert(tok->n_args >= 5);
router = tor_malloc_zero(sizeof(routerinfo_t)); router = tor_malloc_zero(sizeof(routerinfo_t));
@ -1345,8 +1346,10 @@ router_parse_entry_from_string(const char *s, const char *end,
} }
if (ed_sig_tok) { if (ed_sig_tok) {
tor_assert(ed_cert_tok && cc_tap_tok && cc_ntor_tok); tor_assert(ed_cert_tok && cc_tap_tok && cc_ntor_tok);
if (ed_cert_tok != smartlist_get(tokens, 0) && const int ed_cert_token_pos = smartlist_pos(tokens, ed_cert_tok);
ed_cert_tok != smartlist_get(tokens, 1)) { if (ed_cert_token_pos == -1 || router_token_pos == -1 ||
(ed_cert_token_pos != router_token_pos + 1 &&
ed_cert_token_pos != router_token_pos - 1)) {
log_warn(LD_DIR, "Ed25519 certificate in wrong position"); log_warn(LD_DIR, "Ed25519 certificate in wrong position");
goto err; goto err;
} }

View File

@ -495,6 +495,43 @@ test_container_smartlist_join(void *arg)
tor_free(joined); tor_free(joined);
} }
static void
test_container_smartlist_pos(void *arg)
{
(void) arg;
smartlist_t *sl = smartlist_new();
smartlist_add(sl, tor_strdup("This"));
smartlist_add(sl, tor_strdup("is"));
smartlist_add(sl, tor_strdup("a"));
smartlist_add(sl, tor_strdup("test"));
smartlist_add(sl, tor_strdup("for"));
smartlist_add(sl, tor_strdup("a"));
smartlist_add(sl, tor_strdup("function"));
/* Test string_pos */
tt_int_op(smartlist_string_pos(NULL, "Fred"), ==, -1);
tt_int_op(smartlist_string_pos(sl, "Fred"), ==, -1);
tt_int_op(smartlist_string_pos(sl, "This"), ==, 0);
tt_int_op(smartlist_string_pos(sl, "a"), ==, 2);
tt_int_op(smartlist_string_pos(sl, "function"), ==, 6);
/* Test pos */
tt_int_op(smartlist_pos(NULL, "Fred"), ==, -1);
tt_int_op(smartlist_pos(sl, "Fred"), ==, -1);
tt_int_op(smartlist_pos(sl, "This"), ==, -1);
tt_int_op(smartlist_pos(sl, "a"), ==, -1);
tt_int_op(smartlist_pos(sl, "function"), ==, -1);
tt_int_op(smartlist_pos(sl, smartlist_get(sl,0)), ==, 0);
tt_int_op(smartlist_pos(sl, smartlist_get(sl,2)), ==, 2);
tt_int_op(smartlist_pos(sl, smartlist_get(sl,5)), ==, 5);
tt_int_op(smartlist_pos(sl, smartlist_get(sl,6)), ==, 6);
done:
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_free(sl);
}
static void static void
test_container_smartlist_ints_eq(void *arg) test_container_smartlist_ints_eq(void *arg)
{ {
@ -1053,6 +1090,7 @@ struct testcase_t container_tests[] = {
CONTAINER_LEGACY(smartlist_overlap), CONTAINER_LEGACY(smartlist_overlap),
CONTAINER_LEGACY(smartlist_digests), CONTAINER_LEGACY(smartlist_digests),
CONTAINER_LEGACY(smartlist_join), CONTAINER_LEGACY(smartlist_join),
CONTAINER_LEGACY(smartlist_pos),
CONTAINER(smartlist_ints_eq, 0), CONTAINER(smartlist_ints_eq, 0),
CONTAINER_LEGACY(bitarray), CONTAINER_LEGACY(bitarray),
CONTAINER_LEGACY(digestset), CONTAINER_LEGACY(digestset),