test_status.c 27 KB

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