test_status.c 27 KB

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