From b2b2e1c7f24d9b65059e3d089768d6c49ba4f58f Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 9 Aug 2016 19:11:47 -0400 Subject: checkpoint basic protover backend --- src/test/include.am | 1 + src/test/test.c | 1 + src/test/test.h | 1 + src/test/test_protover.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/test/test_protover.c (limited to 'src/test') diff --git a/src/test/include.am b/src/test/include.am index 2e91f7c9dc..a64310d918 100644 --- a/src/test/include.am +++ b/src/test/include.am @@ -107,6 +107,7 @@ src_test_test_SOURCES = \ src/test/test_options.c \ src/test/test_policy.c \ src/test/test_procmon.c \ + src/test/test_protover.c \ src/test/test_pt.c \ src/test/test_pubsub.c \ src/test/test_relay.c \ diff --git a/src/test/test.c b/src/test/test.c index 2f10c7e90b..9a41b976b8 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1214,6 +1214,7 @@ struct testgroup_t testgroups[] = { { "options/", options_tests }, { "policy/" , policy_tests }, { "procmon/", procmon_tests }, + { "protover/", protover_tests }, { "pt/", pt_tests }, { "relay/" , relay_tests }, { "relaycell/", relaycell_tests }, diff --git a/src/test/test.h b/src/test/test.h index c0643e154d..d9ea57d367 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -206,6 +206,7 @@ extern struct testcase_t oos_tests[]; extern struct testcase_t options_tests[]; extern struct testcase_t policy_tests[]; extern struct testcase_t procmon_tests[]; +extern struct testcase_t protover_tests[]; extern struct testcase_t pubsub_tests[]; extern struct testcase_t pt_tests[]; extern struct testcase_t relay_tests[]; diff --git a/src/test/test_protover.c b/src/test/test_protover.c new file mode 100644 index 0000000000..606ca57b1b --- /dev/null +++ b/src/test/test_protover.c @@ -0,0 +1,91 @@ +/* Copyright (c) 2016, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#define PROTOVER_PRIVATE + +#include "orconfig.h" +#include "test.h" + +#include "protover.h" + +static void +test_protover_parse(void *arg) +{ + (void) arg; + char *re_encoded = NULL; + + const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900"; + smartlist_t *elts = parse_protocol_list(orig); + + tt_assert(elts); + tt_int_op(smartlist_len(elts), OP_EQ, 4); + + const proto_entry_t *e; + const proto_range_t *r; + e = smartlist_get(elts, 0); + tt_str_op(e->name, OP_EQ, "Foo"); + tt_int_op(smartlist_len(e->ranges), OP_EQ, 2); + { + r = smartlist_get(e->ranges, 0); + tt_int_op(r->low, OP_EQ, 1); + tt_int_op(r->high, OP_EQ, 1); + + r = smartlist_get(e->ranges, 1); + tt_int_op(r->low, OP_EQ, 3); + tt_int_op(r->high, OP_EQ, 3); + } + + e = smartlist_get(elts, 1); + tt_str_op(e->name, OP_EQ, "Bar"); + tt_int_op(smartlist_len(e->ranges), OP_EQ, 1); + { + r = smartlist_get(e->ranges, 0); + tt_int_op(r->low, OP_EQ, 3); + tt_int_op(r->high, OP_EQ, 3); + } + + e = smartlist_get(elts, 2); + tt_str_op(e->name, OP_EQ, "Baz"); + tt_int_op(smartlist_len(e->ranges), OP_EQ, 0); + + e = smartlist_get(elts, 3); + tt_str_op(e->name, OP_EQ, "Quux"); + tt_int_op(smartlist_len(e->ranges), OP_EQ, 4); + { + r = smartlist_get(e->ranges, 0); + tt_int_op(r->low, OP_EQ, 9); + tt_int_op(r->high, OP_EQ, 12); + + r = smartlist_get(e->ranges, 1); + tt_int_op(r->low, OP_EQ, 14); + tt_int_op(r->high, OP_EQ, 14); + + r = smartlist_get(e->ranges, 2); + tt_int_op(r->low, OP_EQ, 15); + tt_int_op(r->high, OP_EQ, 16); + + r = smartlist_get(e->ranges, 3); + tt_int_op(r->low, OP_EQ, 900); + tt_int_op(r->high, OP_EQ, 900); + } + + re_encoded = encode_protocol_list(elts); + tt_assert(re_encoded); + tt_str_op(re_encoded, OP_EQ, orig); + + done: + if (elts) + SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent)); + smartlist_free(elts); + tor_free(re_encoded); +} + + +#define PV_TEST(name, flags) \ + { #name, test_protover_ ##name, (flags), NULL, NULL } + +struct testcase_t protover_tests[] = { + PV_TEST(parse, 0), + END_OF_TESTCASES +}; + -- cgit v1.2.3-54-g00ecf From 4df12239f60a79c9543c21e665d2569f064b98ea Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 19 Aug 2016 14:10:20 -0400 Subject: Emit and parse protocol lists in router descriptors --- src/or/or.h | 3 +++ src/or/router.c | 13 ++++++++++++- src/or/routerlist.c | 1 + src/or/routerparse.c | 6 ++++++ src/test/test_dir.c | 2 -- 5 files changed, 22 insertions(+), 3 deletions(-) (limited to 'src/test') diff --git a/src/or/or.h b/src/or/or.h index 34089ad994..39b124fb48 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -2073,6 +2073,9 @@ typedef struct { char *platform; /**< What software/operating system is this OR using? */ + char *protocol_list; /**< Encoded list of subprotocol versions supported + * by this OR */ + /* link info */ uint32_t bandwidthrate; /**< How many bytes does this OR add to its token * bucket per second? */ diff --git a/src/or/router.c b/src/or/router.c index b664a88760..7f227b5bec 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -23,6 +23,7 @@ #include "networkstatus.h" #include "nodelist.h" #include "policies.h" +#include "protover.h" #include "relay.h" #include "rephist.h" #include "router.h" @@ -2124,6 +2125,8 @@ router_build_fresh_descriptor(routerinfo_t **r, extrainfo_t **e) get_platform_str(platform, sizeof(platform)); ri->platform = tor_strdup(platform); + ri->protocol_list = tor_strdup(get_supported_protocols()); + /* compute ri->bandwidthrate as the min of various options */ ri->bandwidthrate = get_effective_bwrate(options); @@ -2616,6 +2619,7 @@ router_dump_router_to_string(routerinfo_t *router, char *ed_cert_line = NULL; char *rsa_tap_cc_line = NULL; char *ntor_cc_line = NULL; + char *proto_line = NULL; /* Make sure the identity key matches the one in the routerinfo. */ if (!crypto_pk_eq_keys(ident_key, router->identity_pkey)) { @@ -2780,6 +2784,12 @@ router_dump_router_to_string(routerinfo_t *router, } } + if (router->protocol_list) { + tor_asprintf(&proto_line, "proto %s\n", router->protocol_list); + } else { + proto_line = tor_strdup(""); + } + address = tor_dup_ip(router->addr); chunks = smartlist_new(); @@ -2789,7 +2799,7 @@ router_dump_router_to_string(routerinfo_t *router, "%s" "%s" "platform %s\n" - "protocols Link 1 2 Circuit 1\n" + "%s" "published %s\n" "fingerprint %s\n" "uptime %ld\n" @@ -2806,6 +2816,7 @@ router_dump_router_to_string(routerinfo_t *router, ed_cert_line ? ed_cert_line : "", extra_or_address ? extra_or_address : "", router->platform, + proto_line, published, fingerprint, stats_n_seconds_working, diff --git a/src/or/routerlist.c b/src/or/routerlist.c index 74b8d1b1d3..56e2385ebe 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -3102,6 +3102,7 @@ routerinfo_free(routerinfo_t *router) tor_free(router->cache_info.signed_descriptor_body); tor_free(router->nickname); tor_free(router->platform); + tor_free(router->protocol_list); tor_free(router->contact_info); if (router->onion_pkey) crypto_pk_free(router->onion_pkey); diff --git a/src/or/routerparse.c b/src/or/routerparse.c index 03f8f4eded..abad6a8231 100644 --- a/src/or/routerparse.c +++ b/src/or/routerparse.c @@ -58,6 +58,7 @@ typedef enum { K_RUNNING_ROUTERS, K_ROUTER_STATUS, K_PLATFORM, + K_PROTO, K_OPT, K_BANDWIDTH, K_CONTACT, @@ -306,6 +307,7 @@ static token_rule_t routerdesc_token_table[] = { T01("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ), T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ ), T01("platform", K_PLATFORM, CONCAT_ARGS, NO_OBJ ), + T01("proto", K_PROTO, CONCAT_ARGS, NO_OBJ ), T01("contact", K_CONTACT, CONCAT_ARGS, NO_OBJ ), T01("read-history", K_READ_HISTORY, ARGS, NO_OBJ ), T01("write-history", K_WRITE_HISTORY, ARGS, NO_OBJ ), @@ -2092,6 +2094,10 @@ router_parse_entry_from_string(const char *s, const char *end, router->platform = tor_strdup(tok->args[0]); } + if ((tok = find_opt_by_keyword(tokens, K_PROTO))) { + router->protocol_list = tor_strdup(tok->args[0]); + } + if ((tok = find_opt_by_keyword(tokens, K_CONTACT))) { router->contact_info = tor_strdup(tok->args[0]); } diff --git a/src/test/test_dir.c b/src/test/test_dir.c index 5d9ae3c8a1..617f6ff344 100644 --- a/src/test/test_dir.c +++ b/src/test/test_dir.c @@ -231,7 +231,6 @@ test_dir_formats(void *arg) "platform Tor "VERSION" on ", sizeof(buf2)); strlcat(buf2, get_uname(), sizeof(buf2)); strlcat(buf2, "\n" - "protocols Link 1 2 Circuit 1\n" "published 1970-01-01 00:00:00\n" "fingerprint ", sizeof(buf2)); tt_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1)); @@ -300,7 +299,6 @@ test_dir_formats(void *arg) strlcat(buf2, "platform Tor "VERSION" on ", sizeof(buf2)); strlcat(buf2, get_uname(), sizeof(buf2)); strlcat(buf2, "\n" - "protocols Link 1 2 Circuit 1\n" "published 1970-01-01 00:00:05\n" "fingerprint ", sizeof(buf2)); tt_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1)); -- cgit v1.2.3-54-g00ecf From c1be8f9d574dace9941c532ce5d612c315bc74c7 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 19 Aug 2016 18:04:02 -0400 Subject: Basic backend for the protocol-versions voting algorithm. [This is a brute-force method that potentially uses way too much RAM. Need to rethink this a little. Right now you can DOS an authority by saying "Foo=1-4294967295".] --- src/or/protover.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++ src/or/protover.h | 3 + src/test/test_protover.c | 31 +++++++ 3 files changed, 251 insertions(+) (limited to 'src/test') diff --git a/src/or/protover.c b/src/or/protover.c index b78978e628..3a2d4014cd 100644 --- a/src/or/protover.c +++ b/src/or/protover.c @@ -309,6 +309,223 @@ encode_protocol_list(const smartlist_t *sl) return result; } +/** Voting helper: Given a list of proto_entry_t, return a newly allocated + * smartlist of newly allocated strings, one for each included protocol + * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6', + * 'Foo=7'.) + * + * Do not list any protocol version more than once. */ +static smartlist_t * +expand_protocol_list(const smartlist_t *protos) +{ + // XXXX This can make really huge lists from small inputs; that's a DoS + // problem. + + smartlist_t *expanded = smartlist_new(); + if (!protos) + return expanded; + + SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) { + const char *name = ent->name; + SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) { + uint32_t u; + for (u = range->low; u <= range->high; ++u) { + smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u); + } + } SMARTLIST_FOREACH_END(range); + } SMARTLIST_FOREACH_END(ent); + + smartlist_sort_strings(expanded); + smartlist_uniq_strings(expanded); // This makes voting work. do not remove + return expanded; +} + +/** Voting helper: compare two singleton proto_entry_t items by version + * alone. (A singleton item is one with a single range entry where + * low==high.) */ +static int +cmp_single_ent_by_version(const void **a_, const void **b_) +{ + const proto_entry_t *ent_a = *a_; + const proto_entry_t *ent_b = *b_; + + tor_assert(smartlist_len(ent_a->ranges) == 1); + tor_assert(smartlist_len(ent_b->ranges) == 1); + + const proto_range_t *a = smartlist_get(ent_a->ranges, 0); + const proto_range_t *b = smartlist_get(ent_b->ranges, 0); + + tor_assert(a->low == a->high); + tor_assert(b->low == b->high); + + if (a->low < b->low) { + return -1; + } else if (a->low == b->low) { + return 0; + } else { + return 1; + } +} + +/** Voting helper: Given a list of singleton protocol strings (of the form + * Foo=7), return a canonical listing of all the protocol versions listed, + * with as few ranges as possible, with protocol versions sorted lexically and + * versions sorted in numerically increasing order, using as few range entries + * as possible. + **/ +static char * +contract_protocol_list(const smartlist_t *proto_strings) +{ + // map from name to list of single-version entries + strmap_t *entry_lists_by_name = strmap_new(); + // list of protocol names + smartlist_t *all_names = smartlist_new(); + // list of strings for the output we're building + smartlist_t *chunks = smartlist_new(); + + // Parse each item and stick it entry_lists_by_name. Build + // 'all_names' at the same time. + SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) { + proto_entry_t *ent = parse_single_entry(s, s+strlen(s)); + if (BUG(!ent)) + continue; + smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name); + if (!lst) { + smartlist_add(all_names, ent->name); + lst = smartlist_new(); + strmap_set(entry_lists_by_name, ent->name, lst); + } + smartlist_add(lst, ent); + } SMARTLIST_FOREACH_END(s); + + // We want to output the protocols sorted by their name. + smartlist_sort_strings(all_names); + + SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) { + const int first_entry = (name_sl_idx == 0); + smartlist_t *lst = strmap_get(entry_lists_by_name, name); + tor_assert(lst); + // Sort every entry with this name by version. They are + // singletons, so there can't be overlap. + smartlist_sort(lst, cmp_single_ent_by_version); + + if (! first_entry) + smartlist_add(chunks, tor_strdup(" ")); + + /* We're going to construct this entry from the ranges. */ + proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t)); + entry->ranges = smartlist_new(); + entry->name = tor_strdup(name); + + // Now, find all the ranges of versions start..end where + // all of start, start+1, start+2, ..end are included. + int start_of_cur_series = 0; + while (start_of_cur_series < smartlist_len(lst)) { + const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series); + const proto_range_t *range = smartlist_get(ent->ranges, 0); + const uint32_t ver_low = range->low; + uint32_t ver_high = ver_low; + + int idx; + for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) { + ent = smartlist_get(lst, idx); + range = smartlist_get(ent->ranges, 0); + if (range->low != ver_high + 1) + break; + ver_high += 1; + } + + // Now idx is either off the end of the list, or the first sequence + // break in the list. + start_of_cur_series = idx; + + proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t)); + new_range->low = ver_low; + new_range->high = ver_high; + smartlist_add(entry->ranges, new_range); + } + proto_entry_encode_into(chunks, entry); + proto_entry_free(entry); + + } SMARTLIST_FOREACH_END(name); + + // Build the result... + char *result = smartlist_join_strings(chunks, "", 0, NULL); + + // And free all the stuff we allocated. + SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) { + smartlist_t *lst = strmap_get(entry_lists_by_name, name); + tor_assert(lst); + SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e)); + smartlist_free(lst); + } SMARTLIST_FOREACH_END(name); + + strmap_free(entry_lists_by_name, NULL); + smartlist_free(all_names); + SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp)); + smartlist_free(chunks); + + return result; +} + +/** + * Protocol voting implementation. + * + * Given a list of strings describing protocol versions, return a newly + * allocated string encoding all of the protocols that are listed by at + * least threshold of the inputs. + * + * The string is minimal and sorted according to the rules of + * contract_protocol_list above. + */ +char * +compute_protover_vote(const smartlist_t *list_of_proto_strings, + int threshold) +{ + // XXXX This algorithm can be made to use too much RAM. Fix that. + + smartlist_t *all_entries = smartlist_new(); + + // First, parse the inputs and break them into singleton entries. + SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) { + smartlist_t *unexpanded = parse_protocol_list(vote); + smartlist_t *this_vote = expand_protocol_list(unexpanded); + smartlist_add_all(all_entries, this_vote); + smartlist_free(this_vote); + SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e)); + smartlist_free(unexpanded); + } SMARTLIST_FOREACH_END(vote); + + // Now sort the singleton entries + smartlist_sort_strings(all_entries); + + // Now find all the strings that appear at least 'threshold' times. + smartlist_t *include_entries = smartlist_new(); + const char *cur_entry = smartlist_get(all_entries, 0); + int n_times = 0; + SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) { + if (!strcmp(ent, cur_entry)) { + n_times++; + } else { + if (n_times >= threshold) + smartlist_add(include_entries, (void*)cur_entry); + cur_entry = ent; + n_times = 1 ; + } + } SMARTLIST_FOREACH_END(ent); + + if (n_times >= threshold) + smartlist_add(include_entries, (void*)cur_entry); + + // Finally, compress that list. + char *result = contract_protocol_list(include_entries); + smartlist_free(include_entries); + SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp)); + smartlist_free(all_entries); + + return result; +} + /** Return true if every protocol version described in the string s is * one that we support, and false otherwise. If missing_out is * provided, set it to the list of protocols we do not support. diff --git a/src/or/protover.h b/src/or/protover.h index d12a067578..f809a8dab8 100644 --- a/src/or/protover.h +++ b/src/or/protover.h @@ -27,6 +27,9 @@ int protover_all_supported(const char *s, char **missing); int protover_is_supported_here(protocol_type_t pr, uint32_t ver); const char *get_supported_protocols(void); +char * compute_protover_vote(const smartlist_t *list_of_proto_strings, + int threshold); + void protover_free_all(void); #ifdef PROTOVER_PRIVATE diff --git a/src/test/test_protover.c b/src/test/test_protover.c index 606ca57b1b..2c72282a5c 100644 --- a/src/test/test_protover.c +++ b/src/test/test_protover.c @@ -80,12 +80,43 @@ test_protover_parse(void *arg) tor_free(re_encoded); } +static void +test_protover_vote(void *arg) +{ + (void) arg; + + smartlist_t *lst = smartlist_new(); + char *result = compute_protover_vote(lst, 1); + + tt_str_op(result, OP_EQ, ""); + tor_free(result); + + smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8"); + result = compute_protover_vote(lst, 1); + tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500"); + tor_free(result); + + smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9"); + result = compute_protover_vote(lst, 1); + tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456"); + tor_free(result); + + result = compute_protover_vote(lst, 2); + tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9"); + tor_free(result); + + done: + tor_free(result); + smartlist_free(lst); +} + #define PV_TEST(name, flags) \ { #name, test_protover_ ##name, (flags), NULL, NULL } struct testcase_t protover_tests[] = { PV_TEST(parse, 0), + PV_TEST(vote, 0), END_OF_TESTCASES }; -- cgit v1.2.3-54-g00ecf From 0697e413efa32b71d080353e5151beee117fca04 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 23 Aug 2016 13:47:14 -0400 Subject: Unit tests for protover_all_supported --- src/test/test_protover.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/test') diff --git a/src/test/test_protover.c b/src/test/test_protover.c index 2c72282a5c..651d2fd52d 100644 --- a/src/test/test_protover.c +++ b/src/test/test_protover.c @@ -110,6 +110,44 @@ test_protover_vote(void *arg) smartlist_free(lst); } +static void +test_protover_all_supported(void *arg) +{ + (void)arg; + char *msg = NULL; + + tt_assert(protover_all_supported(NULL, &msg)); + tt_assert(msg == NULL); + + tt_assert(protover_all_supported("", &msg)); + tt_assert(msg == NULL); + + // Some things that we do support + tt_assert(protover_all_supported("Link=3-4", &msg)); + tt_assert(msg == NULL); + tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg)); + tt_assert(msg == NULL); + + // Some things we don't support + tt_assert(! protover_all_supported("Wombat=9", &msg)); + tt_str_op(msg, OP_EQ, "Wombat=9"); + tor_free(msg); + tt_assert(! protover_all_supported("Link=999", &msg)); + tt_str_op(msg, OP_EQ, "Link=999"); + tor_free(msg); + + // Mix of things we support and things we don't + tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg)); + tt_str_op(msg, OP_EQ, "Wombat=9"); + tor_free(msg); + tt_assert(! protover_all_supported("Link=3-999", &msg)); + tt_str_op(msg, OP_EQ, "Link=3-999"); + tor_free(msg); + + done: + tor_free(msg); +} + #define PV_TEST(name, flags) \ { #name, test_protover_ ##name, (flags), NULL, NULL } @@ -117,6 +155,7 @@ test_protover_vote(void *arg) struct testcase_t protover_tests[] = { PV_TEST(parse, 0), PV_TEST(vote, 0), + PV_TEST(all_supported, 0), END_OF_TESTCASES }; -- cgit v1.2.3-54-g00ecf From a232161f7beeecebf31b2259a571e8b26cb0b541 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 23 Aug 2016 14:02:48 -0400 Subject: Cover the error cases of parsing protocol versions Also, detect an additional failure type. Thanks, tests! (How distinctly I recall thee) --- src/or/protover.c | 12 +++++++++--- src/test/test_protover.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) (limited to 'src/test') diff --git a/src/or/protover.c b/src/or/protover.c index 3a2d4014cd..2bde02c6ae 100644 --- a/src/or/protover.c +++ b/src/or/protover.c @@ -39,8 +39,10 @@ protocol_type_to_str(protocol_type_t pr) if (PROTOCOL_NAMES[i].protover_type == pr) return PROTOCOL_NAMES[i].name; } + /* LCOV_EXCL_START */ tor_assert_nonfatal_unreached_once(); return "UNKNOWN"; + /* LCOV_EXCL_STOP */ } /** @@ -150,6 +152,10 @@ parse_single_entry(const char *s, const char *end_of_entry) if (!equals) goto error; + /* The name must be nonempty */ + if (equals == s) + goto error; + out->name = tor_strndup(s, equals-s); tor_assert(equals < end_of_entry); @@ -388,7 +394,7 @@ contract_protocol_list(const smartlist_t *proto_strings) SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) { proto_entry_t *ent = parse_single_entry(s, s+strlen(s)); if (BUG(!ent)) - continue; + continue; // LCOV_EXCL_LINE smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name); if (!lst) { smartlist_add(all_names, ent->name); @@ -591,11 +597,11 @@ protocol_list_contains(const smartlist_t *protos, protocol_type_t pr, uint32_t ver) { if (BUG(protos == NULL)) { - return 0; + return 0; // LCOV_EXCL_LINE } const char *pr_name = protocol_type_to_str(pr); if (BUG(pr_name == NULL)) { - return 0; + return 0; // LCOV_EXCL_LINE } SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) { diff --git a/src/test/test_protover.c b/src/test/test_protover.c index 651d2fd52d..c86c3b2f69 100644 --- a/src/test/test_protover.c +++ b/src/test/test_protover.c @@ -80,6 +80,41 @@ test_protover_parse(void *arg) tor_free(re_encoded); } +static void +test_protover_parse_fail(void *arg) +{ + (void)arg; + smartlist_t *elts; + + /* random junk */ + elts = parse_protocol_list("!!3@*"); + tt_assert(elts == NULL); + + /* Missing equals sign in an entry */ + elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9"); + tt_assert(elts == NULL); + + /* Missing word. */ + elts = parse_protocol_list("Link=4 =3 Desc=9"); + tt_assert(elts == NULL); + + /* Broken numbers */ + elts = parse_protocol_list("Link=fred"); + tt_assert(elts == NULL); + elts = parse_protocol_list("Link=1,fred"); + tt_assert(elts == NULL); + elts = parse_protocol_list("Link=1,fred,3"); + tt_assert(elts == NULL); + + /* Broken range */ + elts = parse_protocol_list("Link=1,9-8,3"); + tt_assert(elts == NULL); + + done: + ; +} + + static void test_protover_vote(void *arg) { @@ -154,6 +189,7 @@ test_protover_all_supported(void *arg) struct testcase_t protover_tests[] = { PV_TEST(parse, 0), + PV_TEST(parse_fail, 0), PV_TEST(vote, 0), PV_TEST(all_supported, 0), END_OF_TESTCASES -- cgit v1.2.3-54-g00ecf From e402cddefeaabc6d65a5efe6f1e0e958d40aa013 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 26 Aug 2016 13:09:26 -0400 Subject: Clean whitespace, add missing documentation --- src/or/protover.c | 4 +++- src/or/protover.h | 25 ++++++++++++++++--------- src/test/test_protover.c | 2 -- 3 files changed, 19 insertions(+), 12 deletions(-) (limited to 'src/test') diff --git a/src/or/protover.c b/src/or/protover.c index 66d20c5851..d29cdba099 100644 --- a/src/or/protover.c +++ b/src/or/protover.c @@ -620,7 +620,6 @@ protover_all_supported(const char *s, char **missing_out) smartlist_add(missing, (void*) ent); } SMARTLIST_FOREACH_END(ent); - if (missing_out && !all_supported) { tor_assert(0 != smartlist_len(missing)); *missing_out = encode_protocol_list(missing); @@ -633,6 +632,8 @@ protover_all_supported(const char *s, char **missing_out) return all_supported; } +/** Helper: Given a list of proto_entry_t, return true iff + * pr=ver is included in that list. */ static int protocol_list_contains(const smartlist_t *protos, protocol_type_t pr, uint32_t ver) @@ -697,3 +698,4 @@ protover_free_all(void) supported_protocol_list = NULL; } } + diff --git a/src/or/protover.h b/src/or/protover.h index 352fa7cbe8..520db37de2 100644 --- a/src/or/protover.h +++ b/src/or/protover.h @@ -4,9 +4,13 @@ #include "container.h" +/** The first version of Tor that included "proto" entries in its + * descriptors. Authorities should use this to decide whether to + * guess proto lines. */ /* This is a guess. */ #define FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS "0.2.9.3-alpha" +/** List of recognized subprotocols. */ typedef enum protocol_type_t { PRT_LINK, PRT_LINKAUTH, @@ -19,19 +23,12 @@ typedef enum protocol_type_t { PRT_CONS, } protocol_type_t; -/* -const protover_set_t *protover_get_supported(void); -protover_set_t *protover_set_parse(const char *s); -int protover_is_supported_here_str(const char *name, uint32_t ver); -int protover_is_supported_by(protocol_type_t pr, uint32_t ver); -*/ - int protover_all_supported(const char *s, char **missing); int protover_is_supported_here(protocol_type_t pr, uint32_t ver); const char *get_supported_protocols(void); -char * compute_protover_vote(const smartlist_t *list_of_proto_strings, - int threshold); +char *compute_protover_vote(const smartlist_t *list_of_proto_strings, + int threshold); const char *protover_compute_for_old_tor(const char *version); int protocol_list_supports_protocol(const char *list, protocol_type_t tp, uint32_t version); @@ -39,13 +36,22 @@ int protocol_list_supports_protocol(const char *list, protocol_type_t tp, void protover_free_all(void); #ifdef PROTOVER_PRIVATE +/** Represents a range of subprotocols of a given type. All subprotocols + * between low and high inclusive are included. */ typedef struct proto_range_t { uint32_t low; uint32_t high; } proto_range_t; +/** Represents a set of ranges of subprotocols of a given type. */ typedef struct proto_entry_t { + /** The name of the protocol. + * + * (This needs to handle voting on protocols which + * we don't recognize yet, so it's a char* rather than a protocol_type_t.) + */ char *name; + /** Smartlist of proto_range_t */ smartlist_t *ranges; } proto_entry_t; @@ -57,3 +63,4 @@ STATIC int str_to_protocol_type(const char *s, protocol_type_t *pr_out); #endif #endif + diff --git a/src/test/test_protover.c b/src/test/test_protover.c index c86c3b2f69..eacbbcb81d 100644 --- a/src/test/test_protover.c +++ b/src/test/test_protover.c @@ -114,7 +114,6 @@ test_protover_parse_fail(void *arg) ; } - static void test_protover_vote(void *arg) { @@ -183,7 +182,6 @@ test_protover_all_supported(void *arg) tor_free(msg); } - #define PV_TEST(name, flags) \ { #name, test_protover_ ##name, (flags), NULL, NULL } -- cgit v1.2.3-54-g00ecf From 3a3120819c8f600ef7d22ddf3f5476791c7e1d9b Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 12 Sep 2016 14:11:44 -0400 Subject: Rename compute_protover_vote to protover_compute_vote --- src/or/dirvote.c | 2 +- src/or/protover.c | 2 +- src/or/protover.h | 2 +- src/test/test_protover.c | 8 ++++---- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/test') diff --git a/src/or/dirvote.c b/src/or/dirvote.c index f606b07f7a..c65243cf09 100644 --- a/src/or/dirvote.c +++ b/src/or/dirvote.c @@ -1256,7 +1256,7 @@ compute_nth_protocol_set(int n, int n_voters, const smartlist_t *votes) smartlist_add(proto_votes, (void*)v); } SMARTLIST_FOREACH_END(ns); - char *protocols = compute_protover_vote(proto_votes, threshold); + char *protocols = protover_compute_vote(proto_votes, threshold); smartlist_free(proto_votes); char *result = NULL; diff --git a/src/or/protover.c b/src/or/protover.c index d29cdba099..434d3f36be 100644 --- a/src/or/protover.c +++ b/src/or/protover.c @@ -520,7 +520,7 @@ contract_protocol_list(const smartlist_t *proto_strings) * contract_protocol_list above. */ char * -compute_protover_vote(const smartlist_t *list_of_proto_strings, +protover_compute_vote(const smartlist_t *list_of_proto_strings, int threshold) { smartlist_t *all_entries = smartlist_new(); diff --git a/src/or/protover.h b/src/or/protover.h index 520db37de2..654127ee54 100644 --- a/src/or/protover.h +++ b/src/or/protover.h @@ -27,7 +27,7 @@ int protover_all_supported(const char *s, char **missing); int protover_is_supported_here(protocol_type_t pr, uint32_t ver); const char *get_supported_protocols(void); -char *compute_protover_vote(const smartlist_t *list_of_proto_strings, +char *protover_compute_vote(const smartlist_t *list_of_proto_strings, int threshold); const char *protover_compute_for_old_tor(const char *version); int protocol_list_supports_protocol(const char *list, protocol_type_t tp, diff --git a/src/test/test_protover.c b/src/test/test_protover.c index eacbbcb81d..f00955d1b4 100644 --- a/src/test/test_protover.c +++ b/src/test/test_protover.c @@ -120,22 +120,22 @@ test_protover_vote(void *arg) (void) arg; smartlist_t *lst = smartlist_new(); - char *result = compute_protover_vote(lst, 1); + char *result = protover_compute_vote(lst, 1); tt_str_op(result, OP_EQ, ""); tor_free(result); smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8"); - result = compute_protover_vote(lst, 1); + result = protover_compute_vote(lst, 1); tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500"); tor_free(result); smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9"); - result = compute_protover_vote(lst, 1); + result = protover_compute_vote(lst, 1); tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456"); tor_free(result); - result = compute_protover_vote(lst, 2); + result = protover_compute_vote(lst, 2); tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9"); tor_free(result); -- cgit v1.2.3-54-g00ecf