test_status.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. extern int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1];
  250. extern int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1];
  251. static void
  252. NS(test_main)(void *arg)
  253. {
  254. int expected, actual;
  255. (void)arg;
  256. NS_MOCK(tls_get_write_overhead_ratio);
  257. NS_MOCK(we_are_hibernating);
  258. NS_MOCK(public_server_mode);
  259. NS_MOCK(router_get_my_routerinfo);
  260. NS_MOCK(node_get_by_id);
  261. NS_MOCK(logv);
  262. NS_MOCK(server_mode);
  263. log_global_min_severity_ = LOG_DEBUG;
  264. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP] = 1;
  265. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP] = 1;
  266. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR] = 1;
  267. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR] = 1;
  268. expected = 0;
  269. actual = log_heartbeat(0);
  270. tt_int_op(actual, OP_EQ, expected);
  271. tt_int_op(CALLED(logv), OP_EQ, 4);
  272. done:
  273. NS_UNMOCK(tls_get_write_overhead_ratio);
  274. NS_UNMOCK(we_are_hibernating);
  275. NS_UNMOCK(public_server_mode);
  276. NS_UNMOCK(router_get_my_routerinfo);
  277. NS_UNMOCK(node_get_by_id);
  278. NS_UNMOCK(logv);
  279. NS_UNMOCK(server_mode);
  280. tor_free(mock_routerinfo);
  281. }
  282. static double
  283. NS(tls_get_write_overhead_ratio)(void)
  284. {
  285. return 1.0;
  286. }
  287. static int
  288. NS(we_are_hibernating)(void)
  289. {
  290. return 0;
  291. }
  292. static int
  293. NS(public_server_mode)(const or_options_t *options)
  294. {
  295. (void)options;
  296. return 1;
  297. }
  298. static const routerinfo_t *
  299. NS(router_get_my_routerinfo)(void)
  300. {
  301. mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  302. return mock_routerinfo;
  303. }
  304. static const node_t *
  305. NS(node_get_by_id)(const char *identity_digest)
  306. {
  307. (void)identity_digest;
  308. return NULL;
  309. }
  310. static void
  311. NS(logv)(int severity, log_domain_mask_t domain,
  312. const char *funcname, const char *suffix, const char *format, va_list ap)
  313. {
  314. switch (CALLED(logv))
  315. {
  316. case 0:
  317. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  318. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  319. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  320. tt_ptr_op(suffix, OP_EQ, NULL);
  321. tt_str_op(format, OP_EQ,
  322. "Heartbeat: It seems like we are not in the cached consensus.");
  323. break;
  324. case 1:
  325. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  326. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  327. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  328. tt_ptr_op(suffix, OP_EQ, NULL);
  329. tt_str_op(format, OP_EQ,
  330. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  331. "I've sent %s and received %s.%s");
  332. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  333. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  334. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  335. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  336. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  337. break;
  338. case 2:
  339. tt_int_op(severity, OP_EQ, LOG_INFO);
  340. break;
  341. case 3:
  342. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  343. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  344. tt_ptr_op(strstr(funcname, "rep_hist_log_circuit_handshake_stats"),
  345. OP_NE, NULL);
  346. tt_ptr_op(suffix, OP_EQ, NULL);
  347. tt_str_op(format, OP_EQ,
  348. "Circuit handshake stats since last time: %d/%d TAP, %d/%d NTor.");
  349. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (TAP) */
  350. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (TAP) */
  351. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (NTOR) */
  352. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (NTOR) */
  353. break;
  354. default:
  355. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  356. break;
  357. }
  358. done:
  359. CALLED(logv)++;
  360. }
  361. static int
  362. NS(server_mode)(const or_options_t *options)
  363. {
  364. (void)options;
  365. return 0;
  366. }
  367. #undef NS_SUBMODULE
  368. #define NS_SUBMODULE ASPECT(log_heartbeat, simple)
  369. /*
  370. * Tests that log_heartbeat() correctly logs heartbeat information
  371. * normally.
  372. */
  373. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  374. NS_DECL(int, we_are_hibernating, (void));
  375. NS_DECL(int, public_server_mode, (const or_options_t *options));
  376. NS_DECL(long, get_uptime, (void));
  377. NS_DECL(uint64_t, get_bytes_read, (void));
  378. NS_DECL(uint64_t, get_bytes_written, (void));
  379. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  380. const char *funcname, const char *suffix, const char *format, va_list ap));
  381. NS_DECL(int, server_mode, (const or_options_t *options));
  382. static int NS(n_msgs) = 0;
  383. static void
  384. NS(test_main)(void *arg)
  385. {
  386. int expected, actual;
  387. (void)arg;
  388. NS_MOCK(tls_get_write_overhead_ratio);
  389. NS_MOCK(we_are_hibernating);
  390. NS_MOCK(public_server_mode);
  391. NS_MOCK(get_uptime);
  392. NS_MOCK(get_bytes_read);
  393. NS_MOCK(get_bytes_written);
  394. NS_MOCK(logv);
  395. NS_MOCK(server_mode);
  396. log_global_min_severity_ = LOG_DEBUG;
  397. expected = 0;
  398. actual = log_heartbeat(0);
  399. tt_int_op(actual, OP_EQ, expected);
  400. tt_int_op(NS(n_msgs), OP_EQ, 1);
  401. done:
  402. NS_UNMOCK(tls_get_write_overhead_ratio);
  403. NS_UNMOCK(we_are_hibernating);
  404. NS_UNMOCK(public_server_mode);
  405. NS_UNMOCK(get_uptime);
  406. NS_UNMOCK(get_bytes_read);
  407. NS_UNMOCK(get_bytes_written);
  408. NS_UNMOCK(logv);
  409. NS_UNMOCK(server_mode);
  410. }
  411. static double
  412. NS(tls_get_write_overhead_ratio)(void)
  413. {
  414. return 1.0;
  415. }
  416. static int
  417. NS(we_are_hibernating)(void)
  418. {
  419. return 1;
  420. }
  421. static int
  422. NS(public_server_mode)(const or_options_t *options)
  423. {
  424. (void)options;
  425. return 0;
  426. }
  427. static long
  428. NS(get_uptime)(void)
  429. {
  430. return 0;
  431. }
  432. static uint64_t
  433. NS(get_bytes_read)(void)
  434. {
  435. return 0;
  436. }
  437. static uint64_t
  438. NS(get_bytes_written)(void)
  439. {
  440. return 0;
  441. }
  442. static void
  443. NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
  444. const char *suffix, const char *format, va_list ap)
  445. {
  446. if (severity == LOG_INFO)
  447. return;
  448. ++NS(n_msgs);
  449. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  450. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  451. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  452. tt_ptr_op(suffix, OP_EQ, NULL);
  453. tt_str_op(format, OP_EQ,
  454. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  455. "I've sent %s and received %s.%s");
  456. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  457. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  458. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  459. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  460. tt_str_op(va_arg(ap, char *), OP_EQ, " We are currently hibernating.");
  461. done:
  462. ;
  463. }
  464. static int
  465. NS(server_mode)(const or_options_t *options)
  466. {
  467. (void)options;
  468. return 0;
  469. }
  470. #undef NS_SUBMODULE
  471. #define NS_SUBMODULE ASPECT(log_heartbeat, calls_log_accounting)
  472. /*
  473. * Tests that log_heartbeat() correctly logs heartbeat information
  474. * and accounting information when configured.
  475. */
  476. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  477. NS_DECL(int, we_are_hibernating, (void));
  478. NS_DECL(int, public_server_mode, (const or_options_t *options));
  479. NS_DECL(long, get_uptime, (void));
  480. NS_DECL(uint64_t, get_bytes_read, (void));
  481. NS_DECL(uint64_t, get_bytes_written, (void));
  482. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  483. const char *funcname, const char *suffix, const char *format, va_list ap));
  484. NS_DECL(int, server_mode, (const or_options_t *options));
  485. NS_DECL(or_state_t *, get_or_state, (void));
  486. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  487. NS_DECL(time_t, accounting_get_end_time, (void));
  488. static or_state_t * NS(mock_state) = NULL;
  489. static or_options_t * NS(mock_options) = NULL;
  490. static void
  491. NS(test_main)(void *arg)
  492. {
  493. int expected, actual;
  494. (void)arg;
  495. NS_MOCK(tls_get_write_overhead_ratio);
  496. NS_MOCK(we_are_hibernating);
  497. NS_MOCK(public_server_mode);
  498. NS_MOCK(get_uptime);
  499. NS_MOCK(get_bytes_read);
  500. NS_MOCK(get_bytes_written);
  501. NS_MOCK(logv);
  502. NS_MOCK(server_mode);
  503. NS_MOCK(get_or_state);
  504. NS_MOCK(accounting_is_enabled);
  505. NS_MOCK(accounting_get_end_time);
  506. log_global_min_severity_ = LOG_DEBUG;
  507. expected = 0;
  508. actual = log_heartbeat(0);
  509. tt_int_op(actual, OP_EQ, expected);
  510. tt_int_op(CALLED(logv), OP_EQ, 3);
  511. done:
  512. NS_UNMOCK(tls_get_write_overhead_ratio);
  513. NS_UNMOCK(we_are_hibernating);
  514. NS_UNMOCK(public_server_mode);
  515. NS_UNMOCK(get_uptime);
  516. NS_UNMOCK(get_bytes_read);
  517. NS_UNMOCK(get_bytes_written);
  518. NS_UNMOCK(logv);
  519. NS_UNMOCK(server_mode);
  520. NS_UNMOCK(accounting_is_enabled);
  521. NS_UNMOCK(accounting_get_end_time);
  522. tor_free_(NS(mock_state));
  523. tor_free_(NS(mock_options));
  524. }
  525. static double
  526. NS(tls_get_write_overhead_ratio)(void)
  527. {
  528. return 1.0;
  529. }
  530. static int
  531. NS(we_are_hibernating)(void)
  532. {
  533. return 0;
  534. }
  535. static int
  536. NS(public_server_mode)(const or_options_t *options)
  537. {
  538. (void)options;
  539. return 0;
  540. }
  541. static long
  542. NS(get_uptime)(void)
  543. {
  544. return 0;
  545. }
  546. static uint64_t
  547. NS(get_bytes_read)(void)
  548. {
  549. return 0;
  550. }
  551. static uint64_t
  552. NS(get_bytes_written)(void)
  553. {
  554. return 0;
  555. }
  556. static void
  557. NS(logv)(int severity, log_domain_mask_t domain,
  558. const char *funcname, const char *suffix, const char *format, va_list ap)
  559. {
  560. switch (CALLED(logv))
  561. {
  562. case 0:
  563. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  564. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  565. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  566. tt_ptr_op(suffix, OP_EQ, NULL);
  567. tt_str_op(format, OP_EQ,
  568. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  569. "I've sent %s and received %s.%s");
  570. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  571. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  572. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  573. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  574. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  575. break;
  576. case 1:
  577. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  578. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  579. tt_ptr_op(strstr(funcname, "log_accounting"), OP_NE, NULL);
  580. tt_ptr_op(suffix, OP_EQ, NULL);
  581. tt_str_op(format, OP_EQ,
  582. "Heartbeat: Accounting enabled. Sent: %s / %s, Received: %s / %s. "
  583. "The current accounting interval ends on %s, in %s.");
  584. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_sent */
  585. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
  586. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_rcvd */
  587. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
  588. /* format_local_iso_time uses local tz, just check mins and secs. */
  589. tt_ptr_op(strstr(va_arg(ap, char *), ":01:00"),
  590. OP_NE, NULL); /* end_buf */
  591. tt_str_op(va_arg(ap, char *), OP_EQ, "0:01 hours"); /* remaining */
  592. break;
  593. case 2:
  594. tt_int_op(severity, OP_EQ, LOG_INFO);
  595. break;
  596. default:
  597. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  598. break;
  599. }
  600. done:
  601. CALLED(logv)++;
  602. }
  603. static int
  604. NS(server_mode)(const or_options_t *options)
  605. {
  606. (void)options;
  607. return 1;
  608. }
  609. static int
  610. NS(accounting_is_enabled)(const or_options_t *options)
  611. {
  612. (void)options;
  613. return 1;
  614. }
  615. static time_t
  616. NS(accounting_get_end_time)(void)
  617. {
  618. return 60;
  619. }
  620. static or_state_t *
  621. NS(get_or_state)(void)
  622. {
  623. NS(mock_state) = tor_malloc_zero(sizeof(or_state_t));
  624. NS(mock_state)->AccountingBytesReadInInterval = 0;
  625. NS(mock_state)->AccountingBytesWrittenInInterval = 0;
  626. return NS(mock_state);
  627. }
  628. #undef NS_SUBMODULE
  629. #define NS_SUBMODULE ASPECT(log_heartbeat, packaged_cell_fullness)
  630. /*
  631. * Tests that log_heartbeat() correctly logs packaged cell
  632. * fullness information.
  633. */
  634. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  635. NS_DECL(int, we_are_hibernating, (void));
  636. NS_DECL(int, public_server_mode, (const or_options_t *options));
  637. NS_DECL(long, get_uptime, (void));
  638. NS_DECL(uint64_t, get_bytes_read, (void));
  639. NS_DECL(uint64_t, get_bytes_written, (void));
  640. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  641. const char *funcname, const char *suffix, const char *format, va_list ap));
  642. NS_DECL(int, server_mode, (const or_options_t *options));
  643. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  644. static void
  645. NS(test_main)(void *arg)
  646. {
  647. int expected, actual;
  648. (void)arg;
  649. NS_MOCK(tls_get_write_overhead_ratio);
  650. NS_MOCK(we_are_hibernating);
  651. NS_MOCK(public_server_mode);
  652. NS_MOCK(get_uptime);
  653. NS_MOCK(get_bytes_read);
  654. NS_MOCK(get_bytes_written);
  655. NS_MOCK(logv);
  656. NS_MOCK(server_mode);
  657. NS_MOCK(accounting_is_enabled);
  658. log_global_min_severity_ = LOG_DEBUG;
  659. stats_n_data_bytes_packaged = RELAY_PAYLOAD_SIZE;
  660. stats_n_data_cells_packaged = 2;
  661. expected = 0;
  662. actual = log_heartbeat(0);
  663. tt_int_op(actual, OP_EQ, expected);
  664. tt_int_op(CALLED(logv), OP_EQ, 2);
  665. done:
  666. stats_n_data_bytes_packaged = 0;
  667. stats_n_data_cells_packaged = 0;
  668. NS_UNMOCK(tls_get_write_overhead_ratio);
  669. NS_UNMOCK(we_are_hibernating);
  670. NS_UNMOCK(public_server_mode);
  671. NS_UNMOCK(get_uptime);
  672. NS_UNMOCK(get_bytes_read);
  673. NS_UNMOCK(get_bytes_written);
  674. NS_UNMOCK(logv);
  675. NS_UNMOCK(server_mode);
  676. NS_UNMOCK(accounting_is_enabled);
  677. }
  678. static double
  679. NS(tls_get_write_overhead_ratio)(void)
  680. {
  681. return 1.0;
  682. }
  683. static int
  684. NS(we_are_hibernating)(void)
  685. {
  686. return 0;
  687. }
  688. static int
  689. NS(public_server_mode)(const or_options_t *options)
  690. {
  691. (void)options;
  692. return 0;
  693. }
  694. static long
  695. NS(get_uptime)(void)
  696. {
  697. return 0;
  698. }
  699. static uint64_t
  700. NS(get_bytes_read)(void)
  701. {
  702. return 0;
  703. }
  704. static uint64_t
  705. NS(get_bytes_written)(void)
  706. {
  707. return 0;
  708. }
  709. static void
  710. NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
  711. const char *suffix, const char *format, va_list ap)
  712. {
  713. switch (CALLED(logv))
  714. {
  715. case 0:
  716. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  717. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  718. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  719. tt_ptr_op(suffix, OP_EQ, NULL);
  720. tt_str_op(format, OP_EQ,
  721. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  722. "I've sent %s and received %s.%s");
  723. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  724. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  725. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  726. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  727. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  728. break;
  729. case 1:
  730. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  731. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  732. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  733. tt_ptr_op(suffix, OP_EQ, NULL);
  734. tt_str_op(format, OP_EQ,
  735. "Average packaged cell fullness: %2.3f%%. TLS write overhead: %.f%%");
  736. tt_double_op(fabs(va_arg(ap, double) - 50.0), <=, DBL_EPSILON);
  737. tt_double_op(fabs(va_arg(ap, double) - 0.0), <=, DBL_EPSILON);
  738. break;
  739. default:
  740. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  741. break;
  742. }
  743. done:
  744. CALLED(logv)++;
  745. }
  746. static int
  747. NS(server_mode)(const or_options_t *options)
  748. {
  749. (void)options;
  750. return 0;
  751. }
  752. static int
  753. NS(accounting_is_enabled)(const or_options_t *options)
  754. {
  755. (void)options;
  756. return 0;
  757. }
  758. #undef NS_SUBMODULE
  759. #define NS_SUBMODULE ASPECT(log_heartbeat, tls_write_overhead)
  760. /*
  761. * Tests that log_heartbeat() correctly logs the TLS write overhead information
  762. * when the TLS write overhead ratio exceeds 1.
  763. */
  764. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  765. NS_DECL(int, we_are_hibernating, (void));
  766. NS_DECL(int, public_server_mode, (const or_options_t *options));
  767. NS_DECL(long, get_uptime, (void));
  768. NS_DECL(uint64_t, get_bytes_read, (void));
  769. NS_DECL(uint64_t, get_bytes_written, (void));
  770. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  771. const char *funcname, const char *suffix, const char *format, va_list ap));
  772. NS_DECL(int, server_mode, (const or_options_t *options));
  773. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  774. static void
  775. NS(test_main)(void *arg)
  776. {
  777. int expected, actual;
  778. (void)arg;
  779. NS_MOCK(tls_get_write_overhead_ratio);
  780. NS_MOCK(we_are_hibernating);
  781. NS_MOCK(public_server_mode);
  782. NS_MOCK(get_uptime);
  783. NS_MOCK(get_bytes_read);
  784. NS_MOCK(get_bytes_written);
  785. NS_MOCK(logv);
  786. NS_MOCK(server_mode);
  787. NS_MOCK(accounting_is_enabled);
  788. stats_n_data_cells_packaged = 0;
  789. log_global_min_severity_ = LOG_DEBUG;
  790. expected = 0;
  791. actual = log_heartbeat(0);
  792. tt_int_op(actual, OP_EQ, expected);
  793. tt_int_op(CALLED(logv), OP_EQ, 2);
  794. done:
  795. NS_UNMOCK(tls_get_write_overhead_ratio);
  796. NS_UNMOCK(we_are_hibernating);
  797. NS_UNMOCK(public_server_mode);
  798. NS_UNMOCK(get_uptime);
  799. NS_UNMOCK(get_bytes_read);
  800. NS_UNMOCK(get_bytes_written);
  801. NS_UNMOCK(logv);
  802. NS_UNMOCK(server_mode);
  803. NS_UNMOCK(accounting_is_enabled);
  804. }
  805. static double
  806. NS(tls_get_write_overhead_ratio)(void)
  807. {
  808. return 2.0;
  809. }
  810. static int
  811. NS(we_are_hibernating)(void)
  812. {
  813. return 0;
  814. }
  815. static int
  816. NS(public_server_mode)(const or_options_t *options)
  817. {
  818. (void)options;
  819. return 0;
  820. }
  821. static long
  822. NS(get_uptime)(void)
  823. {
  824. return 0;
  825. }
  826. static uint64_t
  827. NS(get_bytes_read)(void)
  828. {
  829. return 0;
  830. }
  831. static uint64_t
  832. NS(get_bytes_written)(void)
  833. {
  834. return 0;
  835. }
  836. static void
  837. NS(logv)(int severity, log_domain_mask_t domain,
  838. const char *funcname, const char *suffix, const char *format, va_list ap)
  839. {
  840. switch (CALLED(logv))
  841. {
  842. case 0:
  843. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  844. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  845. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  846. tt_ptr_op(suffix, OP_EQ, NULL);
  847. tt_str_op(format, OP_EQ,
  848. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  849. "I've sent %s and received %s.%s");
  850. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  851. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  852. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  853. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  854. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  855. break;
  856. case 1:
  857. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  858. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  859. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  860. tt_ptr_op(suffix, OP_EQ, NULL);
  861. tt_str_op(format, OP_EQ,
  862. "Average packaged cell fullness: %2.3f%%. TLS write overhead: %.f%%");
  863. tt_int_op(fabs(va_arg(ap, double) - 100.0) <= DBL_EPSILON, OP_EQ, 1);
  864. tt_double_op(fabs(va_arg(ap, double) - 100.0), <=, DBL_EPSILON);
  865. break;
  866. default:
  867. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  868. break;
  869. }
  870. done:
  871. CALLED(logv)++;
  872. }
  873. static int
  874. NS(server_mode)(const or_options_t *options)
  875. {
  876. (void)options;
  877. return 0;
  878. }
  879. static int
  880. NS(accounting_is_enabled)(const or_options_t *options)
  881. {
  882. (void)options;
  883. return 0;
  884. }
  885. #undef NS_SUBMODULE
  886. struct testcase_t status_tests[] = {
  887. TEST_CASE(count_circuits),
  888. TEST_CASE(secs_to_uptime),
  889. TEST_CASE(bytes_to_usage),
  890. TEST_CASE_ASPECT(log_heartbeat, fails),
  891. TEST_CASE_ASPECT(log_heartbeat, simple),
  892. TEST_CASE_ASPECT(log_heartbeat, not_in_consensus),
  893. TEST_CASE_ASPECT(log_heartbeat, calls_log_accounting),
  894. TEST_CASE_ASPECT(log_heartbeat, packaged_cell_fullness),
  895. TEST_CASE_ASPECT(log_heartbeat, tls_write_overhead),
  896. END_OF_TESTCASES
  897. };