test_status.c 26 KB

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