test_status.c 26 KB

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