test_status.c 26 KB

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