test_status.c 27 KB

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