test_status.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /* Copyright (c) 2014-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define STATUS_PRIVATE
  4. #define HIBERNATE_PRIVATE
  5. #define LOG_PRIVATE
  6. #define REPHIST_PRIVATE
  7. #include "orconfig.h"
  8. #include <float.h>
  9. #include <math.h>
  10. #include "core/or/or.h"
  11. #include "lib/log/log.h"
  12. #include "tor_queue.h"
  13. #include "core/or/status.h"
  14. #include "core/or/circuitlist.h"
  15. #include "app/config/config.h"
  16. #include "feature/hibernate/hibernate.h"
  17. #include "feature/stats/rephist.h"
  18. #include "core/or/relay.h"
  19. #include "feature/relay/router.h"
  20. #include "feature/relay/routermode.h"
  21. #include "core/mainloop/mainloop.h"
  22. #include "feature/nodelist/nodelist.h"
  23. #include "app/config/statefile.h"
  24. #include "lib/tls/tortls.h"
  25. #include "core/or/origin_circuit_st.h"
  26. #include "app/config/or_state_st.h"
  27. #include "feature/nodelist/routerinfo_st.h"
  28. #include "test/test.h"
  29. #define NS_MODULE status
  30. #define NS_SUBMODULE count_circuits
  31. /*
  32. * Test that count_circuits() is correctly counting the number of
  33. * global circuits.
  34. */
  35. static smartlist_t * mock_global_circuitlist = NULL;
  36. NS_DECL(smartlist_t *, circuit_get_global_list, (void));
  37. static void
  38. NS(test_main)(void *arg)
  39. {
  40. /* Choose origin_circuit_t wlog. */
  41. origin_circuit_t *mock_circuit1, *mock_circuit2;
  42. int expected_circuits = 2, actual_circuits;
  43. (void)arg;
  44. mock_circuit1 = tor_malloc_zero(sizeof(origin_circuit_t));
  45. mock_circuit2 = tor_malloc_zero(sizeof(origin_circuit_t));
  46. mock_global_circuitlist = smartlist_new();
  47. smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit1));
  48. smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit2));
  49. NS_MOCK(circuit_get_global_list);
  50. actual_circuits = count_circuits();
  51. tt_assert(expected_circuits == actual_circuits);
  52. done:
  53. tor_free(mock_circuit1);
  54. tor_free(mock_circuit2);
  55. smartlist_free(mock_global_circuitlist);
  56. mock_global_circuitlist = NULL;
  57. NS_UNMOCK(circuit_get_global_list);
  58. }
  59. static smartlist_t *
  60. NS(circuit_get_global_list)(void)
  61. {
  62. return mock_global_circuitlist;
  63. }
  64. #undef NS_SUBMODULE
  65. #define NS_SUBMODULE secs_to_uptime
  66. /*
  67. * Test that secs_to_uptime() is converting the number of seconds that
  68. * Tor is up for into the appropriate string form containing hours and minutes.
  69. */
  70. static void
  71. NS(test_main)(void *arg)
  72. {
  73. const char *expected;
  74. char *actual;
  75. (void)arg;
  76. expected = "0:00 hours";
  77. actual = secs_to_uptime(0);
  78. tt_str_op(actual, OP_EQ, expected);
  79. tor_free(actual);
  80. expected = "0:00 hours";
  81. actual = secs_to_uptime(1);
  82. tt_str_op(actual, OP_EQ, expected);
  83. tor_free(actual);
  84. expected = "0:01 hours";
  85. actual = secs_to_uptime(60);
  86. tt_str_op(actual, OP_EQ, expected);
  87. tor_free(actual);
  88. expected = "0:59 hours";
  89. actual = secs_to_uptime(60 * 59);
  90. tt_str_op(actual, OP_EQ, expected);
  91. tor_free(actual);
  92. expected = "1:00 hours";
  93. actual = secs_to_uptime(60 * 60);
  94. tt_str_op(actual, OP_EQ, expected);
  95. tor_free(actual);
  96. expected = "23:59 hours";
  97. actual = secs_to_uptime(60 * 60 * 23 + 60 * 59);
  98. tt_str_op(actual, OP_EQ, expected);
  99. tor_free(actual);
  100. expected = "1 day 0:00 hours";
  101. actual = secs_to_uptime(60 * 60 * 23 + 60 * 60);
  102. tt_str_op(actual, OP_EQ, expected);
  103. tor_free(actual);
  104. expected = "1 day 0:00 hours";
  105. actual = secs_to_uptime(86400 + 1);
  106. tt_str_op(actual, OP_EQ, expected);
  107. tor_free(actual);
  108. expected = "1 day 0:01 hours";
  109. actual = secs_to_uptime(86400 + 60);
  110. tt_str_op(actual, OP_EQ, expected);
  111. tor_free(actual);
  112. expected = "10 days 0:00 hours";
  113. actual = secs_to_uptime(86400 * 10);
  114. tt_str_op(actual, OP_EQ, expected);
  115. tor_free(actual);
  116. expected = "10 days 0:00 hours";
  117. actual = secs_to_uptime(864000 + 1);
  118. tt_str_op(actual, OP_EQ, expected);
  119. tor_free(actual);
  120. expected = "10 days 0:01 hours";
  121. actual = secs_to_uptime(864000 + 60);
  122. tt_str_op(actual, OP_EQ, expected);
  123. tor_free(actual);
  124. done:
  125. if (actual != NULL)
  126. tor_free(actual);
  127. }
  128. #undef NS_SUBMODULE
  129. #define NS_SUBMODULE bytes_to_usage
  130. /*
  131. * Test that bytes_to_usage() is correctly converting the number of bytes that
  132. * Tor has read/written into the appropriate string form containing kilobytes,
  133. * megabytes, or gigabytes.
  134. */
  135. static void
  136. NS(test_main)(void *arg)
  137. {
  138. const char *expected;
  139. char *actual;
  140. (void)arg;
  141. expected = "0 kB";
  142. actual = bytes_to_usage(0);
  143. tt_str_op(actual, OP_EQ, expected);
  144. tor_free(actual);
  145. expected = "0 kB";
  146. actual = bytes_to_usage(1);
  147. tt_str_op(actual, OP_EQ, expected);
  148. tor_free(actual);
  149. expected = "1 kB";
  150. actual = bytes_to_usage(1024);
  151. tt_str_op(actual, OP_EQ, expected);
  152. tor_free(actual);
  153. expected = "1023 kB";
  154. actual = bytes_to_usage((1 << 20) - 1);
  155. tt_str_op(actual, OP_EQ, expected);
  156. tor_free(actual);
  157. expected = "1.00 MB";
  158. actual = bytes_to_usage((1 << 20));
  159. tt_str_op(actual, OP_EQ, expected);
  160. tor_free(actual);
  161. expected = "1.00 MB";
  162. actual = bytes_to_usage((1 << 20) + 5242);
  163. tt_str_op(actual, OP_EQ, expected);
  164. tor_free(actual);
  165. expected = "1.01 MB";
  166. actual = bytes_to_usage((1 << 20) + 5243);
  167. tt_str_op(actual, OP_EQ, expected);
  168. tor_free(actual);
  169. expected = "1024.00 MB";
  170. actual = bytes_to_usage((1 << 30) - 1);
  171. tt_str_op(actual, OP_EQ, expected);
  172. tor_free(actual);
  173. expected = "1.00 GB";
  174. actual = bytes_to_usage((1 << 30));
  175. tt_str_op(actual, OP_EQ, expected);
  176. tor_free(actual);
  177. expected = "1.00 GB";
  178. actual = bytes_to_usage((1 << 30) + 5368709);
  179. tt_str_op(actual, OP_EQ, expected);
  180. tor_free(actual);
  181. expected = "1.01 GB";
  182. actual = bytes_to_usage((1 << 30) + 5368710);
  183. tt_str_op(actual, OP_EQ, expected);
  184. tor_free(actual);
  185. expected = "10.00 GB";
  186. actual = bytes_to_usage((UINT64_C(1) << 30) * 10L);
  187. tt_str_op(actual, OP_EQ, expected);
  188. tor_free(actual);
  189. done:
  190. if (actual != NULL)
  191. tor_free(actual);
  192. }
  193. #undef NS_SUBMODULE
  194. #define NS_SUBMODULE ASPECT(log_heartbeat, fails)
  195. /*
  196. * Tests that log_heartbeat() fails when in the public server mode,
  197. * not hibernating, and we couldn't get the current routerinfo.
  198. */
  199. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  200. NS_DECL(int, we_are_hibernating, (void));
  201. NS_DECL(int, public_server_mode, (const or_options_t *options));
  202. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  203. static void
  204. NS(test_main)(void *arg)
  205. {
  206. int expected, actual;
  207. (void)arg;
  208. NS_MOCK(tls_get_write_overhead_ratio);
  209. NS_MOCK(we_are_hibernating);
  210. NS_MOCK(public_server_mode);
  211. NS_MOCK(router_get_my_routerinfo);
  212. expected = -1;
  213. actual = log_heartbeat(0);
  214. tt_int_op(actual, OP_EQ, expected);
  215. done:
  216. NS_UNMOCK(tls_get_write_overhead_ratio);
  217. NS_UNMOCK(we_are_hibernating);
  218. NS_UNMOCK(public_server_mode);
  219. NS_UNMOCK(router_get_my_routerinfo);
  220. }
  221. static double
  222. NS(tls_get_write_overhead_ratio)(void)
  223. {
  224. return 2.0;
  225. }
  226. static int
  227. NS(we_are_hibernating)(void)
  228. {
  229. return 0;
  230. }
  231. static int
  232. NS(public_server_mode)(const or_options_t *options)
  233. {
  234. (void)options;
  235. return 1;
  236. }
  237. static const routerinfo_t *
  238. NS(router_get_my_routerinfo)(void)
  239. {
  240. return NULL;
  241. }
  242. #undef NS_SUBMODULE
  243. #define NS_SUBMODULE ASPECT(log_heartbeat, not_in_consensus)
  244. /*
  245. * Tests that log_heartbeat() logs appropriately if we are not in the cached
  246. * consensus.
  247. */
  248. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  249. NS_DECL(int, we_are_hibernating, (void));
  250. NS_DECL(int, public_server_mode, (const or_options_t *options));
  251. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  252. NS_DECL(const node_t *, node_get_by_id, (const char *identity_digest));
  253. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  254. const char *funcname, const char *suffix, const char *format, va_list ap));
  255. NS_DECL(int, server_mode, (const or_options_t *options));
  256. static routerinfo_t *mock_routerinfo;
  257. static void
  258. NS(test_main)(void *arg)
  259. {
  260. int expected, actual;
  261. (void)arg;
  262. NS_MOCK(tls_get_write_overhead_ratio);
  263. NS_MOCK(we_are_hibernating);
  264. NS_MOCK(public_server_mode);
  265. NS_MOCK(router_get_my_routerinfo);
  266. NS_MOCK(node_get_by_id);
  267. NS_MOCK(logv);
  268. NS_MOCK(server_mode);
  269. log_global_min_severity_ = LOG_DEBUG;
  270. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP] = 1;
  271. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP] = 1;
  272. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR] = 1;
  273. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR] = 1;
  274. expected = 0;
  275. actual = log_heartbeat(0);
  276. tt_int_op(actual, OP_EQ, expected);
  277. tt_int_op(CALLED(logv), OP_EQ, 6);
  278. done:
  279. NS_UNMOCK(tls_get_write_overhead_ratio);
  280. NS_UNMOCK(we_are_hibernating);
  281. NS_UNMOCK(public_server_mode);
  282. NS_UNMOCK(router_get_my_routerinfo);
  283. NS_UNMOCK(node_get_by_id);
  284. NS_UNMOCK(logv);
  285. NS_UNMOCK(server_mode);
  286. tor_free(mock_routerinfo);
  287. }
  288. static double
  289. NS(tls_get_write_overhead_ratio)(void)
  290. {
  291. return 1.0;
  292. }
  293. static int
  294. NS(we_are_hibernating)(void)
  295. {
  296. return 0;
  297. }
  298. static int
  299. NS(public_server_mode)(const or_options_t *options)
  300. {
  301. (void)options;
  302. return 1;
  303. }
  304. static const routerinfo_t *
  305. NS(router_get_my_routerinfo)(void)
  306. {
  307. mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  308. return mock_routerinfo;
  309. }
  310. static const node_t *
  311. NS(node_get_by_id)(const char *identity_digest)
  312. {
  313. (void)identity_digest;
  314. return NULL;
  315. }
  316. static void
  317. NS(logv)(int severity, log_domain_mask_t domain,
  318. const char *funcname, const char *suffix, const char *format, va_list ap)
  319. {
  320. switch (CALLED(logv))
  321. {
  322. case 0:
  323. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  324. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  325. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  326. tt_ptr_op(suffix, OP_EQ, NULL);
  327. tt_str_op(format, OP_EQ,
  328. "Heartbeat: It seems like we are not in the cached consensus.");
  329. break;
  330. case 1:
  331. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  332. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  333. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  334. tt_ptr_op(suffix, OP_EQ, NULL);
  335. tt_str_op(format, OP_EQ,
  336. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  337. "I've sent %s and received %s.%s");
  338. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  339. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  340. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  341. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  342. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  343. break;
  344. case 2:
  345. tt_int_op(severity, OP_EQ, LOG_INFO);
  346. break;
  347. case 3:
  348. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  349. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  350. tt_ptr_op(strstr(funcname, "rep_hist_log_circuit_handshake_stats"),
  351. OP_NE, NULL);
  352. tt_ptr_op(suffix, OP_EQ, NULL);
  353. tt_str_op(format, OP_EQ,
  354. "Circuit handshake stats since last time: %d/%d TAP, %d/%d NTor.");
  355. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (TAP) */
  356. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (TAP) */
  357. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (NTOR) */
  358. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (NTOR) */
  359. break;
  360. case 4:
  361. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  362. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  363. tt_ptr_op(strstr(funcname, "rep_hist_log_link_protocol_counts"),
  364. OP_NE, NULL);
  365. break;
  366. case 5:
  367. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  368. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  369. tt_str_op(format, OP_EQ, "DoS mitigation since startup:%s%s%s%s");
  370. tt_str_op(va_arg(ap, char *), OP_EQ,
  371. " 0 circuits killed with too many cells.");
  372. tt_str_op(va_arg(ap, char *), OP_EQ, " [cc not enabled]");
  373. tt_str_op(va_arg(ap, char *), OP_EQ, " [conn not enabled]");
  374. tt_str_op(va_arg(ap, char *), OP_EQ, "");
  375. break;
  376. default:
  377. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  378. break;
  379. }
  380. done:
  381. CALLED(logv)++;
  382. }
  383. static int
  384. NS(server_mode)(const or_options_t *options)
  385. {
  386. (void)options;
  387. return 0;
  388. }
  389. #undef NS_SUBMODULE
  390. #define NS_SUBMODULE ASPECT(log_heartbeat, simple)
  391. /*
  392. * Tests that log_heartbeat() correctly logs heartbeat information
  393. * normally.
  394. */
  395. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  396. NS_DECL(int, we_are_hibernating, (void));
  397. NS_DECL(int, public_server_mode, (const or_options_t *options));
  398. NS_DECL(long, get_uptime, (void));
  399. NS_DECL(uint64_t, get_bytes_read, (void));
  400. NS_DECL(uint64_t, get_bytes_written, (void));
  401. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  402. const char *funcname, const char *suffix, const char *format, va_list ap));
  403. NS_DECL(int, server_mode, (const or_options_t *options));
  404. static int NS(n_msgs) = 0;
  405. static void
  406. NS(test_main)(void *arg)
  407. {
  408. int expected, actual;
  409. (void)arg;
  410. NS_MOCK(tls_get_write_overhead_ratio);
  411. NS_MOCK(we_are_hibernating);
  412. NS_MOCK(public_server_mode);
  413. NS_MOCK(get_uptime);
  414. NS_MOCK(get_bytes_read);
  415. NS_MOCK(get_bytes_written);
  416. NS_MOCK(logv);
  417. NS_MOCK(server_mode);
  418. log_global_min_severity_ = LOG_DEBUG;
  419. expected = 0;
  420. actual = log_heartbeat(0);
  421. tt_int_op(actual, OP_EQ, expected);
  422. tt_int_op(NS(n_msgs), OP_EQ, 1);
  423. done:
  424. NS_UNMOCK(tls_get_write_overhead_ratio);
  425. NS_UNMOCK(we_are_hibernating);
  426. NS_UNMOCK(public_server_mode);
  427. NS_UNMOCK(get_uptime);
  428. NS_UNMOCK(get_bytes_read);
  429. NS_UNMOCK(get_bytes_written);
  430. NS_UNMOCK(logv);
  431. NS_UNMOCK(server_mode);
  432. }
  433. static double
  434. NS(tls_get_write_overhead_ratio)(void)
  435. {
  436. return 1.0;
  437. }
  438. static int
  439. NS(we_are_hibernating)(void)
  440. {
  441. return 1;
  442. }
  443. static int
  444. NS(public_server_mode)(const or_options_t *options)
  445. {
  446. (void)options;
  447. return 0;
  448. }
  449. static long
  450. NS(get_uptime)(void)
  451. {
  452. return 0;
  453. }
  454. static uint64_t
  455. NS(get_bytes_read)(void)
  456. {
  457. return 0;
  458. }
  459. static uint64_t
  460. NS(get_bytes_written)(void)
  461. {
  462. return 0;
  463. }
  464. static void
  465. NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
  466. const char *suffix, const char *format, va_list ap)
  467. {
  468. if (severity == LOG_INFO)
  469. return;
  470. ++NS(n_msgs);
  471. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  472. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  473. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  474. tt_ptr_op(suffix, OP_EQ, NULL);
  475. tt_str_op(format, OP_EQ,
  476. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  477. "I've sent %s and received %s.%s");
  478. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  479. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  480. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  481. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  482. tt_str_op(va_arg(ap, char *), OP_EQ, " We are currently hibernating.");
  483. done:
  484. ;
  485. }
  486. static int
  487. NS(server_mode)(const or_options_t *options)
  488. {
  489. (void)options;
  490. return 0;
  491. }
  492. #undef NS_SUBMODULE
  493. #define NS_SUBMODULE ASPECT(log_heartbeat, calls_log_accounting)
  494. /*
  495. * Tests that log_heartbeat() correctly logs heartbeat information
  496. * and accounting information when configured.
  497. */
  498. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  499. NS_DECL(int, we_are_hibernating, (void));
  500. NS_DECL(int, public_server_mode, (const or_options_t *options));
  501. NS_DECL(long, get_uptime, (void));
  502. NS_DECL(uint64_t, get_bytes_read, (void));
  503. NS_DECL(uint64_t, get_bytes_written, (void));
  504. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  505. const char *funcname, const char *suffix, const char *format, va_list ap));
  506. NS_DECL(int, server_mode, (const or_options_t *options));
  507. NS_DECL(or_state_t *, get_or_state, (void));
  508. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  509. NS_DECL(time_t, accounting_get_end_time, (void));
  510. static or_state_t * NS(mock_state) = NULL;
  511. static or_options_t * NS(mock_options) = NULL;
  512. static void
  513. NS(test_main)(void *arg)
  514. {
  515. int expected, actual;
  516. (void)arg;
  517. NS_MOCK(tls_get_write_overhead_ratio);
  518. NS_MOCK(we_are_hibernating);
  519. NS_MOCK(public_server_mode);
  520. NS_MOCK(get_uptime);
  521. NS_MOCK(get_bytes_read);
  522. NS_MOCK(get_bytes_written);
  523. NS_MOCK(logv);
  524. NS_MOCK(server_mode);
  525. NS_MOCK(get_or_state);
  526. NS_MOCK(accounting_is_enabled);
  527. NS_MOCK(accounting_get_end_time);
  528. log_global_min_severity_ = LOG_DEBUG;
  529. expected = 0;
  530. actual = log_heartbeat(0);
  531. tt_int_op(actual, OP_EQ, expected);
  532. tt_int_op(CALLED(logv), OP_EQ, 3);
  533. done:
  534. NS_UNMOCK(tls_get_write_overhead_ratio);
  535. NS_UNMOCK(we_are_hibernating);
  536. NS_UNMOCK(public_server_mode);
  537. NS_UNMOCK(get_uptime);
  538. NS_UNMOCK(get_bytes_read);
  539. NS_UNMOCK(get_bytes_written);
  540. NS_UNMOCK(logv);
  541. NS_UNMOCK(server_mode);
  542. NS_UNMOCK(accounting_is_enabled);
  543. NS_UNMOCK(accounting_get_end_time);
  544. tor_free_(NS(mock_state));
  545. tor_free_(NS(mock_options));
  546. }
  547. static double
  548. NS(tls_get_write_overhead_ratio)(void)
  549. {
  550. return 1.0;
  551. }
  552. static int
  553. NS(we_are_hibernating)(void)
  554. {
  555. return 0;
  556. }
  557. static int
  558. NS(public_server_mode)(const or_options_t *options)
  559. {
  560. (void)options;
  561. return 0;
  562. }
  563. static long
  564. NS(get_uptime)(void)
  565. {
  566. return 0;
  567. }
  568. static uint64_t
  569. NS(get_bytes_read)(void)
  570. {
  571. return 0;
  572. }
  573. static uint64_t
  574. NS(get_bytes_written)(void)
  575. {
  576. return 0;
  577. }
  578. static void
  579. NS(logv)(int severity, log_domain_mask_t domain,
  580. const char *funcname, const char *suffix, const char *format, va_list ap)
  581. {
  582. switch (CALLED(logv))
  583. {
  584. case 0:
  585. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  586. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  587. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  588. tt_ptr_op(suffix, OP_EQ, NULL);
  589. tt_str_op(format, OP_EQ,
  590. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  591. "I've sent %s and received %s.%s");
  592. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  593. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  594. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  595. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  596. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  597. break;
  598. case 1:
  599. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  600. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  601. tt_ptr_op(strstr(funcname, "log_accounting"), OP_NE, NULL);
  602. tt_ptr_op(suffix, OP_EQ, NULL);
  603. tt_str_op(format, OP_EQ,
  604. "Heartbeat: Accounting enabled. Sent: %s, Received: %s, Used: %s / "
  605. "%s, Rule: %s. The current accounting interval ends on %s, in %s.");
  606. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_sent */
  607. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_rcvd */
  608. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_used */
  609. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
  610. tt_str_op(va_arg(ap, char *), OP_EQ, "max"); /* acc_rule */
  611. /* format_local_iso_time uses local tz, so we can't just compare
  612. * the string against a constant */
  613. char datetime[ISO_TIME_LEN+1];
  614. format_local_iso_time(datetime, 60);
  615. tt_str_op(va_arg(ap, char *), OP_EQ, datetime); /* end_buf */
  616. tt_str_op(va_arg(ap, char *), OP_EQ, "0:01 hours"); /* remaining */
  617. break;
  618. case 2:
  619. tt_int_op(severity, OP_EQ, LOG_INFO);
  620. break;
  621. default:
  622. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  623. break;
  624. }
  625. done:
  626. CALLED(logv)++;
  627. }
  628. static int
  629. NS(server_mode)(const or_options_t *options)
  630. {
  631. (void)options;
  632. return 1;
  633. }
  634. static int
  635. NS(accounting_is_enabled)(const or_options_t *options)
  636. {
  637. (void)options;
  638. return 1;
  639. }
  640. static time_t
  641. NS(accounting_get_end_time)(void)
  642. {
  643. return 60;
  644. }
  645. static or_state_t *
  646. NS(get_or_state)(void)
  647. {
  648. NS(mock_state) = tor_malloc_zero(sizeof(or_state_t));
  649. NS(mock_state)->AccountingBytesReadInInterval = 0;
  650. NS(mock_state)->AccountingBytesWrittenInInterval = 0;
  651. return NS(mock_state);
  652. }
  653. #undef NS_SUBMODULE
  654. #define NS_SUBMODULE ASPECT(log_heartbeat, packaged_cell_fullness)
  655. /*
  656. * Tests that log_heartbeat() correctly logs packaged cell
  657. * fullness information.
  658. */
  659. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  660. NS_DECL(int, we_are_hibernating, (void));
  661. NS_DECL(int, public_server_mode, (const or_options_t *options));
  662. NS_DECL(long, get_uptime, (void));
  663. NS_DECL(uint64_t, get_bytes_read, (void));
  664. NS_DECL(uint64_t, get_bytes_written, (void));
  665. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  666. const char *funcname, const char *suffix, const char *format, va_list ap));
  667. NS_DECL(int, server_mode, (const or_options_t *options));
  668. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  669. static void
  670. NS(test_main)(void *arg)
  671. {
  672. int expected, actual;
  673. (void)arg;
  674. NS_MOCK(tls_get_write_overhead_ratio);
  675. NS_MOCK(we_are_hibernating);
  676. NS_MOCK(public_server_mode);
  677. NS_MOCK(get_uptime);
  678. NS_MOCK(get_bytes_read);
  679. NS_MOCK(get_bytes_written);
  680. NS_MOCK(logv);
  681. NS_MOCK(server_mode);
  682. NS_MOCK(accounting_is_enabled);
  683. log_global_min_severity_ = LOG_DEBUG;
  684. stats_n_data_bytes_packaged = RELAY_PAYLOAD_SIZE;
  685. stats_n_data_cells_packaged = 2;
  686. expected = 0;
  687. actual = log_heartbeat(0);
  688. tt_int_op(actual, OP_EQ, expected);
  689. tt_int_op(CALLED(logv), OP_EQ, 2);
  690. done:
  691. stats_n_data_bytes_packaged = 0;
  692. stats_n_data_cells_packaged = 0;
  693. NS_UNMOCK(tls_get_write_overhead_ratio);
  694. NS_UNMOCK(we_are_hibernating);
  695. NS_UNMOCK(public_server_mode);
  696. NS_UNMOCK(get_uptime);
  697. NS_UNMOCK(get_bytes_read);
  698. NS_UNMOCK(get_bytes_written);
  699. NS_UNMOCK(logv);
  700. NS_UNMOCK(server_mode);
  701. NS_UNMOCK(accounting_is_enabled);
  702. }
  703. static double
  704. NS(tls_get_write_overhead_ratio)(void)
  705. {
  706. return 1.0;
  707. }
  708. static int
  709. NS(we_are_hibernating)(void)
  710. {
  711. return 0;
  712. }
  713. static int
  714. NS(public_server_mode)(const or_options_t *options)
  715. {
  716. (void)options;
  717. return 0;
  718. }
  719. static long
  720. NS(get_uptime)(void)
  721. {
  722. return 0;
  723. }
  724. static uint64_t
  725. NS(get_bytes_read)(void)
  726. {
  727. return 0;
  728. }
  729. static uint64_t
  730. NS(get_bytes_written)(void)
  731. {
  732. return 0;
  733. }
  734. static void
  735. NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
  736. const char *suffix, const char *format, va_list ap)
  737. {
  738. switch (CALLED(logv))
  739. {
  740. case 0:
  741. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  742. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  743. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  744. tt_ptr_op(suffix, OP_EQ, NULL);
  745. tt_str_op(format, OP_EQ,
  746. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  747. "I've sent %s and received %s.%s");
  748. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  749. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  750. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  751. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  752. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  753. break;
  754. case 1:
  755. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  756. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  757. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  758. tt_ptr_op(suffix, OP_EQ, NULL);
  759. tt_str_op(format, OP_EQ,
  760. "Average packaged cell fullness: %2.3f%%. "
  761. "TLS write overhead: %.f%%");
  762. tt_double_op(fabs(va_arg(ap, double) - 50.0), OP_LE, DBL_EPSILON);
  763. tt_double_op(fabs(va_arg(ap, double) - 0.0), OP_LE, DBL_EPSILON);
  764. break;
  765. default:
  766. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  767. break;
  768. }
  769. done:
  770. CALLED(logv)++;
  771. }
  772. static int
  773. NS(server_mode)(const or_options_t *options)
  774. {
  775. (void)options;
  776. return 0;
  777. }
  778. static int
  779. NS(accounting_is_enabled)(const or_options_t *options)
  780. {
  781. (void)options;
  782. return 0;
  783. }
  784. #undef NS_SUBMODULE
  785. #define NS_SUBMODULE ASPECT(log_heartbeat, tls_write_overhead)
  786. /*
  787. * Tests that log_heartbeat() correctly logs the TLS write overhead information
  788. * when the TLS write overhead ratio exceeds 1.
  789. */
  790. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  791. NS_DECL(int, we_are_hibernating, (void));
  792. NS_DECL(int, public_server_mode, (const or_options_t *options));
  793. NS_DECL(long, get_uptime, (void));
  794. NS_DECL(uint64_t, get_bytes_read, (void));
  795. NS_DECL(uint64_t, get_bytes_written, (void));
  796. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  797. const char *funcname, const char *suffix, const char *format, va_list ap));
  798. NS_DECL(int, server_mode, (const or_options_t *options));
  799. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  800. static void
  801. NS(test_main)(void *arg)
  802. {
  803. int expected, actual;
  804. (void)arg;
  805. NS_MOCK(tls_get_write_overhead_ratio);
  806. NS_MOCK(we_are_hibernating);
  807. NS_MOCK(public_server_mode);
  808. NS_MOCK(get_uptime);
  809. NS_MOCK(get_bytes_read);
  810. NS_MOCK(get_bytes_written);
  811. NS_MOCK(logv);
  812. NS_MOCK(server_mode);
  813. NS_MOCK(accounting_is_enabled);
  814. stats_n_data_cells_packaged = 0;
  815. log_global_min_severity_ = LOG_DEBUG;
  816. expected = 0;
  817. actual = log_heartbeat(0);
  818. tt_int_op(actual, OP_EQ, expected);
  819. tt_int_op(CALLED(logv), OP_EQ, 2);
  820. done:
  821. NS_UNMOCK(tls_get_write_overhead_ratio);
  822. NS_UNMOCK(we_are_hibernating);
  823. NS_UNMOCK(public_server_mode);
  824. NS_UNMOCK(get_uptime);
  825. NS_UNMOCK(get_bytes_read);
  826. NS_UNMOCK(get_bytes_written);
  827. NS_UNMOCK(logv);
  828. NS_UNMOCK(server_mode);
  829. NS_UNMOCK(accounting_is_enabled);
  830. }
  831. static double
  832. NS(tls_get_write_overhead_ratio)(void)
  833. {
  834. return 2.0;
  835. }
  836. static int
  837. NS(we_are_hibernating)(void)
  838. {
  839. return 0;
  840. }
  841. static int
  842. NS(public_server_mode)(const or_options_t *options)
  843. {
  844. (void)options;
  845. return 0;
  846. }
  847. static long
  848. NS(get_uptime)(void)
  849. {
  850. return 0;
  851. }
  852. static uint64_t
  853. NS(get_bytes_read)(void)
  854. {
  855. return 0;
  856. }
  857. static uint64_t
  858. NS(get_bytes_written)(void)
  859. {
  860. return 0;
  861. }
  862. static void
  863. NS(logv)(int severity, log_domain_mask_t domain,
  864. const char *funcname, const char *suffix, const char *format, va_list ap)
  865. {
  866. switch (CALLED(logv))
  867. {
  868. case 0:
  869. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  870. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  871. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  872. tt_ptr_op(suffix, OP_EQ, NULL);
  873. tt_str_op(format, OP_EQ,
  874. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  875. "I've sent %s and received %s.%s");
  876. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  877. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  878. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  879. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  880. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  881. break;
  882. case 1:
  883. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  884. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  885. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  886. tt_ptr_op(suffix, OP_EQ, NULL);
  887. tt_str_op(format, OP_EQ,
  888. "Average packaged cell fullness: %2.3f%%. "
  889. "TLS write overhead: %.f%%");
  890. tt_int_op(fabs(va_arg(ap, double) - 100.0) <= DBL_EPSILON, OP_EQ, 1);
  891. tt_double_op(fabs(va_arg(ap, double) - 100.0), OP_LE, DBL_EPSILON);
  892. break;
  893. default:
  894. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  895. break;
  896. }
  897. done:
  898. CALLED(logv)++;
  899. }
  900. static int
  901. NS(server_mode)(const or_options_t *options)
  902. {
  903. (void)options;
  904. return 0;
  905. }
  906. static int
  907. NS(accounting_is_enabled)(const or_options_t *options)
  908. {
  909. (void)options;
  910. return 0;
  911. }
  912. #undef NS_SUBMODULE
  913. struct testcase_t status_tests[] = {
  914. TEST_CASE(count_circuits),
  915. TEST_CASE(secs_to_uptime),
  916. TEST_CASE(bytes_to_usage),
  917. TEST_CASE_ASPECT(log_heartbeat, fails),
  918. TEST_CASE_ASPECT(log_heartbeat, simple),
  919. TEST_CASE_ASPECT(log_heartbeat, not_in_consensus),
  920. TEST_CASE_ASPECT(log_heartbeat, calls_log_accounting),
  921. TEST_CASE_ASPECT(log_heartbeat, packaged_cell_fullness),
  922. TEST_CASE_ASPECT(log_heartbeat, tls_write_overhead),
  923. END_OF_TESTCASES
  924. };