static void test_container_smartlist_sort_ptrs(void *arg) { (void)arg; int array[10]; int *arrayptrs[11]; smartlist_t *sl = smartlist_new(); unsigned i=0, j; for (j = 0; j < ARRAY_LENGTH(array); ++j) { smartlist_add(sl, &array[j]); arrayptrs[i++] = &array[j]; if (j == 5) { smartlist_add(sl, &array[j]); arrayptrs[i++] = &array[j]; } } for (i = 0; i < 10; ++i) { smartlist_shuffle(sl); smartlist_sort_pointers(sl); for (j = 0; j < ARRAY_LENGTH(arrayptrs); ++j) { tt_ptr_op(smartlist_get(sl, j), ==, arrayptrs[j]); } } done: smartlist_free(sl); }
static smartlist_t * descbr_get_digests_mock(void) { char digest[DIGEST_LEN], *tmp; int len; smartlist_t *list = NULL; if (!disable_descbr) { /* Just pretend we have only the two hard-coded digests listed above */ list = smartlist_new(); len = base16_decode(digest, DIGEST_LEN, descbr_digest_1_str, strlen(descbr_digest_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(digest, DIGEST_LEN, descbr_digest_2_str, strlen(descbr_digest_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); } done: return list; }
/** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>. */ static void geoip_add_entry(uint32_t low, uint32_t high, const char *country) { intptr_t idx; geoip_entry_t *ent; void *idxplus1_; if (high < low) return; idxplus1_ = strmap_get_lc(country_idxplus1_by_lc_code, country); if (!idxplus1_) { geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t)); strlcpy(c->countrycode, country, sizeof(c->countrycode)); tor_strlower(c->countrycode); smartlist_add(geoip_countries, c); idx = smartlist_len(geoip_countries) - 1; strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1)); } else { idx = ((uintptr_t)idxplus1_)-1; } { geoip_country_t *c = smartlist_get(geoip_countries, idx); tor_assert(!strcasecmp(c->countrycode, country)); } ent = tor_malloc_zero(sizeof(geoip_entry_t)); ent->ip_low = low; ent->ip_high = high; ent->country = idx; smartlist_add(geoip_entries, ent); }
/** Write the <b>datalen</b> bytes from <b>data</b> to the file named * <b>fname</b> in the tagged-data format. This format contains a * 32-byte header, followed by the data itself. The header is the * NUL-padded string "== <b>typestring</b>: <b>tag</b> ==". The length * of <b>typestring</b> and <b>tag</b> must therefore be no more than * 24. **/ int crypto_write_tagged_contents_to_file(const char *fname, const char *typestring, const char *tag, const uint8_t *data, size_t datalen) { char header[32]; smartlist_t *chunks = smartlist_new(); sized_chunk_t ch0, ch1; int r = -1; memset(header, 0, sizeof(header)); if (tor_snprintf(header, sizeof(header), "== %s: %s ==", typestring, tag) < 0) goto end; ch0.bytes = header; ch0.len = 32; ch1.bytes = (const char*) data; ch1.len = datalen; smartlist_add(chunks, &ch0); smartlist_add(chunks, &ch1); r = write_chunks_to_file(fname, chunks, 1, 0); end: smartlist_free(chunks); return r; }
static smartlist_t * cert_dl_status_auth_ids_mock(void) { char digest[DIGEST_LEN], *tmp; int len; smartlist_t *list = NULL; /* Just pretend we have only the two hard-coded digests listed above */ list = smartlist_new(); len = base16_decode(digest, DIGEST_LEN, auth_id_digest_1_str, strlen(auth_id_digest_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(digest, DIGEST_LEN, auth_id_digest_2_str, strlen(auth_id_digest_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); done: return list; }
/** Run unit tests for smartlist-of-digests functions. */ static void test_container_smartlist_digests(void) { smartlist_t *sl = smartlist_new(); /* contains_digest */ smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN)); smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN)); smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN)); test_eq(0, smartlist_contains_digest(NULL, "AAAAAAAAAAAAAAAAAAAA")); test_assert(smartlist_contains_digest(sl, "AAAAAAAAAAAAAAAAAAAA")); test_assert(smartlist_contains_digest(sl, "\00090AAB2AAAAaasdAAAAA")); test_eq(0, smartlist_contains_digest(sl, "\00090AAB2AAABaasdAAAAA")); /* sort digests */ smartlist_sort_digests(sl); test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN); test_eq(3, smartlist_len(sl)); /* uniq_digests */ smartlist_uniq_digests(sl); test_eq(2, smartlist_len(sl)); test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN); done: SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); smartlist_free(sl); }
/** Run unit tests for smartlist-of-digests functions. */ static void test_container_smartlist_digests(void *unused) { smartlist_t *sl = smartlist_create(); /* digest_isin. */ smartlist_add(sl, xmemdup("AAAAAAAAAAAAAAAAAAAA", SHA256_LENGTH)); smartlist_add(sl, xmemdup("\00090AAB2AAAAaasdAAAAA", SHA256_LENGTH)); smartlist_add(sl, xmemdup("\00090AAB2AAAAaasdAAAAA", SHA256_LENGTH)); tt_int_op(smartlist_digest_isin(NULL, "AAAAAAAAAAAAAAAAAAAA"), ==, 0); tt_assert(smartlist_digest_isin(sl, "AAAAAAAAAAAAAAAAAAAA")); tt_assert(smartlist_digest_isin(sl, "\00090AAB2AAAAaasdAAAAA")); tt_int_op(smartlist_digest_isin(sl, "\00090AAB2AAABaasdAAAAA"), ==, 0); /* sort digests */ smartlist_sort_digests(sl); tt_mem_op(smartlist_get(sl, 0), ==, "\00090AAB2AAAAaasdAAAAA", SHA256_LENGTH); tt_mem_op(smartlist_get(sl, 1), ==, "\00090AAB2AAAAaasdAAAAA", SHA256_LENGTH); tt_mem_op(smartlist_get(sl, 2), ==, "AAAAAAAAAAAAAAAAAAAA", SHA256_LENGTH); tt_int_op(smartlist_len(sl), ==, 3); /* uniq_digests */ smartlist_uniq_digests(sl); tt_int_op(smartlist_len(sl), ==, 2); tt_mem_op(smartlist_get(sl, 0), ==, "\00090AAB2AAAAaasdAAAAA", SHA256_LENGTH); tt_mem_op(smartlist_get(sl, 1), ==, "AAAAAAAAAAAAAAAAAAAA", SHA256_LENGTH); end: SMARTLIST_FOREACH(sl, char *, cp, free(cp)); smartlist_free(sl); }
/* Given the previous SRV and the current SRV, return a heap allocated * string with their data that could be put in a vote or a consensus. Caller * must free the returned string. Return NULL if no SRVs were provided. */ static char * get_ns_str_from_sr_values(const sr_srv_t *prev_srv, const sr_srv_t *cur_srv) { smartlist_t *chunks = NULL; char *srv_str; if (!prev_srv && !cur_srv) { return NULL; } chunks = smartlist_new(); if (prev_srv) { char *srv_line = srv_to_ns_string(prev_srv, previous_srv_str); smartlist_add(chunks, srv_line); } if (cur_srv) { char *srv_line = srv_to_ns_string(cur_srv, current_srv_str); smartlist_add(chunks, srv_line); } /* Join the line(s) here in one string to return. */ srv_str = smartlist_join_strings(chunks, "", 0, NULL); SMARTLIST_FOREACH(chunks, char *, s, tor_free(s)); smartlist_free(chunks); return srv_str; }
static void NS(test_main)(void *arg) { /* Choose origin_circuit_t wlog. */ origin_circuit_t *mock_circuit1, *mock_circuit2; int expected_circuits = 2, actual_circuits; (void)arg; mock_circuit1 = tor_malloc_zero(sizeof(origin_circuit_t)); mock_circuit2 = tor_malloc_zero(sizeof(origin_circuit_t)); mock_global_circuitlist = smartlist_new(); smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit1)); smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit2)); NS_MOCK(circuit_get_global_list); actual_circuits = count_circuits(); tt_assert(expected_circuits == actual_circuits); done: tor_free(mock_circuit1); tor_free(mock_circuit2); smartlist_free(mock_global_circuitlist); mock_global_circuitlist = NULL; NS_UNMOCK(circuit_get_global_list); }
static void NS(test_main)(void *arg) { routerset_t *set = routerset_new(); smartlist_t *out = smartlist_new(); int r; (void)arg; NS_MOCK(nodelist_get_list); smartlist_add(set->country_names, tor_strdup("{xx}")); NS(mock_smartlist) = smartlist_new(); NS(mock_node).is_running = 0; smartlist_add(NS(mock_smartlist), (void *)&NS(mock_node)); routerset_get_all_nodes(out, set, NULL, 1); r = smartlist_len(out); routerset_free(set); smartlist_free(out); smartlist_free(NS(mock_smartlist)); tt_int_op(r, ==, 0); tt_int_op(CALLED(nodelist_get_list), ==, 1); done: ; }
/** Replace all "private" entries in *<b>policy</b> with their expanded * equivalents. */ void policy_expand_private(smartlist_t **policy) { uint16_t port_min, port_max; int i; smartlist_t *tmp; if (!*policy) /*XXXX disallow NULL policies? */ return; tmp = smartlist_create(); SMARTLIST_FOREACH(*policy, addr_policy_t *, p, { if (! p->is_private) { smartlist_add(tmp, p); continue; } for (i = 0; private_nets[i]; ++i) { addr_policy_t policy; memcpy(&policy, p, sizeof(addr_policy_t)); policy.is_private = 0; policy.is_canonical = 0; if (tor_addr_parse_mask_ports(private_nets[i], &policy.addr, &policy.maskbits, &port_min, &port_max)<0) { tor_assert(0); } smartlist_add(tmp, addr_policy_get_canonical_entry(&policy)); } addr_policy_free(p); });
/** Run unit tests for basic dynamic-sized array functionality. */ static void test_container_smartlist_basic(void *arg) { smartlist_t *sl; char *v0 = tor_strdup("v0"); char *v1 = tor_strdup("v1"); char *v2 = tor_strdup("v2"); char *v3 = tor_strdup("v3"); char *v4 = tor_strdup("v4"); char *v22 = tor_strdup("v22"); char *v99 = tor_strdup("v99"); char *v555 = tor_strdup("v555"); /* XXXX test sort_digests, uniq_strings, uniq_digests */ /* Test smartlist add, del_keeporder, insert, get. */ (void)arg; sl = smartlist_new(); smartlist_add(sl, v1); smartlist_add(sl, v2); smartlist_add(sl, v3); smartlist_add(sl, v4); smartlist_del_keeporder(sl, 1); smartlist_insert(sl, 1, v22); smartlist_insert(sl, 0, v0); smartlist_insert(sl, 5, v555); tt_ptr_op(v0,OP_EQ, smartlist_get(sl,0)); tt_ptr_op(v1,OP_EQ, smartlist_get(sl,1)); tt_ptr_op(v22,OP_EQ, smartlist_get(sl,2)); tt_ptr_op(v3,OP_EQ, smartlist_get(sl,3)); tt_ptr_op(v4,OP_EQ, smartlist_get(sl,4)); tt_ptr_op(v555,OP_EQ, smartlist_get(sl,5)); /* Try deleting in the middle. */ smartlist_del(sl, 1); tt_ptr_op(v555,OP_EQ, smartlist_get(sl, 1)); /* Try deleting at the end. */ smartlist_del(sl, 4); tt_int_op(4,OP_EQ, smartlist_len(sl)); /* test isin. */ tt_assert(smartlist_contains(sl, v3)); tt_assert(!smartlist_contains(sl, v99)); done: smartlist_free(sl); tor_free(v0); tor_free(v1); tor_free(v2); tor_free(v3); tor_free(v4); tor_free(v22); tor_free(v99); tor_free(v555); }
static void test_oos_kill_conn_list(void *arg) { connection_t *c1, *c2; or_connection_t *or_c1 = NULL; dir_connection_t *dir_c2 = NULL; smartlist_t *l = NULL; (void)arg; /* Set up mocks */ mark_calls = 0; MOCK(connection_mark_for_close_internal_, mark_for_close_oos_mock); cfe_calls = 0; MOCK(connection_or_close_for_error, close_for_error_mock); /* Make fake conns */ or_c1 = tor_malloc_zero(sizeof(*or_c1)); or_c1->base_.magic = OR_CONNECTION_MAGIC; or_c1->base_.type = CONN_TYPE_OR; c1 = TO_CONN(or_c1); dir_c2 = tor_malloc_zero(sizeof(*dir_c2)); dir_c2->base_.magic = DIR_CONNECTION_MAGIC; dir_c2->base_.type = CONN_TYPE_DIR; dir_c2->base_.state = DIR_CONN_STATE_MIN_; dir_c2->base_.purpose = DIR_PURPOSE_MIN_; c2 = TO_CONN(dir_c2); tt_assert(c1 != NULL); tt_assert(c2 != NULL); /* Make list */ l = smartlist_new(); smartlist_add(l, c1); smartlist_add(l, c2); /* Run kill_conn_list_for_oos() */ kill_conn_list_for_oos(l); /* Check call counters */ tt_int_op(mark_calls, OP_EQ, 1); tt_int_op(cfe_calls, OP_EQ, 1); done: UNMOCK(connection_or_close_for_error); UNMOCK(connection_mark_for_close_internal_); if (l) smartlist_free(l); tor_free(or_c1); tor_free(dir_c2); return; }
static smartlist_t * cert_dl_status_sks_for_auth_id_mock(const char *digest) { smartlist_t *list = NULL; char sk[DIGEST_LEN]; char digest_str[HEX_DIGEST_LEN+1]; char *tmp; int len; tt_assert(digest != NULL); base16_encode(digest_str, HEX_DIGEST_LEN + 1, digest, DIGEST_LEN); digest_str[HEX_DIGEST_LEN] = '\0'; /* * Build a list of two hard-coded digests, depending on what we * were just passed. */ if (strcmp(digest_str, auth_id_digest_1_str) == 0) { list = smartlist_new(); len = base16_decode(sk, DIGEST_LEN, auth_1_sk_1_str, strlen(auth_1_sk_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(sk, DIGEST_LEN, auth_1_sk_2_str, strlen(auth_1_sk_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) { list = smartlist_new(); len = base16_decode(sk, DIGEST_LEN, auth_2_sk_1_str, strlen(auth_2_sk_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(sk, DIGEST_LEN, auth_2_sk_2_str, strlen(auth_2_sk_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); } done: return list; }
static void test_pt_get_transport_options(void *arg) { char **execve_args; smartlist_t *transport_list = smartlist_new(); managed_proxy_t *mp; or_options_t *options = get_options_mutable(); char *opt_str = NULL; config_line_t *cl = NULL; (void)arg; execve_args = tor_malloc(sizeof(char*)*2); execve_args[0] = tor_strdup("cheeseshop"); execve_args[1] = NULL; mp = managed_proxy_create(transport_list, execve_args, 1); tt_ptr_op(mp, OP_NE, NULL); opt_str = get_transport_options_for_server_proxy(mp); tt_ptr_op(opt_str, OP_EQ, NULL); smartlist_add(mp->transports_to_launch, tor_strdup("gruyere")); smartlist_add(mp->transports_to_launch, tor_strdup("roquefort")); smartlist_add(mp->transports_to_launch, tor_strdup("stnectaire")); tt_assert(options); cl = tor_malloc_zero(sizeof(config_line_t)); cl->value = tor_strdup("gruyere melty=10 hardness=se;ven"); options->ServerTransportOptions = cl; cl = tor_malloc_zero(sizeof(config_line_t)); cl->value = tor_strdup("stnectaire melty=4 hardness=three"); cl->next = options->ServerTransportOptions; options->ServerTransportOptions = cl; cl = tor_malloc_zero(sizeof(config_line_t)); cl->value = tor_strdup("pepperjack melty=12 hardness=five"); cl->next = options->ServerTransportOptions; options->ServerTransportOptions = cl; opt_str = get_transport_options_for_server_proxy(mp); tt_str_op(opt_str, OP_EQ, "gruyere:melty=10;gruyere:hardness=se\\;ven;" "stnectaire:melty=4;stnectaire:hardness=three"); done: tor_free(opt_str); config_free_lines(cl); managed_proxy_destroy(mp, 0); smartlist_free(transport_list); }
/** Parse the string <b>s</b> to create a set of routerset entries, and add * them to <b>target</b>. In log messages, refer to the string as * <b>description</b>. Return 0 on success, -1 on failure. * * Three kinds of elements are allowed in routersets: nicknames, IP address * patterns, and fingerprints. They may be surrounded by optional space, and * must be separated by commas. */ int routerset_parse(routerset_t *target, const char *s, const char *description) { int r = 0; int added_countries = 0; char *countryname; smartlist_t *list = smartlist_new(); int malformed_list; smartlist_split_string(list, s, ",", SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0); SMARTLIST_FOREACH_BEGIN(list, char *, nick) { addr_policy_t *p; /* if it doesn't pass our validation, assume it's malformed */ malformed_list = 1; if (is_legal_hexdigest(nick)) { char d[DIGEST_LEN]; if (*nick == '$') ++nick; log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description); base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN); digestmap_set(target->digests, d, (void*)1); } else if (is_legal_nickname(nick)) { log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description); strmap_set_lc(target->names, nick, (void*)1); } else if ((countryname = routerset_get_countryname(nick)) != NULL) { log_debug(LD_CONFIG, "Adding country %s to %s", nick, description); smartlist_add(target->country_names, countryname); added_countries = 1; } else if ((strchr(nick,'.') || strchr(nick, ':') || strchr(nick, '*')) && (p = router_parse_addr_policy_item_from_string( nick, ADDR_POLICY_REJECT, &malformed_list))) { /* IPv4 addresses contain '.', IPv6 addresses contain ':', * and wildcard addresses contain '*'. */ log_debug(LD_CONFIG, "Adding address %s to %s", nick, description); smartlist_add(target->policies, p); } else if (malformed_list) { log_warn(LD_CONFIG, "Entry '%s' in %s is malformed. Discarding entire" " list.", nick, description); r = -1; tor_free(nick); SMARTLIST_DEL_CURRENT(list, nick); } else { log_notice(LD_CONFIG, "Entry '%s' in %s is ignored. Using the" " remainder of the list.", nick, description); tor_free(nick); SMARTLIST_DEL_CURRENT(list, nick); } } SMARTLIST_FOREACH_END(nick);
/** Append to <b>out</b> all circuits in state OR_WAIT waiting for * the given connection. */ void circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn) { tor_assert(out); tor_assert(or_conn); if (!circuits_pending_or_conns) return; SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns, circuit_t *, circ) { if (circ->marked_for_close) continue; if (!circ->n_hop) continue; tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT); if (tor_digest_is_zero(circ->n_hop->identity_digest)) { /* Look at addr/port. This is an unkeyed connection. */ if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) || circ->n_hop->port != or_conn->_base.port) continue; } else { /* We expected a key. See if it's the right one. */ if (memcmp(or_conn->identity_digest, circ->n_hop->identity_digest, DIGEST_LEN)) continue; } smartlist_add(out, circ); } SMARTLIST_FOREACH_END(circ); }
/** Decode the hashed, base64'd passwords stored in <b>passwords</b>. * Return a smartlist of acceptable passwords (unterminated strings of * length S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN) on success, or NULL on * failure. */ smartlist_t * decode_hashed_passwords(config_line_t *passwords) { char decoded[64]; config_line_t *cl; smartlist_t *sl = smartlist_new(); tor_assert(passwords); for (cl = passwords; cl; cl = cl->next) { const char *hashed = cl->value; if (!strcmpstart(hashed, "16:")) { if (base16_decode(decoded, sizeof(decoded), hashed+3, strlen(hashed+3)) != S2K_RFC2440_SPECIFIER_LEN + DIGEST_LEN || strlen(hashed+3) != (S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN)*2) { goto err; } } else { if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed)) != S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN) { goto err; } } smartlist_add(sl, tor_memdup(decoded, S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN)); } return sl; err: SMARTLIST_FOREACH(sl, char*, cp, tor_free(cp)); smartlist_free(sl); return NULL; }
static void NS(test_main)(void *arg) { smartlist_t *out = smartlist_new(); routerset_t *set = routerset_new(); int out_len; node_t *ent; (void)arg; NS_MOCK(node_get_by_nickname); NS(mock_nickname) = tor_strdup("foo"); smartlist_add(set->list, NS(mock_nickname)); routerset_get_all_nodes(out, set, NULL, 0); out_len = smartlist_len(out); ent = (node_t *)smartlist_get(out, 0); smartlist_free(out); routerset_free(set); tt_int_op(out_len, ==, 1); tt_ptr_op(ent, ==, &NS(mock_node)); tt_int_op(CALLED(node_get_by_nickname), ==, 1); done: ; }
static void test_routerlist_initiate_descriptor_downloads(void *arg) { const char *prose = "unhurried and wise, we perceive."; smartlist_t *digests = smartlist_new(); (void)arg; for (int i = 0; i < 20; i++) { smartlist_add(digests, (char*)prose); } MOCK(directory_get_from_dirserver, mock_get_from_dirserver); initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC, digests, 3, 7, 0); UNMOCK(directory_get_from_dirserver); tt_str_op(output, OP_EQ, "d/" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4" ".z"); done: smartlist_free(digests); }
static void test_routerlist_launch_descriptor_downloads(void *arg) { smartlist_t *downloadable = smartlist_new(); time_t now = time(NULL); char *cp; (void)arg; for (int i = 0; i < 100; i++) { cp = tor_malloc(DIGEST256_LEN); tt_assert(cp); crypto_rand(cp, DIGEST256_LEN); smartlist_add(downloadable, cp); } MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads); launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable, NULL, now); tt_int_op(3, ==, count); UNMOCK(initiate_descriptor_downloads); done: SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1)); smartlist_free(downloadable); }
/* Using a list of <b>votes</b>, return the SRV object from them that has * been voted by the majority of dirauths. If <b>current</b> is set, we look * for the current SRV value else the previous one. The returned pointer is * an object located inside a vote. NULL is returned if no appropriate value * could be found. */ STATIC sr_srv_t * get_majority_srv_from_votes(const smartlist_t *votes, int current) { int count = 0; sr_srv_t *most_frequent_srv = NULL; sr_srv_t *the_srv = NULL; smartlist_t *srv_list; tor_assert(votes); srv_list = smartlist_new(); /* Walk over votes and register any SRVs found. */ SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) { sr_srv_t *srv_tmp = NULL; if (!v->sr_info.participate) { /* Ignore vote that do not participate. */ continue; } /* Do we want previous or current SRV? */ srv_tmp = current ? v->sr_info.current_srv : v->sr_info.previous_srv; if (!srv_tmp) { continue; } smartlist_add(srv_list, srv_tmp); } SMARTLIST_FOREACH_END(v);
static void NS(test_main)(void *arg) { routerset_t *set = routerset_new(); smartlist_t *list = smartlist_new(); const char *nickname = "foo"; routerinfo_t ri; node_t mock_node; (void)arg; strmap_set_lc(set->names, nickname, (void *)1); ri.nickname = (char *)nickname; mock_node.rs = NULL; mock_node.ri = &ri; smartlist_add(list, (void *)&mock_node); tt_int_op(smartlist_len(list), !=, 0); routerset_subtract_nodes(list, set); tt_int_op(smartlist_len(list), ==, 0); done: routerset_free(set); smartlist_free(list); }
static smartlist_t * pick_oos_victims_mock(int n) { smartlist_t *l = NULL; int i; ++pick_oos_mock_calls; tt_int_op(n, OP_GT, 0); if (!pick_oos_mock_fail) { /* * connection_check_oos() just passes the list onto * kill_conn_list_for_oos(); we don't need to simulate * its content for this mock, just its existence, but * we do need to check the parameter. */ l = smartlist_new(); for (i = 0; i < n; ++i) smartlist_add(l, NULL); } else { l = NULL; } pick_oos_mock_last_n = n; done: return l; }
static void NS(test_main)(void *arg) { routerset_t *set = routerset_new(); (void)arg; NS_MOCK(geoip_is_loaded); NS_MOCK(geoip_get_n_countries); NS_MOCK(geoip_get_country); smartlist_add(set->country_names, tor_strndup("foo", 3)); routerset_refresh_countries(set); tt_ptr_op(set->countries, !=, NULL); tt_int_op(set->n_countries, ==, 2); tt_int_op(CALLED(geoip_is_loaded), ==, 1); tt_int_op(CALLED(geoip_get_n_countries), ==, 1); tt_int_op(CALLED(geoip_get_country), ==, 1); tt_int_op((unsigned int)(*set->countries), ==, 0); done: NS_UNMOCK(geoip_is_loaded); NS_UNMOCK(geoip_get_n_countries); NS_UNMOCK(geoip_get_country); routerset_free(set); }
static void NS(test_main)(void *arg) { const routerset_t *set; int needs_geoip; (void)arg; set = NULL; needs_geoip = routerset_needs_geoip(set); tt_int_op(needs_geoip, ==, 0); set = routerset_new(); needs_geoip = routerset_needs_geoip(set); routerset_free((routerset_t *)set); tt_int_op(needs_geoip, ==, 0); set = NULL; set = routerset_new(); smartlist_add(set->country_names, tor_strndup("xx", 2)); needs_geoip = routerset_needs_geoip(set); routerset_free((routerset_t *)set); set = NULL; tt_int_op(needs_geoip, !=, 0); done: ; }
/** Run unit tests for concatenate-a-smartlist-of-strings functions. */ static void test_container_smartlist_join(void) { smartlist_t *sl = smartlist_new(); smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(), *sl4 = smartlist_new(); char *joined=NULL; /* unique, sorted. */ smartlist_split_string(sl, "Abashments Ambush Anchorman Bacon Banks Borscht " "Bunks Inhumane Insurance Knish Know Manners " "Maraschinos Stamina Sunbonnets Unicorns Wombats", " ", 0, 0); /* non-unique, sorted. */ smartlist_split_string(sl2, "Ambush Anchorman Anchorman Anemias Anemias Bacon " "Crossbowmen Inhumane Insurance Knish Know Manners " "Manners Maraschinos Wombats Wombats Work", " ", 0, 0); SMARTLIST_FOREACH_JOIN(sl, char *, cp1, sl2, char *, cp2, strcmp(cp1,cp2), smartlist_add(sl3, cp2)) { test_streq(cp1, cp2); smartlist_add(sl4, cp1); } SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
static void NS(test_main)(void *arg) { routerset_t *set = NULL; int is_empty; (void)arg; is_empty = routerset_is_empty(set); tt_int_op(is_empty, !=, 0); set = routerset_new(); is_empty = routerset_is_empty(set); routerset_free(set); set = NULL; tt_int_op(is_empty, !=, 0); set = routerset_new(); smartlist_add(set->list, tor_strdup("{xx}")); is_empty = routerset_is_empty(set); routerset_free(set); set = NULL; tt_int_op(is_empty, ==, 0); done: ; }
/* Note the cell digest in the circuit sendme last digests FIFO if applicable. * It is safe to pass a circuit that isn't meant to track those digests. */ void sendme_record_cell_digest(circuit_t *circ) { const uint8_t *digest; tor_assert(circ); /* We only keep the cell digest if we are the Exit on that circuit and if * this cell is the last one before the client should send a SENDME. */ if (CIRCUIT_IS_ORIGIN(circ)) { return; } /* Is this the last cell before a SENDME? The idea is that if the * package_window reaches a multiple of the increment, after this cell, we * should expect a SENDME. */ if (!sendme_circuit_cell_is_next(circ->package_window)) { return; } /* Add the digest to the last seen list in the circuit. */ digest = relay_crypto_get_sendme_digest(&TO_OR_CIRCUIT(circ)->crypto); if (circ->sendme_last_digests == NULL) { circ->sendme_last_digests = smartlist_new(); } smartlist_add(circ->sendme_last_digests, tor_memdup(digest, DIGEST_LEN)); }
static void NS(test_main)(void *arg) { smartlist_t *out = smartlist_new(); routerset_t *set = routerset_new(); int out_len; (void)arg; NS_MOCK(node_get_by_nickname); NS(mock_node).is_running = 0; NS(mock_nickname) = "foo"; smartlist_add(set->list, tor_strdup(NS(mock_nickname))); routerset_get_all_nodes(out, set, NULL, 1); out_len = smartlist_len(out); smartlist_free(out); routerset_free(set); tt_int_op(out_len, ==, 0); tt_int_op(CALLED(node_get_by_nickname), ==, 1); done: ; }