test_status.c 27 KB

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