test_status.c 25 KB

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