test_status.c 26 KB

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