dirvote: Fix memleak when computing consensus
[tor.git] / src / test / test_hs_metrics.c
blobacb064943463013cf6111ecfe03695d2bc6b4511
1 /* Copyright (c) 2020-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file test_hs_metrics.c
6 * \brief Test hidden service metrics.
7 */
9 #define HS_SERVICE_PRIVATE
11 #include "test/test.h"
12 #include "test/test_helpers.h"
13 #include "test/log_test_helpers.h"
15 #include "app/config/config.h"
17 #include "feature/hs/hs_metrics.h"
18 #include "feature/hs/hs_service.h"
20 #include "lib/crypt_ops/crypto_ed25519.h"
22 static void
23 test_metrics(void *arg)
25 hs_service_t *service = NULL;
27 (void) arg;
29 hs_init();
31 service = hs_service_new(get_options());
32 tt_assert(service);
33 service->config.version = HS_VERSION_THREE;
34 ed25519_secret_key_generate(&service->keys.identity_sk, 0);
35 ed25519_public_key_generate(&service->keys.identity_pk,
36 &service->keys.identity_sk);
37 register_service(get_hs_service_map(), service);
39 tt_assert(service->metrics.store);
41 /* Update entry by identifier. */
42 hs_metrics_update_by_ident(HS_METRICS_NUM_INTRODUCTIONS,
43 &service->keys.identity_pk, 0, NULL, 42,
44 0, false);
46 /* Confirm the entry value. */
47 const smartlist_t *entries = metrics_store_get_all(service->metrics.store,
48 "tor_hs_intro_num_total");
49 tt_assert(entries);
50 tt_int_op(smartlist_len(entries), OP_EQ, 1);
51 const metrics_store_entry_t *entry = smartlist_get(entries, 0);
52 tt_assert(entry);
53 tt_int_op(metrics_store_entry_get_value(entry), OP_EQ, 42);
55 /* Update entry by service now. */
56 hs_metrics_update_by_service(HS_METRICS_NUM_INTRODUCTIONS,
57 service, 0, NULL, 42, 0, false);
58 tt_int_op(metrics_store_entry_get_value(entry), OP_EQ, 84);
60 const char *reason = HS_METRICS_ERR_INTRO_REQ_BAD_AUTH_KEY;
62 /* Update tor_hs_intro_rejected_intro_req_count */
63 hs_metrics_update_by_ident(HS_METRICS_NUM_REJECTED_INTRO_REQ,
64 &service->keys.identity_pk, 0,
65 reason, 112, 0, false);
67 entries = metrics_store_get_all(service->metrics.store,
68 "tor_hs_intro_rejected_intro_req_count");
69 tt_assert(entries);
70 tt_int_op(smartlist_len(entries), OP_EQ,
71 hs_metrics_intro_req_error_reasons_size);
73 entry = metrics_store_find_entry_with_label(
74 entries, "reason=\"bad_auth_key\"");
75 tt_assert(entry);
76 tt_int_op(metrics_store_entry_get_value(entry), OP_EQ, 112);
78 /* Update tor_hs_intro_rejected_intro_req_count entry by service now. */
79 hs_metrics_update_by_service(HS_METRICS_NUM_REJECTED_INTRO_REQ, service, 0,
80 reason, 10, 0, false);
81 tt_int_op(metrics_store_entry_get_value(entry), OP_EQ, 122);
83 /* So far these have been relative updates. Test updates with reset */
84 hs_metrics_update_by_service(HS_METRICS_NUM_REJECTED_INTRO_REQ,
85 service, 0, reason, 10, 0, true);
86 tt_int_op(metrics_store_entry_get_value(entry), OP_EQ, 10);
88 hs_metrics_update_by_ident(HS_METRICS_NUM_REJECTED_INTRO_REQ,
89 &service->keys.identity_pk, 0, reason,
90 345, 0, true);
91 tt_int_op(metrics_store_entry_get_value(entry), OP_EQ, 345);
93 done:
94 hs_free_all();
97 struct testcase_t hs_metrics_tests[] = {
99 { "metrics", test_metrics, TT_FORK, NULL, NULL },
101 END_OF_TESTCASES