test_status.c 27 KB

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