test_status.c 26 KB

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