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